Commit 7531311
fix(crypto): route createSecretKey/createPrivateKey/createPublicKey through value-dispatch (#6680)
The dynamic crypto value-dispatch (`js_crypto_native_dispatch`) — reached when a
key-material constructor is called as a VALUE rather than a direct member call:
a named import, a computed property access, or a bundled
`require('crypto').createPrivateKey` (turbopack/webpack) — had no arms for
`createSecretKey`, `createPrivateKey`, or `createPublicKey`, so they fell to
`_ => undefined`.
That silently broke jsonwebtoken's HS* signing path. `sign.js` normalizes a
string secret with:
try { secretOrPrivateKey = createPrivateKey(secret) }
catch { secretOrPrivateKey = createSecretKey(Buffer.from(secret)) }
...
if (alg.startsWith('HS') && secretOrPrivateKey.type !== 'secret') { ... }
Node's `createPrivateKey('<plain string>')` THROWS on non-key material, so the
`catch` runs and produces a `secret` KeyObject. Under Perry the value-dispatch
returned `undefined` without throwing, the `catch` never ran,
`secretOrPrivateKey` became `undefined`, and reading `.type` crashed with
"Cannot read properties of undefined (reading 'type')".
Add the three arms, routing to the same runtime helpers the codegen fast-path
uses (`js_crypto_create_secret_key` / `js_crypto_create_private_key_value` /
`js_crypto_create_public_key_value`). Those helpers already throw on invalid
material, so the value-form now matches the direct member-call form and Node.
Fixes #6675.
Co-authored-by: Ralph Küpper <ralph@skelpo.com>1 parent a3b3a2e commit 7531311
2 files changed
Lines changed: 102 additions & 0 deletions
File tree
- crates/perry-stdlib/src/crypto
- test-files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
346 | 346 | | |
347 | 347 | | |
348 | 348 | | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
349 | 375 | | |
350 | 376 | | |
351 | 377 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
0 commit comments