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+
importtmpdirfrom'../common/tmpdir.js';
3+
importassertfrom'node:assert/strict';
4+
import{once}from'node:events';
5+
importfsfrom'node:fs/promises';
6+
import{describe,test,before}from'node:test';
7+
import{Worker}from'node:worker_threads';
8+
9+
constaccessInternalsSource=`
10+
import 'node:internal/freelist'
11+
`;
12+
13+
functionconvertScriptSourceToDataUrl(script){
14+
returnnewURL(`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+
constmoduleFilepath=tmpdir.resolve('test-worker-internal-modules.mjs');
22+
awaitfs.writeFile(moduleFilepath,accessInternalsSource);
23+
constw=newWorker(moduleFilepath);
24+
awaitassert.rejects(once(w,'exit'),{code: 'ERR_UNKNOWN_BUILTIN_MODULE'});
25+
});
26+
27+
test('worker instantiated with module source',async()=>{
28+
constw=newWorker(accessInternalsSource,{eval: true});
29+
awaitassert.rejects(once(w,'exit'),{code: 'ERR_UNKNOWN_BUILTIN_MODULE'});
30+
});
31+
32+
test('worker instantiated with data: URL',async()=>{
33+
constw=newWorker(convertScriptSourceToDataUrl(accessInternalsSource));
34+
awaitassert.rejects(once(w,'exit'),{code: 'ERR_UNKNOWN_BUILTIN_MODULE'});
35+
});
36+
});

0 commit comments

Comments
(0)