/** * Convert a wildcard pattern into a safe regular expression that only * treats asterisks as wildcards while escaping all other regex metacharacters. */ export function wildcardToSafeRegex(pattern: string): RegExp { // Prevent ReDoS attacks by normalizing consecutive wildcards const normalized = pattern.replace(/\*+/g, '*'); const specialChars = /[.*+?^${}()|[\]\\]/g; const escape = (value: string): string => value.replace(specialChars, "\\$&"); const parts = normalized.split('*').map(escape); return new RegExp(`^${parts.join('.*')}$`); }