Modern regex languages have strayed far from well-understood traditional regular expressions: they include features that fundamentally transform the matching problem. In exchange for these features, modern regex engines at times suffer from exponential complexity blowups, a frequent source of denial-of-service vulnerabilities in JavaScript applications. Worse, regex semantics differ across languages, and the impact of these divergences on algorithmic design and worst-case matching complexity has seldom been investigated. This paper provides a novel perspective on JavaScript's regex semantics by identifying a larger-than-previously-understood subset of the language that can be matched with linear time guarantees. In the process, we discover several cases where state-of-the-art algorithms were either wrong (semantically incorrect), inefficient (suffering from superlinear complexity) or excessively restrictive (assuming certain features could not be matched linearly). We introduce novel algorithms to restore correctness and linear complexity. We further advance the state-of-the-art in linear regex matching by presenting the first nonbacktracking algorithms for matching lookarounds in linear time: one supporting captureless lookbehinds in any regex language, and another leveraging a JavaScript property to support unrestricted lookaheads and lookbehinds. Finally, we describe new time and space complexity tradeoffs for regex engines. All of our algorithms are practical: we validated them in a prototype implementation, and some have also been merged in the V8 JavaScript implementation used in Chrome and Node.js.
翻译:现代正则表达式语言已远非人们所熟知的传统正则表达式:它们包含了从根本上改变匹配问题的特性。尽管这些特性带来了便利,但现代正则表达式引擎有时会遭遇指数级复杂度膨胀,这是JavaScript应用程序中拒绝服务漏洞的常见根源。更糟糕的是,正则表达式语义在不同语言间存在差异,而此类分歧对算法设计与最坏情况匹配复杂度的影响鲜有研究。本文通过识别JavaScript正则表达式中一个比先前认知范围更广的可在线性时间内完成匹配的子集,为其语义提供了全新视角。在此过程中,我们发现了若干案例:现有最优算法或存在错误(语义不正确)、或效率低下(存在超线性复杂度)、或限制过于严格(认定某些特性无法线性匹配)。我们引入创新算法以恢复正确性与线性复杂度。通过首次提出支持零宽断言线性匹配的非回溯算法(其中一种算法支持任意正则语言的捕获无关后顾断言,另一种算法利用JavaScript特性支持无限制的前瞻断言与后顾断言),我们进一步推动了线性正则匹配技术的现有水平。最后,我们描述了正则表达式引擎在时间与空间复杂度之间新的权衡策略。所有算法均具有实用性:我们在原型实现中完成了验证,其中部分算法已合并至Chrome与Node.js所使用的V8 JavaScript实现中。