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

Location path 2 #15365

Merged
merged 2 commits into from
Nov 9, 2016
Merged
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
9 changes: 9 additions & 0 deletions docs/content/error/$location/badpath.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@ngdoc error
@name $location:badpath
@fullName Invalid Path
@description

This error occurs when the path of a location contains invalid characters.
The most common fault is when the path starts with double slashes (`//`) or backslashes ('\\').
For example if the base path of an application is `https://a.b.c/` then the following path is
invalid `https://a.b.c///d/e/f`.
16 changes: 11 additions & 5 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ function parseAbsoluteUrl(absoluteUrl, locationObj) {
locationObj.$$port = toInt(parsedUrl.port) || DEFAULT_PORTS[parsedUrl.protocol] || null;
}

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

function parseAppUrl(relativeUrl, locationObj) {
var prefixed = (relativeUrl.charAt(0) !== '/');
if (DOUBLE_SLASH_REGEX.test(url)) {
throw $locationMinErr('badpath', 'Invalid url "{0}".', url);
}

var prefixed = (url.charAt(0) !== '/');
if (prefixed) {
relativeUrl = '/' + relativeUrl;
url = '/' + url;
}
var match = urlResolve(relativeUrl);
var match = urlResolve(url);
locationObj.$$path = decodeURIComponent(prefixed && match.pathname.charAt(0) === '/' ?
match.pathname.substring(1) : match.pathname);
locationObj.$$search = parseKeyValue(match.search);
Expand Down Expand Up @@ -144,9 +149,10 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
var appUrl, prevAppUrl;
var rewrittenUrl;


if (isDefined(appUrl = stripBaseUrl(appBase, url))) {
prevAppUrl = appUrl;
if (isDefined(appUrl = stripBaseUrl(basePrefix, appUrl))) {
if (basePrefix && isDefined(appUrl = stripBaseUrl(basePrefix, appUrl))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interestingly I think this was actually the cause of the bug in the first place...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no tests for this change, are there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No new tests, specifically, but it doesn't break any of the current tests
And there are the new tests with the double slashes.
I couldn't think of a specific test for this change. Any ideas?

rewrittenUrl = appBaseNoFile + (stripBaseUrl('/', appUrl) || appUrl);
} else {
rewrittenUrl = appBase + prevAppUrl;
Expand Down
23 changes: 18 additions & 5 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ describe('$location', function() {


it('should throw error when invalid server url given', function() {
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', 'http://server.org/base/', '/base');
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', 'http://server.org/base/');

expect(function() {
locationUrl.$$parse('http://other.server.org/path#/path');
Expand All @@ -398,7 +398,7 @@ describe('$location', function() {


it('should throw error when invalid base url given', function() {
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', 'http://server.org/base/', '/base');
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', 'http://server.org/base/');

expect(function() {
locationUrl.$$parse('http://server.org/path#/path');
Expand Down Expand Up @@ -2453,9 +2453,9 @@ describe('$location', function() {
var locationUrl, locationUmlautUrl, locationIndexUrl;

beforeEach(function() {
locationUrl = new LocationHtml5Url('http://server/pre/', 'http://server/pre/', 'http://server/pre/path');
locationUmlautUrl = new LocationHtml5Url('http://särver/pre/', 'http://särver/pre/', 'http://särver/pre/path');
locationIndexUrl = new LocationHtml5Url('http://server/pre/index.html', 'http://server/pre/', 'http://server/pre/path');
locationUrl = new LocationHtml5Url('http://server/pre/', 'http://server/pre/');
locationUmlautUrl = new LocationHtml5Url('http://särver/pre/', 'http://särver/pre/');
locationIndexUrl = new LocationHtml5Url('http://server/pre/index.html', 'http://server/pre/');
});

it('should rewrite URL', function() {
Expand All @@ -2480,6 +2480,19 @@ describe('$location', function() {
expect(parseLinkAndReturn(locationUrl, 'someIgnoredAbsoluteHref', '#test')).toEqual('http://server/pre/otherPath#test');
});

it('should complain if the path starts with double slashes', function() {
expect(function() {
parseLinkAndReturn(locationUrl, 'http://server/pre///other/path');
}).toThrowMinErr('$location', 'badpath');

expect(function() {
parseLinkAndReturn(locationUrl, 'http://server/pre/\\\\other/path');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case with mixed slashes (pre//\\//other/path) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

}).toThrowMinErr('$location', 'badpath');

expect(function() {
parseLinkAndReturn(locationUrl, 'http://server/pre//\\//other/path');
}).toThrowMinErr('$location', 'badpath');
});

it('should complain if no base tag present', function() {
module(function($locationProvider) {
Expand Down