Skip to content

Commit f1810ed

Browse files
Mike AtkinsShigeki Ohtsu
Mike Atkins
authored and
Shigeki Ohtsu
committed
tls: handle empty cert in checkServerIndentity
This resolves nodejs/node-v0.x-archive#9272. `tlsSocket.getPeerCertificate` will return an empty object when the peer does not provide a certificate, but, prior to this, when the certificate is empty, `checkServerIdentity` would throw because the `subject` wasn't present on the cert. `checkServerIdentity` must return an error, not throw one, so this returns an error when the cert is empty instead of throwing a `TypeError`. PR-URL: #2343 Reviewed-By: Fedor Indutny <fedor@indutny.com> Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
1 parent ec6e5c7 commit f1810ed

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/tls.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
151151
host,
152152
ips.join(', '));
153153
}
154-
} else {
154+
} else if (cert.subject) {
155155
// Transform hostname to canonical form
156156
if (!/\.$/.test(host)) host += '.';
157157

@@ -204,6 +204,8 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
204204
cert.subject.CN);
205205
}
206206
}
207+
} else {
208+
reason = 'Cert is empty';
207209
}
208210

209211
if (!valid) {

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

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ var tests = [
3030
'DNS:omg.com'
3131
},
3232

33+
// Empty Cert
34+
{
35+
host: 'a.com',
36+
cert: { },
37+
error: 'Cert is empty'
38+
},
39+
3340
// Multiple CN fields
3441
{
3542
host: 'foo.com', cert: {

0 commit comments

Comments
 (0)