From f7ede33f09187cd9b1874982e813380cd292ef17 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 25 Mar 2014 14:16:55 -0700 Subject: [PATCH] url: treat \ the same as / See https://code.google.com/p/chromium/issues/detail?id=25916 Parse URLs with backslashes the same as web browsers, by replacing all backslashes with forward slashes, except those that occur after the first # character. Manual rebase of 9520ade Signed-off-by: Trevor Norris --- lib/url.js | 6 ++++++ test/simple/test-url.js | 28 +++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/url.js b/lib/url.js index f277da03aa69..292ce3344b9b 100644 --- a/lib/url.js +++ b/lib/url.js @@ -107,6 +107,12 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { throw new TypeError("Parameter 'url' must be a string, not " + typeof url); } + // Copy chrome, IE, opera backslash-handling behavior. + // See: https://code.google.com/p/chromium/issues/detail?id=25916 + var hashSplit = url.split('#'); + hashSplit[0] = hashSplit[0].replace(/\\/g, '/'); + url = hashSplit.join('#'); + var rest = url; // trim before proceeding. diff --git a/test/simple/test-url.js b/test/simple/test-url.js index 89397bca6770..cb39844827a9 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -34,6 +34,28 @@ var parseTests = { 'path': '//some_path' }, + 'http:\\\\evil-phisher\\foo.html#h\\a\\s\\h': { + protocol: 'http:', + slashes: true, + host: 'evil-phisher', + hostname: 'evil-phisher', + pathname: '/foo.html', + path: '/foo.html', + hash: '#h%5Ca%5Cs%5Ch', + href: 'http://evil-phisher/foo.html#h%5Ca%5Cs%5Ch' + }, + + + 'http:\\\\evil-phisher\\foo.html': { + protocol: 'http:', + slashes: true, + host: 'evil-phisher', + hostname: 'evil-phisher', + pathname: '/foo.html', + path: '/foo.html', + href: 'http://evil-phisher/foo.html' + }, + 'HTTP://www.example.com/' : { 'href': 'http://www.example.com/', 'protocol': 'http:', @@ -757,9 +779,9 @@ var parseTests = { host: 'x:1', port: '1', hostname: 'x', - pathname: '/%27%20%3C%3E%22%60/%7B%7D%7C%5C%5E~%60/', - path: '/%27%20%3C%3E%22%60/%7B%7D%7C%5C%5E~%60/', - href: 'http://x:1/%27%20%3C%3E%22%60/%7B%7D%7C%5C%5E~%60/' + pathname: '/%27%20%3C%3E%22%60/%7B%7D%7C/%5E~%60/', + path: '/%27%20%3C%3E%22%60/%7B%7D%7C/%5E~%60/', + href: 'http://x:1/%27%20%3C%3E%22%60/%7B%7D%7C/%5E~%60/' }, 'http://a@b@c/': {