Skip to content

Commit

Permalink
fix(ext/node): scrypt panic when log_n > 64 (#27816)
Browse files Browse the repository at this point in the history
Throws an error instead of panic. Ref
#27716
  • Loading branch information
littledivy authored Jan 27, 2025
1 parent 8ccc05e commit 1efc773
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ext/node/ops/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ fn scrypt(
parallelization,
keylen as usize,
)
.unwrap();
.map_err(|_| JsErrorBox::generic("scrypt params construction failed"))?;

// Call into scrypt
let res = scrypt::scrypt(&password, &salt, &params, output_buffer);
Expand Down
30 changes: 13 additions & 17 deletions ext/node/polyfills/internal/crypto/scrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_node/crypto/crypto_scrypt_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,13 @@ Deno.test("scryptSync with options works correctly", () => {
]),
);
});

Deno.test("log_n > 64 doesn't panic", async () => {
const { promise, resolve } = Promise.withResolvers<void>();

scrypt("password", "salt", 128, () => {
resolve();
});

await promise;
});

0 comments on commit 1efc773

Please sign in to comment.