Skip to content

Commit f41411a

Browse files
AriPerkkioFcmam5
andauthored
fix: guard worker filename from proto pollution (#135)
* test: add failing test * refactor: null-ify prototypes * refactor: move `withNullPrototype` to common module --------- Co-authored-by: Abdeldjalil Fortas <[email protected]>
1 parent 4b5229a commit f41411a

3 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎src/common.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ export function markMovable(value: object): void {
111111
})
112112
}
113113

114+
// Copy own properties onto a prototype-less object, so that options are never
115+
// resolved through a polluted prototype chain.
116+
export function withNullPrototype<T extends object>(source: T): T {
117+
return Object.assign(Object.create(null), source)
118+
}
119+
114120
export interface Transferable {
115121
readonly [kTransferable]: object
116122
readonly [kValue]: object

‎src/index.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
isTransferable,
2929
markMovable,
3030
isMovable,
31+
withNullPrototype,
3132
kTransferable,
3233
kValue,
3334
type TinypoolData,
@@ -656,12 +657,12 @@ class ThreadPool {
656657
? maybeFileURLToPath(options.filename)
657658
: null
658659

659-
this.options = Object.assign(
660-
Object.create(null),
661-
kDefaultOptions,
662-
options,
663-
{ filename, maxQueue: 0 }
664-
)
660+
this.options = withNullPrototype({
661+
...kDefaultOptions,
662+
...options,
663+
filename,
664+
maxQueue: 0,
665+
})
665666

666667
// The >= and <= could be > and < but this way we get 100 % coverage 🙃
667668
if (
@@ -1108,7 +1109,8 @@ class ThreadPool {
11081109
await Promise.all(exitEvents)
11091110
}
11101111

1111-
async recycleWorkers(options: Pick<Options, 'runtime'> = {}) {
1112+
async recycleWorkers(_options: Pick<Options, 'runtime'> = {}) {
1113+
const options = withNullPrototype(_options)
11121114
const runtimeChanged =
11131115
options?.runtime && options.runtime !== this.options.runtime
11141116

@@ -1148,7 +1150,9 @@ class ThreadPool {
11481150
class Tinypool extends EventEmitterAsyncResource {
11491151
#pool: ThreadPool
11501152

1151-
constructor(options: Options = {}) {
1153+
constructor(_options: Options = {}) {
1154+
const options = withNullPrototype(_options)
1155+
11521156
// convert fractional option values to int
11531157
if (
11541158
options.minThreads !== undefined &&
@@ -1187,7 +1191,8 @@ class Tinypool extends EventEmitterAsyncResource {
11871191
}
11881192

11891193
run(task: any, options: RunOptions = kDefaultRunOptions) {
1190-
const { transferList, filename, name, signal, runtime, channel } = options
1194+
const { transferList, filename, name, signal, runtime, channel } =
1195+
withNullPrototype(options)
11911196

11921197
return this.#pool.runTask(task, {
11931198
transferList,

‎test/options.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ test('ignores worker options from prototype', async () => {
7373
expect(result).toBe(42)
7474
})
7575

76+
test('ignores worker filename from prototype', async () => {
77+
{
78+
const failsWhenLoaded = resolve(__dirname, 'fixtures/fails-when-loaded.mjs')
79+
80+
onTestFinished(() => {
81+
// @ts-expect-error -- intentional
82+
delete Object.prototype.filename
83+
})
84+
85+
// @ts-expect-error -- intentional
86+
Object.prototype.filename = failsWhenLoaded
87+
}
88+
89+
const worker = new Tinypool({
90+
filename: resolve(__dirname, 'fixtures/eval.js'),
91+
})
92+
const result = await worker.run('42', {
93+
signal: new AbortController().signal,
94+
})
95+
expect(result).toBe(42)
96+
})
97+
7698
vi.mock(import('node:os'), async (importOriginal) => {
7799
const original = await importOriginal()
78100
return {

0 commit comments

Comments
 (0)