File size: 8,777 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | type QuantifierRead = {
consumed: number;
minRepeat: number;
maxRepeat: number | null;
};
type TokenState = {
containsRepetition: boolean;
hasAmbiguousAlternation: boolean;
minLength: number;
maxLength: number;
};
type ParseFrame = {
lastToken: TokenState | null;
containsRepetition: boolean;
hasAlternation: boolean;
branchMinLength: number;
branchMaxLength: number;
altMinLength: number | null;
altMaxLength: number | null;
};
type PatternToken =
| { kind: "simple-token" }
| { kind: "group-open" }
| { kind: "group-close" }
| { kind: "alternation" }
| { kind: "quantifier"; quantifier: QuantifierRead };
const SAFE_REGEX_CACHE_MAX = 256;
const SAFE_REGEX_TEST_WINDOW = 2048;
const safeRegexCache = new Map<string, RegExp | null>();
function createParseFrame(): ParseFrame {
return {
lastToken: null,
containsRepetition: false,
hasAlternation: false,
branchMinLength: 0,
branchMaxLength: 0,
altMinLength: null,
altMaxLength: null,
};
}
function addLength(left: number, right: number): number {
if (!Number.isFinite(left) || !Number.isFinite(right)) {
return Number.POSITIVE_INFINITY;
}
return left + right;
}
function multiplyLength(length: number, factor: number): number {
if (!Number.isFinite(length)) {
return factor === 0 ? 0 : Number.POSITIVE_INFINITY;
}
return length * factor;
}
function recordAlternative(frame: ParseFrame): void {
if (frame.altMinLength === null || frame.altMaxLength === null) {
frame.altMinLength = frame.branchMinLength;
frame.altMaxLength = frame.branchMaxLength;
return;
}
frame.altMinLength = Math.min(frame.altMinLength, frame.branchMinLength);
frame.altMaxLength = Math.max(frame.altMaxLength, frame.branchMaxLength);
}
function readQuantifier(source: string, index: number): QuantifierRead | null {
const ch = source[index];
const consumed = source[index + 1] === "?" ? 2 : 1;
if (ch === "*") {
return { consumed, minRepeat: 0, maxRepeat: null };
}
if (ch === "+") {
return { consumed, minRepeat: 1, maxRepeat: null };
}
if (ch === "?") {
return { consumed, minRepeat: 0, maxRepeat: 1 };
}
if (ch !== "{") {
return null;
}
let i = index + 1;
while (i < source.length && /\d/.test(source[i])) {
i += 1;
}
if (i === index + 1) {
return null;
}
const minRepeat = Number.parseInt(source.slice(index + 1, i), 10);
let maxRepeat: number | null = minRepeat;
if (source[i] === ",") {
i += 1;
const maxStart = i;
while (i < source.length && /\d/.test(source[i])) {
i += 1;
}
maxRepeat = i === maxStart ? null : Number.parseInt(source.slice(maxStart, i), 10);
}
if (source[i] !== "}") {
return null;
}
i += 1;
if (source[i] === "?") {
i += 1;
}
if (maxRepeat !== null && maxRepeat < minRepeat) {
return null;
}
return { consumed: i - index, minRepeat, maxRepeat };
}
function tokenizePattern(source: string): PatternToken[] {
const tokens: PatternToken[] = [];
let inCharClass = false;
for (let i = 0; i < source.length; i += 1) {
const ch = source[i];
if (ch === "\\") {
i += 1;
tokens.push({ kind: "simple-token" });
continue;
}
if (inCharClass) {
if (ch === "]") {
inCharClass = false;
}
continue;
}
if (ch === "[") {
inCharClass = true;
tokens.push({ kind: "simple-token" });
continue;
}
if (ch === "(") {
tokens.push({ kind: "group-open" });
continue;
}
if (ch === ")") {
tokens.push({ kind: "group-close" });
continue;
}
if (ch === "|") {
tokens.push({ kind: "alternation" });
continue;
}
const quantifier = readQuantifier(source, i);
if (quantifier) {
tokens.push({ kind: "quantifier", quantifier });
i += quantifier.consumed - 1;
continue;
}
tokens.push({ kind: "simple-token" });
}
return tokens;
}
function analyzeTokensForNestedRepetition(tokens: PatternToken[]): boolean {
const frames: ParseFrame[] = [createParseFrame()];
const emitToken = (token: TokenState) => {
const frame = frames[frames.length - 1];
frame.lastToken = token;
if (token.containsRepetition) {
frame.containsRepetition = true;
}
frame.branchMinLength = addLength(frame.branchMinLength, token.minLength);
frame.branchMaxLength = addLength(frame.branchMaxLength, token.maxLength);
};
const emitSimpleToken = () => {
emitToken({
containsRepetition: false,
hasAmbiguousAlternation: false,
minLength: 1,
maxLength: 1,
});
};
for (const token of tokens) {
if (token.kind === "simple-token") {
emitSimpleToken();
continue;
}
if (token.kind === "group-open") {
frames.push(createParseFrame());
continue;
}
if (token.kind === "group-close") {
if (frames.length > 1) {
const frame = frames.pop() as ParseFrame;
if (frame.hasAlternation) {
recordAlternative(frame);
}
const groupMinLength = frame.hasAlternation
? (frame.altMinLength ?? 0)
: frame.branchMinLength;
const groupMaxLength = frame.hasAlternation
? (frame.altMaxLength ?? 0)
: frame.branchMaxLength;
emitToken({
containsRepetition: frame.containsRepetition,
hasAmbiguousAlternation:
frame.hasAlternation &&
frame.altMinLength !== null &&
frame.altMaxLength !== null &&
frame.altMinLength !== frame.altMaxLength,
minLength: groupMinLength,
maxLength: groupMaxLength,
});
}
continue;
}
if (token.kind === "alternation") {
const frame = frames[frames.length - 1];
frame.hasAlternation = true;
recordAlternative(frame);
frame.branchMinLength = 0;
frame.branchMaxLength = 0;
frame.lastToken = null;
continue;
}
const frame = frames[frames.length - 1];
const previousToken = frame.lastToken;
if (!previousToken) {
continue;
}
if (previousToken.containsRepetition) {
return true;
}
if (previousToken.hasAmbiguousAlternation && token.quantifier.maxRepeat === null) {
return true;
}
const previousMinLength = previousToken.minLength;
const previousMaxLength = previousToken.maxLength;
previousToken.minLength = multiplyLength(previousToken.minLength, token.quantifier.minRepeat);
previousToken.maxLength =
token.quantifier.maxRepeat === null
? Number.POSITIVE_INFINITY
: multiplyLength(previousToken.maxLength, token.quantifier.maxRepeat);
previousToken.containsRepetition = true;
frame.containsRepetition = true;
frame.branchMinLength = frame.branchMinLength - previousMinLength + previousToken.minLength;
const branchMaxBase =
Number.isFinite(frame.branchMaxLength) && Number.isFinite(previousMaxLength)
? frame.branchMaxLength - previousMaxLength
: Number.POSITIVE_INFINITY;
frame.branchMaxLength = addLength(branchMaxBase, previousToken.maxLength);
}
return false;
}
function testRegexFromStart(regex: RegExp, value: string): boolean {
regex.lastIndex = 0;
return regex.test(value);
}
export function testRegexWithBoundedInput(
regex: RegExp,
input: string,
maxWindow = SAFE_REGEX_TEST_WINDOW,
): boolean {
if (maxWindow <= 0) {
return false;
}
if (input.length <= maxWindow) {
return testRegexFromStart(regex, input);
}
const head = input.slice(0, maxWindow);
if (testRegexFromStart(regex, head)) {
return true;
}
return testRegexFromStart(regex, input.slice(-maxWindow));
}
export function hasNestedRepetition(source: string): boolean {
// Conservative parser: tokenize first, then check if repeated tokens/groups are repeated again.
// Non-goal: complete regex AST support; keep strict enough for config safety checks.
return analyzeTokensForNestedRepetition(tokenizePattern(source));
}
export function compileSafeRegex(source: string, flags = ""): RegExp | null {
const trimmed = source.trim();
if (!trimmed) {
return null;
}
const cacheKey = `${flags}::${trimmed}`;
if (safeRegexCache.has(cacheKey)) {
return safeRegexCache.get(cacheKey) ?? null;
}
let compiled: RegExp | null = null;
if (!hasNestedRepetition(trimmed)) {
try {
compiled = new RegExp(trimmed, flags);
} catch {
compiled = null;
}
}
safeRegexCache.set(cacheKey, compiled);
if (safeRegexCache.size > SAFE_REGEX_CACHE_MAX) {
const oldestKey = safeRegexCache.keys().next().value;
if (oldestKey) {
safeRegexCache.delete(oldestKey);
}
}
return compiled;
}
|