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