arXiv:2605.07289cs.DScs.CL2026-05

揭示带反向引用正则表达式匹配的复杂性边界,提出首个近线性时间算法

On the Complexity of the Matching Problem of Regular Expressions with Backreferences

  • 研究带反向引用的正则表达式匹配复杂度,定义r-使用k-REWBs模型
  • 证明k-REWB匹配在SETH假设下无法在O(n^{2k−ε})时间内完成
  • 给出1-使用REWB的O(n log²n)算法,适合安全系统开发者参考

ReDoS是一种典型的算法复杂度攻击,攻击者通过构造恶意字符串使正则表达式匹配引擎耗尽计算资源。即使二次时间复杂度也曾导致重大故障,如Stack Overflow(2016)和Cloudflare(2019)事件。这引出根本问题:能否构建可证明高效的匹配引擎,在输入长度上近乎线性运行?经典正则表达式可通过Thompson构造实现线性时间匹配,但实际引擎支持的反向引用功能虽增强表达力,却显著提升ReDoS风险。本文研究带反向引用的正则表达式(REWBs)的精细复杂度,聚焦r-使用k-REWBs。在难解性方面,证明k-REWB匹配无法在O(n^{2k−ε})时间内求解(对任意ε>0),除非SETH不成立;且参数化于表达式长度时为W[2]-难,强于此前的W[1]-难结论。此外,证明2-使用2-REWB匹配若能在n^{1+o(1)}时间内解决,则三角形检测也可在该时间内完成。在算法方面,提出1-使用REWB的O(n log²n)时间算法,显著优于近期的O(n²)算法(Nogami & Terauchi, MFCS 2025),采用后缀树、正则表达式转移幺半群、因子森林结构及字符串周期性等技术。

原文摘要 · Abstract (English)

ReDoS is a well-known type of algorithmic complexity attack, where an adversary supplies maliciously crafted strings to a regular expression matching engine, aiming to exhaust computational resources of systems. Even quadratic-time behavior in matching engines has been exploited in successful attacks, as exemplified by major outages at Stack Overflow (2016) and Cloudflare (2019). These incidents motivate a fundamental question: Is it possible to construct matching engines that are provably efficient, running in (near-)linear time in the length of the input string? For classical regular expressions (REGEX), Thompson's construction yields a linear-time algorithm. However, practical engines support powerful features such as backreferences, which strictly extend the expressive power of REGEX but unfortunately increase the risk of ReDoS attacks. This paper investigates the fine-grained complexity of the string matching problem for regular expressions with backreferences (REWBs). Specifically, we consider $r$-use $k$-REWBs. On the hardness side, we show that the string matching problem for $k$-REWBs cannot be solved in $O(n^{2k-ε})$ time for any $ε> 0$ under SETH. We also prove that this problem is \textbf{W[2]}-hard when parameterized by the length of the REWB expression, strengthening the previous \textbf{W[1]}-hardness. Moreover, we prove that this problem for $2$-use $2$-REWBs cannot be solved in $n^{1+o(1)}$ time unless the triangle detection problem can be solved in that time. On the algorithmic side, we present an $O(n \log^2 n)$-time algorithm for $1$-use REWBs, which significantly improves upon the recent $O(n^2)$-time algorithm by Nogami and Terauchi (MFCS, 2025). Our algorithm employs several techniques including suffix trees, transition monoids of REGEXes, factorization forest data structures, and periodicity of strings.

正则表达式复杂度分析算法安全

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。