fix(ext/node): improve crypto.generateKeyPair validation#32620
Merged
bartlomieju merged 3 commits intomainfrom Mar 12, 2026
Merged
fix(ext/node): improve crypto.generateKeyPair validation#32620bartlomieju merged 3 commits intomainfrom
bartlomieju merged 3 commits intomainfrom
Conversation
Adds proper synchronous validation to generateKeyPair/generateKeyPairSync matching Node.js behavior: - Restructure generateKeyPair to call createJob synchronously so validation errors throw synchronously instead of rejecting a promise - Add key encoding validation (format, type, cipher, passphrase) with proper error codes (ERR_INVALID_ARG_VALUE, ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS) - Add DH group name validation (ERR_CRYPTO_UNKNOWN_DH_GROUP) - Add RSA-PSS hash algorithm validation (ERR_CRYPTO_INVALID_DIGEST) - Fix RSA key generation to return Result instead of panicking on invalid parameters - Add RSA public exponent validation to prevent infinite loops with even/small exponents - Fix EC curve error message to match Node.js - Enable test-crypto-keygen.js Node compat test Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… padding - Support parsing private key PEM/DER in publicEncrypt to extract the public key (matching Node.js behavior where publicEncrypt accepts private keys) - Reject encrypt/decrypt operations on non-RSA key types (rsa-pss, dsa, ec, ed25519, etc.) with "operation not supported for this keytype" - Reject PKCS1 padding for RSA-PSS keys in sign/verify operations with "illegal or unsupported padding mode" - Enable 5 new Node compat tests: test-crypto-keygen-sync, test-crypto-keygen-rsa-pss, test-crypto-keygen-eddsa, test-crypto-keygen-key-object-without-encoding, test-crypto-keygen-promisify Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
kajukitli
approved these changes
Mar 10, 2026
Contributor
kajukitli
left a comment
There was a problem hiding this comment.
lgtm, solid set of validation improvements
highlights:
- sync validation errors now throw sync (not in promise rejection) — matches Node.js
- RSA exponent validation prevents infinite loops with even/small exponents
- good coverage of error codes matching Node.js (
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,ERR_CRYPTO_UNKNOWN_DH_GROUP, etc.) - rejecting PKCS1 padding for RSA-PSS keys is correct
one minor note on the DH group check:
if (
group !== "modp5" && group !== "modp14" && group !== "modp15" &&
group !== "modp16" && group !== "modp17" && group !== "modp18"
) {could use a Set for cleaner lookup, but it's only 6 values so not a big deal.
enabling 6 new node compat tests is a nice win 👍
…alidation # Conflicts: # ext/node/polyfills/internal/crypto/keygen.ts # tests/node_compat/config.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
generateKeyPairto callcreateJobsynchronously so validation errors throw synchronously (matching Node.js behavior)ERR_INVALID_ARG_VALUE,ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,ERR_CRYPTO_UNKNOWN_CIPHER)ERR_CRYPTO_UNKNOWN_DH_GROUP)ERR_CRYPTO_INVALID_DIGEST)Resultinstead of panicking on invalid parameters, and validates public exponents to prevent infinite loops with even/small exponentspublicEncryptto extract the public key (matching Node.js behavior)test-crypto-keygen,test-crypto-keygen-sync,test-crypto-keygen-rsa-pss,test-crypto-keygen-eddsa,test-crypto-keygen-key-object-without-encoding,test-crypto-keygen-promisify