Skip to content

Apply [EnforceRange] to the deriveBits length - #8776

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-21-derivebits-length
Nov 23, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-21-derivebits-length

Conversation

@littledivy

@littledivy littledivy commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

subtle.deriveBits() took its length as an int, and JSG converts NaN and the infinities to 0 for int. So deriveBits(algorithm, key, NaN) derived 0 bits and returned an empty buffer for HKDF, PBKDF2, ECDH and X25519. The spec types the length as an [EnforceRange] unsigned long, and WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.js expects a TypeError.

In this patch the length is a uint32_t, which JSG unwraps with [EnforceRange]. NaN, the infinities, negative values and values above 2^32-1 throw a TypeError, and fractions are truncated. undefined and null still mean no length. A negative length now gets JSG's message instead of "deriveBits length must be an unsigned long integer." The eight expected failures for that WPT are removed. Generated types don't change.

This changes deployed behaviour without a compatibility flag, and we accept that risk. A NaN or infinite length only ever produced an empty buffer, so code that relies on it is unlikely to be doing anything useful.

//src/wpt:WebCryptoAPI@ passed 100 of 100 runs with --runs_per_test=100, and //src/wpt:WebCryptoAPI@all-autogates and the crypto and node:crypto tests pass.

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

@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 57b7af77.

This review, of bfab4487

💬 Approved with comments — Correct spec-conformance fix. One WARNING: deriveBits now rejects NaN and ±Infinity lengths, which it used to accept, with no compatibility flag and no accepted-risk note in the commit message.

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 #8776.

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] deriveBits now throws a TypeError for NaN and ±Infinity lengths, which used to return an empty buffer. Deployed behaviour changes with no compat flag and no accepted-risk statement in the commit message.
- **File:** src/workerd/api/crypto/crypto.h:601

📋 Code Quality: ⚠️ 1 warning

The change is correct, but it changes deployed behaviour without a compat flag or a recorded compatibility decision.

Recommendation: 📝 Minor Comments

  • [WARNING] deriveBits now throws a TypeError for NaN and ±Infinity lengths, which used to return an empty buffer. Deployed behaviour changes with no compat flag and no accepted-risk statement in the commit message. (src/workerd/api/crypto/crypto.h:601)
View full review

Behaviour change with no compatibility decision (src/workerd/api/crypto/crypto.h:601, crypto.c++:849-857)

The int unwrap (src/workerd/jsg/value.h:236-238) turned NaN and ±Infinity into 0. Because of that, deriveBits(alg, key, NaN) used to resolve with an empty buffer. It now rejects with a TypeError.

The repository's backward-compatibility rules say deployed behaviour is not changed without a compatibility flag, and that the compatibility decision goes in the commit message. The commit message frames this as a spec fix only and does not say whether existing Workers that pass a non-finite length are affected.

To fix it, do one of these:

  • Gate the strict conversion behind a compat flag.
  • Add an explicit accepted-risk statement to the commit message. For example: NaN and ±Infinity lengths gave an empty result, which is unlikely to be relied on.

The rest of the change is correct. The uint32_t unwrap enforces the range, and the implementations already take kj::Maybe<uint32_t>.


✅ All other checks passed

🔒 Security: ✅ LGTM

The length check is stricter now, and no unchecked narrowing remains.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates are needed for this change.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The updated header comment matches the new parameter type.


Reviewed commit: bfab4487

Comment thread src/workerd/api/crypto/crypto.h Resolved
subtle.deriveBits() took its length as an int, and JSG converts NaN and
the infinities to 0 for int, so deriveBits(algorithm, key, NaN) derived
0 bits and returned an empty buffer for HKDF, PBKDF2, ECDH and X25519.
The spec types the length as an [EnforceRange] unsigned long, and the
WPT derived_bits_length.https.any.js expects a TypeError.

In this patch the length is a uint32_t, which JSG unwraps with
[EnforceRange]: NaN, the infinities, negative values and values above
2^32-1 throw a TypeError, and fractions are truncated. undefined and
null still mean no length. A negative length now gets JSG's message
instead of "deriveBits length must be an unsigned long integer."

This changes deployed behaviour without a compatibility flag, and we
accept that risk: a NaN or infinite length only ever produced an empty
buffer, so code that relies on it is unlikely to be doing anything
useful.
@littledivy
littledivy force-pushed the littledivy/2026-11-21-derivebits-length branch from bfab448 to 57b7af7 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.

💬 Approved with comments — Correct spec-conformance fix. One WARNING: deriveBits now rejects NaN and ±Infinity lengths, which it used to accept, with no compatibility flag and no accepted-risk note in the commit message.

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 #8776.

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] deriveBits now throws a TypeError for NaN and ±Infinity lengths, which used to return an empty buffer. Deployed behaviour changes with no compat flag and no accepted-risk statement in the commit message.
- **File:** src/workerd/api/crypto/crypto.h:601

📋 Code Quality: ⚠️ 1 warning

The change is correct, but it changes deployed behaviour without a compat flag or a recorded compatibility decision.

Recommendation: 📝 Minor Comments

  • [WARNING] deriveBits now throws a TypeError for NaN and ±Infinity lengths, which used to return an empty buffer. Deployed behaviour changes with no compat flag and no accepted-risk statement in the commit message. (src/workerd/api/crypto/crypto.h:601)
View full review

Behaviour change with no compatibility decision (src/workerd/api/crypto/crypto.h:601, crypto.c++:849-857)

The int unwrap (src/workerd/jsg/value.h:236-238) turned NaN and ±Infinity into 0. Because of that, deriveBits(alg, key, NaN) used to resolve with an empty buffer. It now rejects with a TypeError.

The repository's backward-compatibility rules say deployed behaviour is not changed without a compatibility flag, and that the compatibility decision goes in the commit message. The commit message frames this as a spec fix only and does not say whether existing Workers that pass a non-finite length are affected.

To fix it, do one of these:

  • Gate the strict conversion behind a compat flag.
  • Add an explicit accepted-risk statement to the commit message. For example: NaN and ±Infinity lengths gave an empty result, which is unlikely to be relied on.

The rest of the change is correct. The uint32_t unwrap enforces the range, and the implementations already take kj::Maybe<uint32_t>.


✅ All other checks passed

🔒 Security: ✅ LGTM

The length check is stricter now, and no unchecked narrowing remains.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates are needed for this change.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The updated header comment matches the new parameter type.


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

@littledivy
littledivy merged commit 5f71182 into main Nov 23, 2026
36 of 40 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-21-derivebits-length branch November 23, 2026 15: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