diff --git a/packages/common/src/location/location.ts b/packages/common/src/location/location.ts index 475d04bc402a..9532a34d4b91 100644 --- a/packages/common/src/location/location.ts +++ b/packages/common/src/location/location.ts @@ -301,9 +301,14 @@ export function createLocation() { } function _stripBasePath(basePath: string, url: string): string { - return basePath && new RegExp(`^${basePath}([/;?#]|$)`).test(url) ? - url.substring(basePath.length) : - url; + if (!basePath || !url.startsWith(basePath)) { + return url; + } + const strippedUrl = url.substring(basePath.length); + if (strippedUrl === '' || ['/', ';', '?', '#'].includes(strippedUrl[0])) { + return strippedUrl; + } + return url; } function _stripIndexHtml(url: string): string { diff --git a/packages/common/test/location/location_spec.ts b/packages/common/test/location/location_spec.ts index 9df67c5ab741..36d85aa324aa 100644 --- a/packages/common/test/location/location_spec.ts +++ b/packages/common/test/location/location_spec.ts @@ -286,5 +286,18 @@ describe('Location Class', () => { expect(location.normalize(baseHref + matrixParams)).toBe(matrixParams); expect(location.normalize(baseHref + fragment)).toBe(fragment); }); + + it('in case APP_BASE_HREF contains characters that have special meaning in a regex', () => { + const baseHref = 'c:/users/name(test)/en'; + const path = '/test-path'; + + TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]}); + + const location = TestBed.inject(Location); + + expect(location.normalize(path)).toBe(path); + expect(location.normalize(baseHref)).toBe(''); + expect(location.normalize(baseHref + path)).toBe(path); + }); }); });