From 025cbd6936ce3df5375c2ec75d933eb7cb035f7b Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Mon, 17 Jun 2024 15:17:42 -0300 Subject: [PATCH] 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 --- lib/fs.js | 7 +++++++ lib/internal/errors.js | 6 +++++- test/fixtures/permission/fs-read.js | 29 ++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 3b849407dbc..0cddd128623 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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, diff --git a/lib/internal/errors.js b/lib/internal/errors.js index def49491e45..47e37f5ab4a 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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); diff --git a/test/fixtures/permission/fs-read.js b/test/fixtures/permission/fs-read.js index 43e1718ab15..a4808601fb7 100644 --- a/test/fixtures/permission/fs-read.js +++ b/test/fixtures/permission/fs-read.js @@ -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); + }); } \ No newline at end of file