Skip to content

Commit fa19b0e

Browse files
rluvatonRafaelGSS
authored andcommitted
test_runner: add __proto__ null
PR-URL: #48663 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 314bd60 commit fa19b0e

File tree

12 files changed

+183
-25
lines changed

12 files changed

+183
-25
lines changed

.eslintrc.js

+8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ module.exports = {
111111
},
112112
] },
113113
},
114+
{
115+
files: [
116+
'lib/internal/test_runner/**/*.js',
117+
],
118+
rules: {
119+
'node-core/set-proto-to-null-in-object': 'error',
120+
},
121+
},
114122
],
115123
rules: {
116124
// ESLint built-in rules

lib/internal/test_runner/coverage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class TestCoverage {
250250
let dir;
251251

252252
try {
253-
mkdirSync(this.originalCoverageDirectory, { recursive: true });
253+
mkdirSync(this.originalCoverageDirectory, { __proto__: null, recursive: true });
254254
dir = opendirSync(this.coverageDirectory);
255255

256256
for (let entry; (entry = dir.readSync()) !== null;) {

lib/internal/test_runner/harness.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ function setup(root) {
116116
const globalOptions = parseCommandLine();
117117

118118
const hook = createHook({
119+
__proto__: null,
119120
init(asyncId, type, triggerAsyncId, resource) {
120121
if (resource instanceof Test) {
121122
testResources.set(asyncId, resource);
@@ -216,7 +217,7 @@ function runInParentContext(Factory) {
216217

217218
const test = (name, options, fn) => run(name, options, fn);
218219
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
219-
test[keyword] = (name, options, fn) => run(name, options, fn, { [keyword]: true });
220+
test[keyword] = (name, options, fn) => run(name, options, fn, { __proto__: null, [keyword]: true });
220221
});
221222
return test;
222223
}

lib/internal/test_runner/mock/mock.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class MockTracker {
133133
validateObject(options, 'options');
134134
const { times = Infinity } = options;
135135
validateTimes(times, 'options.times');
136-
const ctx = new MockFunctionContext(implementation, { original }, times);
136+
const ctx = new MockFunctionContext(implementation, { __proto__: null, original }, times);
137137
return this.#setupMock(ctx, original);
138138
}
139139

@@ -189,7 +189,7 @@ class MockTracker {
189189
);
190190
}
191191

192-
const restore = { descriptor, object: objectOrFunction, methodName };
192+
const restore = { __proto__: null, descriptor, object: objectOrFunction, methodName };
193193
const impl = implementation === kDefaultFunction ?
194194
original : implementation;
195195
const ctx = new MockFunctionContext(impl, restore, times);
@@ -238,6 +238,7 @@ class MockTracker {
238238
}
239239

240240
return this.method(object, methodName, implementation, {
241+
__proto__: null,
241242
...options,
242243
getter,
243244
});
@@ -265,6 +266,7 @@ class MockTracker {
265266
}
266267

267268
return this.method(object, methodName, implementation, {
269+
__proto__: null,
268270
...options,
269271
setter,
270272
});
@@ -297,6 +299,7 @@ class MockTracker {
297299
throw err;
298300
} finally {
299301
FunctionPrototypeCall(trackCall, ctx, {
302+
__proto__: null,
300303
arguments: argList,
301304
error,
302305
result,
@@ -321,6 +324,7 @@ class MockTracker {
321324
throw err;
322325
} finally {
323326
FunctionPrototypeCall(trackCall, ctx, {
327+
__proto__: null,
324328
arguments: argList,
325329
error,
326330
result,

lib/internal/test_runner/mock/mock_timers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function setPosition(node, pos) {
4545
}
4646

4747
function abortIt(signal) {
48-
return new AbortError(undefined, { cause: signal.reason });
48+
return new AbortError(undefined, { __proto__: null, cause: signal.reason });
4949
}
5050

5151
const SUPPORTED_TIMERS = ['setTimeout', 'setInterval'];
@@ -194,7 +194,9 @@ class MockTimers {
194194

195195
#toggleEnableTimers(activate) {
196196
const options = {
197+
__proto__: null,
197198
toFake: {
199+
__proto__: null,
198200
setTimeout: () => {
199201
this.#realSetTimeout = globalThis.setTimeout;
200202
this.#realClearTimeout = globalThis.clearTimeout;
@@ -233,6 +235,7 @@ class MockTimers {
233235
},
234236
},
235237
toReal: {
238+
__proto__: null,
236239
setTimeout: () => {
237240
globalThis.setTimeout = this.#realSetTimeout;
238241
globalThis.clearTimeout = this.#realClearTimeout;

lib/internal/test_runner/reporter/spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SpecReporter extends Transform {
4242
#failedTests = [];
4343

4444
constructor() {
45-
super({ writableObjectMode: true });
45+
super({ __proto__: null, writableObjectMode: true });
4646
}
4747

4848
#indent(nesting) {
@@ -127,7 +127,7 @@ class SpecReporter extends Transform {
127127
}
128128
}
129129
_transform({ type, data }, encoding, callback) {
130-
callback(null, this.#handleEvent({ type, data }));
130+
callback(null, this.#handleEvent({ __proto__: null, type, data }));
131131
}
132132
_flush(callback) {
133133
if (this.#failedTests.length === 0) {

lib/internal/test_runner/reporter/tap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const kDefaultIndent = ' '; // 4 spaces
1818
const kFrameStartRegExp = /^ {4}at /;
1919
const kLineBreakRegExp = /\n|\r\n/;
2020
const kDefaultTAPVersion = 13;
21-
const inspectOptions = { colors: false, breakLength: Infinity };
21+
const inspectOptions = { __proto__: null, colors: false, breakLength: Infinity };
2222
let testModule; // Lazy loaded due to circular dependency.
2323

2424
function lazyLoadTest() {

lib/internal/test_runner/runner.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function createTestFileList() {
135135
for (let i = 0; i < testPaths.length; i++) {
136136
const absolutePath = resolve(testPaths[i]);
137137

138-
processPath(absolutePath, testFiles, { userSupplied: true });
138+
processPath(absolutePath, testFiles, { __proto__: null, userSupplied: true });
139139
}
140140
} catch (err) {
141141
if (err?.code === 'ENOENT') {
@@ -348,9 +348,9 @@ class FileTest extends Test {
348348
function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
349349
const watchMode = filesWatcher != null;
350350
const subtest = root.createSubtest(FileTest, path, async (t) => {
351-
const args = getRunArgs({ path, inspectPort, testNamePatterns });
351+
const args = getRunArgs({ __proto__: null, path, inspectPort, testNamePatterns });
352352
const stdio = ['pipe', 'pipe', 'pipe'];
353-
const env = { ...process.env, NODE_TEST_CONTEXT: 'child-v8' };
353+
const env = { __proto__: null, ...process.env, NODE_TEST_CONTEXT: 'child-v8' };
354354
if (watchMode) {
355355
stdio.push('ipc');
356356
env.WATCH_REPORT_DEPENDENCIES = '1';
@@ -359,7 +359,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
359359
env.FORCE_COLOR = '1';
360360
}
361361

362-
const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio });
362+
const child = spawn(process.execPath, args, { __proto__: null, signal: t.signal, encoding: 'utf8', env, stdio });
363363
if (watchMode) {
364364
filesWatcher.runningProcesses.set(path, child);
365365
filesWatcher.watcher.watchChildProcessModules(child, path);
@@ -376,7 +376,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
376376
subtest.parseMessage(data);
377377
});
378378

379-
const rl = createInterface({ input: child.stderr });
379+
const rl = createInterface({ __proto__: null, input: child.stderr });
380380
rl.on('line', (line) => {
381381
if (isInspectorMessage(line)) {
382382
process.stderr.write(line + '\n');
@@ -394,8 +394,8 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
394394
});
395395

396396
const { 0: { 0: code, 1: signal } } = await SafePromiseAll([
397-
once(child, 'exit', { signal: t.signal }),
398-
finished(child.stdout, { signal: t.signal }),
397+
once(child, 'exit', { __proto__: null, signal: t.signal }),
398+
finished(child.stdout, { __proto__: null, signal: t.signal }),
399399
]);
400400

401401
if (watchMode) {
@@ -428,7 +428,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
428428
function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
429429
const runningProcesses = new SafeMap();
430430
const runningSubtests = new SafeMap();
431-
const watcher = new FilesWatcher({ debounce: 200, mode: 'filter', signal });
431+
const watcher = new FilesWatcher({ __proto__: null, debounce: 200, mode: 'filter', signal });
432432
const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests };
433433

434434
watcher.on('changed', ({ owners }) => {
@@ -513,7 +513,7 @@ function run(options) {
513513
});
514514
}
515515

516-
const root = createTestTree({ concurrency, timeout, signal });
516+
const root = createTestTree({ __proto__: null, concurrency, timeout, signal });
517517
let testFiles = files ?? createTestFileList();
518518

519519
if (shard) {

lib/internal/test_runner/test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function stopTest(timeout, signal) {
7979
if (timeout === kDefaultTimeout) {
8080
return once(signal, 'abort');
8181
}
82-
return PromisePrototypeThen(setTimeout(timeout, null, { ref: false, signal }), () => {
82+
return PromisePrototypeThen(setTimeout(timeout, null, { __proto__: null, ref: false, signal }), () => {
8383
throw new ERR_TEST_FAILURE(
8484
`test timed out after ${timeout}ms`,
8585
kTestTimeoutFailure,
@@ -506,7 +506,7 @@ class Test extends AsyncResource {
506506

507507
getRunArgs() {
508508
const ctx = new TestContext(this);
509-
return { ctx, args: [ctx] };
509+
return { __proto__: null, ctx, args: [ctx] };
510510
}
511511

512512
async runHook(hook, args) {
@@ -540,12 +540,12 @@ class Test extends AsyncResource {
540540
const { args, ctx } = this.getRunArgs();
541541
const after = async () => {
542542
if (this.hooks.after.length > 0) {
543-
await this.runHook('after', { args, ctx });
543+
await this.runHook('after', { __proto__: null, args, ctx });
544544
}
545545
};
546546
const afterEach = runOnce(async () => {
547547
if (this.parent?.hooks.afterEach.length > 0) {
548-
await this.parent.runHook('afterEach', { args, ctx });
548+
await this.parent.runHook('afterEach', { __proto__: null, args, ctx });
549549
}
550550
});
551551

@@ -554,7 +554,7 @@ class Test extends AsyncResource {
554554
await this.parent.runHook('before', this.parent.getRunArgs());
555555
}
556556
if (this.parent?.hooks.beforeEach.length > 0) {
557-
await this.parent.runHook('beforeEach', { args, ctx });
557+
await this.parent.runHook('beforeEach', { __proto__: null, args, ctx });
558558
}
559559
const stopPromise = stopTest(this.timeout, this.signal);
560560
const runArgs = ArrayPrototypeSlice(args);
@@ -811,7 +811,7 @@ class Suite extends Test {
811811

812812
getRunArgs() {
813813
const ctx = new SuiteContext(this);
814-
return { ctx, args: [ctx] };
814+
return { __proto__: null, ctx, args: [ctx] };
815815
}
816816

817817
async run() {

lib/internal/test_runner/tests_stream.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TestsStream extends Readable {
1212
#canPush;
1313

1414
constructor() {
15-
super({ objectMode: true });
15+
super({ __proto__: null, objectMode: true });
1616
this.#buffer = [];
1717
this.#canPush = true;
1818
}
@@ -83,6 +83,8 @@ class TestsStream extends Readable {
8383

8484
[kEmitMessage](type, data) {
8585
this.emit(type, data);
86+
// Disabling as this going to the user-land
87+
// eslint-disable-next-line node-core/set-proto-to-null-in-object
8688
this.#tryPush({ type, data });
8789
}
8890

lib/internal/test_runner/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function createDeferredCallback() {
8181
resolve();
8282
};
8383

84-
return { promise, cb };
84+
return { __proto__: null, promise, cb };
8585
}
8686

8787
function isTestFailureError(err) {

0 commit comments

Comments
 (0)