Repository navigation
Reject SharedArrayBuffer-backed views in SubtleCrypto - #8775
Conversation
|
@jasnell Should this be a compat flag? |
There was a problem hiding this comment.
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
c8d2487 to
955263c
Compare
There was a problem hiding this comment.
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)
npaun
left a comment
There was a problem hiding this comment.
LGTM modulo James deciding if this needs a compat flag
955263c to
2b983ea
Compare
There was a problem hiding this comment.
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.
requireNotSharedis applied to everyJsBufferSourcedata 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/decapsulateKeynow take the ciphertext as aJsBufferSource. The ciphertext is read throughnonNullBytes, and only insideevalNow.- Each rejection case in
crypto-shared-buffer-testhas 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
|
Definitely needs a compat flag... this could just be added to |
2b983ea to
525b341
Compare
There was a problem hiding this comment.
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 unlesspedantic_wptis set. - Every
JsBufferSourcemember of the encrypt, sign, generate-key and derive-key algorithm dictionaries is checked. - The
.wd-testpins one service topedantic_wptand the other tonon_pedantic_wpt, so the two variants don't depend on the compat date. decapsulateBitsanddecapsulateKeyread the ciphertext withnonNullBytes(asArrayPtr())insideevalNow.
⚡ 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.
525b341 to
8f15f77
Compare
There was a problem hiding this comment.
✅ 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
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 aJsBufferSource, but a view onto one got through. For example,digest('SHA-256', new Uint8Array(sharedMemory.buffer))hashed shared memory.WebCryptoAPI/sharedarraybuffer.https.any.jsexpects 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 thepedantic_wptcompatibility flag:The check runs where the other argument TypeErrors do. Without
pedantic_wptnothing changes.decapsulateBits()anddecapsulateKey()now take the ciphertext as aJsBufferSourcerather 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-inwebcrypto_modern_algorithmsflag, which says the API may still change. node:crypto and otherJsBufferSourceusers don't change, and generated types don't change.The WPT runs with
pedantic_wpt, so its four expected failures are removed. The newcrypto-shared-buffer-testcovers 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-autogatespass, and so docrypto-shared-buffer-testin all three variants andcrypto-extras-test.Fixes #7833.