Skip to content

Commit 3a74316

Browse files
rluvatontargos
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 a781d24 commit 3a74316

File tree

12 files changed

+186
-25
lines changed

12 files changed

+186
-25
lines changed

.eslintrc.js

+8
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ module.exports = {
122122
'curly': 'error',
123123
},
124124
},
125+
{
126+
files: [
127+
'lib/internal/test_runner/**/*.js',
128+
],
129+
rules: {
130+
'node-core/set-proto-to-null-in-object': 'error',
131+
},
132+
},
125133
],
126134
rules: {
127135
// 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
@@ -114,6 +114,7 @@ function setup(root) {
114114
const globalOptions = parseCommandLine();
115115

116116
const hook = createHook({
117+
__proto__: null,
117118
init(asyncId, type, triggerAsyncId, resource) {
118119
if (resource instanceof Test) {
119120
testResources.set(asyncId, resource);
@@ -214,7 +215,7 @@ function runInParentContext(Factory) {
214215

215216
const test = (name, options, fn) => run(name, options, fn);
216217
ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
217-
test[keyword] = (name, options, fn) => run(name, options, fn, { [keyword]: true });
218+
test[keyword] = (name, options, fn) => run(name, options, fn, { __proto__: null, [keyword]: true });
218219
});
219220
return test;
220221
}

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
@@ -44,7 +44,7 @@ function setPosition(node, pos) {
4444
}
4545

4646
function abortIt(signal) {
47-
return new AbortError(undefined, { cause: signal.reason });
47+
return new AbortError(undefined, { __proto__: null, cause: signal.reason });
4848
}
4949

5050
const SUPPORTED_TIMERS = ['setTimeout', 'setInterval'];
@@ -193,7 +193,9 @@ class MockTimers {
193193

194194
#toggleEnableTimers(activate) {
195195
const options = {
196+
__proto__: null,
196197
toFake: {
198+
__proto__: null,
197199
setTimeout: () => {
198200
this.#realSetTimeout = globalThis.setTimeout;
199201
this.#realClearTimeout = globalThis.clearTimeout;
@@ -232,6 +234,7 @@ class MockTimers {
232234
},
233235
},
234236
toReal: {
237+
__proto__: null,
235238
setTimeout: () => {
236239
globalThis.setTimeout = this.#realSetTimeout;
237240
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

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

137-
processPath(absolutePath, testFiles, { userSupplied: true });
137+
processPath(absolutePath, testFiles, {
138+
__proto__: null,
139+
userSupplied: true,
140+
});
138141
}
139142
} catch (err) {
140143
if (err?.code === 'ENOENT') {
@@ -347,9 +350,9 @@ class FileTest extends Test {
347350
function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
348351
const watchMode = filesWatcher != null;
349352
const subtest = root.createSubtest(FileTest, path, async (t) => {
350-
const args = getRunArgs({ path, inspectPort, testNamePatterns });
353+
const args = getRunArgs({ __proto__: null, path, inspectPort, testNamePatterns });
351354
const stdio = ['pipe', 'pipe', 'pipe'];
352-
const env = { ...process.env, NODE_TEST_CONTEXT: 'child-v8' };
355+
const env = { __proto__: null, ...process.env, NODE_TEST_CONTEXT: 'child-v8' };
353356
if (watchMode) {
354357
stdio.push('ipc');
355358
env.WATCH_REPORT_DEPENDENCIES = '1';
@@ -358,7 +361,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
358361
env.FORCE_COLOR = '1';
359362
}
360363

361-
const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio });
364+
const child = spawn(process.execPath, args, { __proto__: null, signal: t.signal, encoding: 'utf8', env, stdio });
362365
if (watchMode) {
363366
filesWatcher.runningProcesses.set(path, child);
364367
filesWatcher.watcher.watchChildProcessModules(child, path);
@@ -375,7 +378,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
375378
subtest.parseMessage(data);
376379
});
377380

378-
const rl = createInterface({ input: child.stderr });
381+
const rl = createInterface({ __proto__: null, input: child.stderr });
379382
rl.on('line', (line) => {
380383
if (isInspectorMessage(line)) {
381384
process.stderr.write(line + '\n');
@@ -393,8 +396,8 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
393396
});
394397

395398
const { 0: { 0: code, 1: signal } } = await SafePromiseAll([
396-
once(child, 'exit', { signal: t.signal }),
397-
finished(child.stdout, { signal: t.signal }),
399+
once(child, 'exit', { __proto__: null, signal: t.signal }),
400+
finished(child.stdout, { __proto__: null, signal: t.signal }),
398401
]);
399402

400403
if (watchMode) {
@@ -427,7 +430,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
427430
function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
428431
const runningProcesses = new SafeMap();
429432
const runningSubtests = new SafeMap();
430-
const watcher = new FilesWatcher({ debounce: 200, mode: 'filter', signal });
433+
const watcher = new FilesWatcher({ __proto__: null, debounce: 200, mode: 'filter', signal });
431434
const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests };
432435

433436
watcher.on('changed', ({ owners }) => {
@@ -512,7 +515,7 @@ function run(options) {
512515
});
513516
}
514517

515-
const root = createTestTree({ concurrency, timeout, signal });
518+
const root = createTestTree({ __proto__: null, concurrency, timeout, signal });
516519
let testFiles = files ?? createTestFileList();
517520

518521
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
@@ -82,7 +82,7 @@ function createDeferredCallback() {
8282
resolve();
8383
};
8484

85-
return { promise, cb };
85+
return { __proto__: null, promise, cb };
8686
}
8787

8888
function isTestFailureError(err) {

0 commit comments

Comments
 (0)