From b80b08fe35c9032bc2939f2e07c71fd1e5daa6f1 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 9 Apr 2020 18:44:28 +0200 Subject: [PATCH] worker: fix type check in receiveMessageOnPort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the same type check we use in `MoveToContext()` in `ReceiveMessage()`. Fixes: https://github.com/nodejs/node/issues/32742 PR-URL: https://github.com/nodejs/node/pull/32745 Reviewed-By: Colin Ihrig Reviewed-By: Gerhard Stöbich Reviewed-By: Yongsheng Zhang Reviewed-By: Zeyu Yang --- src/node_messaging.cc | 7 ++++++- test/parallel/test-worker-message-port-receive-message.js | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 657b409627c838..28e31bbbb9b3f0 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -867,7 +867,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' + }); +}