Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c9786bb

Browse filesBrowse 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: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6a04cc0 commit c9786bb
Copy full SHA for c9786bb

File tree

Expand file treeCollapse file tree

3 files changed

+25
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-0
lines changed
Open diff view settings
Collapse file

‎lib/_http_client.js‎

Copy file name to clipboardExpand all lines: lib/_http_client.js
+3Lines changed: 3 additions & 0 deletions
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
}
Collapse file

‎lib/https.js‎

Copy file name to clipboardExpand all lines: lib/https.js
+3Lines changed: 3 additions & 0 deletions
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
}
Collapse file
+19Lines changed: 19 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.