GeezerVison is showing github.com — lib,permission: support Buffer to permission.has · nodejs/node@afbf2c0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1554,7 +1554,8 @@ function lstat(path, options = { bigint: false }, callback) { | 1554 | 1554 | callback = makeStatsCallback(callback); | 1555 | 1555 | path = getValidatedPath(path); | 1556 | 1556 | if (permission.isEnabled() && !permission.has('fs.read', path)) { | 1557 | | - callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path)); | | 1557 | + const resource = BufferIsBuffer(path) ? BufferToString(path) : path; | | 1558 | + callback(new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource)); | 1558 | 1559 | return; | 1559 | 1560 | } | 1560 | 1561 |
| @@ -1634,7 +1635,8 @@ function fstatSync(fd, options = { bigint: false }) { | 1634 | 1635 | function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) { | 1635 | 1636 | path = getValidatedPath(path); | 1636 | 1637 | if (permission.isEnabled() && !permission.has('fs.read', path)) { | 1637 | | - throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', path); | | 1638 | + const resource = BufferIsBuffer(path) ? BufferToString(path) : path; | | 1639 | + throw new ERR_ACCESS_DENIED('Access to this API has been restricted', 'FileSystemRead', resource); | 1638 | 1640 | } | 1639 | 1641 | const stats = binding.lstat( | 1640 | 1642 | pathModule.toNamespacedPath(path), | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/permission.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | } = primordials; | 6 | 6 |
| 7 | 7 | 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; | 9 | 11 |
| 10 | 12 | let experimentalPermission; | 11 | 13 |
| @@ -22,7 +24,11 @@ module.exports = ObjectFreeze({ | 22 | 24 | validateString(scope, 'scope'); | 23 | 25 | if (reference != null) { | 24 | 26 | // 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 | + } | 26 | 32 | } | 27 | 33 |
| 28 | 34 | return permission.has(scope, reference); | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/permission/fs-read.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ const fs = require('fs'); | 7 | 7 | const path = require('path'); | 8 | 8 |
| 9 | 9 | const blockedFile = process.env.BLOCKEDFILE; | | 10 | +const bufferBlockedFile = Buffer.from(process.env.BLOCKEDFILE); | 10 | 11 | const blockedFileURL = new URL('file://' + process.env.BLOCKEDFILE); | 11 | 12 | const blockedFolder = process.env.BLOCKEDFOLDER; | 12 | 13 | const allowedFolder = process.env.ALLOWEDFOLDER; | @@ -410,6 +411,11 @@ const regularFile = __filename; | 410 | 411 | }, common.expectsError({ | 411 | 412 | code: 'ERR_ACCESS_DENIED', | 412 | 413 | })); | | 414 | + assert.throws(() => { | | 415 | + fs.lstatSync(bufferBlockedFile); | | 416 | + }, common.expectsError({ | | 417 | + code: 'ERR_ACCESS_DENIED', | | 418 | + })); | 413 | 419 |
| 414 | 420 | // doesNotThrow | 415 | 421 | fs.lstat(regularFile, (err) => { | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/permission/fs-traversal.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -69,6 +69,16 @@ const uint8ArrayTraversalPath = new TextEncoder().encode(traversalPath); | 69 | 69 | })); | 70 | 70 | } | 71 | 71 |
| | 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 | + | 72 | 82 | { | 73 | 83 | fs.readFile(uint8ArrayTraversalPath, common.expectsError({ | 74 | 84 | code: 'ERR_ACCESS_DENIED', | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-permission-has.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,3 +21,7 @@ const assert = require('assert'); | 21 | 21 | message: 'The "reference" argument must be of type string. Received an instance of Object', | 22 | 22 | })); | 23 | 23 | } | | 24 | + | | 25 | +{ | | 26 | + assert.ok(!process.permission.has('FileSystemWrite', Buffer.from('reference'))); | | 27 | +} |
|