Skip to content

Commit b80b08f

Browse files
addaleaxBridgeAR
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 fd4320c commit b80b08f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/node_messaging.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,12 @@ void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
867867
}
868868

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

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

+8
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)