Skip to content

Commit 7147df5

Browse files
addaleaxBethGriggs
authored andcommitted
worker: fix type check in receiveMessageOnPort
Use the same type check we use in `MoveToContext()` in `ReceiveMessage()`. Fixes: #32742 PR-URL: #32745 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Zeyu Yang <[email protected]>
1 parent 0538dd1 commit 7147df5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/node_messaging.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,12 @@ void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
865865
}
866866

867867
void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
868-
CHECK(args[0]->IsObject());
868+
Environment* env = Environment::GetCurrent(args);
869+
if (!args[0]->IsObject() ||
870+
!env->message_port_constructor_template()->HasInstance(args[0])) {
871+
return THROW_ERR_INVALID_ARG_TYPE(env,
872+
"First argument needs to be a MessagePort instance");
873+
}
869874
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
870875
if (port == nullptr) {
871876
// Return 'no messages' for a closed port.

test/parallel/test-worker-message-port-receive-message.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ port2.on('message', common.mustNotCall());
2323
port1.postMessage(message1);
2424
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
2525
port1.close();
26+
27+
for (const value of [null, 0, -1, {}, []]) {
28+
assert.throws(() => receiveMessageOnPort(value), {
29+
name: 'TypeError',
30+
code: 'ERR_INVALID_ARG_TYPE',
31+
message: 'First argument needs to be a MessagePort instance'
32+
});
33+
}

0 commit comments

Comments
 (0)