Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($location): dont throw while parsing invalid URLs #9972

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ function parseAppUrl(relativeUrl, locationObj, appBase) {
relativeUrl = '/' + relativeUrl;
}
var match = urlResolve(relativeUrl, appBase);
locationObj.$$path = decodeURIComponent(prefixed && match.pathname.charAt(0) === '/' ?
locationObj.$$path = tryDecodeURIComponent(prefixed && match.pathname.charAt(0) === '/' ?
match.pathname.substring(1) : match.pathname);
locationObj.$$search = parseKeyValue(match.search);
locationObj.$$hash = decodeURIComponent(match.hash);
locationObj.$$hash = tryDecodeURIComponent(match.hash);

// make sure path starts with '/';
if (locationObj.$$path && locationObj.$$path.charAt(0) != '/') {
Expand Down
8 changes: 6 additions & 2 deletions src/ng/urlUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ function urlResolve(url, base) {
if (msie) {
// Normalize before parse. Refer Implementation Notes on why this is
// done in two steps on IE.
urlParsingNode.setAttribute("href", href);
href = urlParsingNode.href;
try {
urlParsingNode.setAttribute("href", href);
href = urlParsingNode.href;
} catch (e) {
href = '';
}
}

urlParsingNode.setAttribute('href', href);
Expand Down
10 changes: 10 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,16 @@ describe('$location', function() {
expect(url.hash()).toBe('x <>#');
});


it('should not break on invalid URLs', function() {
url = new LocationHtml5Url('http://example.com/');
url.$$parse('http://example.com/%?filter=%%U65-*55z65>ß.2á.3á02312-3-214.32@#%7B%7D%5D%5B%%%');
expect(url.path()).toBe('/');
expect(url.search()).toEqual({'filter': undefined});
expect(url.hash()).toBeUndefined();
});


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