Skip to content

fix: negated @rx must not match when the regex fails - #3709

Merged
airween merged 1 commit into
owasp-modsecurity:v2/masterfrom
airween:v2/issue-3123-negated-rx-regex-error
Oct 26, 2026
Merged

airween merged 1 commit into
owasp-modsecurity:v2/masterfrom
airween:v2/issue-3123-negated-rx-regex-error

Conversation

@airween

@airween airween commented Oct 26, 2026

Copy link
Copy Markdown
Member

what

  • @rx: when the PCRE match/recursion limit is exceeded or a pattern built from macros fails to compile at runtime, the operator now returns the rule's negation flag instead of 0, so neither @rx nor !@rx matches; the rule continues with the remaining targets
  • a runtime compile error and any other regex execution error set TX:MSC_PCRE_ERROR=1; a compile error is also logged
  • modsecurity.conf-recommended: list MSC_PCRE_ERROR among the flags checked by rule 200005
  • new regression tests: tests/regression/misc/10-pcre-negated.t

why

A regex failure was reported as "no match", and the negation in re.c turned it into a match: every !@rx rule fired on a regex error. A client can trigger the match limit at will, especially with the low SecPcreMatchLimit of the recommended configuration. This is the v2 counterpart of the libmodsecurity fix in #3700.

Returning -1 is not an option: re.c aborts the rule on an operator error and skips the remaining targets. Instead, the failure is made visible through TX:MSC_PCRE_ERROR, which the recommended TX:/^MSC_/ rule already blocks on, so a regex error fails closed.

references

AI disclosure:

Made with help of Claude Code, Opus 5.5.

When the PCRE match or recursion limit was exceeded, or a pattern built
from macros failed to compile at runtime, msre_op_rx_execute() returned
0 ("no match"). The negation in re.c turned that into a match, so every
!@rx rule fired on a regex error. With the low SecPcreMatchLimit from
the recommended configuration, a client could trigger this on purpose.

Return rule->op_negated from these paths instead, so neither @rx nor
!@rx matches and the rule goes on with the remaining targets. Returning
-1 would abort the rule and skip those targets.

A runtime compile error and any other regex execution error now also
set TX:MSC_PCRE_ERROR, and a compile error is logged. The TX:/^MSC_/
rule (200005) in the recommended configuration therefore catches every
regex error, not only exceeded limits.
@coderabbitai

coderabbitai Bot commented Oct 26, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2c8b9db4-09d0-4db6-993e-6e3c25a68f18

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

sonarqubecloud Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
2 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@fzipi fzipi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @airween, this looks good to me.

Returning rule->op_negated is correct for v2. execute_operator() in re.c treats rc == 0 without negation, or rc == 1 with negation, as "no match", so neither @rx nor !@rx matches on a regex error. Because this path returns before -1, the rule still checks its remaining targets. The 5 cases in 10-pcre-negated.t pass in CI, and cases 1, 2, 4 and 5 would fail without the change.

A few comments. None of them block this PR:

  1. Other execution errors still abort the rule. With TX:MSC_PCRE_ERROR set in the rc < -1 branch, rule 200005 now fails closed on any "Regex execution failed" error. That includes PCRE2_ERROR_JIT_STACKLIMIT / PCRE_ERROR_JIT_STACKLIMIT and PCRE2_ERROR_HEAPLIMIT, which a client can trigger with large input much like the match limit (v2 sets no JIT stack, so the 32 KiB default applies). That branch still returns -1, though, so the rule skips its remaining targets. Without rule 200005, a large early argument can hide later ones from that rule. This is not new, but it might be worth treating the JIT stack and heap limits like MATCHLIMIT (set MSC_PCRE_LIMITS_EXCEEDED, return op_negated) here or in a follow-up.
  2. @validateHash has the same issue. msre_op_validateHash_execute() returns 0 on a runtime compile error and on the match limit, so !@validateHash fires on a regex error. That's for a separate issue.
  3. Nit: msre_op_rx_set_error_flag() duplicates the MSC_PCRE_LIMITS_EXCEEDED block, which already appears in both @rx and @validateHash. A helper that takes the flag name could serve all three.
  4. v3 parity: in v3, MSC_PCRE_ERROR is a standalone variable that TX:/^MSC_/ doesn't see. For the v3 follow-up you mentioned, setting TX:MSC_PCRE_ERROR with the same name would let one recommended-config rule cover both engines.
  5. Docs: the reference manual at modsecurity.org/docs currently describes MSC_PCRE_ERROR as v3-only, and the TX flag list doesn't mention it. I'll open a docs PR once this is merged.

@airween

airween commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Thanks @fzipi for the review. CI is green, so I'm merging this. On your points:

  1. JIT stack and heap limits. For PCRE2 builds, which are the default, this doesn't reach @rx. On PCRE2_ERROR_JIT_STACKLIMIT, msc_regexec_ex() reruns the match without JIT (msc_pcre.c:257-260), so the 32 KiB JIT stack is not a limit there. v2 doesn't set a heap limit, and the PCRE2 default is 20,000,000 KiB, which a request can't realistically exhaust. You're right for legacy PCRE builds (--with-pcre) with JIT, though: pcre_exec() returns PCRE_ERROR_JITSTACKLIMIT without a fallback, so the rule takes the rc < -1 branch and skips its remaining targets. I'll handle it like MATCHLIMIT in a follow-up. Note, that the newest distributions don't contain old PCRE library, theoretically it's not possible to build with that.
  2. @validateHash. Agreed, same pattern. I'll fix it in that follow-up too.
  3. Shared helper. Agreed. Since the follow-up touches @validateHash anyway, I'll replace the three blocks with one helper that takes the flag name there, and keep this PR as it is.
  4. v3 parity. Good idea, that's better than what I had in mind (a separate recommended rule for the standalone variable). Setting TX:MSC_PCRE_ERROR in rx.cc and rx_global.cc, as is already done for MSC_PCRE_LIMITS_EXCEEDED, lets rule 200005 cover both engines without any configuration change. I'll open that PR once fix: do not let !@rx and !@rxGlobal match on a regex error #3700 is merged.
  5. Docs. Thanks for taking that.

@airween
airween merged commit 42b02c6 into owasp-modsecurity:v2/master Oct 26, 2026
93 checks passed
airween added a commit that referenced this pull request Oct 27, 2026
Since #3700, a negated @rx/@rxglobal no longer matches on a regex error.
Exceeded match limits are still caught by the TX:/^MSC_/ rule (200005)
of the recommended configuration through TX:MSC_PCRE_LIMITS_EXCEEDED.
Compile errors and other regex errors only set the standalone
MSC_PCRE_ERROR variable, though, which TX:/^MSC_/ does not see, so
such a request passed both the negated rule and rule 200005.

Set TX:MSC_PCRE_ERROR as well, the same way as MSC_PCRE_LIMITS_EXCEEDED.
Rule 200005 now blocks on any regex error without a configuration
change, as it does in ModSecurity v2 (#3709). The standalone variable
is kept.
airween added a commit that referenced this pull request Oct 27, 2026
Follow-up to #3709, as promised in its review:

- @validateHash returned 0 on a compile error of the macro-expanded
  pattern and on exceeded PCRE limits, which a negated rule turned into
  a match. A compile error was also not logged and not flagged, so a
  non-negated rule skipped the hash check silently. Both paths now
  return what re.c turns into "no match", and a compile error is logged
  and sets TX:MSC_PCRE_ERROR. Other execution errors set
  TX:MSC_PCRE_ERROR too, as in @rx.
- With legacy PCRE and JIT, pcre_exec() returns PCRE_ERROR_JITSTACKLIMIT
  without retrying without JIT, as the PCRE2 path does. Handle it like
  the match limit in @rx and @validateHash.
- Replace the TX flag blocks in re_operators.c with one helper that
  takes the flag name.
airween added a commit that referenced this pull request Oct 27, 2026
Follow-up to #3709, which added the flag to modsecurity.conf-recommended
only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants