Skip to content

Commit 71274b0

Browse files
Guilherme SouzaFishrock123
Guilherme Souza
authored andcommitted
tls_wrap: use localhost if options.host is empty
tls.connect(options) with no options.host should accept a certificate with CN: 'localhost'. Fix Error: Hostname/IP doesn't match certificate's altnames: "Host: undefined. is not cert's CN: localhost" 'localhost' is not added directly to defaults because that is not always desired (for example, when using options.socket) PR-URL: #1493 PORT-PR-URL: #1560 PORT-FROM: v2.x / a7d7463 Fixes: #1489 Reviewed-By: Brendan Ashworth <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent 0eb74a8 commit 71274b0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/_tls_wrap.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,8 @@ exports.connect = function(/* [port, host], options, cb */) {
871871

872872
var hostname = options.servername ||
873873
options.host ||
874-
options.socket && options.socket._host,
874+
(options.socket && options.socket._host) ||
875+
'localhost',
875876
NPN = {},
876877
context = tls.createSecureContext(options);
877878
tls.convertNPNProtocols(options.NPNProtocols, NPN);
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var common = require('../common');
2+
3+
if (!common.hasCrypto) {
4+
console.log('1..0 # Skipped: missing crypto');
5+
process.exit();
6+
}
7+
var tls = require('tls');
8+
9+
var assert = require('assert');
10+
var fs = require('fs');
11+
var path = require('path');
12+
13+
var cert = fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem'));
14+
var key = fs.readFileSync(path.join(common.fixturesDir, 'test_key.pem'));
15+
16+
// https://github.com/iojs/io.js/issues/1489
17+
// tls.connect(options) with no options.host should accept a cert with
18+
// CN:'localhost'
19+
tls.createServer({
20+
key: key,
21+
cert: cert
22+
}).listen(common.PORT);
23+
24+
var socket = tls.connect({
25+
port: common.PORT,
26+
ca: cert,
27+
// No host set here. 'localhost' is the default,
28+
// but tls.checkServerIdentity() breaks before the fix with:
29+
// Error: Hostname/IP doesn't match certificate's altnames:
30+
// "Host: undefined. is not cert's CN: localhost"
31+
}, function() {
32+
assert(socket.authorized);
33+
process.exit();
34+
});

0 commit comments

Comments
 (0)