Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/internal/tls/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const {
ObjectSetPrototypeOf,
ReflectApply,
RegExp,
StringPrototypeEndsWith,
StringPrototypeSlice,
Symbol,
SymbolFor,
} = primordials;
Expand Down Expand Up @@ -1752,13 +1754,22 @@ Server.prototype.setTicketKeys = function setTicketKeys(keys) {
};

// SNI Contexts High-Level API

// A terminal dot denotes the DNS root and does not change the name: remove it
// so that 'host.example.' selects the context registered for 'host.example'.
function removeTerminalDot(servername) {
return StringPrototypeEndsWith(servername, '.') ?
StringPrototypeSlice(servername, 0, -1) :
servername;
}

Server.prototype.addContext = function(servername, context) {
if (!servername) {
throw new ERR_TLS_REQUIRED_SERVER_NAME();
}

const re = new RegExp(`^${
servername
removeTerminalDot(servername)
.replace(/([.^$+?\-\\[\]{}])/g, '\\$1')
.replaceAll('*', '[^.]*')
}$`, 'i');
Expand All @@ -1782,6 +1793,7 @@ Server.prototype[EE.captureRejectionSymbol] = function(

function SNICallback(servername, callback) {
const contexts = this.server._contexts;
servername = removeTerminalDot(servername);

for (let i = contexts.length - 1; i >= 0; --i) {
const elem = contexts[i];
Expand Down
74 changes: 74 additions & 0 deletions test/parallel/test-tls-sni-trailing-dot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

// A server name with a terminal root dot refers to the same host as the
// name without it, so it must select the same SNI context. The context
// that was selected is detected from the server certificate presented to
// the client.

const assert = require('assert');
const tls = require('tls');
const fixtures = require('../common/fixtures');

const defaultCreds = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
};
const sniCreds = {
key: fixtures.readKey('agent2-key.pem'),
cert: fixtures.readKey('agent2-cert.pem'),
};

function getServerCommonName(port, servername, version) {
return new Promise((resolve, reject) => {
const socket = tls.connect({
port,
servername,
rejectUnauthorized: false,
minVersion: version,
maxVersion: version,
}, common.mustCall(() => {
const { CN } = socket.getPeerCertificate().subject;
socket.end();
resolve(CN);
}));
socket.on('error', reject);
});
}

const cases = [
// [registered name, names a client may send]
['host.example', ['host.example', 'host.example.']],
['host.example.', ['host.example', 'host.example.']],
['*.host.example', ['a.host.example', 'a.host.example.']],
];

async function test(version, registered, servernames) {
const server = tls.createServer({
...defaultCreds,
minVersion: version,
maxVersion: version,
}, (socket) => socket.end());
server.addContext(registered, sniCreds);
await new Promise((resolve) => server.listen(0, resolve));

const { port } = server.address();
for (const servername of servernames) {
assert.strictEqual(await getServerCommonName(port, servername, version),
'agent2', `${version} ${registered} ${servername}`);
}

// A name that matches no context still uses the default one.
assert.strictEqual(await getServerCommonName(port, 'other.example.', version),
'agent1', `${version} ${registered} other.example.`);
await new Promise((resolve) => server.close(resolve));
}

(async () => {
for (const version of ['TLSv1.2', 'TLSv1.3']) {
for (const [registered, servernames] of cases)
await test(version, registered, servernames);
}
})().then(common.mustCall());
Loading