Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removes multiline from default regex modifiers #876

Merged
merged 5 commits into from
Nov 18, 2024

Conversation

M4tteoP
Copy link
Member

@M4tteoP M4tteoP commented Sep 18, 2023

This PR evaluates the needed changes to align Coraza to Modsec v2 behavior rather than Modsec v3 in terms of default modifiers when compiling regexes. This necessity has been raised by coreruleset/coreruleset#3277.

We are currently running with two default modifiers:

  • s (dotall)
  • m (multiline mode)

While Modsec v2 (the reference implementation for the CRS) has:

  • PCRE2_DOTALL
  • PCRE2_DOLLAR_ENDONLY

Running the CRS rules with multiline may lead to false positives and lower performance.

Opening the PR to further discuss this, and whether we accept a breaking change for v3.* or not.

EDIT: The change has been implemented under the coraza.rule.no_regex_multiline build flag and is disabled by default.

// The so called DOLLAR_ENDONLY modifier in PCRE2 is meant to tweak the meaning of dollar '$'
// so that it matches only at the very end of the string (see: https://www.pcre.org/current/doc/html/pcre2pattern.html#SEC6)
// It seems that re2 already behaves like that by default.
pattern: `123$`,
Copy link
Member Author

Choose a reason for hiding this comment

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

I was not able to find the DOLLAR_ENDONLY modifier for RE2, but it seems that re2 by default behaves like DOLLAR_ENDONLY enabled.
See also https://regex101.com/r/mUcy69/1 (Mind the regex option D (dollar end only) enabled).
Am I missing something?

@codecov
Copy link

codecov bot commented Sep 18, 2023

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 81.66%. Comparing base (3bc5ef6) to head (3c50384).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/operators/rx.go 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #876   +/-   ##
=======================================
  Coverage   81.66%   81.66%           
=======================================
  Files         168      168           
  Lines        9647     9655    +8     
=======================================
+ Hits         7878     7885    +7     
  Misses       1519     1519           
- Partials      250      251    +1     
Flag Coverage Δ
coraza.rule.case_sensitive_args_keys 81.62% <92.30%> (+<0.01%) ⬆️
coraza.rule.multiphase_valuation 81.66% <92.30%> (+<0.01%) ⬆️
coraza.rule.no_regex_multiline 81.60% <46.15%> (?)
default 81.66% <92.30%> (+<0.01%) ⬆️
examples+ 16.45% <53.84%> (+<0.01%) ⬆️
examples+coraza.rule.case_sensitive_args_keys 81.62% <92.30%> (+<0.01%) ⬆️
examples+coraza.rule.multiphase_valuation 81.50% <53.84%> (-0.05%) ⬇️
examples+coraza.rule.no_regex_multiline 81.52% <46.15%> (?)
examples+memoize_builders 81.62% <53.84%> (-0.05%) ⬇️
examples+no_fs_access 80.94% <53.84%> (-0.05%) ⬇️
ftw 81.66% <92.30%> (+<0.01%) ⬆️
memoize_builders 81.76% <92.30%> (+<0.01%) ⬆️
no_fs_access 81.10% <92.30%> (+<0.01%) ⬆️
tinygo 81.63% <92.30%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@anuraaga
Copy link
Contributor

Good to see ftw passing with this change. I wouldn't merge it until seeing the PR to ModSec v3 doing the same, otherwise someone will just ask in the future why we aren't aligned with ModSec v3.

@jptosso jptosso mentioned this pull request Dec 19, 2023
16 tasks
@jcchavezs
Copy link
Member

Maybe it is a good idea to coordinate this in @M4tteoP @airween

@M4tteoP
Copy link
Member Author

M4tteoP commented Feb 8, 2024

Totally 💯

@M4tteoP
Copy link
Member Author

M4tteoP commented Aug 26, 2024

Apart from aligning the behavior with ModSec, are we going to accept this breaking change and release it into a 3.x version or we aim to have it into 4.x? If the latter is the case, I would guard the feature with a build flag and merge this PR with the fix disabled by default

@jptosso jptosso added the v4 Work for v4 label Nov 7, 2024
@jptosso
Copy link
Member

jptosso commented Nov 7, 2024

We agreed to implement this as a build tag for v3 and mandatory for v4
Once merged, another PR has to be created to remove the flag and force it by default

@M4tteoP M4tteoP marked this pull request as ready for review November 13, 2024 00:05
@M4tteoP M4tteoP requested a review from a team as a code owner November 13, 2024 00:05
@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 13, 2024

We agreed to implement this as a build tag for v3 and mandatory for v4

Done, added coraza.rule.no_regex_multiline build tag. It should be ready to be reviewed.

@jptosso
Copy link
Member

jptosso commented Nov 13, 2024

can you add this to the testing matrix? within the github actions. As mentioned here #1182, we should start covering all permutations of flags

@jptosso
Copy link
Member

jptosso commented Nov 13, 2024

Please add this build tag to magefile.go

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 13, 2024

Done, let's if tests are happy ^_^

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 13, 2024

Looks so 🚀
Note: codecov is failing because the new logic is tested on a new test file that has the conditional build, so we have coverage, but codecov does not get it.

Copy link
Member

@jptosso jptosso left a comment

Choose a reason for hiding this comment

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

LGTM

@M4tteoP M4tteoP changed the title fix: removes multiline from default regex modifiers [breaking] fix: removes multiline from default regex modifiers Nov 18, 2024
@M4tteoP M4tteoP merged commit c5606ed into corazawaf:main Nov 18, 2024
71 checks passed
@M4tteoP M4tteoP deleted the regex_default_mods branch November 18, 2024 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v4 Work for v4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants