Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/parallel/test-worker-internal-modules.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
import assert from 'node:assert/strict';
import { once } from 'node:events';
import fs from 'node:fs/promises';
import { describe, test, before } from 'node:test';
import { Worker } from 'node:worker_threads';

const accessInternalsSource = `
import 'node:internal/freelist';
`;

function convertScriptSourceToDataUrl(script) {
return new URL(`data:text/javascript,${encodeURIComponent(script)}`);
}

describe('Worker threads should not be able to access internal modules', () => {
before(() => tmpdir.refresh());

test('worker instantiated with module file path', async () => {
const moduleFilepath = tmpdir.resolve('test-worker-internal-modules.mjs');
await fs.writeFile(moduleFilepath, accessInternalsSource);
const w = new Worker(moduleFilepath);
await assert.rejects(once(w, 'exit'), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' });
});

test('worker instantiated with module source', async () => {
const w = new Worker(accessInternalsSource, { eval: true });
await assert.rejects(once(w, 'exit'), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' });
});

test('worker instantiated with data: URL', async () => {
const w = new Worker(convertScriptSourceToDataUrl(accessInternalsSource));
await assert.rejects(once(w, 'exit'), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' });
});
});
Loading