node/test/parallel/test-tls-set-default-ca-certificates-precedence-empty.js
Joyee Cheung edd66d0130
crypto: add tls.setDefaultCACertificates()
This API allows dynamically configuring CA certificates that
will be used by the Node.js TLS clients by default.

Once called, the provided certificates will become the default CA
certificate list returned by `tls.getCACertificates('default')` and
used by TLS connections that don't specify their own CA certificates.

This function only affects the current Node.js thread.

PR-URL: https://github.com/nodejs/node/pull/58822
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
2025-07-18 19:57:53 +00:00

51 lines
1.6 KiB
JavaScript

'use strict';
// This tests that per-connection ca option overrides empty default CA certificates
const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');
const assert = require('assert');
const https = require('https');
const tls = require('tls');
const fixtures = require('../common/fixtures');
const server = https.createServer({
cert: fixtures.readKey('agent8-cert.pem'),
key: fixtures.readKey('agent8-key.pem'),
}, common.mustCall((req, res) => {
res.writeHead(200);
res.end('per-connection ca works');
}, 1));
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const fakeStartcomCert = fixtures.readKey('fake-startcom-root-cert.pem');
// Set default CA to empty array - connections should normally fail
tls.setDefaultCACertificates([]);
// Verify that default CA is empty
const defaultCerts = tls.getCACertificates('default');
assert.deepStrictEqual(defaultCerts, []);
// Connection with per-connection ca option should succeed despite empty default
const req = https.request({
hostname: 'localhost',
port: port,
path: '/',
method: 'GET',
ca: [fakeStartcomCert] // This should override the empty default
}, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', common.mustCall(() => {
assert.strictEqual(data, 'per-connection ca works');
server.close();
}));
}));
req.on('error', common.mustNotCall('Should not error with per-connection ca option'));
req.end();
}));