Skip to content

Regular Expression Failure Triggers !@rx #3123

Open

Description

@ssigwart

Describe the bug

When there's a regular expression error due to SecPcreMatchLimit or SecPcreMatchLimitRecursion (i.e. MSC_PCRE_LIMITS_EXCEEDED), a rule using !@rx will say that the rule was triggered. However, failures with @rx will say that the rule was not triggered. I think both should assume the rule was not triggered. See coreruleset/coreruleset#3640 (comment) for additional context.

To Reproduce

See coreruleset/coreruleset#3640 (comment).

You can probably reproduce by setting SecPcreMatchLimit and SecPcreMatchLimitRecursion really low (maybe 5) and adding a !@rx rule.

Expected behavior

I would expect !@rx to not trigger a rule if there's a MSC_PCRE_LIMITS_EXCEEDED error.

Server (please complete the following information):

  • ModSecurity version (and connector): ModSecurity v3.0.12 with nginx-connector v1.0.3
  • WebServer: nginx-1.24.0
  • OS (and distro): Amazon Linux 2

Rule Set (please complete the following information):

  • Running any public or commercial rule set? CRS
  • What is the version number? 4.1.0

Activity

  1. added
    3.xRelated to ModSecurity version 3.x
    on Apr 19, 2024
  2. airween commented on Apr 19, 2024

    @airween
    Member

    Hi @ssigwart,

    thanks for reporting, I will take a look at this issue soon.

  3. self-assigned this
    on Apr 19, 2024
  4. airween commented on Oct 14, 2024

    @airween
    Member

    Hi @ssigwart,

    meanwhile I faced this problem somewhere else, now I can confirm this is a bug. Thanks for reporting.

  5. ssigwart commented on Oct 14, 2024

    @ssigwart
    Author

    Great. Thanks, @airween.

  6. airween commented on Feb 12, 2025

    @airween
    Member

    Hi @ssigwart,

    first of all, sorry for the long delay.

    I checked this issue again and tried to figure out what would be the solution. I agree with your suggestion:

    Expected behavior

    I would expect !@rx to not trigger a rule if there's a MSC_PCRE_LIMITS_EXCEEDED error.

    I took a look at @rx's behavior, and I think we don't need to care about "other direction" - I mean if a rule uses @rx PATTERN syntax and the MSC_PCRE_LIMITS_EXCEEDED is set then the rule won't trigger.

    Here is the condition table:

    Operator syntax Operator evaluation result MSC_PCRE_LIMITS_EXCEEDED is set Rule result
    !@rx PATTERN false no matter not triggered
    !@rx PATTERN true no triggered
    !@rx PATTERN true yes not triggered

    The last row is a new condition. We should implement this in both engines (mod_security2 and libmodsecurity3), but in first step only at the @rx operator (I mean this modification won't affect the other regex's behavior, eg. targets like ARGS:/^foo.*$/).

    What do you think about this solution?

    Please note this solution @marcstern, @dune73, @fzipi, @theseion.

    Also note to @jptosso, @M4tteoP, @jcchavezs - for Coraza compatibility.

  7. ssigwart commented on Feb 12, 2025

    @ssigwart
    Author

    Hi @airween, I agree that the !@rx condition when MSC_PCRE_LIMITS_EXCEEDED happens is the only one that needs to be updated to not trigger the rule. Thanks!

  8. fzipi commented on Feb 14, 2025

    @fzipi
    Collaborator

    Sound good. This condition is specific to modsecurity, as Coraza does not not use pcre2 engine.

  9. fzipi commented on Oct 12, 2026

    @fzipi
    Collaborator

    Revisiting this one, with the mechanism from the code. Rx::evaluate returns false whenever the regex call fails (src/operators/rx.cc, after setting MSC_PCRE_LIMITS_EXCEEDED), and the generic negation in Operator::evaluateInternal (src/operators/operator.cc) then turns that into a match for !@rx. The fix @airween agreed to needs the error to be distinguishable from "no match": either a tri-state result from the operator, or a transaction flag set on regex error that the negation wrapper checks before flipping the result. The same applies to @rxGlobal and the other regex-backed operators. Confirmed bug.

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

Metadata

Metadata

Assignees

Labels

3.xRelated to ModSecurity version 3.xbugIt is a confirmed bugpr available

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions