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

Commit 353e3a6

Browse files
fix($location): throw if the path starts with double (back)slashes
Previously `$location` was rewriting such paths to remove not only the double slashes but also the first segment of the path, leading to an invalid path. In this change, we deem leading double (back)slashes an invalid path and now throw a `$location:badpath` error if that occurs. Closes #15365
1 parent 6ce2913 commit 353e3a6

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@ngdoc error
2+
@name $location:badpath
3+
@fullName Invalid Path
4+
@description
5+
6+
This error occurs when the path of a location contains invalid characters.
7+
The most common fault is when the path starts with double slashes (`//`) or backslashes ('\\').
8+
For example if the base path of an application is `https://a.b.c/` then the following path is
9+
invalid `https://a.b.c///d/e/f`.

src/ng/location.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ function parseAbsoluteUrl(absoluteUrl, locationObj) {
3030
locationObj.$$port = toInt(parsedUrl.port) || DEFAULT_PORTS[parsedUrl.protocol] || null;
3131
}
3232

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

34-
function parseAppUrl(relativeUrl, locationObj) {
35-
var prefixed = (relativeUrl.charAt(0) !== '/');
36+
if (DOUBLE_SLASH_REGEX.test(url)) {
37+
throw $locationMinErr('badpath', 'Invalid url "{0}".', url);
38+
}
39+
40+
var prefixed = (url.charAt(0) !== '/');
3641
if (prefixed) {
37-
relativeUrl = '/' + relativeUrl;
42+
url = '/' + url;
3843
}
39-
var match = urlResolve(relativeUrl);
44+
var match = urlResolve(url);
4045
locationObj.$$path = decodeURIComponent(prefixed && match.pathname.charAt(0) === '/' ?
4146
match.pathname.substring(1) : match.pathname);
4247
locationObj.$$search = parseKeyValue(match.search);
@@ -144,9 +149,10 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
144149
var appUrl, prevAppUrl;
145150
var rewrittenUrl;
146151

152+
147153
if (isDefined(appUrl = stripBaseUrl(appBase, url))) {
148154
prevAppUrl = appUrl;
149-
if (isDefined(appUrl = stripBaseUrl(basePrefix, appUrl))) {
155+
if (basePrefix && isDefined(appUrl = stripBaseUrl(basePrefix, appUrl))) {
150156
rewrittenUrl = appBaseNoFile + (stripBaseUrl('/', appUrl) || appUrl);
151157
} else {
152158
rewrittenUrl = appBase + prevAppUrl;

test/ng/locationSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,19 @@ describe('$location', function() {
24092409
expect(parseLinkAndReturn(locationUrl, 'someIgnoredAbsoluteHref', '#test')).toEqual('http://server/pre/otherPath#test');
24102410
});
24112411

2412+
it('should complain if the path starts with double slashes', function() {
2413+
expect(function() {
2414+
parseLinkAndReturn(locationUrl, 'http://server/pre///other/path');
2415+
}).toThrowMinErr('$location', 'badpath');
2416+
2417+
expect(function() {
2418+
parseLinkAndReturn(locationUrl, 'http://server/pre/\\\\other/path');
2419+
}).toThrowMinErr('$location', 'badpath');
2420+
2421+
expect(function() {
2422+
parseLinkAndReturn(locationUrl, 'http://server/pre//\\//other/path');
2423+
}).toThrowMinErr('$location', 'badpath');
2424+
});
24122425

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

0 commit comments

Comments
 (0)