Skip to content

fix: send only a byte view's own bytes over structured-clone transports - #287

Merged
ndisidore merged 1 commit into
mainfrom
fix/buffer-leak
Oct 26, 2026
Merged

ndisidore merged 1 commit into
mainfrom
fix/buffer-leak

Conversation

@ndisidore

Copy link
Copy Markdown
Member

Previously a byte view sent over a MessagePort session carried its entire backing buffer. Structured clone copies the whole ArrayBuffer behind a view, so the peer could read the bytes around a subarray or typed array view, and any small Node Buffer shipped the sender's whole shared pool (64 KB on Node 24). #201's copy runs on the receiving side, so it can't stop a peer from reading the raw message.

Now the sender copies out just the view's bytes whenever the view doesn't cover its whole buffer, at both the structuredClonable and jsonCompatibleWithBytes levels. Views that already cover their buffer are sent unchanged. WebSocket and HTTP batch sessions were never affected, since base64 encodes only the view.

At the structuredClonable and jsonCompatibleWithBytes levels, byte views
were handed to the transport as-is. Structured clone copies a view's
entire backing buffer, so a MessagePort peer received memory the sender
never passed: the bytes around a subarray or typed array view, or
Node's whole shared Buffer pool for any small Buffer.

Copy out the view's range on the sending side whenever it doesn't cover
its whole buffer. #201's copy runs on the receiving side, so it can't
stop a peer from reading the raw message.
@changeset-bot

changeset-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 92cf9de

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
capnweb Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Oct 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@287
npm i https://pkg.pr.new/cloudflare/capnweb/capnweb-validate@287

commit: 92cf9de

@ask-bonk

ask-bonk Bot commented Oct 26, 2026

Copy link
Copy Markdown

LGTM

github run

@clanky-the-clanker

Copy link
Copy Markdown

✅ Approved — The fix is small and correct. It copies only partial views, leaves views that cover their whole buffer unchanged, and tests both the plain Buffer path and the type-marked path. No issues found.

The decision is advisory: this comment never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The fix in src/serialize.ts:393-396 is correct.

  • What the copy does: new Uint8Array(bytes) copies only the view's own bytes into a fresh buffer, and that buffer is exactly the size of the view.
  • Node Buffer: a Buffer input becomes a plain Uint8Array. Structured clone already delivered a Buffer as a Uint8Array, so the receiving side sees no change.
  • Byte-swap path: on big-endian hosts, typed arrays are already copied with slice() before the swap. That copy covers its whole buffer, so the new check doesn't copy it a second time.
  • Views that cover their whole buffer are still sent without a copy, so the common case costs nothing extra.
  • Base64 levels are unaffected, as the description says.
  • Tests: the new test covers both the plain Buffer path and the type-marked Int16Array path.

📝 Documentation: ✅ LGTM

The changeset entry describes the fix accurately and at the right level of detail. No documentation issues found.


Reviewed commit: 92cf9dea

@ndisidore
ndisidore merged commit 143d13d into main Oct 26, 2026
12 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 26, 2026
@dimitropoulos
dimitropoulos deleted the fix/buffer-leak branch October 26, 2026 17:33
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.

2 participants