ModSecurity
ModSecurity copied to clipboard
Reuse the precompiled regex for collection variable selectors
what
- Add a fast-path
InMemoryPerProcess::resolveRegularExpression(const Utils::Regex *)overload that takes an already-compiled regex. Tx_DictElementRegexp(theTX:/regex/variable selector) now reuses its pre-compiledm_r: itdynamic_casts the TX collection toInMemoryPerProcessand calls the fast path if possible.- Adds a regression test for
TX:/regex/selection (positive + negative cases).
why
- The collection backends'
resolveRegularExpression(const std::string&, ...)built a freshUtils::Regex— i.e. apcre2_compile()andpcre2_jit_compile()— on every call. - For a regex variable selector such as
TX:/regex/that is evaluated per transaction (e.g. CRS 921180), this recompiled the same pattern on every request, even though the callingVariableRegexalready holds it compiled once at configuration time inm_r. That compiled regex was simply being ignored. - Behaviour is unchanged:
m_ris constructed with the same arguments —Utils::Regex(pattern, /*ignoreCase=*/true)— that the backend used, so the identical regex is applied; it is just compiled once instead of per transaction. - Measured (built against
v3/master, gcc-O2, system PCRE2 with JIT):- Microbenchmark of the resolution call: reusing the compiled regex vs recompiling is ~1.6× faster for a short selector, and the saving grows with pattern length (it is the eliminated compile + JIT).
- No behaviour change: the full regression suite passes (634 passed, 80 skipped — unchanged), including the new test.
references
- Related (not fixed by this change): #2972 and #3409 discuss general PCRE2 overhead; this PR removes one specific source of repeated PCRE2 compilation.
The failing cppcheck check is unrelated to this PR. On a clean v3/master
checkout with no changes, make check-static already fails under cppcheck
2.21.0 (and passes under 2.20.0) — the job installs cppcheck unpinned, so the
analyzer version drifts. All 31 findings are in files this PR doesn't touch.
Filed as #3574.
All other CI jobs (Linux x32/x64 gcc+clang, all configs, macOS) pass.
Quality Gate passed
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code