From c6409cb624c0c9e401636118211b79d0edbbd73e Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Fri, 17 Jul 2015 02:01:34 -0700 Subject: [PATCH] fix(router): throw when reserved characters used in route definition Closes #3021 --- modules/angular2/src/router/path_recognizer.ts | 13 +++++++++++++ .../test/router/path_recognizer_spec.ts | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/modules/angular2/src/router/path_recognizer.ts b/modules/angular2/src/router/path_recognizer.ts index 537aecc167..c27ccef96b 100644 --- a/modules/angular2/src/router/path_recognizer.ts +++ b/modules/angular2/src/router/path_recognizer.ts @@ -178,6 +178,18 @@ function splitBySlash(url: string): List { return url.split('/'); } +var RESERVED_CHARS = RegExpWrapper.create('//|\\(|\\)|;|\\?|='); +function assertPath(path: string) { + if (StringWrapper.contains(path, '#')) { + throw new BaseException( + `Path "${path}" should not include "#". Use "HashLocationStrategy" instead.`); + } + var illegalCharacter = RegExpWrapper.firstMatch(RESERVED_CHARS, path); + if (isPresent(illegalCharacter)) { + throw new BaseException( + `Path "${path}" contains "${illegalCharacter[0]}" which is not allowed in a route config.`); + } +} // represents something like '/foo/:bar' export class PathRecognizer { @@ -187,6 +199,7 @@ export class PathRecognizer { terminal: boolean = true; constructor(public path: string, public handler: RouteHandler) { + assertPath(path); var parsed = parsePathString(path); var specificity = parsed['specificity']; var segments = parsed['segments']; diff --git a/modules/angular2/test/router/path_recognizer_spec.ts b/modules/angular2/test/router/path_recognizer_spec.ts index da9a6e4658..3f6962fbb7 100644 --- a/modules/angular2/test/router/path_recognizer_spec.ts +++ b/modules/angular2/test/router/path_recognizer_spec.ts @@ -21,6 +21,24 @@ var mockRouteHandler = new SyncRouteHandler(DummyClass); export function main() { describe('PathRecognizer', () => { + + it('should throw when given an invalid path', () => { + expect(() => new PathRecognizer('/hi#', mockRouteHandler)) + .toThrowError(`Path "/hi#" should not include "#". Use "HashLocationStrategy" instead.`); + expect(() => new PathRecognizer('hi?', mockRouteHandler)) + .toThrowError(`Path "hi?" contains "?" which is not allowed in a route config.`); + expect(() => new PathRecognizer('hi;', mockRouteHandler)) + .toThrowError(`Path "hi;" contains ";" which is not allowed in a route config.`); + expect(() => new PathRecognizer('hi=', mockRouteHandler)) + .toThrowError(`Path "hi=" contains "=" which is not allowed in a route config.`); + expect(() => new PathRecognizer('hi(', mockRouteHandler)) + .toThrowError(`Path "hi(" contains "(" which is not allowed in a route config.`); + expect(() => new PathRecognizer('hi)', mockRouteHandler)) + .toThrowError(`Path "hi)" contains ")" which is not allowed in a route config.`); + expect(() => new PathRecognizer('hi//there', mockRouteHandler)) + .toThrowError(`Path "hi//there" contains "//" which is not allowed in a route config.`); + }); + describe('matrix params', () => { it('should recognize a trailing matrix value on a path value and assign it to the params return value', () => {