Skip to content

Commit b02d503

Browse files
misticevilebottnawi
authored andcommitted
fix: do not allow empty or invalid node args when spin up child process (#73)
1 parent d529c77 commit b02d503

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/WorkerPool.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ class PoolWorker {
1818
this.activeJobs = 0;
1919
this.onJobDone = onJobDone;
2020
this.id = workerId;
21+
2122
workerId += 1;
22-
this.worker = childProcess.spawn(process.execPath, [].concat(options.nodeArgs || []).concat(workerPath, options.parallelJobs), {
23+
// Empty or invalid node args would break the child process
24+
const sanitizedNodeArgs = (options.nodeArgs || []).filter(opt => !!opt);
25+
26+
this.worker = childProcess.spawn(process.execPath, [].concat(sanitizedNodeArgs).concat(workerPath, options.parallelJobs), {
2327
detached: true,
2428
stdio: ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'],
2529
});

test/workerPool.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,28 @@ describe('workerPool', () => {
4141
workerPool.terminate();
4242
expect(workerPool.isAbleToRun()).toBe(false);
4343
});
44+
45+
it('should sanitize nodeArgs when spawn a child process', () => {
46+
childProcess.spawn.mockClear();
47+
childProcess.spawn.mockImplementationOnce(() => {
48+
return {
49+
stdio: new Array(5).fill(new stream.PassThrough()),
50+
unref: jest.fn(),
51+
};
52+
});
53+
54+
const workerPool = new WorkerPool({
55+
workerNodeArgs: [
56+
'--max-old-space-size=1024',
57+
'',
58+
null,
59+
],
60+
workerParallelJobs: 20,
61+
});
62+
63+
expect(() => workerPool.createWorker()).not.toThrow();
64+
65+
const nonSanitizedNodeArgs = childProcess.spawn.mock.calls[0][1].filter(opt => !opt);
66+
expect(nonSanitizedNodeArgs.length).toEqual(0);
67+
});
4468
});

0 commit comments

Comments
 (0)