Skip to content

Commit

Permalink
child_process: ensure message sanity at source
Browse files Browse the repository at this point in the history
Error messages coming out of de-serialization at the send target
is not consumable. So detect the scenario and fix it at the send source

Ref: #20314

PR-URL: #24787
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
gireeshpunathil committed Mar 18, 2019
1 parent 993bdff commit daa97df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,18 @@ function setupChannel(target, channel) {
if (message === undefined)
throw new ERR_MISSING_ARGS('message');

// Non-serializable messages should not reach the remote
// end point; as any failure in the stringification there
// will result in error message that is weakly consumable.
// So perform a sanity check on message prior to sending.
if (typeof message !== 'string' &&
typeof message !== 'object' &&
typeof message !== 'number' &&
typeof message !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE(
'message', ['string', 'object', 'number', 'boolean'], message);
}

// Support legacy function signature
if (typeof options === 'boolean') {
options = { swallowErrors: options };
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-child-process-fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ assert.throws(() => n.send(), {
code: 'ERR_MISSING_ARGS'
});

assert.throws(() => n.send(Symbol()), {
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "message" argument must be one of type string,' +
' object, number, or boolean. Received type symbol',
code: 'ERR_INVALID_ARG_TYPE'
});
n.send({ hello: 'world' });

n.on('exit', common.mustCall((c) => {
Expand Down

0 comments on commit daa97df

Please sign in to comment.