mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
test: add tests ensuring worker threads cannot access internals
Refs: https://github.com/nodejs/node/pull/57804#discussion_r2079257457 Refs: https://github.com/nodejs/node/pull/57804#issuecomment-2863740780 PR-URL: https://github.com/nodejs/node/pull/58332 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com>
This commit is contained in:
parent
6f045771fa
commit
e7255b6bab
36
test/parallel/test-worker-internal-modules.mjs
Normal file
36
test/parallel/test-worker-internal-modules.mjs
Normal file
|
|
@ -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…
Reference in New Issue
Block a user