Skip to content

Commit 358ff74

Browse files
authored
lib,permission: support Buffer to permission.has
PR-URL: #54104 Fixes: #54100 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent 492032f commit 358ff74

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

lib/fs.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,8 @@ function lstat(path, options = { bigint: false }, callback) {
15461546
callback = makeStatsCallback(callback);
15471547
path = getValidatedPath(path);
15481548
if (permission.isEnabled() && !permission.has('fs.read', path)) {
1549-
callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path));
1549+
const resource = BufferIsBuffer(path) ? BufferToString(path) : path;
1550+
callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource));
15501551
return;
15511552
}
15521553

@@ -1623,7 +1624,8 @@ function fstatSync(fd, options = { bigint: false }) {
16231624
function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) {
16241625
path = getValidatedPath(path);
16251626
if (permission.isEnabled() && !permission.has('fs.read', path)) {
1626-
throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path);
1627+
const resource = BufferIsBuffer(path) ? BufferToString(path) : path;
1628+
throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource);
16271629
}
16281630
const stats = binding.lstat(
16291631
getValidatedPath(path),

lib/internal/process/permission.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const {
55
} = primordials;
66

77
const permission = internalBinding('permission');
8-
const { validateString } = require('internal/validators');
8+
const { validateString, validateBuffer } = require('internal/validators');
9+
const { Buffer } = require('buffer');
10+
const { isBuffer } = Buffer;
911

1012
let experimentalPermission;
1113

@@ -22,7 +24,11 @@ module.exports = ObjectFreeze({
2224
validateString(scope, 'scope');
2325
if (reference != null) {
2426
// TODO: add support for WHATWG URLs and Uint8Arrays.
25-
validateString(reference, 'reference');
27+
if (isBuffer(reference)) {
28+
validateBuffer(reference, 'reference');
29+
} else {
30+
validateString(reference, 'reference');
31+
}
2632
}
2733

2834
return permission.has(scope, reference);

test/fixtures/permission/fs-read.js

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('fs');
77
const path = require('path');
88

99
const blockedFile = process.env.BLOCKEDFILE;
10+
const bufferBlockedFile = Buffer.from(process.env.BLOCKEDFILE);
1011
const blockedFileURL = new URL('file://' + process.env.BLOCKEDFILE);
1112
const blockedFolder = process.env.BLOCKEDFOLDER;
1213
const allowedFolder = process.env.ALLOWEDFOLDER;
@@ -408,6 +409,11 @@ const regularFile = __filename;
408409
}, common.expectsError({
409410
code: 'ERR_ACCESS_DENIED',
410411
}));
412+
assert.throws(() => {
413+
fs.lstatSync(bufferBlockedFile);
414+
}, common.expectsError({
415+
code: 'ERR_ACCESS_DENIED',
416+
}));
411417

412418
// doesNotThrow
413419
fs.lstat(regularFile, (err) => {

test/fixtures/permission/fs-traversal.js

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ const uint8ArrayTraversalPath = new TextEncoder().encode(traversalPath);
6969
}));
7070
}
7171

72+
{
73+
fs.lstat(bufferTraversalPath, common.expectsError({
74+
code: 'ERR_ACCESS_DENIED',
75+
permission: 'FileSystemRead',
76+
// lstat checks and throw on JS side.
77+
// resource is only resolved on C++ (is_granted)
78+
resource: bufferTraversalPath.toString(),
79+
}));
80+
}
81+
7282
{
7383
fs.readFile(uint8ArrayTraversalPath, common.expectsError({
7484
code: 'ERR_ACCESS_DENIED',

test/parallel/test-permission-has.js

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ const assert = require('assert');
2121
message: 'The "reference" argument must be of type string. Received an instance of Object',
2222
}));
2323
}
24+
25+
{
26+
assert.ok(!process.permission.has('FileSystemWrite', Buffer.from('reference')));
27+
}

0 commit comments

Comments
 (0)