From 640ad632e3bf04fe07fa2b9dc3ca940c2e8d0261 Mon Sep 17 00:00:00 2001 From: Evan Rutledge Borden Date: Fri, 26 Sep 2014 11:59:39 -0400 Subject: [PATCH] url: fixed encoding for slash switching emulation. Fixes: https://github.com/joyent/node/issues/8458 Reviewed-by: Trevor Norris Reviewed-by: Chris Dickinson --- lib/url.js | 12 ++++++++---- test/simple/test-url.js | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/url.js b/lib/url.js index 6463424207d..d772b4f58c6 100644 --- a/lib/url.js +++ b/lib/url.js @@ -111,10 +111,14 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { } // Copy chrome, IE, opera backslash-handling behavior. + // Back slashes before the query string get converted to forward slashes // 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 queryIndex = url.indexOf('?'), + splitter = (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', + uSplit = url.split(splitter), + slashRegex = /\\/g; + uSplit[0] = uSplit[0].replace(slashRegex, '/'); + url = uSplit.join(splitter); var rest = url; @@ -122,7 +126,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { // This is to support parse stuff like " http://foo.com \n" rest = rest.trim(); - if (!slashesDenoteHost && hashSplit.length === 1) { + if (!slashesDenoteHost && url.split('#').length === 1) { // Try fast path regexp var simplePath = simplePathPattern.exec(rest); if (simplePath) { diff --git a/test/simple/test-url.js b/test/simple/test-url.js index e0a1b872d28..8bfedcdf3d6 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -45,6 +45,30 @@ var parseTests = { href: 'http://evil-phisher/foo.html#h%5Ca%5Cs%5Ch' }, + 'http:\\\\evil-phisher\\foo.html?json="\\"foo\\""#h\\a\\s\\h': { + protocol: 'http:', + slashes: true, + host: 'evil-phisher', + hostname: 'evil-phisher', + pathname: '/foo.html', + search: '?json=%22%5C%22foo%5C%22%22', + query: 'json=%22%5C%22foo%5C%22%22', + path: '/foo.html?json=%22%5C%22foo%5C%22%22', + hash: '#h%5Ca%5Cs%5Ch', + href: 'http://evil-phisher/foo.html?json=%22%5C%22foo%5C%22%22#h%5Ca%5Cs%5Ch' + }, + + 'http:\\\\evil-phisher\\foo.html#h\\a\\s\\h?blarg': { + protocol: 'http:', + slashes: true, + host: 'evil-phisher', + hostname: 'evil-phisher', + pathname: '/foo.html', + path: '/foo.html', + hash: '#h%5Ca%5Cs%5Ch?blarg', + href: 'http://evil-phisher/foo.html#h%5Ca%5Cs%5Ch?blarg' + }, + 'http:\\\\evil-phisher\\foo.html': { protocol: 'http:',