Skip to content

Commit fce822f

Browse files
gireeshpunathilBethGriggs
authored andcommitted
child_process: ensure message sanity at source
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 <luigipinca@gmail.com>
1 parent 1e6faf9 commit fce822f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/internal/child_process.js

+12
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,18 @@ function setupChannel(target, channel) {
648648
if (message === undefined)
649649
throw new ERR_MISSING_ARGS('message');
650650

651+
// Non-serializable messages should not reach the remote
652+
// end point; as any failure in the stringification there
653+
// will result in error message that is weakly consumable.
654+
// So perform a sanity check on message prior to sending.
655+
if (typeof message !== 'string' &&
656+
typeof message !== 'object' &&
657+
typeof message !== 'number' &&
658+
typeof message !== 'boolean') {
659+
throw new ERR_INVALID_ARG_TYPE(
660+
'message', ['string', 'object', 'number', 'boolean'], message);
661+
}
662+
651663
// Support legacy function signature
652664
if (typeof options === 'boolean') {
653665
options = { swallowErrors: options };

test/parallel/test-child-process-fork.js

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ assert.throws(() => n.send(), {
4949
code: 'ERR_MISSING_ARGS'
5050
});
5151

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

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

0 commit comments

Comments
 (0)