fix(ext/node): fix error type in DiffieHellman constructor (#29721)

This commit is contained in:
Yoshiya Hinosawa 2025-06-17 11:43:26 +09:00 committed by GitHub
parent e5b96243ac
commit b979c886e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 6 deletions

View file

@ -117,7 +117,10 @@ export class DiffieHellmanImpl {
// The supplied parameter is our primeLength, generate a suitable prime. // The supplied parameter is our primeLength, generate a suitable prime.
this.#primeLength = sizeOrKey as number; this.#primeLength = sizeOrKey as number;
if (this.#primeLength < 2) { if (this.#primeLength < 2) {
throw new NodeError("ERR_OSSL_BN_BITS_TOO_SMALL", "bits too small"); throw new NodeError(
"ERR_OSSL_DH_MODULUS_TOO_SMALL",
"modulus too small",
);
} }
this.#prime = Buffer.from( this.#prime = Buffer.from(

View file

@ -43,7 +43,6 @@
"test-console-formatTime.js", "test-console-formatTime.js",
"test-console-instance.js", "test-console-instance.js",
"test-console-tty-colors.js", "test-console-tty-colors.js",
"test-crypto-dh-errors.js",
"test-crypto-hash.js", "test-crypto-hash.js",
"test-crypto-hkdf.js", "test-crypto-hkdf.js",
"test-crypto-hmac.js", "test-crypto-hmac.js",

View file

@ -2,7 +2,7 @@
// deno-lint-ignore-file // deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license. // Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 20.11.1 // Taken from Node 23.9.0
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. // This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
'use strict'; 'use strict';
@ -12,6 +12,7 @@ if (!common.hasCrypto)
const assert = require('assert'); const assert = require('assert');
const crypto = require('crypto'); const crypto = require('crypto');
const { hasOpenSSL3 } = require('../common/crypto');
// https://github.com/nodejs/node/issues/32738 // https://github.com/nodejs/node/issues/32738
// XXX(bnoordhuis) validateInt32() throwing ERR_OUT_OF_RANGE and RangeError // XXX(bnoordhuis) validateInt32() throwing ERR_OUT_OF_RANGE and RangeError
@ -31,7 +32,7 @@ assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), {
}); });
for (const bits of [-1, 0, 1]) { for (const bits of [-1, 0, 1]) {
if (common.hasOpenSSL3) { if (hasOpenSSL3) {
assert.throws(() => crypto.createDiffieHellman(bits), { assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL', code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL',
name: 'Error', name: 'Error',
@ -50,7 +51,7 @@ for (const g of [-1, 1]) {
const ex = { const ex = {
code: 'ERR_OSSL_DH_BAD_GENERATOR', code: 'ERR_OSSL_DH_BAD_GENERATOR',
name: 'Error', name: 'Error',
message: /bad generator/, message: /(?:bad[_ ]generator)/i,
}; };
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex); assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex); assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);
@ -62,7 +63,7 @@ for (const g of [Buffer.from([]),
const ex = { const ex = {
code: 'ERR_OSSL_DH_BAD_GENERATOR', code: 'ERR_OSSL_DH_BAD_GENERATOR',
name: 'Error', name: 'Error',
message: /bad generator/, message: /(?:bad[_ ]generator)/i,
}; };
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex); assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex); assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);