lib,permission: support fs.lstat

PR-URL: https://github.com/nodejs-private/node-private/pull/486
Backport-PR-URL: https://github.com/nodejs-private/node-private/pull/604
CVE-ID: CVE-2024-22018
This commit is contained in:
RafaelGSS committed 2024-07-03 16:58:40 -03:00
1 parent d38ea17341
commit 025cbd6936
3 files changed
+36 -6

No files matched your search

+7
View File
@@ -1554,6 +1554,10 @@ function lstat(path, options = { bigint: false }, callback) {
}
callback = makeStatsCallback(callback);
path = getValidatedPath(path);
if (permission.isEnabled() && !permission.has('fs.read', path)) {
callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path));
return;
}
const req = new FSReqCallback(options.bigint);
req.oncomplete = callback;
@@ -1630,6 +1634,9 @@ function fstatSync(fd, options = { bigint: false }) {
*/
function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) {
path = getValidatedPath(path);
if (permission.isEnabled() && !permission.has('fs.read', path)) {
throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path);
}
const stats = binding.lstat(
pathModule.toNamespacedPath(path),
options.bigint,
+5 -1
View File
@@ -1113,7 +1113,11 @@ module.exports = {
// Note: Node.js specific errors must begin with the prefix ERR_
E('ERR_ACCESS_DENIED',
'Access to this API has been restricted. Permission: %s',
function(msg, permission = '', resource = '') {
this.permission = permission;
this.resource = resource;
return msg;
},
Error);
E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError);
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
+24 -5
View File
@@ -161,23 +161,23 @@ const regularFile = __filename;
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
// cpSync calls statSync before reading blockedFile
resource: path.toNamespacedPath(blockedFolder),
// cpSync calls lstatSync before reading blockedFile
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.cpSync(blockedFileURL, path.join(blockedFolder, 'any-other-file'));
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
// cpSync calls statSync before reading blockedFile
resource: path.toNamespacedPath(blockedFolder),
// cpSync calls lstatSync before reading blockedFile
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.cpSync(blockedFile, path.join(__dirname, 'any-other-file'));
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(__dirname),
resource: path.toNamespacedPath(blockedFile),
}));
}
@@ -385,4 +385,23 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
}
// fs.lstat
{
assert.throws(() => {
fs.lstatSync(blockedFile);
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
}));
assert.throws(() => {
fs.lstatSync(path.join(blockedFolder, 'anyfile'));
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
}));
// doesNotThrow
fs.lstat(regularFile, (err) => {
assert.ifError(err);
});
}