Skip to content

Commit

Permalink
permission: handle buffer path on fs calls
Browse files Browse the repository at this point in the history
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
Refs: https://hackerone.com/bugs?subject=nodejs&report_id=2038134
PR-URL: nodejs-private/node-private#439
  • Loading branch information
RafaelGSS committed Aug 9, 2023
1 parent 4aa0eff commit 1f64147
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ function possiblyTransformPath(path) {
if (permission.isEnabled()) {
if (typeof path === 'string') {
return pathModule.resolve(path);
} else if (Buffer.isBuffer(path)) {
return Buffer.from(pathModule.resolve(path.toString()));
}
}
return path;
Expand Down
30 changes: 29 additions & 1 deletion test/fixtures/permission/fs-traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const path = require('path');

const blockedFolder = process.env.BLOCKEDFOLDER;
const allowedFolder = process.env.ALLOWEDFOLDER;
const traversalPath = allowedFolder + '../file.md'
const traversalPath = allowedFolder + '../file.md';
const traversalFolderPath = allowedFolder + '../folder';
const bufferTraversalPath = Buffer.from(allowedFolder + '../file.md');

{
assert.ok(process.permission.has('fs.read', allowedFolder));
Expand Down Expand Up @@ -41,7 +43,33 @@ const traversalPath = allowedFolder + '../file.md'
}));
}

{
assert.throws(() => {
fs.mkdtempSync(traversalFolderPath, (error) => {
assert.ifError(error);
});
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(path.resolve(traversalFolderPath + 'XXXXXX')),
}));
}

{
assert.throws(() => {
fs.readFile(bufferTraversalPath, (error) => {
assert.ifError(error);
});
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.resolve(traversalPath),
}));
}

{
assert.ok(!process.permission.has('fs.read', traversalPath));
assert.ok(!process.permission.has('fs.write', traversalPath));
assert.ok(!process.permission.has('fs.read', traversalFolderPath));
assert.ok(!process.permission.has('fs.write', traversalFolderPath));
}

0 comments on commit 1f64147

Please sign in to comment.