Skip to content

Commit cca2a21

Browse files
bartlomiejuclaude
andcommitted
fix: reject zero-valued options.add in generatePrime
A zero-length or all-zero add buffer would pass validation and reach the Rust generator where modulus operations assume non-zero add, causing a division-by-zero panic. Throw ERR_OUT_OF_RANGE early. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bffd0a0 commit cca2a21

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

ext/node/polyfills/internal/crypto/random.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,13 @@ function validateRandomPrimeJob(
310310
const remBuf = rem ? toUint8Array(rem) : undefined;
311311

312312
if (addBuf) {
313-
// Node.js/OpenSSL: bit count of add must not exceed requested size
313+
// add must be non-zero; a zero modulus would cause division by zero in the
314+
// Rust generator.
314315
const addBitCount = bitCount(addBuf);
316+
if (addBitCount === 0) {
317+
throw new NodeRangeError("ERR_OUT_OF_RANGE", "invalid options.add");
318+
}
319+
// Node.js/OpenSSL: bit count of add must not exceed requested size
315320
if (addBitCount > size) {
316321
throw new NodeRangeError("ERR_OUT_OF_RANGE", "invalid options.add");
317322
}

0 commit comments

Comments
 (0)