Skip to content

Feature Request: Wildcard/pattern support in ctl:ruleRemoveTargetById (v3) #3505

Open

Description

@etiennemunnich

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

  1. Path/method exclusion: Loses protection for the entire endpoint.
  2. 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: In init(), detect the COLLECTION:/.../ pattern. If it's a regex, compile via Utils::Regex and push to the transaction state.
  • src/rule_with_operator.cc: In getFinalVars and the evaluate loop, check exclusions against regex targets using m_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:

  1. Subject Length Limit: Cap the evaluated string (variable name) at 256 characters. If exceeded, skip the regex match (fail closed/evaluate the rule).
  2. Match Limits: Utilize pcre2_set_match_limit() for this specific code path.
  3. Compile-time validation: Reject known ReDoS patterns at config load.

Activity

  1. changed the title [-]Feature Request: Wildcard/pattern support in ctl:ruleRemoveTargetById (v3)`\[/-] [+]Feature Request: Wildcard/pattern support in ctl:ruleRemoveTargetById (v3)[/+] on Feb 28, 2026
  2. airween commented on Feb 28, 2026

    @airween
    Member

    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.

  3. etiennemunnich commented on Feb 28, 2026

    @etiennemunnich
    Author

    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!

  4. airween commented on Mar 1, 2026

    @airween
    Member

    I'd be more than happy to help with the implementation, writing tests, or benchmarking if that would help move it forward.

    I'll let you know when I'm in that stage :)

    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.

    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.

  5. etiennemunnich commented on Mar 5, 2026

    @etiennemunnich
    Author

    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?

  6. etiennemunnich commented on Mar 25, 2026

    @etiennemunnich
    Author

    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.

  7. airween commented on Mar 25, 2026

    @airween
    Member

    BTW: Is there a performance test-harness / environment beyond that of the benchmark script?

    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 with gnuplot. It's not a benchmark but it could be a good reference, especially we make this on the same machine with different configs/features.

  8. added a commit that references this issue on Mar 28, 2026
    370f93b
  9. etiennemunnich commented on Mar 28, 2026

    @etiennemunnich
    Author

    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$/
    1. During init(), the code detects the /pattern/ delimiters in the target string
    2. The pattern between the slashes is compiled via Utils::Regex (PCRE2 by default, PCRE1 with --with-pcre)
    3. The compiled regex is stored as a shared_ptr — it's allocated once and shared across all requests
    4. At request time, searchAll() runs against the short variable-key string (e.g. json.0.JobDescription, typically 10-40 characters)
    5. If you don't use /pattern/, the existing literal == comparison is used — no change in behavior for existing rules

    The same logic is applied to both ctl:ruleRemoveTargetById and ctl:ruleRemoveTargetByTag, using a shared RuleRemoveTargetSpec struct so there's no code duplication.

    What's in the PR

    • 11 files changed — the core logic is in rule_remove_target_by_id.cc, rule_remove_target_by_tag.cc, and rule_with_operator.cc
    • 7 regression tests covering regex keys for both ById and ByTag, plus a backward-compatibility test proving literal targets still work
    • A small lexer change — REMOVE_RULE_TARGET_VALUE (the character class that allows regex metacharacters like ^, $, \d) is now shared by both ById and ByTag scanner rules

    Performance

    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):

    Keys/request No exclusions With regex exclusions Overhead
    20 1,209 ms 1,326 ms +9.7%
    50 2,620 ms 2,873 ms +9.7%
    100 5,199 ms 5,543 ms +6.6%

    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:ruleRemoveTargetByMsg is 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.

  10. added a commit that references this issue on Mar 28, 2026
    637ad9c
  11. added
    3.x
    on May 9, 2026
  12. fzipi commented on Oct 12, 2026

    @fzipi
    Collaborator

    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.

  13. etiennemunnich commented on Oct 18, 2026

    @etiennemunnich
    Author

    Hi Filipe, I am cleaning up 3526 and will work to have that out in the next week or so.

  14. etiennemunnich commented on Oct 26, 2026

    @etiennemunnich
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xpr available

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions