Skip to content

Commit 55c2702

Browse files
committed
worker: throw for duplicates in transfer list
Throw a `DataCloneError` exception when encountering duplicate `ArrayBuffer`s or `MessagePort`s in the transfer list. Fixes: #25786 PR-URL: #25815 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent d683da7 commit 55c2702

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/node_messaging.cc

+19
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ Maybe<bool> Message::Serialize(Environment* env,
289289
// take ownership of its memory, copying the buffer will have to do.
290290
if (!ab->IsNeuterable() || ab->IsExternal())
291291
continue;
292+
if (std::find(array_buffers.begin(), array_buffers.end(), ab) !=
293+
array_buffers.end()) {
294+
ThrowDataCloneException(
295+
env,
296+
FIXED_ONE_BYTE_STRING(
297+
env->isolate(),
298+
"Transfer list contains duplicate ArrayBuffer"));
299+
return Nothing<bool>();
300+
}
292301
// We simply use the array index in the `array_buffers` list as the
293302
// ID that we write into the serialized buffer.
294303
uint32_t id = array_buffers.size();
@@ -314,6 +323,15 @@ Maybe<bool> Message::Serialize(Environment* env,
314323
"MessagePort in transfer list is already detached"));
315324
return Nothing<bool>();
316325
}
326+
if (std::find(delegate.ports_.begin(), delegate.ports_.end(), port) !=
327+
delegate.ports_.end()) {
328+
ThrowDataCloneException(
329+
env,
330+
FIXED_ONE_BYTE_STRING(
331+
env->isolate(),
332+
"Transfer list contains duplicate MessagePort"));
333+
return Nothing<bool>();
334+
}
317335
delegate.ports_.push_back(port);
318336
continue;
319337
}
@@ -601,6 +619,7 @@ void MessagePort::OnClose() {
601619
}
602620

603621
std::unique_ptr<MessagePortData> MessagePort::Detach() {
622+
CHECK(data_);
604623
Mutex::ScopedLock lock(data_->mutex_);
605624
data_->owner_ = nullptr;
606625
return std::move(data_);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { MessageChannel } = require('worker_threads');
5+
6+
// Test that passing duplicate transferrables in the transfer list throws
7+
// DataCloneError exceptions.
8+
9+
{
10+
const { port1, port2 } = new MessageChannel();
11+
port2.once('message', common.mustNotCall());
12+
13+
const port3 = new MessageChannel().port1;
14+
assert.throws(() => {
15+
port1.postMessage(port3, [port3, port3]);
16+
}, /^DataCloneError: Transfer list contains duplicate MessagePort$/);
17+
port1.close();
18+
}
19+
20+
{
21+
const { port1, port2 } = new MessageChannel();
22+
port2.once('message', common.mustNotCall());
23+
24+
const buf = new Uint8Array(10);
25+
assert.throws(() => {
26+
port1.postMessage(buf, [buf.buffer, buf.buffer]);
27+
}, /^DataCloneError: Transfer list contains duplicate ArrayBuffer$/);
28+
port1.close();
29+
}

0 commit comments

Comments
 (0)