Skip to content

Commit beba631

Browse files
Lordfirespeedtargos
authored andcommitted
test: add tests ensuring worker threads cannot access internals
Refs: #57804 (comment) Refs: #57804 (comment) PR-URL: #58332 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Dario Piotrowicz <[email protected]>
1 parent 252acc1 commit beba631

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import '../common/index.mjs';
2+
import tmpdir from '../common/tmpdir.js';
3+
import assert from 'node:assert/strict';
4+
import { once } from 'node:events';
5+
import fs from 'node:fs/promises';
6+
import { describe, test, before } from 'node:test';
7+
import { Worker } from 'node:worker_threads';
8+
9+
const accessInternalsSource = `
10+
import 'node:internal/freelist';
11+
`;
12+
13+
function convertScriptSourceToDataUrl(script) {
14+
return new URL(`data:text/javascript,${encodeURIComponent(script)}`);
15+
}
16+
17+
describe('Worker threads should not be able to access internal modules', () => {
18+
before(() => tmpdir.refresh());
19+
20+
test('worker instantiated with module file path', async () => {
21+
const moduleFilepath = tmpdir.resolve('test-worker-internal-modules.mjs');
22+
await fs.writeFile(moduleFilepath, accessInternalsSource);
23+
const w = new Worker(moduleFilepath);
24+
await assert.rejects(once(w, 'exit'), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' });
25+
});
26+
27+
test('worker instantiated with module source', async () => {
28+
const w = new Worker(accessInternalsSource, { eval: true });
29+
await assert.rejects(once(w, 'exit'), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' });
30+
});
31+
32+
test('worker instantiated with data: URL', async () => {
33+
const w = new Worker(convertScriptSourceToDataUrl(accessInternalsSource));
34+
await assert.rejects(once(w, 'exit'), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' });
35+
});
36+
});

0 commit comments

Comments
 (0)