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

http{s}: fix connecting to localhost if URL is invalid #2967

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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: 3 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function ClientRequest(options, cb) {

if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new Error('Unable to determine the domain name');
}
} else {
options = util._extend({}, options);
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 we also need the check here? Why not just check after these if statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Fishrock123 At this place, if user doesn't pass hostname, then the default value undefined will be used

Copy link
Contributor

Choose a reason for hiding this comment

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

Won't that default to localhost again? Isn't that what this is trying to prevent? I don't understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Fishrock123 If the user doesn't specify any hostname in the options, then defaulting to localhost is expected. But when I give google.com (without http://), it should not take me to localhost.

}
Expand Down
3 changes: 3 additions & 0 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ exports.Agent = Agent;
exports.request = function(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new Error('Unable to determine the domain name');
}
} else {
options = util._extend({}, options);
}
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-http-invalid-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

require('../common');
const assert = require('assert');
const http = require('http');
const https = require('https');
const error = 'Unable to determine the domain name';

function test(host) {
['get', 'request'].forEach((method) => {
[http, https].forEach((module) => {
assert.throws(() => module[method](host, () => {
throw new Error(`${module}.${method} should not connect to ${host}`);
}), error);
});
});
}

['www.nodejs.org', 'localhost', '127.0.0.1', 'http://:80/'].forEach(test);