|
1
|
export function buildRegexpIfValid(str: string): RegExp | undefined { |
|
2
|
try { |
|
3
|
const m = str.match(/^([\/~@;%#'])(.*?)\1([gimsuy]*)$/) |
|
4
|
if (m) { |
|
5
|
// Validate the flags as written first, so an invalid combination |
|
6
|
// (e.g. a duplicated flag) is still rejected. |
|
7
|
new RegExp(m[2], m[3]) |
|
8
|
|
|
9
|
// `g` and `y` make `RegExp#test` stateful (they advance `lastIndex` |
|
10
|
// across calls), which corrupts repeated whole-value checks such as |
|
11
|
// CORS origin matching. Neither flag adds anything for a single |
|
12
|
// whole-value test, so they are stripped here. |
|
13
|
const flags = m[3].replace(/[gy]/g, "") |
|
14
|
return new RegExp(m[2], flags) |
|
15
|
} |
|
16
|
} catch (e) {} |
|
17
|
|
|
18
|
return |
|
19
|
} |