Skip to content

Commit adaaa2c

Browse files
committed
Strictly limit supported TLS hostnames
1 parent 6e35c7d commit adaaa2c

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/tls-handler.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ const getSNIPrefixParts = (servername: string, rootDomain: string) => {
2828
const serverNamePrefix = servername.endsWith(rootDomain)
2929
? servername.slice(0, -rootDomain.length - 1)
3030
: servername;
31+
32+
if (serverNamePrefix === '') return [];
3133
return serverNamePrefix.split('.');
3234
};
3335

36+
const VALID_SNI_PARTS = new Set([
37+
...Object.keys(SNI_PROTOCOL_FILTERS),
38+
'no-tls',
39+
'example'
40+
]);
41+
42+
const MAX_SNI_PARTS = 3;
43+
3444
const PROACTIVE_DOMAIN_REFRESH_INTERVAL = 1000 * 60 * 60 * 24; // Daily cert check for proactive domains
3545

3646
function proactivelyRefreshDomains(domains: string[], certGenerator: CertGenerator) {
@@ -73,6 +83,15 @@ export async function createTlsHandler(
7383
},
7484
SNICallback: (domain: string, cb: Function) => {
7585
const serverNameParts = getSNIPrefixParts(domain, tlsConfig.rootDomain);
86+
87+
if (serverNameParts.length > MAX_SNI_PARTS) {
88+
return cb(new Error(`Too many SNI parts (${serverNameParts.length})`), null);
89+
}
90+
91+
if (serverNameParts.some(part => !VALID_SNI_PARTS.has(part))) {
92+
return cb(new Error(`Invalid SNI part in '${domain}'`), null);
93+
}
94+
7695
if (serverNameParts.includes('no-tls')) {
7796
// This closes the unwanted TLS connection without response
7897
return cb(new Error('Intentionally rejecting TLS connection'), null);

test/https.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Connection: keep-alive
125125
].map(async ({ protocols, expected }) => {
126126
const conn = tls.connect({
127127
port: serverPort,
128-
servername: 'do-anything.localhost',
128+
servername: 'localhost',
129129
ALPNProtocols: protocols,
130130
rejectUnauthorized: false // Needed as it's untrusted
131131
});

0 commit comments

Comments
 (0)