From a665b2e0501621c1f3cc34afe54b7e6014d6973f Mon Sep 17 00:00:00 2001 From: Jeffrey Jagoda Date: Thu, 26 Mar 2015 09:41:57 -0400 Subject: [PATCH] URL: Fix resolving from non-file to file URLs. When resolving a reference URL with the 'file' scheme an no host against a base URL without the 'file' scheme, the first path element of the reference URL is used as the host for the target URL. This results in an invalid target URL. This change makes an exception for file URLs so that the host is not mangled during URL resolution. --- lib/url.js | 4 +++- test/simple/test-url.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/url.js b/lib/url.js index 4b9ab82f93a4..01613be63b96 100644 --- a/lib/url.js +++ b/lib/url.js @@ -510,7 +510,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/simple/test-url.js b/test/simple/test-url.js index e81908a883fa..f82d538e50a1 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -1202,7 +1202,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]),