Files
Netcatty/lib/regexSafety.ts
T
278f528deb Improve terminal keyword highlight refresh (#1619)
* improve terminal keyword highlight refresh

* fix(terminal): address Codex review on keyword highlight refresh

Map viewport probe diffs to line numbers so off-cursor ANSI redraws mark the
rewritten line dirty without forcing full-viewport rescans on normal typing,
and cache wrapped-line context per processLineRange to avoid repeated scans.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: mam15mon <mam15mon@gmail.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 14:16:16 +08:00

54 lines
1.6 KiB
TypeScript

/**
* Best-effort regex safety guard for user-provided patterns.
*
* Reject nested quantifier shapes such as `(a+)+`, `(a*)*`, `(a+){2,}`
* that are common catastrophic-backtracking sources.
*/
export type RegexSafetyReason = "nested_quantifier";
export type RegexValidationIssue = "syntax_invalid" | "safety_rejected";
export type RegexSafetyCheckResult =
| { safe: true }
| { safe: false; reason: RegexSafetyReason };
export type RegexValidationResult =
| { valid: true }
| { valid: false; issue: RegexValidationIssue; reason?: RegexSafetyReason; errorMessage?: string };
export function checkRegexSafetyPattern(pattern: string): RegexSafetyCheckResult {
const nestedUnboundedQuantifier = /\((?:\?:)?[^)]*(?:\+|\*|\{\d+,\}|\{,\d+\})[^)]*\)(?:\+|\*|\{\d+,\}|\{,\d+\})/;
if (nestedUnboundedQuantifier.test(pattern)) {
return { safe: false, reason: "nested_quantifier" };
}
return { safe: true };
}
export function validateUserRegexPattern(pattern: string, flags = "gi"): RegexValidationResult {
try {
// Syntax validation only.
new RegExp(pattern, flags);
} catch (err) {
return {
valid: false,
issue: "syntax_invalid",
errorMessage: err instanceof Error ? err.message : String(err),
};
}
const safety = checkRegexSafetyPattern(pattern);
if (safety.safe === false) {
return {
valid: false,
issue: "safety_rejected",
reason: safety.reason,
};
}
return { valid: true };
}
export function isSafeRegexPattern(pattern: string): boolean {
return checkRegexSafetyPattern(pattern).safe;
}