Skip to content

Commit 9d057ae

Browse files
committed
test_runner: protect internals against prototype tampering
1 parent 42ad967 commit 9d057ae

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

lib/internal/test_runner/harness.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ function test(name, options, fn) {
126126
return subtest.start();
127127
}
128128

129-
const root = new Test({ name: '<root>' });
129+
const root = new Test({ __proto__: null, name: '<root>' });
130130

131131
module.exports = FunctionPrototypeBind(test, root);

lib/internal/test_runner/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class Test extends AsyncResource {
217217
}
218218
}
219219

220-
const test = new Test({ fn, name, parent, ...options });
220+
const test = new Test({ __proto__: null, fn, name, parent, ...options });
221221

222222
if (parent.waitingOn === 0) {
223223
parent.waitingOn = test.testNumber;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
Object.prototype.skip = true;

test/parallel/test-runner-cli.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ const testFixtures = fixtures.path('test-runner');
3434
assert.match(stdout, /ok 4 - .+random\.cjs/);
3535
}
3636

37+
{
38+
// Same but with a prototype mutation in require scripts.
39+
const args = ['--require', join(testFixtures, 'protoMutation.js'), '--test', testFixtures];
40+
const child = spawnSync(process.execPath, args);
41+
42+
const stdout = child.stdout.toString();
43+
assert.match(stdout, /ok 1 - .+index\.test\.js/);
44+
assert.match(stdout, /not ok 2 - .+random\.test\.mjs/);
45+
assert.match(stdout, /not ok 1 - this should fail/);
46+
assert.match(stdout, /ok 3 - .+subdir.+subdir_test\.js/);
47+
assert.match(stdout, /ok 4 - .+random\.cjs/);
48+
assert.strictEqual(child.status, 1);
49+
assert.strictEqual(child.signal, null);
50+
assert.strictEqual(child.stderr.toString(), '');
51+
}
52+
3753
{
3854
// User specified files that don't match the pattern are still run.
3955
const args = ['--test', testFixtures, join(testFixtures, 'index.js')];

0 commit comments

Comments
 (0)