Skip to content

fix(ext/web): validate the transfer list before serializing and detach only on success - #37122

Open
Aschenbath wants to merge 1 commit into
denoland:mainfrom
Aschenbath:fix/structured-clone-transfer-order
Open

Aschenbath wants to merge 1 commit into
denoland:mainfrom
Aschenbath:fix/structured-clone-transfer-order

Conversation

@Aschenbath

Copy link
Copy Markdown

Closes #37105

structuredClone(value, { transfer }) had side effects when it threw, and checked the transfer list too late:

  • op_serialize detached the transferred ArrayBuffers right after write_value, before checking whether serialization succeeded. A getter that throws, or a value that can't be cloned, left the caller's buffers detached.
  • serializeJsMessageData rejected entries that aren't transferable (Value not transferable) only after core.serialize, so getters on the value ran first, and a getter's exception replaced the DataCloneError. A SharedArrayBuffer in the list had the same problem.
  • Duplicate entries weren't rejected. MessagePort.prototype.postMessage has its own duplicate check, but structuredClone doesn't go through it.

StructuredSerializeWithTransfer rejects duplicate and non-transferable entries first, then serializes, and transfers only after serialization succeeds.

Changes:

  • ext/web/13_message_port.js: the loop in serializeJsMessageData that already runs before core.serialize now also throws DataCloneError for a duplicate entry and for an entry that is neither an ArrayBuffer nor a host object. The message for the second is the same Value not transferable as before, so the existing SharedArrayBuffer test still matches.
  • libs/core/ops_builtin_v8.rs: op_serialize detaches the buffers only when write_value succeeded and nothing was caught. On failure it leaves them attached and takes their backing stores back out of shared_array_buffer_store.

With this change, the reproduction in the issue prints exactly the expected output:

getter throws: Error 4
function value: DataCloneError 4
invalid entry after buffer: DataCloneError 4
invalid entry: DataCloneError 0
invalid entry, getter throws: DataCloneError
duplicate entry: DataCloneError 0 4

Testing

  • Added two tests to tests/unit/structured_clone_test.ts, covering the cases from the issue plus a successful transfer. Both fail on main and pass with this change (deno rebuilt for each run).
  • unit::structured_clone_test, unit::message_channel_test, unit::worker_test and unit_node::worker_threads_test pass.
  • tools/lint.js --js and dprint check pass on the changed files, and cargo clippy -p deno_core -- -D warnings is clean.
  • Built and tested on Windows.

AI disclosure

This PR was written primarily by Claude Code (Claude Opus 5.5), including the change, the tests and this description. It has not yet been reviewed by a human.

🤖 Generated with Claude Code

…h only on success

Closes denoland#37105

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NetOpWibby pushed a commit to theende/ende that referenced this pull request Nov 7, 2026
…h only on success

structuredClone(value, { transfer }) had side effects when it threw, and
checked the transfer list too late. op_serialize detached the transferred
ArrayBuffers before checking whether serialization succeeded, so a getter
that throws or a value that can't be cloned left the caller's buffers
detached. Entries that aren't transferable were rejected only after
serializing, so getters ran first and their exceptions replaced the
DataCloneError, and duplicate entries weren't rejected at all.

As StructuredSerializeWithTransfer requires, reject duplicate and
non-transferable entries first, then serialize, and detach only once
serialization has succeeded. On failure the buffers stay attached and
their backing stores are taken out of the store again.

Ende change: op_serialize also checks every transferred buffer before
registering any, so a non-detachable entry (a WebAssembly.Memory buffer)
no longer leaves the backing stores of the entries before it in the
store.

(cherry picked from commit 02f93cafd3e2589898723782ff03e5a630d24455)

Upstream: denoland/deno#37122
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

structuredClone validates the transfer list after serialization and detaches buffers on failure

2 participants