Skip to content

Commit 74b00cc

Browse files
jasonmacgowanrichardlau
authored andcommitted
tls: allow empty subject even with altNames defined
Behavior described in #11771 is still true even though the issue is closed. This PR is to allow DNS and URI names, even when there is not a subject. Refs: #11771 PR-URL: #22906 Reviewed-By: James M Snell <[email protected]>
1 parent 745b329 commit 74b00cc

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/tls.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,28 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
214214
let valid = false;
215215
let reason = 'Unknown reason';
216216

217+
const hasAltNames =
218+
dnsNames.length > 0 || ips.length > 0 || uriNames.length > 0;
219+
220+
hostname = unfqdn(hostname); // Remove trailing dot for error messages.
221+
217222
if (net.isIP(hostname)) {
218223
valid = ips.includes(canonicalizeIP(hostname));
219224
if (!valid)
220225
reason = `IP: ${hostname} is not in the cert's list: ${ips.join(', ')}`;
221226
// TODO(bnoordhuis) Also check URI SANs that are IP addresses.
222-
} else if (subject) {
223-
hostname = unfqdn(hostname); // Remove trailing dot for error messages.
227+
} else if (hasAltNames || subject) {
224228
const hostParts = splitHost(hostname);
225229
const wildcard = (pattern) => check(hostParts, pattern, true);
226-
const noWildcard = (pattern) => check(hostParts, pattern, false);
227230

228-
// Match against Common Name only if no supported identifiers are present.
229-
if (dnsNames.length === 0 && ips.length === 0 && uriNames.length === 0) {
231+
if (hasAltNames) {
232+
const noWildcard = (pattern) => check(hostParts, pattern, false);
233+
valid = dnsNames.some(wildcard) || uriNames.some(noWildcard);
234+
if (!valid)
235+
reason =
236+
`Host: ${hostname}. is not in the cert's altnames: ${altNames}`;
237+
} else {
238+
// Match against Common Name only if no supported identifiers exist.
230239
const cn = subject.CN;
231240

232241
if (Array.isArray(cn))
@@ -236,11 +245,6 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
236245

237246
if (!valid)
238247
reason = `Host: ${hostname}. is not cert's CN: ${cn}`;
239-
} else {
240-
valid = dnsNames.some(wildcard) || uriNames.some(noWildcard);
241-
if (!valid)
242-
reason =
243-
`Host: ${hostname}. is not in the cert's altnames: ${altNames}`;
244248
}
245249
} else {
246250
reason = 'Cert is empty';

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

+14
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ const tests = [
9494
error: 'Cert is empty'
9595
},
9696

97+
// Empty Subject w/DNS name
98+
{
99+
host: 'a.com', cert: {
100+
subjectaltname: 'DNS:a.com',
101+
}
102+
},
103+
104+
// Empty Subject w/URI name
105+
{
106+
host: 'a.b.a.com', cert: {
107+
subjectaltname: 'URI:http://a.b.a.com/',
108+
}
109+
},
110+
97111
// Multiple CN fields
98112
{
99113
host: 'foo.com', cert: {

0 commit comments

Comments
 (0)