Skip to content

Commit

Permalink
fix($location): do not decode forward slashes in the path in HTML5 mode
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Nov 2, 2017
1 parent 817ac56 commit c3d18b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ function encodePath(path) {
i = segments.length;

while (i--) {
segments[i] = encodeUriSegment(segments[i]);
// decode forward slashes to prevent them from being double encoded
segments[i] = encodeUriSegment(segments[i].replace(/%2F/g, '/'));
}

return segments.join('/');
}

function decodePath(path, html5Mode) {
var segments = path.split('/'),
i = segments.length;

while (i--) {
segments[i] = decodeURIComponent(segments[i]);
if (html5Mode) {
// encode forward slashes to prevent them from being mistaken for path separators
segments[i] = segments[i].replace(/\//g, '%2F');
}
}

return segments.join('/');
Expand All @@ -31,7 +47,7 @@ function parseAbsoluteUrl(absoluteUrl, locationObj) {
}

var DOUBLE_SLASH_REGEX = /^\s*[\\/]{2,}/;
function parseAppUrl(url, locationObj) {
function parseAppUrl(url, locationObj, html5Mode) {

if (DOUBLE_SLASH_REGEX.test(url)) {
throw $locationMinErr('badpath', 'Invalid url "{0}".', url);
Expand All @@ -42,8 +58,8 @@ function parseAppUrl(url, locationObj) {
url = '/' + url;
}
var match = urlResolve(url);
locationObj.$$path = decodeURIComponent(prefixed && match.pathname.charAt(0) === '/' ?
match.pathname.substring(1) : match.pathname);
var path = prefixed && match.pathname.charAt(0) === '/' ? match.pathname.substring(1) : match.pathname;
locationObj.$$path = decodePath(path, html5Mode);
locationObj.$$search = parseKeyValue(match.search);
locationObj.$$hash = decodeURIComponent(match.hash);

Expand Down Expand Up @@ -118,7 +134,7 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
appBaseNoFile);
}

parseAppUrl(pathUrl, this);
parseAppUrl(pathUrl, this, true);

if (!this.$$path) {
this.$$path = '/';
Expand Down Expand Up @@ -221,7 +237,7 @@ function LocationHashbangUrl(appBase, appBaseNoFile, hashPrefix) {
}
}

parseAppUrl(withoutHashUrl, this);
parseAppUrl(withoutHashUrl, this, false);

this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase);

Expand Down Expand Up @@ -406,7 +422,7 @@ var locationPrototype = {
}

var match = PATH_MATCH.exec(url);
if (match[1] || url === '') this.path(decodeURIComponent(match[1]));
if (match[1] || url === '') this.path(decodeURI(match[1]));
if (match[2] || match[1] || url === '') this.search(match[3] || '');
this.hash(match[5] || '');

Expand Down
11 changes: 11 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ describe('$location', function() {
expect(locationUrl.hash()).toBe('x <>#');
});


it('should not decode encoded forward slashes in the path', function() {
var locationUrl = new LocationHtml5Url('http://host.com/base/', 'http://host.com/base/');
locationUrl.$$parse('http://host.com/base/a/ng2;path=%2Fsome%2Fpath');
expect(locationUrl.path()).toBe('/a/ng2;path=%2Fsome%2Fpath');
expect(locationUrl.search()).toEqual({});
expect(locationUrl.hash()).toBe('');
expect(locationUrl.url()).toBe('/a/ng2;path=%2Fsome%2Fpath');
expect(locationUrl.absUrl()).toBe('http://host.com/base/a/ng2;path=%2Fsome%2Fpath');
});

it('should decode pluses as spaces in urls', function() {
var locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
locationUrl.$$parse('http://host.com/?a+b=c+d');
Expand Down

0 comments on commit c3d18b2

Please sign in to comment.