Skip to content

Commit

Permalink
fix: do not allow empty or invalid node args when spin up child proce…
Browse files Browse the repository at this point in the history
…ss (#73)
  • Loading branch information
mistic authored and evilebottnawi committed Aug 6, 2019
1 parent d529c77 commit b02d503
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/WorkerPool.js
Expand Up @@ -18,8 +18,12 @@ class PoolWorker {
this.activeJobs = 0;
this.onJobDone = onJobDone;
this.id = workerId;

workerId += 1;
this.worker = childProcess.spawn(process.execPath, [].concat(options.nodeArgs || []).concat(workerPath, options.parallelJobs), {
// Empty or invalid node args would break the child process
const sanitizedNodeArgs = (options.nodeArgs || []).filter(opt => !!opt);

this.worker = childProcess.spawn(process.execPath, [].concat(sanitizedNodeArgs).concat(workerPath, options.parallelJobs), {
detached: true,
stdio: ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'],
});
Expand Down
24 changes: 24 additions & 0 deletions test/workerPool.test.js
Expand Up @@ -41,4 +41,28 @@ describe('workerPool', () => {
workerPool.terminate();
expect(workerPool.isAbleToRun()).toBe(false);
});

it('should sanitize nodeArgs when spawn a child process', () => {
childProcess.spawn.mockClear();
childProcess.spawn.mockImplementationOnce(() => {
return {
stdio: new Array(5).fill(new stream.PassThrough()),
unref: jest.fn(),
};
});

const workerPool = new WorkerPool({
workerNodeArgs: [
'--max-old-space-size=1024',
'',
null,
],
workerParallelJobs: 20,
});

expect(() => workerPool.createWorker()).not.toThrow();

const nonSanitizedNodeArgs = childProcess.spawn.mock.calls[0][1].filter(opt => !opt);
expect(nonSanitizedNodeArgs.length).toEqual(0);
});
});

0 comments on commit b02d503

Please sign in to comment.