Skip to content

Reject SharedArrayBuffer-backed views in SubtleCrypto - #8775

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-21-crypto-reject-shared-views
Nov 26, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-21-crypto-reject-shared-views

Conversation

@littledivy

@littledivy littledivy commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

None of SubtleCrypto's BufferSource arguments or dictionary members are [AllowShared], so a SharedArrayBuffer or a view onto one should be a TypeError. JSG already rejects a bare SharedArrayBuffer for a JsBufferSource, but a view onto one got through. For example, digest('SHA-256', new Uint8Array(sharedMemory.buffer)) hashed shared memory. WebCryptoAPI/sharedarraybuffer.https.any.js expects a TypeError.

In this patch the first commit adds JsBufferSource::isBackedBySharedArrayBuffer(), which looks through a view to its buffer. The second uses it in SubtleCrypto, under the pedantic_wpt compatibility flag:

  • Data arguments: encrypt, decrypt, sign, verify, digest, importKey and unwrapKey.
  • Algorithm members: iv, additionalData, counter, label, salt, info, publicExponent and the ML-DSA context.

The check runs where the other argument TypeErrors do. Without pedantic_wpt nothing changes.

decapsulateBits() and decapsulateKey() now take the ciphertext as a JsBufferSource rather than a byte array, so they get the same check. With or without the flag, they also reject a bare SharedArrayBuffer. They sit behind the opt-in webcrypto_modern_algorithms flag, which says the API may still change. node:crypto and other JsBufferSource users don't change, and generated types don't change.

The WPT runs with pedantic_wpt, so its four expected failures are removed. The new crypto-shared-buffer-test covers the arguments and members the WPT doesn't, and checks that a shared view is still accepted without the flag.

//src/wpt:WebCryptoAPI@ and @all-autogates pass, and so do crypto-shared-buffer-test in all three variants and crypto-extras-test.

Fixes #7833.

@littledivy
littledivy requested review from a team as code owners November 21, 2026 04:44
@littledivy

Copy link
Copy Markdown
Member Author

@jasnell Should this be a compat flag?

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 955263c7.

This review, of c8d2487a

💬 Approved with comments — The checks are correct and cover every BufferSource argument and member. One WARNING: the new TypeError applies to all existing Workers with no compatibility flag, and the commits don't record that compatibility decision.

Findings: 1 warning (1 is an inline comment on the diff)

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

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8775.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] The rejection of SharedArrayBuffer-backed views is ungated, so deployed Workers that pass views onto shared WebAssembly memory to crypto.subtle will start throwing TypeError. Add a compatibility flag or record the accepted risk in the commit message.
- **File:** src/workerd/api/crypto/crypto.c++:42

📋 Code Quality: ⚠️ 1 warning

The implementation is correct. The behaviour change is ungated, and the commits don't record the compatibility decision.

Recommendation: 📝 Minor Comments

  • [WARNING] The rejection of SharedArrayBuffer-backed views is ungated, so deployed Workers that pass views onto shared WebAssembly memory to crypto.subtle will start throwing TypeError. Add a compatibility flag or record the accepted risk in the commit message. (src/workerd/api/crypto/crypto.c++:42)
View full review

isBackedBySharedArrayBuffer() is correct. It returns false for a view whose buffer V8 hasn't allocated yet, and does so without allocating one. It also uses the same V8 version guard as the other call sites in the repository.

The requireNotShared checks cover every JsBufferSource member of the algorithm dictionaries and every data argument:

  • Members: iv, additionalData, counter, label, salt, info, publicExponent and the ML-DSA context.
  • Arguments: encrypt, decrypt, sign, verify, digest, importKey, unwrapKey and decapsulate*.

In decapsulateBits/decapsulateKey, the ciphertext bytes are only used inside evalNow, so their lifetime is fine.

The one concern is backward compatibility. Shared memory is reachable in Workers today through new WebAssembly.Memory({ shared: true }), and the new test relies on that at the default compatibility date. Any deployed Worker that passes a view onto shared memory to crypto.subtle currently works and will start getting a TypeError. No compatibility flag gates this, and the commit messages don't explicitly accept the risk, which the repository's backward-compatibility guidance asks for.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change only adds rejection of shared memory. A view whose buffer hasn't been allocated yet keeps its bytes on the V8 heap, so treating it as unshared is safe. Switching the decapsulate ciphertext to JsBufferSource with nonNullBytes keeps the bytes valid while they are used.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


⚡ Performance: ✅ LGTM

No performance concerns. The checks are a few constant-time V8 type queries per call.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: c8d2487a

Comment thread src/workerd/api/crypto/crypto.c++ Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-21-crypto-reject-shared-views branch from c8d2487 to 955263c Compare November 21, 2026 04:47

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 2b983ea4.

This review, of 955263c7

💬 Approved with comments — The checks are correct and cover every BufferSource argument and member. One WARNING: the new TypeError applies to all existing Workers with no compatibility flag, and the commits don't record that compatibility decision.

Findings: 1 warning

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

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8775.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] The rejection of SharedArrayBuffer-backed views is ungated, so deployed Workers that pass views onto shared WebAssembly memory to crypto.subtle will start throwing TypeError. Add a compatibility flag or record the accepted risk in the commit message.
- **File:** src/workerd/api/crypto/crypto.c++:42

📋 Code Quality: ⚠️ 1 warning

The implementation is correct. The behaviour change is ungated, and the commits don't record the compatibility decision.

Recommendation: 📝 Minor Comments

  • [WARNING] The rejection of SharedArrayBuffer-backed views is ungated, so deployed Workers that pass views onto shared WebAssembly memory to crypto.subtle will start throwing TypeError. Add a compatibility flag or record the accepted risk in the commit message. (src/workerd/api/crypto/crypto.c++:42)
View full review

isBackedBySharedArrayBuffer() is correct. It returns false for a view whose buffer V8 hasn't allocated yet, and does so without allocating one. It also uses the same V8 version guard as the other call sites in the repository.

The requireNotShared checks cover every JsBufferSource member of the algorithm dictionaries and every data argument:

  • Members: iv, additionalData, counter, label, salt, info, publicExponent and the ML-DSA context.
  • Arguments: encrypt, decrypt, sign, verify, digest, importKey, unwrapKey and decapsulate*.

In decapsulateBits/decapsulateKey, the ciphertext bytes are only used inside evalNow, so their lifetime is fine.

The one concern is backward compatibility. Shared memory is reachable in Workers today through new WebAssembly.Memory({ shared: true }), and the new test relies on that at the default compatibility date. Any deployed Worker that passes a view onto shared memory to crypto.subtle currently works and will start getting a TypeError. No compatibility flag gates this, and the commit messages don't explicitly accept the risk, which the repository's backward-compatibility guidance asks for.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change only adds rejection of shared memory. A view whose buffer hasn't been allocated yet keeps its bytes on the V8 heap, so treating it as unshared is safe. Switching the decapsulate ciphertext to JsBufferSource with nonNullBytes keeps the bytes valid while they are used.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


⚡ Performance: ✅ LGTM

No performance concerns. The checks are a few constant-time V8 type queries per call.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 955263c7 — same changes as c8d2487a, so this is that commit's review (no new review was run; line numbers are as of c8d2487a)

Comment thread src/workerd/api/crypto/crypto.c++ Resolved

@npaun npaun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo James deciding if this needs a compat flag

@littledivy
littledivy force-pushed the littledivy/2026-11-21-crypto-reject-shared-views branch from 955263c to 2b983ea Compare November 24, 2026 03:17

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 525b341e.

This review, of 2b983ea4

✅ Approved — No findings. The only earlier warning, about the ungated behaviour change not being recorded, is resolved now that the commit message explicitly accepts the risk.

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


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds rejections, so SubtleCrypto now accepts less than before. A view whose buffer has not been allocated yet keeps its bytes on the V8 heap, so it can't be shared memory, and treating it as unshared is safe. The decapsulate ciphertext bytes stay valid while they are used.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

The second commit message now states that the change alters deployed behaviour without a compatibility flag and that the risk is accepted. That meets the repository's rule for changes to the API surface Workers see.

  • requireNotShared is applied to every JsBufferSource data argument and algorithm member: iv, additionalData, counter, label, salt, info, publicExponent and the ML-DSA context.
  • isBackedBySharedArrayBuffer() returns false for a view that has no buffer yet, so it does not make V8 allocate one. It uses the same V8 version guard as the other call sites.
  • decapsulateBits / decapsulateKey now take the ciphertext as a JsBufferSource. The ciphertext is read through nonNullBytes, and only inside evalNow.
  • Each rejection case in crypto-shared-buffer-test has an unshared control case, and the WPT expected failures are removed.

⚡ Performance: ✅ LGTM

No performance concerns. Each call adds a few constant-time V8 type checks.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 2b983ea4

@jasnell

jasnell commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Definitely needs a compat flag... this could just be added to pedantic_wpt

@littledivy
littledivy force-pushed the littledivy/2026-11-21-crypto-reject-shared-views branch from 2b983ea to 525b341 Compare November 25, 2026 06:46

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 8f15f772.

This review, of 525b341e

✅ Approved — No issues found. The shared-view rejection is now gated on pedantic_wpt as requested in the discussion, and tests cover the behaviour with and without the flag.

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


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds rejections, so SubtleCrypto accepts less than before.

  • isBackedBySharedArrayBuffer() is correct for a bare SharedArrayBuffer and for a view onto one.
  • It is also correct for a view whose buffer V8 has not allocated yet: those bytes are on the V8 heap, so they cannot be shared memory.
  • The decapsulate ciphertext bytes stay valid while they are used.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

The gating is consistent.

  • Every check routes through requireNotShared, which returns early unless pedantic_wpt is set.
  • Every JsBufferSource member of the encrypt, sign, generate-key and derive-key algorithm dictionaries is checked.
  • The .wd-test pins one service to pedantic_wpt and the other to non_pedantic_wpt, so the two variants don't depend on the compat date.
  • decapsulateBits and decapsulateKey read the ciphertext with nonNullBytes(asArrayPtr()) inside evalNow.

⚡ Performance: ✅ LGTM

Each call adds a few constant-time type checks. The checks don't allocate or copy, and they don't force V8 to allocate a buffer for a view.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 525b341e

isSharedArrayBuffer() only looks at the value itself, so it is false for
a typed array or DataView onto a SharedArrayBuffer. Callers that must
reject shared memory, as Web IDL does for a BufferSource without
[AllowShared], need to look through the view.

In this patch isBackedBySharedArrayBuffer() is true for a
SharedArrayBuffer and for a view onto one. A view whose buffer V8 hasn't
allocated yet keeps its bytes on the heap, so it returns false without
allocating one.
None of SubtleCrypto's BufferSource arguments or dictionary members are
[AllowShared], so a SharedArrayBuffer or a view onto one should be a
TypeError. JSG already rejects a bare SharedArrayBuffer for a
JsBufferSource, but a view onto one got through, so for example
digest('SHA-256', new Uint8Array(sharedMemory.buffer)) hashed shared
memory. The WPT sharedarraybuffer.https.any.js expects a TypeError.

In this patch, under the pedantic_wpt compatibility flag, SubtleCrypto
throws a TypeError for a shared-backed view in the data arguments of
encrypt, decrypt, sign, verify, digest, importKey and unwrapKey, and in
the iv, additionalData, counter, label, salt, info, publicExponent and
ML-DSA context members. The check runs where the other argument
TypeErrors do. Without the flag nothing changes.

decapsulateBits() and decapsulateKey() now take the ciphertext as a
JsBufferSource rather than a byte array, so they get the same check
and, with or without the flag, reject a bare SharedArrayBuffer. They
are behind the opt-in webcrypto_modern_algorithms flag, which says the
API may still change. node:crypto and other JsBufferSource users don't
change. crypto-shared-buffer-test covers the cases the WPT doesn't,
and that a shared view is still accepted without pedantic_wpt.
@littledivy
littledivy force-pushed the littledivy/2026-11-21-crypto-reject-shared-views branch from 525b341 to 8f15f77 Compare November 25, 2026 07:05

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. The shared-view rejection is gated on pedantic_wpt as the discussion asked, and tests cover the behaviour with and without the flag.

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


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds rejections, so SubtleCrypto accepts less than before. isBackedBySharedArrayBuffer() correctly detects both a bare SharedArrayBuffer and a view onto one. A view whose buffer V8 has not allocated yet keeps its bytes on the V8 heap, so it cannot be shared memory.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

Each requireNotShared overload matches the algorithm struct its call site passes. Every buffer member of those structs is checked, and the checks return early unless pedantic_wpt is set. The decapsulate ciphertext is read while its argument is still alive.


⚡ Performance: ✅ LGTM

Each call adds a few constant-time type checks. The checks don't allocate or copy, and they don't force V8 to allocate a buffer for a view. The decapsulate methods now read the ciphertext in place.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 8f15f772

@littledivy
littledivy merged commit 6b3a821 into main Nov 26, 2026
24 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-21-crypto-reject-shared-views branch November 26, 2026 15:27
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.

WebCrypto shared buffers: reject SharedArrayBuffer-backed views in SubtleCrypto BufferSource arguments and dictionary members

3 participants