116
* whose real host is untrusted, e.g. `https://evil.test/.example.com/tool`.
117
*/
119
>
const regexSource = buildUrlPatternRegexSource(pattern);
120
>
try {
121
>
return new RegExp(regexSource, 'i').test(url);
122
>
} catch {
123
return false;
124
}
126
128
>
// The authority region spans from the start of the pattern up to (but not including) the first
129
>
// `/` of the path. Wildcards there must not cross a `/` so they cannot consume path segments.
130
>
const schemeSeparator = pattern.indexOf('://');
131
>
const authorityStart = schemeSeparator >= 0 ? schemeSeparator + 3 : 0;
132
>
const pathStart = pattern.indexOf('/', authorityStart);
133
>
const authorityEnd = pathStart >= 0 ? pathStart : pattern.length;
134
>
135
>
let source = '^';
136
>
for (let i = 0; i < pattern.length; i++) {
137
>
const char = pattern[i];
138
>
if (char === '*') {
139
source += i < authorityEnd ? '[^/]*' : '.*';
141
>
source += escapeRegExpCharacters(char);
142
>
}
143
>
}
144
>
return source + '$';
145
>
}