Repository navigation
Conversation
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.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
fzipi
left a comment
There was a problem hiding this comment.
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:
- Other execution errors still abort the rule. With
TX:MSC_PCRE_ERRORset in therc < -1branch, rule 200005 now fails closed on any "Regex execution failed" error. That includesPCRE2_ERROR_JIT_STACKLIMIT/PCRE_ERROR_JIT_STACKLIMITandPCRE2_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 likeMATCHLIMIT(setMSC_PCRE_LIMITS_EXCEEDED, returnop_negated) here or in a follow-up. @validateHashhas the same issue.msre_op_validateHash_execute()returns0on a runtime compile error and on the match limit, so!@validateHashfires on a regex error. That's for a separate issue.- Nit:
msre_op_rx_set_error_flag()duplicates theMSC_PCRE_LIMITS_EXCEEDEDblock, which already appears in both@rxand@validateHash. A helper that takes the flag name could serve all three. - v3 parity: in v3,
MSC_PCRE_ERRORis a standalone variable thatTX:/^MSC_/doesn't see. For the v3 follow-up you mentioned, settingTX:MSC_PCRE_ERRORwith the same name would let one recommended-config rule cover both engines. - Docs: the reference manual at modsecurity.org/docs currently describes
MSC_PCRE_ERRORas v3-only, and theTXflag list doesn't mention it. I'll open a docs PR once this is merged.
|
Thanks @fzipi for the review. CI is green, so I'm merging this. On your points:
|
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.
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.
Follow-up to #3709, which added the flag to modsecurity.conf-recommended only.

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 of0, so neither@rxnor!@rxmatches; the rule continues with the remaining targetsTX:MSC_PCRE_ERROR=1; a compile error is also loggedmodsecurity.conf-recommended: listMSC_PCRE_ERRORamong the flags checked by rule 200005tests/regression/misc/10-pcre-negated.twhy
A regex failure was reported as "no match", and the negation in
re.cturned it into a match: every!@rxrule fired on a regex error. A client can trigger the match limit at will, especially with the lowSecPcreMatchLimitof the recommended configuration. This is the v2 counterpart of the libmodsecurity fix in #3700.Returning
-1is not an option:re.caborts the rule on an operator error and skips the remaining targets. Instead, the failure is made visible throughTX:MSC_PCRE_ERROR, which the recommendedTX:/^MSC_/rule already blocks on, so a regex error fails closed.references
!@rx#3123AI disclosure:
Made with help of Claude Code, Opus 5.5.