diff --git a/src/node_messaging.cc b/src/node_messaging.cc index d2f95ccb850bd8..fa7132d7b43a3d 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -895,7 +895,12 @@ void MessagePort::Drain(const FunctionCallbackInfo& args) { } void MessagePort::ReceiveMessage(const FunctionCallbackInfo& args) { - CHECK(args[0]->IsObject()); + Environment* env = Environment::GetCurrent(args); + if (!args[0]->IsObject() || + !env->message_port_constructor_template()->HasInstance(args[0])) { + return THROW_ERR_INVALID_ARG_TYPE(env, + "First argument needs to be a MessagePort instance"); + } MessagePort* port = Unwrap(args[0].As()); if (port == nullptr) { // Return 'no messages' for a closed port. diff --git a/test/parallel/test-worker-message-port-receive-message.js b/test/parallel/test-worker-message-port-receive-message.js index 9bd8e7ed644aa3..e7078ea1e67ee5 100644 --- a/test/parallel/test-worker-message-port-receive-message.js +++ b/test/parallel/test-worker-message-port-receive-message.js @@ -23,3 +23,11 @@ port2.on('message', common.mustNotCall()); port1.postMessage(message1); assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 }); port1.close(); + +for (const value of [null, 0, -1, {}, []]) { + assert.throws(() => receiveMessageOnPort(value), { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + message: 'First argument needs to be a MessagePort instance' + }); +}