diff --git a/lib/url.js b/lib/url.js index 82209db01c8c31..1683f905037c52 100644 --- a/lib/url.js +++ b/lib/url.js @@ -495,7 +495,9 @@ Url.prototype.resolveObject = function(relative) { } result.protocol = relative.protocol; - if (!relative.host && !hostlessProtocol[relative.protocol]) { + if (!relative.host && + !/^file:?$/.test(relative.protocol) && + !hostlessProtocol[relative.protocol]) { var relPath = (relative.pathname || '').split('/'); while (relPath.length && !(relative.host = relPath.shift())); if (!relative.host) relative.host = ''; diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 77d2242ff4b071..121d6caaf612e3 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -1189,7 +1189,9 @@ var relativeTests = [ ['http://example.com/b//c//d;p?q#blarg', 'http:/a/b/c/d', 'http://example.com/a/b/c/d'], - ['/foo/bar/baz', '/../etc/passwd', '/etc/passwd'] + ['/foo/bar/baz', '/../etc/passwd', '/etc/passwd'], + ['http://localhost', 'file:///Users/foo', 'file:///Users/foo'], + ['http://localhost', 'file://foo/Users', 'file://foo/Users'] ]; relativeTests.forEach(function(relativeTest) { var a = url.resolve(relativeTest[0], relativeTest[1]),