Skip to content

Commit e39a468

Browse files
gireeshpunathiltargos
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 01778f5 commit e39a468

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
@@ -665,6 +665,18 @@ function setupChannel(target, channel) {
665665
if (message === undefined)
666666
throw new ERR_MISSING_ARGS('message');
667667

668+
// Non-serializable messages should not reach the remote
669+
// end point; as any failure in the stringification there
670+
// will result in error message that is weakly consumable.
671+
// So perform a sanity check on message prior to sending.
672+
if (typeof message !== 'string' &&
673+
typeof message !== 'object' &&
674+
typeof message !== 'number' &&
675+
typeof message !== 'boolean') {
676+
throw new ERR_INVALID_ARG_TYPE(
677+
'message', ['string', 'object', 'number', 'boolean'], message);
678+
}
679+
668680
// Support legacy function signature
669681
if (typeof options === 'boolean') {
670682
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)