From 3fda34e186258b34b583218fa2a96bf37a74ca88 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Fri, 20 Nov 2026 16:25:20 -0300 Subject: [PATCH] fs: do not disable fd-based APIs in permission audit mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/nodejs/node/pull/65659 Refs: https://github.com/nodejs/node/issues/65419 Signed-off-by: RafaelGSS PR-URL: https://github.com/nodejs/node/pull/66726 Reviewed-By: Ulises Gascón --- doc/api/permissions.md | 3 + lib/fs.js | 22 ++--- lib/internal/fs/promises.js | 12 +-- ...rmission-audit-fs-fd-apis-does-not-deny.js | 86 +++++++++++++++++++ 4 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-permission-audit-fs-fd-apis-does-not-deny.js diff --git a/doc/api/permissions.md b/doc/api/permissions.md index 2cf99bd167a..b0aebdf3848 100644 --- a/doc/api/permissions.md +++ b/doc/api/permissions.md @@ -285,6 +285,9 @@ both to the top-level `node:fs` functions and to the equivalent `FileHandle` methods, and currently includes `fsync`/`fdatasync`, `fchmod`, `fchown`, and `futimes` (and their synchronous variants). +In [audit mode](#audit-mode), these operations are not disabled and run normally. No +diagnostics channel message is published for them. + #### Environment variable permissions When the Permission Model is enforced, the process only has access to the diff --git a/lib/fs.js b/lib/fs.js index c75bfedf73c..c593fbfffb0 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1607,7 +1607,9 @@ function fdatasync(fd, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - if (permission.isEnabled()) { + // TODO(rafaelgss): publish a diagnostics channel event for fd-based APIs in + // audit mode. + if (permission.isEnabled() && !permission.isAuditMode()) { callback(new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.')); return; } @@ -1628,7 +1630,7 @@ function fdatasyncSync(fd) { if (result !== undefined) return; } - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.'); } binding.fdatasync(fd); @@ -1649,7 +1651,7 @@ function fsync(fd, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { callback(new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.')); return; } @@ -1669,7 +1671,7 @@ function fsyncSync(fd) { if (result !== undefined) return; } - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.'); } binding.fsync(fd); @@ -2450,7 +2452,7 @@ function fchmod(fd, mode, callback) { const h = vfsState.handlers; if (h !== null && vfsVoid(h.fchmod(fd, mode), callback)) return; - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { callback(new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.')); return; } @@ -2475,7 +2477,7 @@ function fchmodSync(fd, mode) { if (result !== undefined) return; } - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.'); } binding.fchmod(fd, mode); @@ -2626,7 +2628,7 @@ function fchown(fd, uid, gid, callback) { const h = vfsState.handlers; if (h !== null && vfsVoid(h.fchown(fd), callback)) return; - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { callback(new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.')); return; } @@ -2653,7 +2655,7 @@ function fchownSync(fd, uid, gid) { if (result !== undefined) return; } - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.'); } @@ -2767,7 +2769,7 @@ function futimes(fd, atime, mtime, callback) { const h = vfsState.handlers; if (h !== null && vfsVoid(h.futimes(fd, atime, mtime), callback)) return; - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { callback(new ERR_ACCESS_DENIED('futimes API is disabled when Permission Model is enabled.')); return; } @@ -2796,7 +2798,7 @@ function futimesSync(fd, atime, mtime) { if (result !== undefined) return; } - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('futimes API is disabled when Permission Model is enabled.'); } diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 8507abcf023..7ec15eaeab6 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -1686,7 +1686,9 @@ async function rmdir(path, options) { } async function fdatasync(handle) { - if (permission.isEnabled()) { + // TODO(rafaelgss): publish a diagnostics channel event for fd-based APIs in + // audit mode. + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.'); } return await PromisePrototypeThen( @@ -1697,7 +1699,7 @@ async function fdatasync(handle) { } async function fsync(handle) { - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.'); } return await PromisePrototypeThen( @@ -1971,7 +1973,7 @@ async function unlink(path) { } async function fchmod(handle, mode) { - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fchmod API is disabled when Permission Model is enabled.'); } mode = parseFileMode(mode, 'mode'); @@ -2033,7 +2035,7 @@ 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()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('fchown API is disabled when Permission Model is enabled.'); } return await PromisePrototypeThen( @@ -2079,7 +2081,7 @@ async function utimes(path, atime, mtime) { } async function futimes(handle, atime, mtime) { - if (permission.isEnabled()) { + if (permission.isEnabled() && !permission.isAuditMode()) { throw new ERR_ACCESS_DENIED('futimes API is disabled when Permission Model is enabled.'); } atime = toUnixTimestamp(atime, 'atime'); diff --git a/test/parallel/test-permission-audit-fs-fd-apis-does-not-deny.js b/test/parallel/test-permission-audit-fs-fd-apis-does-not-deny.js new file mode 100644 index 00000000000..5ea4cb2ae0d --- /dev/null +++ b/test/parallel/test-permission-audit-fs-fd-apis-does-not-deny.js @@ -0,0 +1,86 @@ +'use strict'; + +const common = require('../common'); +const { isMainThread } = require('worker_threads'); + +if (!isMainThread) { + common.skip('This test only works on a main thread'); +} + +const assert = require('assert'); +const fs = require('fs'); +const { spawnSync } = require('child_process'); +const { test } = require('node:test'); +const fixtures = require('../common/fixtures'); +const tmpdir = require('../common/tmpdir'); + +tmpdir.refresh(); + +const target = tmpdir.resolve('audit-fd-apis.txt'); +fs.copyFileSync(fixtures.path('permission', 'deny', 'regular-file.md'), target); + +function run(flag, op) { + const childScript = ` + const fs = require('node:fs'); + (async () => { + const fd = fs.openSync(process.env.TARGET, 'r+'); + const fh = await fs.promises.open(process.env.TARGET, 'r+'); + const cb = (fn, ...args) => new Promise((resolve, reject) => { + fn(fd, ...args, (err) => err ? reject(err) : resolve()); + }); + try { + await (${op}); + console.log('RESULT NO_THROW'); + } catch (e) { + console.log('RESULT THREW ' + e.code); + } finally { + await fh.close(); + fs.closeSync(fd); + } + })(); + `; + const { status, stdout, stderr } = spawnSync( + process.execPath, + [ + flag, + '--allow-fs-read=*', + '--allow-fs-write=*', + '--allow-env=TARGET', + '-e', + childScript, + ], + { encoding: 'utf8', env: { ...process.env, TARGET: target } }, + ); + assert.strictEqual(status, 0, stderr); + const resultLine = stdout.split('\n').find((l) => l.startsWith('RESULT ')); + assert.ok(resultLine, stdout); + return resultLine.replace('RESULT ', ''); +} + +const ops = [ + ['fs.fsyncSync()', 'fs.fsyncSync(fd)'], + ['fs.fsync()', 'cb(fs.fsync)'], + ['filehandle.sync()', 'fh.sync()'], + ['fs.fdatasyncSync()', 'fs.fdatasyncSync(fd)'], + ['fs.fdatasync()', 'cb(fs.fdatasync)'], + ['filehandle.datasync()', 'fh.datasync()'], + ['fs.fchmodSync()', 'fs.fchmodSync(fd, 0o644)'], + ['fs.fchmod()', 'cb(fs.fchmod, 0o644)'], + ['filehandle.chmod()', 'fh.chmod(0o644)'], + ['fs.fchownSync()', 'fs.fchownSync(fd, -1, -1)'], + ['fs.fchown()', 'cb(fs.fchown, -1, -1)'], + ['filehandle.chown()', 'fh.chown(-1, -1)'], + ['fs.futimesSync()', 'fs.futimesSync(fd, 1, 1)'], + ['fs.futimes()', 'cb(fs.futimes, 1, 1)'], + ['filehandle.utimes()', 'fh.utimes(1, 1)'], +]; + +for (const [api, op] of ops) { + test(`audit mode does not disable ${api}`, () => { + assert.strictEqual(run('--permission-audit', op), 'NO_THROW'); + }); + + test(`enforce mode still disables ${api}`, () => { + assert.strictEqual(run('--permission', op), 'THREW ERR_ACCESS_DENIED'); + }); +}