Skip to content

test: add tests ensuring worker threads cannot access internals #58332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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