Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls_wrap: use localhost if options.host is empty #1493

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ exports.connect = function(/* [port, host], options, cb */) {

var hostname = options.servername ||
options.host ||
options.socket && options.socket._host,
options.socket && options.socket._host ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't these need to be in parenthesis? Otherwise it will overlap, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they will overlap, but it's much better with () for sure

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Boolean logic defines and at higher precedence than or. So does JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unnecessary, as @kkoopa said: && has higher precedence. I just added the ( ) to make the intent clearer to the eye, since the precendence is not obvious.

What's the recommended style for this situation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we prefer shorter I think, but no harm in adding a few clarity parens, I'd say.

'localhost',
NPN = {},
context = tls.createSecureContext(options);
tls.convertNPNProtocols(options.NPNProtocols, NPN);
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-tls-connect-no-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var common = require('../common');

if (!common.hasCrypto) {
console.log('1..0 # Skipped: missing crypto');
process.exit();
}
var tls = require('tls');

var fs = require('fs');
var path = require('path');

var cert = fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem'));
var key = fs.readFileSync(path.join(common.fixturesDir, 'test_key.pem'));

// https://github.com/iojs/io.js/issues/1489
// tls.connect(options) with no options.host should accept a cert with CN:'localhost'
tls.createServer({
key: key,
cert: cert
}).listen(common.PORT);

tls.connect({
port: common.PORT,
ca: cert,
// No host set here. 'localhost' is the default,
// but tls.checkServerIdentity() breaks before the fix with:
// Error: Hostname/IP doesn't match certificate's altnames: "Host: undefined. is not cert's CN: localhost"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap at 80 columns please :)

}, function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: no space before the () in function() {

console.log('OK');
process.exit();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a terminating newline here so we don't get annoying diff changes later?