Repairing ReDoS by Construction: Certified Algebraic Derivation for Language-Preserving Regex Transformation
Abstract
Regular expressions (regexes) are widely used in modern software systems, yet their backtracking-based execution exposes them to Regular Expression Denial-of-Service (ReDoS) attacks, where crafted inputs trigger super-linear runtime. While prior work has made significant progress in detecting such vulnerabilities, automatically repairing them remains fundamentally challenging: existing approaches rely on heuristic rewriting or search-based mutation, often producing patches that either break functional correctness or fail to eliminate worst-case backtracking, with no formal guarantees. We focus on ReDoS vulnerabilities induced by structural ambiguity under backtracking semantics. We present ReAR, a certified algebraic framework that recasts ReDoS repair as a language-preserving transformation problem. Our key insight is that catastrophic backtracking is not inherent to the language defined by a regex, but instead arises from its structural representation. Based on this observation, ReAR performs repair via a constructive algebraic derivation that transforms vulnerable regexes into language-equivalent repairs that are free from the modeled structural hazards. ReAR follows a three-stage pipeline: ambiguity exposure, vulnerability elimination, and structural reduction. The first stage reveals latent ambiguity and hidden backtracking paths; the second removes the modeled exponential and polynomial backtracking hazards, yielding regexes that are structurally free from the ambiguity patterns targeted by our derivation system; and the third reduces structural redundancy while preserving language and remaining free from the modeled hazards. All transformations are grounded in Kleene algebra, ensuring language equivalence (L(R') = L (R)) while enforcing a complexity safety invariant, and produce machine-checkable derivation certificates. We implement ReAR and evaluate it on 460 real-world vulnerable regexes collected from CVEs and prior benchmarks. ReAR successfully repairs 85.22% of vulnerabilities, outperforming existing state-of-the-art methods, while maintaining an average repair time of 0.86 seconds per regex. We further analyze the algebraic derivation process and find that repairs typically converge within a small number of transformation steps (median: 5), with 78.57% of regexes repaired within 10 steps, demonstrating the practical efficiency of our certified approach. Beyond effectiveness, ReAR provides verifiable proof traces, transforming regex repair from heuristic patching into a principled and auditable process.