Skip to content

Commit c9786bb

Browse files
thefourtheyervagg
authored andcommitted
http{s}: don't connect to localhost on invalid URL
If the URL passed to `http{s}.request` or `http{s}.get` is not properly parsable by `url.parse`, we fall back to use `localhost` and port 80. This creates confusing error messages like in this question http://stackoverflow.com/q/32675907/1903116. This patch throws an error message, if `url.parse` fails to parse the URL properly. Previous Discussion: #2966 PR-URL: #2967 Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6a04cc0 commit c9786bb

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

lib/_http_client.js

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function ClientRequest(options, cb) {
2222

2323
if (typeof options === 'string') {
2424
options = url.parse(options);
25+
if (!options.hostname) {
26+
throw new Error('Unable to determine the domain name');
27+
}
2528
} else {
2629
options = util._extend({}, options);
2730
}

lib/https.js

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ exports.Agent = Agent;
163163
exports.request = function(options, cb) {
164164
if (typeof options === 'string') {
165165
options = url.parse(options);
166+
if (!options.hostname) {
167+
throw new Error('Unable to determine the domain name');
168+
}
166169
} else {
167170
options = util._extend({}, options);
168171
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
const https = require('https');
7+
const error = 'Unable to determine the domain name';
8+
9+
function test(host) {
10+
['get', 'request'].forEach((method) => {
11+
[http, https].forEach((module) => {
12+
assert.throws(() => module[method](host, () => {
13+
throw new Error(`${module}.${method} should not connect to ${host}`);
14+
}), error);
15+
});
16+
});
17+
}
18+
19+
['www.nodejs.org', 'localhost', '127.0.0.1', 'http://:80/'].forEach(test);

0 commit comments

Comments
 (0)