diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index bace4385714..e30a2f1d069 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -17,6 +17,7 @@ const { Symbol, SymbolAsyncDispose, Uint8Array, + uncurryThis, } = primordials; const { fs: constants } = internalBinding('constants'); @@ -30,6 +31,8 @@ const { const binding = internalBinding('fs'); const { Buffer } = require('buffer'); +const { isBuffer: BufferIsBuffer } = Buffer; +const BufferToString = uncurryThis(Buffer.prototype.toString); const { AbortError, @@ -1018,8 +1021,13 @@ async function fstat(handle, options = { bigint: false }) { } async function lstat(path, options = { bigint: false }) { + path = getValidatedPath(path); + if (permission.isEnabled() && !permission.has('fs.read', path)) { + const resource = pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path); + throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource); + } const result = await PromisePrototypeThen( - binding.lstat(getValidatedPath(path), options.bigint, kUsePromises), + binding.lstat(path, options.bigint, kUsePromises), undefined, handleErrorFromBinding, ); @@ -1063,6 +1071,9 @@ async function unlink(path) { } async function fchmod(handle, mode) { + if (permission.isEnabled()) { + throw new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.'); + } mode = parseFileMode(mode, 'mode'); return await PromisePrototypeThen( binding.fchmod(handle.fd, mode, kUsePromises), @@ -1103,6 +1114,9 @@ async function lchown(path, uid, gid) { async function fchown(handle, uid, gid) { validateInteger(uid, 'uid', -1, kMaxUserId); validateInteger(gid, 'gid', -1, kMaxUserId); + if (permission.isEnabled()) { + throw new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.'); + } return await PromisePrototypeThen( binding.fchown(handle.fd, uid, gid, kUsePromises), undefined, diff --git a/test/fixtures/permission/fs-read.js b/test/fixtures/permission/fs-read.js index fa2bc14e443..2e75d57ebd0 100644 --- a/test/fixtures/permission/fs-read.js +++ b/test/fixtures/permission/fs-read.js @@ -4,6 +4,8 @@ const common = require('../../common'); const assert = require('assert'); const fs = require('fs'); +const fsPromises = require('node:fs/promises'); + const path = require('path'); const { pathToFileURL } = require('url'); @@ -469,6 +471,204 @@ const regularFile = __filename; })); } +// fsPromises.readFile +{ + assert.rejects(async () => { + await fsPromises.readFile(blockedFile); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.readFile(blockedFileURL); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); +} + +// fsPromises.stat +{ + assert.rejects(async () => { + await fsPromises.stat(blockedFile); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.stat(blockedFileURL); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.stat(path.join(blockedFolder, 'anyfile')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')), + })).then(common.mustCall()); +} + +// fsPromises.access +{ + assert.rejects(async () => { + await fsPromises.access(blockedFile, fs.constants.R_OK); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.access(blockedFileURL, fs.constants.R_OK); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.access(path.join(blockedFolder, 'anyfile'), fs.constants.R_OK); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')), + })).then(common.mustCall()); +} + +// fsPromises.copyFile +{ + assert.rejects(async () => { + await fsPromises.copyFile(blockedFile, path.join(blockedFolder, 'any-other-file')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.copyFile(blockedFileURL, path.join(blockedFolder, 'any-other-file')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); +} + +// fsPromises.cp +{ + assert.rejects(async () => { + await fsPromises.cp(blockedFile, path.join(blockedFolder, 'any-other-file')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.cp(blockedFileURL, path.join(blockedFolder, 'any-other-file')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); +} + +// fsPromises.open +{ + assert.rejects(async () => { + await fsPromises.open(blockedFile, 'r'); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.open(blockedFileURL, 'r'); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.open(path.join(blockedFolder, 'anyfile'), 'r'); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')), + })).then(common.mustCall()); +} + +// fsPromises.opendir +{ + assert.rejects(async () => { + await fsPromises.opendir(blockedFolder); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFolder), + })).then(common.mustCall()); +} + +// fsPromises.readdir +{ + assert.rejects(async () => { + await fsPromises.readdir(blockedFolder); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFolder), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.readdir(blockedFolder, { recursive: true }); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFolder), + })).then(common.mustCall()); +} + +// fsPromises.rename +{ + assert.rejects(async () => { + await fsPromises.rename(blockedFile, 'newfile'); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.rename(blockedFileURL, 'newfile'); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); +} + +// fsPromises.lstat +{ + assert.rejects(async () => { + await fsPromises.lstat(blockedFile); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.lstat(blockedFileURL); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + })).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.lstat(path.join(blockedFolder, 'anyfile')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + })).then(common.mustCall()); +} + // fs.lstat { assert.throws(() => { diff --git a/test/fixtures/permission/fs-write.js b/test/fixtures/permission/fs-write.js index 27d88911ef8..6771485679c 100644 --- a/test/fixtures/permission/fs-write.js +++ b/test/fixtures/permission/fs-write.js @@ -9,6 +9,7 @@ if (!isMainThread) { const assert = require('assert'); const fs = require('fs'); +const fsPromises = require('node:fs/promises'); const path = require('path'); const regularFolder = process.env.ALLOWEDFOLDER; @@ -212,7 +213,7 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER; permission: 'FileSystemWrite', })); assert.rejects(async () => { - await fs.promises.mkdtemp(path.join(blockedFolder, 'any-folder')); + await fsPromises.mkdtemp(path.join(blockedFolder, 'any-folder')); }, { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemWrite', @@ -360,7 +361,7 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER; permission: 'FileSystemWrite', })); assert.rejects(async () => { - await fs.promises.open(blockedFile, fs.constants.O_RDWR | fs.constants.O_NOFOLLOW); + await fsPromises.open(blockedFile, fs.constants.O_RDWR | fs.constants.O_NOFOLLOW); }, { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemWrite', @@ -399,7 +400,7 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER; permission: 'FileSystemWrite', }); assert.rejects(async () => { - await fs.promises.chmod(blockedFile, 0o755); + await fsPromises.chmod(blockedFile, 0o755); }, { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemWrite', @@ -414,7 +415,7 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER; permission: 'FileSystemWrite', })); assert.rejects(async () => { - await fs.promises.lchmod(blockedFile, 0o755); + await fsPromises.lchmod(blockedFile, 0o755); }, { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemWrite', @@ -439,7 +440,7 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER; permission: 'FileSystemWrite', }); assert.rejects(async () => { - await fs.promises.appendFile(blockedFile, 'new data'); + await fsPromises.appendFile(blockedFile, 'new data'); }, { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemWrite', @@ -628,4 +629,199 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER; }, { code: 'ERR_ACCESS_DENIED', }); +} + +// fsPromises.writeFile +{ + assert.rejects(async () => { + await fsPromises.writeFile(blockedFile, 'example'); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.writeFile(blockedFileURL, 'example'); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.writeFile(path.join(blockedFolder, 'anyfile'), 'example'); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')), + }).then(common.mustCall()); +} + +// fsPromises.utimes +{ + assert.rejects(async () => { + await fsPromises.utimes(blockedFile, new Date(), new Date()); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.utimes(blockedFileURL, new Date(), new Date()); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.utimes(path.join(blockedFolder, 'anyfile'), new Date(), new Date()); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')), + }).then(common.mustCall()); +} + +// fsPromises.lutimes +{ + assert.rejects(async () => { + await fsPromises.lutimes(blockedFile, new Date(), new Date()); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.lutimes(blockedFileURL, new Date(), new Date()); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); +} + +// fsPromises.mkdir +{ + assert.rejects(async () => { + await fsPromises.mkdir(path.join(blockedFolder, 'any-folder')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(blockedFolder, 'any-folder')), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.mkdir(path.join(relativeProtectedFolder, 'any-folder')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-folder')), + }).then(common.mustCall()); +} + +// fsPromises.rename +{ + assert.rejects(async () => { + await fsPromises.rename(blockedFile, path.join(blockedFile, 'renamed')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.rename(blockedFileURL, path.join(blockedFile, 'renamed')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.rename(regularFile, path.join(blockedFolder, 'renamed')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(blockedFolder, 'renamed')), + }).then(common.mustCall()); +} + +// fsPromises.copyFile +{ + assert.rejects(async () => { + await fsPromises.copyFile(regularFile, path.join(blockedFolder, 'any-file')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(blockedFolder, 'any-file')), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.copyFile(regularFile, path.join(relativeProtectedFolder, 'any-file')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-file')), + }).then(common.mustCall()); +} + +// fsPromises.cp +{ + assert.rejects(async () => { + await fsPromises.cp(regularFile, path.join(blockedFolder, 'any-file')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(blockedFolder, 'any-file')), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.cp(regularFile, path.join(relativeProtectedFolder, 'any-file')); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-file')), + }).then(common.mustCall()); +} + +// fsPromises.unlink +{ + assert.rejects(async () => { + await fsPromises.unlink(blockedFile); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); + assert.rejects(async () => { + await fsPromises.unlink(blockedFileURL); + }, { + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemWrite', + resource: path.toNamespacedPath(blockedFile), + }).then(common.mustCall()); +} + +// FileHandle.chmod (fchmod) with read-only fd +{ + assert.rejects(async () => { + // blocked file is allowed to read + const fh = await fsPromises.open(blockedFile, 'r'); + try { + await fh.chmod(0o777); + } finally { + await fh.close(); + } + }, { + code: 'ERR_ACCESS_DENIED', + }).then(common.mustCall()); +} + +// FileHandle.chown (fchown) with read-only fd +{ + assert.rejects(async () => { + // blocked file is allowed to read + const fh = await fsPromises.open(blockedFile, 'r'); + try { + await fh.chown(999, 999); + } finally { + await fh.close(); + } + }, { + code: 'ERR_ACCESS_DENIED', + }).then(common.mustCall()); } \ No newline at end of file