lib: have runFile honor permissions and accept URL/Buffer paths · nodejs/node@ba7baed · GitHub
Skip to content

Commit ba7baed

Browse files
jasnelladuh95
authored andcommitted
lib: have runFile honor permissions and accept URL/Buffer paths
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #65631 Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 55103db commit ba7baed

3 files changed

Lines changed: 120 additions & 19 deletions

File tree

‎doc/api/bench.md‎

Lines changed: 14 additions & 10 deletions

‎lib/internal/bench_runner/cli.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ const {
3636
StringPrototypeStartsWith,
3737
StringPrototypeToUpperCase,
3838
SymbolDispose,
39+
uncurryThis,
3940
} = primordials;
41+
const { Buffer } = require('buffer');
42+
const BufferToString = uncurryThis(Buffer.prototype.toString);
4043
const { spawn } = require('child_process');
4144
const { createWriteStream, statSync } = require('fs');
4245
const { Glob } = require('internal/fs/glob');
46+
const { getValidatedPath } = require('internal/fs/utils');
4347
const {
4448
BenchmarksStream,
4549
} = require('internal/bench_runner/benchmarks_stream');
@@ -53,6 +57,7 @@ const { deserializeError, serializeError } = require('internal/error_serdes');
5357
const {
5458
AbortError,
5559
codes: {
60+
ERR_ACCESS_DENIED,
5661
ERR_INVALID_ARG_TYPE,
5762
ERR_INVALID_ARG_VALUE,
5863
ERR_INVALID_STATE,
@@ -64,6 +69,7 @@ const {
6469
getOptionValue,
6570
getOptionsAsFlagsFromBinding,
6671
} = require('internal/options');
72+
const permission = require('internal/process/permission');
6773
const { TIMEOUT_MAX } = require('internal/timers');
6874
const { kEmptyObject } = require('internal/util');
6975
const {
@@ -75,7 +81,7 @@ const {
7581
} = require('internal/validators');
7682
const { pathToFileURL } = require('internal/url');
7783
const { pipeline } = require('stream/promises');
78-
const { isAbsolute, resolve, sep } = require('path');
84+
const { resolve, sep } = require('path');
7985
const { clearTimeout, setTimeout } = require('timers');
8086

8187
const console = require('internal/console/global');
@@ -758,6 +764,16 @@ async function runChild(path, options, scope, onRecord) {
758764
}),
759765
};
760766
}
767+
const resource = resolve(options.cwd, path);
768+
if (permission.isEnabled() &&
769+
!permission.has('fs.read', resource) &&
770+
!permission.isAuditMode()) {
771+
throw new ERR_ACCESS_DENIED(
772+
'Access to this API has been restricted. Use --allow-fs-read to manage permissions.',
773+
'FileSystemRead',
774+
resource,
775+
);
776+
}
761777
const child = spawn(
762778
options.execPath ?? process.execPath,
763779
getChildArgs(path, options),
@@ -1116,10 +1132,8 @@ async function runIsolated(files, options, output) {
11161132
}
11171133

11181134
function runFile(path, options = kEmptyObject) {
1119-
validateStringWithoutNullBytes(path, 'path');
1120-
if (!isAbsolute(path)) {
1121-
throw new ERR_INVALID_ARG_VALUE('path', path, 'must be an absolute path');
1122-
}
1135+
path = getValidatedPath(path);
1136+
if (typeof path !== 'string') path = BufferToString(path);
11231137
const file = resolve(path);
11241138
validateObject(options, 'options');
11251139
const {

‎test/parallel/test-bench-run-file.js‎

Lines changed: 87 additions & 4 deletions

0 commit comments

Comments
 (0)