Skip to content

Commit 57cd7d2

Browse files
addaleaxtargos
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 26fee8b commit 57cd7d2

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
@@ -895,7 +895,12 @@ void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
895895
}
896896

897897
void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
898-
CHECK(args[0]->IsObject());
898+
Environment* env = Environment::GetCurrent(args);
899+
if (!args[0]->IsObject() ||
900+
!env->message_port_constructor_template()->HasInstance(args[0])) {
901+
return THROW_ERR_INVALID_ARG_TYPE(env,
902+
"First argument needs to be a MessagePort instance");
903+
}
899904
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
900905
if (port == nullptr) {
901906
// 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)