Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
url: fixed encoding for slash switching emulation.
Browse files Browse the repository at this point in the history
Fixes: #8458
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
Reviewed-by: Chris Dickinson <christopher.s.dickinson@gmail.com>
  • Loading branch information
Evan Rutledge Borden authored and chrisdickinson committed Oct 7, 2014
1 parent 8392e8c commit 640ad63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,22 @@ 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;

// trim before proceeding.
// 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) {
Expand Down
24 changes: 24 additions & 0 deletions test/simple/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:',
Expand Down

0 comments on commit 640ad63

Please sign in to comment.