Skip to content

Commit e191887

Browse files
RafaelGSSaduh95
authored andcommitted
permission: block FileHandle fsync and fdatasync
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #65431 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ea9a0fa commit e191887

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

doc/api/permissions.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ does not exist, the wildcard will not be added, and access will be limited to
258258
yet, make sure to explicitly include the wildcard:
259259
`/my-path/folder-do-not-exist/*`.
260260

261+
Some `node:fs` operations act on an already-open file descriptor rather than a
262+
path, so they cannot be tied to a `--allow-fs-read` or `--allow-fs-write` grant.
263+
When the permission model is enabled these operations are disabled and throw
264+
`ERR_ACCESS_DENIED`, regardless of how the descriptor was obtained. This applies
265+
both to the top-level `node:fs` functions and to the equivalent
266+
`FileHandle` methods, and currently includes `fsync`/`fdatasync`,
267+
`fchmod`, and `fchown` (and their synchronous variants).
268+
261269
#### Configuration file support
262270

263271
In addition to passing permission flags on the command line, they can also be

lib/internal/fs/promises.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,9 @@ async function rmdir(path, options) {
15981598
}
15991599

16001600
async function fdatasync(handle) {
1601+
if (permission.isEnabled()) {
1602+
throw new ERR_ACCESS_DENIED('fdatasync API is disabled when Permission Model is enabled.');
1603+
}
16011604
return await PromisePrototypeThen(
16021605
binding.fdatasync(handle.fd, kUsePromises),
16031606
undefined,
@@ -1606,6 +1609,9 @@ async function fdatasync(handle) {
16061609
}
16071610

16081611
async function fsync(handle) {
1612+
if (permission.isEnabled()) {
1613+
throw new ERR_ACCESS_DENIED('fsync API is disabled when Permission Model is enabled.');
1614+
}
16091615
return await PromisePrototypeThen(
16101616
binding.fsync(handle.fd, kUsePromises),
16111617
undefined,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Flags: --permission --allow-fs-read=*
2+
'use strict';
3+
4+
const common = require('../common');
5+
const { isMainThread } = require('worker_threads');
6+
7+
if (!isMainThread) {
8+
common.skip('This test only works on a main thread');
9+
}
10+
11+
const assert = require('assert');
12+
const { open } = require('fs/promises');
13+
const fixtures = require('../common/fixtures');
14+
15+
const regularFile = fixtures.path('permission', 'deny', 'regular-file.md');
16+
17+
// FileHandle sync operations must be blocked when the permission model is
18+
// enabled, consistent with fs.fsync() / fs.fsyncSync() and fdatasync variants.
19+
(async () => {
20+
const fh = await open(regularFile, 'r');
21+
try {
22+
await assert.rejects(
23+
fh.sync(),
24+
common.expectsError({ code: 'ERR_ACCESS_DENIED' }),
25+
);
26+
await assert.rejects(
27+
fh.datasync(),
28+
common.expectsError({ code: 'ERR_ACCESS_DENIED' }),
29+
);
30+
} finally {
31+
await fh.close();
32+
}
33+
})().then(common.mustCall());

0 commit comments

Comments
 (0)