Skip to content

fix(crypto): match Node 26 parity - #6836

Merged
proggeramlug merged 3 commits into
mainfrom
feat/crypto-parity-node24-26
Jul 25, 2026
Merged

fix(crypto): match Node 26 parity#6836
proggeramlug merged 3 commits into
mainfrom
feat/crypto-parity-node24-26

Conversation

@TheHypnoo

@TheHypnoo TheHypnoo commented Jul 25, 2026

Copy link
Copy Markdown
Member

Summary

  • match X509 checkIP invalid-name errors in Node 26
  • accept Node 26 KMAC non-byte-aligned key and output lengths, including zero-length JWK keys
  • call X509Certificate.toLegacyObject() with its certificate receiver so the parity fixture runs

Validation

  • Node 26.5.0: ./run_parity_tests.sh --suite node-suite --module crypto — 243 pass, 0 failures
  • Node 24.16.0 comparison: Node 24 uses Invalid IP for X509 invalid strings and rejects the KMAC cases that Node 26 accepts. Node 26.5.0 remains the repository oracle.
  • cargo fmt --check -p perry-stdlib
  • git diff --check

Closes #6783

Summary by CodeRabbit

  • New Features

    • KMAC operations now support non-byte-aligned key and output lengths.
    • Empty KMAC key material is accepted during key import where supported.
  • Bug Fixes

    • Updated X.509 IP validation errors to match Node.js behavior.
    • Improved compatibility with Node 26 crypto edge cases.
  • Tests

    • Updated KMAC and X.509 coverage for the revised behavior.
  • Documentation

    • Added a changelog entry describing the crypto compatibility updates.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@TheHypnoo, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 25 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 169c1d75-5114-44b8-8004-f21fd43a5464

📥 Commits

Reviewing files that changed from the base of the PR and between 0a73735 and 3dbea7d.

📒 Files selected for processing (7)
  • crates/perry-runtime/src/buffer/header.rs
  • crates/perry-runtime/src/object/field_get_set/crypto_key.rs
  • crates/perry-runtime/src/object/native_module_crypto_key_object.rs
  • crates/perry-stdlib/src/webcrypto/jwk.rs
  • crates/perry-stdlib/src/webcrypto/keys.rs
  • crates/perry-stdlib/src/webcrypto/util.rs
  • test-parity/node-suite/crypto/webcrypto/kmac.ts
📝 Walkthrough

Walkthrough

KMAC key import, key generation, and output sizing now support empty or non-byte-aligned values. X.509 invalid-IP errors and legacy metadata access are aligned with expected behavior, with updated parity coverage and changelog documentation.

Changes

Crypto parity

Layer / File(s) Summary
KMAC key handling
crates/perry-stdlib/src/webcrypto/jwk.rs, crates/perry-stdlib/src/webcrypto/keys.rs
KMAC imports allow empty key material, and key generation uses ceiling division for non-byte-aligned lengths.
KMAC output handling
crates/perry-stdlib/src/webcrypto/hmac.rs, crates/perry-stdlib/src/webcrypto/util.rs, test-parity/node-suite/crypto/webcrypto/kmac.ts
KMAC output sizing accepts non-byte-aligned lengths, with parity coverage for a successful short output.
X.509 parity and release note
crates/perry-stdlib/src/crypto/x509.rs, test-parity/node-suite/crypto/x509/signature-legacy-metadata.ts, changelog.d/6836-node26-crypto-parity.md
Invalid IP parsing uses the updated error message, legacy metadata calls toLegacyObject directly, and the changelog records the crypto parity updates.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately reflects the main crypto parity fix.
Description check ✅ Passed The PR description includes a clear summary, validation notes, and the linked issue reference, though it omits some template sections.
Linked Issues check ✅ Passed The changes address the linked parity issues by matching Node 26 KMAC and X509 behavior and updating the relevant parity fixture.
Out of Scope Changes check ✅ Passed The edits stay within the crypto parity scope and supporting tests/changelog; no unrelated changes stand out.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/crypto-parity-node24-26

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/perry-stdlib/src/webcrypto/jwk.rs`:
- Around line 268-269: The KMAC branches in key import must preserve whether the
JWK k value was explicitly present and valid instead of collapsing missing or
invalid decoding into an empty vector. Update the jwk_import_key_bytes handling
used by KeyAlgo::Kmac128 and KeyAlgo::Kmac256 to reject absent or malformed k
data, while still allowing an explicitly provided k: "" value.

In `@crates/perry-stdlib/src/webcrypto/keys.rs`:
- Line 660: The KMAC key-generation path around the key_bytes allocation must
preserve the requested bit length, not just the rounded byte size. Mask unused
padding bits after OsRng fills the buffer, pass the requested KMAC bit length
into CryptoKeyMaterial::new, and update the parity expectation if Node accepts
non-byte-aligned generated lengths.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3a4e3f95-29f8-44b1-8434-9b4f06dae3bb

📥 Commits

Reviewing files that changed from the base of the PR and between 8b2c736 and 75970f5.

📒 Files selected for processing (7)
  • crates/perry-stdlib/src/crypto/x509.rs
  • crates/perry-stdlib/src/webcrypto/hmac.rs
  • crates/perry-stdlib/src/webcrypto/jwk.rs
  • crates/perry-stdlib/src/webcrypto/keys.rs
  • crates/perry-stdlib/src/webcrypto/util.rs
  • test-parity/node-suite/crypto/webcrypto/kmac.ts
  • test-parity/node-suite/crypto/x509/signature-legacy-metadata.ts
💤 Files with no reviewable changes (1)
  • crates/perry-stdlib/src/webcrypto/hmac.rs

Comment on lines +268 to +269
| KeyAlgo::Kmac128
| KeyAlgo::Kmac256

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== files =="
git ls-files | rg '(^|/)Cargo\.toml$|(^|/)CLAUDE\.md$|crates/perry-stdlib/src/webcrypto/jwk\.rs|changelog\.d/' || true

echo "== jwk relevant sections =="
if [ -f crates/perry-stdlib/src/webcrypto/jwk.rs ]; then
  wc -l crates/perry-stdlib/src/webcrypto/jwk.rs
  sed -n '200,310p' crates/perry-stdlib/src/webcrypto/jwk.rs
  echo "== k/import functions outline =="
  ast-grep outline crates/perry-stdlib/src/webcrypto/jwk.rs --view compact | sed -n '1,220p' || true
fi

echo "== search jwk_import_key_bytes =="
rg -n "jwk_import_key_bytes|KeyAlgo::Kmac|Kmac|k:" crates/perry-stdlib/src/webcrypto || true

Repository: PerryTS/perry

Length of output: 15088


🌐 Web query:

WebCrypto modern algorithms JWK k field decoding invalid JWK fail wicg github.io webcrypto modernalgos

💡 Result:

The "k" field in a JSON Web Key (JWK) is used to carry the raw key material for symmetric algorithms (typically with the "oct" key type) [1][2][3]. Decoding issues related to this field are generally caused by improper handling of Base64URL encoding—a variant of Base64 required by the JOSE specifications (RFC 7517) which replaces '+' with '-' and '/' with '', and omits padding [1][2]. In the context of the Web Cryptography API and its extensions (such as the Modern Algorithms proposal), "invalid JWK" errors during the decoding of the "k" field typically arise from: 1. Improper Encoding/Decoding: The "k" value must be decoded from its Base64URL representation into a raw byte array (Uint8Array) before it can be processed as a key by the WebCrypto subtle.importKey operation [1][3]. If an implementation attempts to treat the string directly as a key or uses standard Base64 decoding (which fails on characters like '-' and ''), a DataError or SyntaxError is standard behavior [1][2]. 2. Validation Requirements: Specifications and browser implementations enforce strict checks on the "k" field. For example, if the key length implied by the decoded "k" parameter does not match the requirements of the algorithm specified in the JWK (or provided to importKey), the operation will fail [4][2]. 3. WebCrypto "Modern Algorithms": The WICG "Modern Algorithms" specification [2] explicitly governs the handling of JWK inputs for new algorithms (like ML-KEM). Similar to the base WebCrypto API, it mandates that the "k" (or "priv"/"pub") fields must be valid Base64URL encoded data [2]. If a JWK is malformed—such as having an incorrect key type ("kty") for the algorithm, missing required fields, or having invalid Base64URL characters—the API is required to throw a DataError [2]. If you are encountering this error, ensure that: - The Base64URL string is correctly padded if your decoder requires it (though RFC 7517 suggests padding should be omitted) or, conversely, ensure your decoder is RFC-compliant and handles unpadded Base64URL correctly [1][2]. - The "kty" (Key Type) and "alg" (Algorithm) fields in your JWK object align with the WebCrypto algorithm you are importing into [2][3]. - You are not passing the JWK object directly as a key material buffer, but rather passing the parsed JWK structure to the importKey function where the browser/runtime handles the internal decoding [3].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== jwk.rs 720-820 =="
sed -n '720,820p' crates/perry-stdlib/src/webcrypto/jwk.rs

echo "== jwk.rs 820-940 for validate/import key bits =="
sed -n '820,940p' crates/perry-stdlib/src/webcrypto/jwk.rs

echo "== jwk.rs 428-470 and 508-535 for export KMAC =="
sed -n '428,470p' crates/perry-stdlib/src/webcrypto/jwk.rs
sed -n '508,535p' crates/perry-stdlib/src/webcrypto/jwk.rs

echo "== imports/top =="
sed -n '1,90p' crates/perry-stdlib/src/webcrypto/jwk.rs

Repository: PerryTS/perry

Length of output: 14707


Do not allow malformed JWK k decoding as an empty KMAC key.

jwk_import_key_bytes(...).unwrap_or_else(|| Vec::new()) converts a missing or invalid decoded k value into the same zero-length byte vector as an explicitly present empty JWK. Since KMAC is now in the empty-key allowlist, malformed JWKs can be imported as zero-length keys instead of failing key-material validation. Preserve the decode result: allow k: "" only when explicit, and reject missing or invalid k data.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry-stdlib/src/webcrypto/jwk.rs` around lines 268 - 269, The KMAC
branches in key import must preserve whether the JWK k value was explicitly
present and valid instead of collapsing missing or invalid decoding into an
empty vector. Update the jwk_import_key_bytes handling used by KeyAlgo::Kmac128
and KeyAlgo::Kmac256 to reject absent or malformed k data, while still allowing
an explicitly provided k: "" value.

Comment thread crates/perry-stdlib/src/webcrypto/keys.rs
@proggeramlug
proggeramlug merged commit 21a3d00 into main Jul 25, 2026
30 of 31 checks passed
@proggeramlug
proggeramlug deleted the feat/crypto-parity-node24-26 branch July 25, 2026 19:08
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.

[parity] node:crypto — 2 failing node-suite tests (2026-07-22 baseline)

2 participants