node/test/common/quic.mjs
James M Snell 5a3ab93c49
quic: improve peer cert verification
On the client, add verifyPeer: 'auto', 'strict', and
'manual' modes. The 'strict' mode will reject invalid
certs at the handshake layer, while the 'manual' mode
allows the application to inspect the peer cert and decide
whether to trust it or not. The 'auto' mode is the default
and will reject invalid certs at a middle layer after the
onhandshake event.

Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opencode/Opus 4.6
PR-URL: https://github.com/nodejs/node/pull/63483
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2026-05-27 11:35:47 +02:00

61 lines
1.9 KiB
JavaScript

// Shared helpers for QUIC tests.
//
// Usage:
// import { key, cert, listen, connect } from '../common/quic.mjs';
//
// Provides pre-loaded TLS credentials and thin wrappers around node:quic
// listen/connect that apply default options suitable for most tests.
import * as fixtures from '../common/fixtures.mjs';
const { createPrivateKey } = await import('node:crypto');
const quic = await import('node:quic');
// Pre-loaded TLS credentials from the standard agent1 fixture pair.
const key = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const cert = fixtures.readKey('agent1-cert.pem');
/**
* Start a QUIC server with sensible test defaults.
* @param {Function} callback The session callback (receives QuicSession).
* @param {object} [options] Options forwarded to quic.listen(). The
* following defaults are applied when not specified:
* - sni: { '*': { keys: [key], certs: [cert] } }
* - alpn: ['quic-test']
* @returns {Promise<QuicEndpoint>}
*/
async function listen(callback, options = {}) {
const {
sni = { '*': { keys: [key], certs: [cert] } },
alpn = ['quic-test'],
...rest
} = options;
return quic.listen(callback, { sni, alpn, ...rest });
}
/**
* Connect a QUIC client with sensible test defaults.
* @param {SocketAddress|string} address The server address.
* @param {object} [options] Options forwarded to quic.connect(). The
* following defaults are applied when not specified:
* - alpn: 'quic-test'
* @returns {Promise<QuicSession>}
*/
async function connect(address, options = {}) {
const {
alpn = 'quic-test',
// Test helper defaults to 'manual' because tests use self-signed
// certs without a CA. Tests that want to verify cert validation
// behavior should set verifyPeer explicitly.
verifyPeer = 'manual',
...rest
} = options;
return quic.connect(address, { alpn, verifyPeer, ...rest });
}
export {
key,
cert,
listen,
connect,
};