Repository navigation
Description
Hi team! This is my first ask/contribution to ModSecurity. I'm hoping to help improve false positive management for folks using CRS — please let me know if I can clarify anything or adjust this proposal.
Is your feature request related to a problem? Please describe.
Currently, ctl:ruleRemoveTargetById only allows excluding a Core Rule Set (CRS) rule from inspecting a literal, exact variable name (e.g., ARGS:json.JobDescription). It does not support wildcards or regular expressions.
This creates a significant pain point for modern APIs accepting JSON arrays or GraphQL payloads, where keys are dynamic:
- Payload:
[{"JobDescription": "..."}, {"JobDescription": "..."}, ...] - Variables:
ARGS:json.0.JobDescription,ARGS:json.1.JobDescription, …
Excluding these requires listing every possible index, which is impractical and leads to users disabling rules entirely or losing protection for the whole endpoint via path exclusions.
Note: This request is related to the legacy v2 PR #3121 and Issue #911, but focuses specifically on a v3 implementation.
Describe the solution you'd like
Extend ctl:ruleRemoveTargetById to accept a pattern (regex) that matches variable names, mirroring the existing regex support in SecRuleUpdateTargetById, but enabling runtime/URI-scoped use.
Example Syntax:
SecRule REQUEST_URI "@beginsWith /api/jobs" \
"id:100100,phase:1,pass,nolog,ctl:ruleRemoveTargetById=932125;ARGS:/^json\.\d+\.JobDescription$/"Expected Behavior: When the target rule inspects variables, any variable whose name matches the compiled pattern is excluded. Literal targets must continue to work unchanged. Invalid regex should fail at config load.
Describe alternatives you've considered
- Path/method exclusion: Loses protection for the entire endpoint.
- SecRuleUpdateTargetById: Applies globally at configure-time, which is risky if other endpoints share the same JSON structure.
Technical Feasibility & Security Considerations (v3)
The change is feasible in v3 with localized edits, as the engine already utilizes Utils::Regex (PCRE2) for variable matching.
src/actions/ctl/rule_remove_target_by_id.cc: Ininit(), detect theCOLLECTION:/.../pattern. If it's a regex, compile viaUtils::Regexand push to the transaction state.src/rule_with_operator.cc: IngetFinalVarsand the evaluate loop, check exclusions against regex targets usingm_regex->searchAll(varName).size() > 0.
ReDoS Mitigations Risks:
Because the variable name (e.g., JSON key) is user-controlled, strict limits are required to prevent ReDoS via the ctl evaluation:
- Subject Length Limit: Cap the evaluated string (variable name) at 256 characters. If exceeded, skip the regex match (fail closed/evaluate the rule).
- Match Limits: Utilize
pcre2_set_match_limit()for this specific code path. - Compile-time validation: Reject known ReDoS patterns at config load.
Activity
[-]Feature Request: Wildcard/pattern support in ctl:ruleRemoveTargetById (v3)`\[/-][+]Feature Request: Wildcard/pattern support in ctl:ruleRemoveTargetById (v3)[/+]airween commentedon Feb 28, 2026
Hi @etiennemunnich,
thanks for bringing this up. As you mentioned in your opening comment, there is a PR for v2: #3121.
Here I explained (I hope that's clear 😃) why I'm not satisfied with that PR, but in short: in that implementation if someone uses the mentioned syntax, during all exclusion check all rexex compilation run every time. This costs a lot.
I want to solve that when the engine starts, it loads and compiles all regexes immediately, and then it can use them.
Of course, it's on my list, and I really hope I can add this feature in next few weeks/months.
etiennemunnich commentedon Feb 28, 2026
Hi @airween ,
Thanks for the quick response and the context! I completely agree with your view regarding PR #3121—recompiling regexes on every single exclusion check would definitely introduce unacceptable overhead. Compiling them once at config/engine-load time is absolutely the right architectural approach.
Because this is currently a significant pain point for our FP management (especially with dynamic JSON/GraphQL payloads), I would love to see if we can prioritize this sooner. I'd be more than happy to help with the implementation, writing tests, or benchmarking if that would help move it forward.
As an aside, to further reduce the performance impact/concerns, would it be feasible to limit the regex capability for this specific action if implemented for every request? For example, capping the pattern/subject length, or rejecting complex nested quantifiers at config load. Since the goal here is strictly FP management on known variable structures, a heavily constrained regex feature feels like a very reasonable trade-off to ensure both safety and performance.
Let me know how I can best support this effort!
airween commentedon Mar 1, 2026
I'll let you know when I'm in that stage :)
There are many feedback on the internet about poor performance of libmodsecurity3. We definitely need to investigate the reason, but I'm afraid that will be a huge task.
etiennemunnich commentedon Mar 5, 2026
Hi @airween,
Let me preface this comment with this, I'm passionate about building secure solutions and want to help - please do not take the below as any form of criticism.
I understand the performance concern. I prototyped the this feature and ran throughput/CPU benchmarks. The impact is surprisingly small; CRS with regex exclusions performs about the same as CRS-only. I’d still treat this as high priority because of the blast radius: when users can’t scope out specific keys, they often fall back to whole-body exclusion and lose protection for all ARGS on that endpoint. Regex scope-out lets them exclude only the FP-prone keys while keeping inspection for everything else.
Happy to share the benchmark details if useful.
BTW: Is there a performance test-harness / environment beyond that of the benchmark script?
etiennemunnich commentedon Mar 25, 2026
Hey team, will give this a go, I'll share the approach I'm taking after I have some testing done to prove out different scenarios.
airween commentedon Mar 25, 2026
Sorry I just saw this now...
I usually use
ab, increase the number of requests and/or number or arguments and measure the response times, put them into a csv file. Then I draw a graph withgnuplot. It's not a benchmark but it could be a good reference, especially we make this on the same machine with different configs/features.feat: regex key support for ctl:ruleRemoveTargetById and ctl:ruleRemo…
etiennemunnich commentedon Mar 28, 2026
Hi @airween,
I've put together an implementation and opened PR #3526. Here's a summary of the approach and what's included.
Approach
Following your feedback about the v2 PR #3121 — where regex compilation ran on every exclusion check — this implementation compiles the regex once at config load time and reuses it across all requests.
How it works in practice:
When you write:
ctl:ruleRemoveTargetById=932125;ARGS:/^json\.\d+\.JobDescription$/init(), the code detects the/pattern/delimiters in the target stringUtils::Regex(PCRE2 by default, PCRE1 with--with-pcre)shared_ptr— it's allocated once and shared across all requestssearchAll()runs against the short variable-key string (e.g.json.0.JobDescription, typically 10-40 characters)/pattern/, the existing literal==comparison is used — no change in behavior for existing rulesThe same logic is applied to both
ctl:ruleRemoveTargetByIdandctl:ruleRemoveTargetByTag, using a sharedRuleRemoveTargetSpecstruct so there's no code duplication.What's in the PR
rule_remove_target_by_id.cc,rule_remove_target_by_tag.cc, andrule_with_operator.ccREMOVE_RULE_TARGET_VALUE(the character class that allows regex metacharacters like^,$,\d) is now shared by both ById and ByTag scanner rulesPerformance
I built a targeted benchmark that actually exercises the regex matching path: JSON POST requests with multiple ARGS keys, checked against 2 regex exclusions (one ById, one ByTag) and 3 detection rules (
@detectSQLi,@detectXSS,@rx).Results (25,000 iterations, median of 5 trials):
The overhead is ~0.005 ms per ARGS key — it scales linearly with no exponential blowup. The cost is one
searchAll()call per variable against a precompiled PCRE2 pattern on a short string.What's intentionally excluded
ctl:ruleRemoveTargetByMsgis not part of this PR. That action doesn't exist in v3 at all (it was in v2 but was never ported), and adding it would be a larger discussion. This PR only adds regex key support to the two actions that v3 already has.Full test suite
5005 total, 4987 pass, 18 skip, 0 fail, 0 error.
Happy to adjust anything based on your review. The benchmark source is included in the branch if you want to run it yourself.
feat: regex key support for ctl:ruleRemoveTargetById and ctl:ruleRemo…
fzipi commentedon Oct 12, 2026
Revisiting this one. Open PR #3526 ("regex key support for ctl:ruleRemoveTargetById and ctl:ruleRemoveTargetByTag") implements this with the regex compiled once at load time. The same request has been open since 2014 as #717/#911 and #2432, so merging #3526 closes all of them. Reviewing that PR is the next step.
etiennemunnich commentedon Oct 18, 2026
Hi Filipe, I am cleaning up 3526 and will work to have that out in the next week or so.
etiennemunnich commentedon Oct 26, 2026
Hi @fzipi and @airween, I've completed updates and resolved review action items. Please let me know if you have any thoughts or changes needed.