|
| 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