Skip to content

Commit 8c9a4ae

Browse files
RafaelGSStargos
authored andcommittedAug 14, 2024
lib,permission: support Buffer to permission.has
PR-URL: #54104 Fixes: #54100 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent db3b0df commit 8c9a4ae

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed
 

Diff for: ‎lib/fs.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,8 @@ function lstat(path, options = { bigint: false }, callback) {
15501550
callback = makeStatsCallback(callback);
15511551
path = getValidatedPath(path);
15521552
if (permission.isEnabled() && !permission.has('fs.read', path)) {
1553-
callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path));
1553+
const resource = BufferIsBuffer(path) ? BufferToString(path) : path;
1554+
callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource));
15541555
return;
15551556
}
15561557

@@ -1627,7 +1628,8 @@ function fstatSync(fd, options = { bigint: false }) {
16271628
function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) {
16281629
path = getValidatedPath(path);
16291630
if (permission.isEnabled() && !permission.has('fs.read', path)) {
1630-
throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path);
1631+
const resource = BufferIsBuffer(path) ? BufferToString(path) : path;
1632+
throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource);
16311633
}
16321634
const stats = binding.lstat(
16331635
getValidatedPath(path),

Diff for: ‎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);

Diff for: ‎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) => {

Diff for: ‎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',

Diff for: ‎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)