Skip to content

Commit eb4201f

Browse files
rlidwkajasnell
authored andcommitted
url: drop auth in url.resolve() if host changes
Fixes: #1435 PR-URL: #1480 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 3ee68f7 commit eb4201f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/url.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ Url.prototype.resolveObject = function(relative) {
754754
if (relative.protocol) {
755755
relative.hostname = null;
756756
relative.port = null;
757+
result.auth = null;
757758
if (relative.host) {
758759
if (relPath[0] === '') relPath[0] = relative.host;
759760
else relPath.unshift(relative.host);
@@ -765,10 +766,14 @@ Url.prototype.resolveObject = function(relative) {
765766

766767
if (isRelAbs) {
767768
// it's absolute.
768-
result.host = (relative.host || relative.host === '') ?
769-
relative.host : result.host;
770-
result.hostname = (relative.hostname || relative.hostname === '') ?
771-
relative.hostname : result.hostname;
769+
if (relative.host || relative.host === '') {
770+
result.host = relative.host;
771+
result.auth = null;
772+
}
773+
if (relative.hostname || relative.hostname === '') {
774+
result.hostname = relative.hostname;
775+
result.auth = null;
776+
}
772777
result.search = relative.search;
773778
result.query = relative.query;
774779
srcPath = relPath;

test/parallel/test-url.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,21 @@ var relativeTests2 = [
15381538
//changeing auth
15391539
['http://diff:[email protected]',
15401540
'http://asdf:[email protected]',
1541-
'http://diff:[email protected]/']
1541+
'http://diff:[email protected]/'],
1542+
1543+
// https://github.com/nodejs/node/issues/1435
1544+
['https://another.host.com/',
1545+
'https://user:[email protected]/',
1546+
'https://another.host.com/'],
1547+
['//another.host.com/',
1548+
'https://user:[email protected]/',
1549+
'https://another.host.com/'],
1550+
['http://another.host.com/',
1551+
'https://user:[email protected]/',
1552+
'http://another.host.com/'],
1553+
['mailto:another.host.com',
1554+
1555+
'mailto:another.host.com'],
15421556
];
15431557
relativeTests2.forEach(function(relativeTest) {
15441558
const a = url.resolve(relativeTest[1], relativeTest[0]);

0 commit comments

Comments
 (0)