Skip to content

Commit 1b66e15

Browse files
committed
fix: guard worker options from proto pollution
1 parent abc247f commit 1b66e15

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,14 @@ class ThreadPool {
655655
const filename = options.filename
656656
? maybeFileURLToPath(options.filename)
657657
: null
658-
this.options = { ...kDefaultOptions, ...options, filename, maxQueue: 0 }
658+
659+
this.options = Object.assign(
660+
Object.create(null),
661+
kDefaultOptions,
662+
options,
663+
{ filename, maxQueue: 0 }
664+
)
665+
659666
// The >= and <= could be > and < but this way we get 100 % coverage 🙃
660667
if (
661668
options.maxThreads !== undefined &&
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('Unexpectedly loaded fails-when-loaded.mjs!')

test/options.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolve } from 'node:path'
12
import { expect, test, vi } from 'vitest'
23

34
let Tinypool: typeof import('tinypool').default
@@ -47,6 +48,31 @@ test('fractional thread limits in the wrong order throw an error', async () => {
4748
}).toThrow()
4849
})
4950

51+
test('ignores worker options from prototype', async () => {
52+
{
53+
const failsWhenLoaded = resolve(__dirname, 'fixtures/fails-when-loaded.mjs')
54+
55+
onTestFinished(() => {
56+
// @ts-expect-error -- intentional
57+
delete Object.prototype.execArgv
58+
// @ts-expect-error -- intentional
59+
delete Object.prototype.env
60+
})
61+
62+
// @ts-expect-error -- intentional
63+
Object.prototype.execArgv = ['--import', failsWhenLoaded]
64+
65+
// @ts-expect-error -- intentional
66+
Object.prototype.env = { NODE_OPTIONS: `--import ${failsWhenLoaded}` }
67+
}
68+
69+
const worker = new Tinypool({
70+
filename: resolve(__dirname, 'fixtures/eval.js'),
71+
})
72+
const result = await worker.run('42')
73+
expect(result).toBe(42)
74+
})
75+
5076
vi.mock(import('node:os'), async (importOriginal) => {
5177
const original = await importOriginal()
5278
return {

0 commit comments

Comments
 (0)