diff --git a/packages/jaeger-ui/src/utils/prefix-url.test.js b/packages/jaeger-ui/src/utils/prefix-url.test.js old mode 100644 new mode 100755 index f111d90ae2..0da8943f4d --- a/packages/jaeger-ui/src/utils/prefix-url.test.js +++ b/packages/jaeger-ui/src/utils/prefix-url.test.js @@ -16,9 +16,10 @@ jest.mock('../site-prefix', () => `${global.location.origin}/a/site/prefix/`); -import prefixUrl from './prefix-url'; +import prefixUrl, { regExEscape } from './prefix-url'; const PATH_PREFIX = '/a/site/prefix'; +const IPV6_HOST_ADDRESS = '2a00:8a00:4000:751e::1c'; describe('prefixUrl()', () => { const tests = [ @@ -37,3 +38,19 @@ describe('prefixUrl()', () => { }); }); }); + +describe('regExEscape()', () => { + const tests = [ + { source: undefined, target: '' }, + { source: null, target: '' }, + { source: `${IPV6_HOST_ADDRESS}`, target: `${IPV6_HOST_ADDRESS}` }, + { source: `[${IPV6_HOST_ADDRESS}]`, target: `\\[${IPV6_HOST_ADDRESS}\\]` }, + { source: `[${IPV6_HOST_ADDRESS}]:8080`, target: `\\[${IPV6_HOST_ADDRESS}\\]:8080` }, + ]; + + tests.forEach(({ source, target }) => { + it(`regexEscape "${source}" correctly`, () => { + expect(regExEscape(source)).toBe(target); + }); + }); +}); diff --git a/packages/jaeger-ui/src/utils/prefix-url.tsx b/packages/jaeger-ui/src/utils/prefix-url.tsx old mode 100644 new mode 100755 index c2c1d44dcd..01477afb7f --- a/packages/jaeger-ui/src/utils/prefix-url.tsx +++ b/packages/jaeger-ui/src/utils/prefix-url.tsx @@ -14,6 +14,17 @@ import sitePrefix from '../site-prefix'; +/** + * Escape any RegEx characters in a given string + * + * @param {string} value The string which needs to be escaped + * @return {string} Escaped RegEx string + */ +export function regExEscape(value?: string) { + const s = value == null ? '' : String(value); + return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); +} + const origin = process.env.NODE_ENV === 'test' ? global.location.origin : window.location.origin; /** * Generate the URL prefix from `sitePrefix` and use it for all subsequent calls @@ -24,7 +35,7 @@ const origin = process.env.NODE_ENV === 'test' ? global.location.origin : window * - `"http://localhost:3000/abc/"` to `"/abc"` * - `"http://localhost:3000/abc/def/"` to `"/abc/def"` */ -const rx = new RegExp(`^${origin}|/$`, 'ig'); +const rx = new RegExp(`^${regExEscape(origin)}|/$`, 'ig'); const pathPrefix = sitePrefix.replace(rx, ''); /**