X Tutup
Skip to content

Commit c6409cb

Browse files
committed
fix(router): throw when reserved characters used in route definition
Closes #3021
1 parent 573c047 commit c6409cb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

modules/angular2/src/router/path_recognizer.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ function splitBySlash(url: string): List<string> {
178178
return url.split('/');
179179
}
180180

181+
var RESERVED_CHARS = RegExpWrapper.create('//|\\(|\\)|;|\\?|=');
182+
function assertPath(path: string) {
183+
if (StringWrapper.contains(path, '#')) {
184+
throw new BaseException(
185+
`Path "${path}" should not include "#". Use "HashLocationStrategy" instead.`);
186+
}
187+
var illegalCharacter = RegExpWrapper.firstMatch(RESERVED_CHARS, path);
188+
if (isPresent(illegalCharacter)) {
189+
throw new BaseException(
190+
`Path "${path}" contains "${illegalCharacter[0]}" which is not allowed in a route config.`);
191+
}
192+
}
181193

182194
// represents something like '/foo/:bar'
183195
export class PathRecognizer {
@@ -187,6 +199,7 @@ export class PathRecognizer {
187199
terminal: boolean = true;
188200

189201
constructor(public path: string, public handler: RouteHandler) {
202+
assertPath(path);
190203
var parsed = parsePathString(path);
191204
var specificity = parsed['specificity'];
192205
var segments = parsed['segments'];

modules/angular2/test/router/path_recognizer_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ var mockRouteHandler = new SyncRouteHandler(DummyClass);
2121

2222
export function main() {
2323
describe('PathRecognizer', () => {
24+
25+
it('should throw when given an invalid path', () => {
26+
expect(() => new PathRecognizer('/hi#', mockRouteHandler))
27+
.toThrowError(`Path "/hi#" should not include "#". Use "HashLocationStrategy" instead.`);
28+
expect(() => new PathRecognizer('hi?', mockRouteHandler))
29+
.toThrowError(`Path "hi?" contains "?" which is not allowed in a route config.`);
30+
expect(() => new PathRecognizer('hi;', mockRouteHandler))
31+
.toThrowError(`Path "hi;" contains ";" which is not allowed in a route config.`);
32+
expect(() => new PathRecognizer('hi=', mockRouteHandler))
33+
.toThrowError(`Path "hi=" contains "=" which is not allowed in a route config.`);
34+
expect(() => new PathRecognizer('hi(', mockRouteHandler))
35+
.toThrowError(`Path "hi(" contains "(" which is not allowed in a route config.`);
36+
expect(() => new PathRecognizer('hi)', mockRouteHandler))
37+
.toThrowError(`Path "hi)" contains ")" which is not allowed in a route config.`);
38+
expect(() => new PathRecognizer('hi//there', mockRouteHandler))
39+
.toThrowError(`Path "hi//there" contains "//" which is not allowed in a route config.`);
40+
});
41+
2442
describe('matrix params', () => {
2543
it('should recognize a trailing matrix value on a path value and assign it to the params return value',
2644
() => {

0 commit comments

Comments
 (0)
X Tutup