mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

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>
51 lines
1.6 KiB
JavaScript
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();
|
|
}));
|