|
| 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 | +}); |