From 6ef3f9f450b1645ac7d2833c09dcee19141b1c91 Mon Sep 17 00:00:00 2001 From: Daisuke <42408108+da1suk8@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:00:01 +0900 Subject: [PATCH] fix(ext/node): return an error instead of panicking on invalid scrypt params --- ext/node/ops/crypto/mod.rs | 55 +++++++++++++++++++- ext/node/polyfills/internal/crypto/scrypt.ts | 30 +++++------ 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/ext/node/ops/crypto/mod.rs b/ext/node/ops/crypto/mod.rs index 44c9af5a..c0922888 100644 --- a/ext/node/ops/crypto/mod.rs +++ b/ext/node/ops/crypto/mod.rs @@ -582,7 +582,7 @@ fn scrypt( parallelization, keylen as usize, ) - .unwrap(); + .map_err(|_| generic_error("Invalid scrypt param"))?; // Call into scrypt let res = scrypt::scrypt(&password, &salt, ¶ms, output_buffer); @@ -1077,3 +1077,56 @@ pub fn op_node_verify_ed25519( Ok(verified) } + +#[cfg(test)] +mod tests { + use super::*; + + fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() + } + + fn run_scrypt( + keylen: u32, + log_n: u32, + r: u32, + p: u32, + ) -> Result, deno_core::error::AnyError> { + let mut out = vec![0u8; keylen as usize]; + super::scrypt( + StringOrBuffer::String("password".into()), + StringOrBuffer::String("salt".into()), + keylen, + log_n, + r, + p, + 32 << 20, + &mut out, + )?; + Ok(out) + } + + // Regression: `scrypt::Params::new` used to be `unwrap()`ed, so parameters + // that pass the JS layer but fail the crate's validation panicked inside a + // sync op. Ops run on the V8 extern "C" path, so that panic aborts the whole + // runtime process instead of surfacing as a JS error. + #[test] + fn scrypt_invalid_params_return_error_instead_of_panicking() { + // keylen 128 passes `Buffer.alloc` in JS but is outside the output + // length the scrypt crate accepts (10..=64). + assert!(run_scrypt(128, 14, 8, 1).is_err()); + // N = 2^16 with r = 1 violates N < 2^(16 * r); Node rejects it with + // ERR_CRYPTO_INVALID_SCRYPT_PARAMS. + assert!(run_scrypt(32, 16, 1, 1).is_err()); + } + + #[test] + fn scrypt_valid_params_unchanged() { + // Node.js: crypto.scryptSync("password", "salt", 32) with default options + // (N = 16384, r = 8, p = 1). + assert_eq!( + hex(&run_scrypt(32, 14, 8, 1).unwrap()), + "745731af4484f323968969eda289aeee005b5903ac561e64a5aca121797bf773" + ); + } +} diff --git a/ext/node/polyfills/internal/crypto/scrypt.ts b/ext/node/polyfills/internal/crypto/scrypt.ts index ce8649bb..c0e485d6 100644 --- a/ext/node/polyfills/internal/crypto/scrypt.ts +++ b/ext/node/polyfills/internal/crypto/scrypt.ts @@ -107,23 +107,19 @@ export function scrypt( throw new Error("exceeds max memory"); } - try { - op_node_scrypt_async( - password, - salt, - keylen, - Math.log2(N), - r, - p, - maxmem, - ).then( - (buf: Uint8Array) => { - cb(null, Buffer.from(buf.buffer)); - }, - ); - } catch (err: unknown) { - return cb(err); - } + op_node_scrypt_async( + password, + salt, + keylen, + Math.log2(N), + r, + p, + maxmem, + ).then( + (buf: Uint8Array) => { + cb(null, Buffer.from(buf.buffer)); + }, + ).catch((err: unknown) => cb(err)); } export default {