Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit b4b750b

Browse files
indutnyisaacs
authored andcommitted
tls: follow RFC6125 more stricly
* Allow wildcards only in left-most part of hostname identifier. * Do not match CN if altnames are present
1 parent 20a3c5d commit b4b750b

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

lib/tls.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ function checkServerIdentity(host, cert) {
9191
// The same applies to hostname with more than one wildcard,
9292
// if hostname has wildcard when wildcards are not allowed,
9393
// or if there are less than two dots after wildcard (i.e. *.com or *d.com)
94-
if (/\*.*\*/.test(host) || !wildcards && /\*/.test(host) ||
94+
//
95+
// also
96+
//
97+
// "The client SHOULD NOT attempt to match a presented identifier in
98+
// which the wildcard character comprises a label other than the
99+
// left-most label (e.g., do not match bar.*.example.net)."
100+
// RFC6125
101+
if (!wildcards && /\*/.test(host) || /[\.\*].*\*/.test(host) ||
95102
/\*/.test(host) && !/\*.*\..+\..+/.test(host)) {
96103
return /$./;
97104
}
@@ -112,6 +119,7 @@ function checkServerIdentity(host, cert) {
112119
var dnsNames = [],
113120
uriNames = [],
114121
ips = [],
122+
matchCN = true,
115123
valid = false;
116124

117125
// There're several names to perform check against:
@@ -120,6 +128,7 @@ function checkServerIdentity(host, cert) {
120128
//
121129
// Walk through altnames and generate lists of those names
122130
if (cert.subjectaltname) {
131+
matchCN = false;
123132
cert.subjectaltname.split(/, /g).forEach(function(altname) {
124133
if (/^DNS:/.test(altname)) {
125134
dnsNames.push(altname.slice(4));
@@ -155,14 +164,24 @@ function checkServerIdentity(host, cert) {
155164

156165
dnsNames = dnsNames.concat(uriNames);
157166

158-
// And only after check if hostname matches CN
159-
var commonNames = cert.subject.CN;
160-
if (Array.isArray(commonNames)) {
161-
for (var i = 0, k = commonNames.length; i < k; ++i) {
162-
dnsNames.push(regexpify(commonNames[i], true));
167+
if (dnsNames.length > 0) matchCN = false;
168+
169+
// Match against Common Name (CN) only if altnames are not present.
170+
//
171+
// "As noted, a client MUST NOT seek a match for a reference identifier
172+
// of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,
173+
// URI-ID, or any application-specific identifier types supported by the
174+
// client."
175+
// RFC6125
176+
if (matchCN) {
177+
var commonNames = cert.subject.CN;
178+
if (Array.isArray(commonNames)) {
179+
for (var i = 0, k = commonNames.length; i < k; ++i) {
180+
dnsNames.push(regexpify(commonNames[i], true));
181+
}
182+
} else {
183+
dnsNames.push(regexpify(commonNames, true));
163184
}
164-
} else {
165-
dnsNames.push(regexpify(commonNames, true));
166185
}
167186

168187
valid = dnsNames.some(function(re) {

test/simple/test-tls-check-server-identity.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ var tests = [
3131
{ host: 'a.com', cert: { subject: { CN: 'b.com' } }, result: false },
3232
{ host: 'a.com', cert: { subject: { CN: 'a.com.' } }, result: true },
3333

34-
// No wildcards in CN
35-
{ host: 'b.a.com', cert: { subject: { CN: '*.a.com' } }, result: false },
34+
// Wildcards in CN
35+
{ host: 'b.a.com', cert: { subject: { CN: '*.a.com' } }, result: true },
36+
{ host: 'b.a.com', cert: {
37+
subjectaltname: 'DNS:omg.com',
38+
subject: { CN: '*.a.com' } },
39+
result: false
40+
},
3641

3742
// Multiple CN fields
3843
{
@@ -69,7 +74,7 @@ var tests = [
6974
subjectaltname: 'DNS:*.a.com',
7075
subject: { CN: 'a.com' }
7176
},
72-
result: true
77+
result: false
7378
},
7479
{
7580
host: 'a.com', cert: {
@@ -193,7 +198,7 @@ var tests = [
193198
subjectaltname: 'DNS:a.com',
194199
subject: { CN: 'localhost' }
195200
},
196-
result: true
201+
result: false
197202
},
198203
];
199204

0 commit comments

Comments
 (0)