-
-
Notifications
You must be signed in to change notification settings - Fork 163
fix(stdlib): crypto.getHashes/getCiphers/getCurves/getCipherInfo through a dynamic receiver #11176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+115
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Fix `crypto.getHashes()`, `getCiphers()`, `getCurves()` and `getCipherInfo()` | ||
| returning `undefined` when the `crypto` receiver is not a statically known | ||
| `node:crypto` reference: a `let` assigned from `require`, a detached method | ||
| value, or a module held in an object. Those calls reach | ||
| `js_crypto_native_dispatch` in `perry-stdlib/src/crypto/random.rs`, which had | ||
| no arm for the four names. It now routes them to the same helpers the direct | ||
| lowering uses. | ||
|
|
||
| undici 8.9.0's `subresource-integrity.js` calls `crypto.getHashes()` this way | ||
| at module init and reads `.length`. Under `PERRY_NO_AUTO_OPTIMIZE=1` that made | ||
| every `require('undici')` throw "Cannot read properties of undefined (reading | ||
| 'length')". This is part of #11046. The auto-optimize failure reported there is | ||
| the `Socket.read()` gap (#10908). Covered by | ||
| `test-files/test_gap_11046_crypto_inventory_dynamic_receiver.cts` and a | ||
| dispatcher unit test. | ||
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
48 changes: 48 additions & 0 deletions
48
test-files/test_gap_11046_crypto_inventory_dynamic_receiver.cts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // #11046: `crypto.getHashes()` / `getCiphers()` / `getCurves()` / | ||
| // `getCipherInfo()` reached through a receiver the compiler cannot prove is | ||
| // `node:crypto`. undici's subresource-integrity module does exactly this at | ||
| // module init: | ||
| // | ||
| // let crypto | ||
| // if (runtimeFeatures.has('crypto')) { | ||
| // crypto = require('node:crypto') | ||
| // const cryptoHashes = crypto.getHashes() | ||
| // if (cryptoHashes.length === 0) { ... } | ||
| // | ||
| // The statically-known `NativeModuleRef` form lowers straight to the runtime | ||
| // helper, but the dynamic form goes through the runtime's native-module | ||
| // dispatcher, which had no arm for these names and returned `undefined`, so | ||
| // `cryptoHashes.length` threw "Cannot read properties of undefined (reading | ||
| // 'length')" and every `require('undici')` failed. | ||
|
|
||
| // 1. undici's exact shape: a `let` assigned from require, then a feature probe. | ||
| let crypto: any; | ||
| crypto = require("node:crypto"); | ||
| const cryptoHashes = crypto.getHashes(); | ||
| console.log("let getHashes:", Array.isArray(cryptoHashes), cryptoHashes.length > 0); | ||
| const tokens = new Map([["sha256", 0], ["sha384", 1], ["sha512", 2]]); | ||
| for (const algorithm of tokens.keys()) { | ||
| if (cryptoHashes.includes(algorithm) === false) { | ||
| tokens.delete(algorithm); | ||
| } | ||
| } | ||
| console.log("supported SRI algorithms:", [...tokens.keys()].join(",")); | ||
|
|
||
| // 2. The other inventories through the same dynamic receiver. | ||
| const ciphers = crypto.getCiphers(); | ||
| console.log("getCiphers:", Array.isArray(ciphers), ciphers.includes("aes-256-gcm"), ciphers.includes("aes-128-cbc")); | ||
| const curves = crypto.getCurves(); | ||
| console.log("getCurves:", Array.isArray(curves), curves.includes("prime256v1"), curves.includes("secp384r1")); | ||
| const info = crypto.getCipherInfo("aes-256-cbc"); | ||
| console.log("getCipherInfo:", info.name, info.mode, info.keyLength, info.ivLength, info.blockSize); | ||
| console.log("getCipherInfo unknown:", crypto.getCipherInfo("no-such-cipher")); | ||
|
|
||
| // 3. Detached method values and a receiver held in an object. | ||
| const getHashes = crypto.getHashes; | ||
| console.log("detached:", typeof getHashes, Array.isArray(getHashes()), getHashes().includes("sha1")); | ||
| const holder: any = { c: require("node:crypto") }; | ||
| console.log("held:", Array.isArray(holder.c.getHashes()), holder.c.getCurves().length > 0); | ||
|
|
||
| // 4. Control: the statically-known receiver (already worked). | ||
| const direct = require("node:crypto"); | ||
| console.log("const receiver:", Array.isArray(direct.getHashes()), direct.getHashes().includes("sha384")); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Correct the source path.
Add the
crates/prefix. The source file iscrates/perry-stdlib/src/crypto/random.rs, so the path in this release-note fragment does not identify it from the repository root.🤖 Prompt for AI Agents