-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Reuse the precompiled regex for collection variable selectors #3573
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
Open
chweidling
wants to merge
5
commits into
owasp-modsecurity:v3/master
Choose a base branch
from
chweidling:perf/precompiled-regex-collection-selector
base: v3/master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4ab2b1
Reuse the precompiled regex for collection variable selectors
chweidling 17474ad
Merge branch 'v3/master' into perf/precompiled-regex-collection-selector
chweidling 59023a3
Merge branch 'v3/master' into perf/precompiled-regex-collection-selector
chweidling fad6153
Address review: take compiled regex by const pointer; fix license header
chweidling 634bb80
Keep the optimization off the Collection ABI
chweidling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * ModSecurity, http://www.modsecurity.org/ | ||
| * Copyright (c) 2026 OWASP ModSecurity project | ||
| * | ||
| * You may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * If any of the files related to licensing are missing or if you have any | ||
| * other questions related to licensing please contact OWASP. | ||
| * directly using the email address modsecurity@owasp.org. | ||
|
chweidling marked this conversation as resolved.
Outdated
|
||
| * | ||
| */ | ||
|
|
||
|
|
||
| #include "modsecurity/collection/collection.h" | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "src/utils/regex.h" | ||
|
|
||
|
|
||
| namespace modsecurity { | ||
|
Check warning on line 25 in src/collection/collection.cc
|
||
| namespace collection { | ||
|
|
||
|
|
||
| void Collection::resolveRegularExpression(Utils::Regex *r, | ||
| std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) { | ||
| // Default behaviour: fall back to the string-based resolution using the | ||
| // pattern carried by the compiled regex. Backends that can take advantage | ||
| // of the already-compiled regex (e.g. InMemoryPerProcess) override this. | ||
| resolveRegularExpression(r->pattern, l, ke); | ||
| } | ||
|
|
||
|
|
||
| } // namespace collection | ||
| } // namespace modsecurity | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
test/test-cases/regression/variable-tx-regex-precompiled.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| [ | ||
| { | ||
| "enabled": 1, | ||
| "version_min": 300000, | ||
| "title": "TX regex variable selector (precompiled regex) - matches in a chained rule", | ||
| "client": { | ||
| "ip": "200.249.12.31", | ||
| "port": 123 | ||
| }, | ||
| "server": { | ||
| "ip": "200.249.12.31", | ||
| "port": 80 | ||
| }, | ||
| "request": { | ||
| "headers": { | ||
| "Host": "localhost", | ||
| "User-Agent": "curl/7.38.0", | ||
| "Content-Length": "0" | ||
| }, | ||
| "uri": "/test", | ||
| "method": "GET" | ||
| }, | ||
| "response": { | ||
| "headers": { | ||
| "Content-Type": "text/html", | ||
| "Content-Length": "8" | ||
| }, | ||
| "body": ["no need."] | ||
| }, | ||
| "expected": { | ||
| "http_code": 403 | ||
| }, | ||
| "rules": [ | ||
| "SecRuleEngine On", | ||
| "SecAction \"id:1,phase:1,nolog,pass,setvar:'tx.score_a=1',setvar:'tx.score_b=1',setvar:'tx.other=1'\"", | ||
| "SecRule REQUEST_URI \"@contains /test\" \"id:2,phase:2,deny,status:403,log,chain\"", | ||
| " SecRule TX:/^score_/ \"@eq 1\" \"t:none\"" | ||
| ] | ||
| }, | ||
| { | ||
| "enabled": 1, | ||
| "version_min": 300000, | ||
| "title": "TX regex variable selector (precompiled regex) - selector excludes non-matching keys", | ||
| "client": { | ||
| "ip": "200.249.12.31", | ||
| "port": 123 | ||
| }, | ||
| "server": { | ||
| "ip": "200.249.12.31", | ||
| "port": 80 | ||
| }, | ||
| "request": { | ||
| "headers": { | ||
| "Host": "localhost", | ||
| "User-Agent": "curl/7.38.0", | ||
| "Content-Length": "0" | ||
| }, | ||
| "uri": "/test", | ||
| "method": "GET" | ||
| }, | ||
| "response": { | ||
| "headers": { | ||
| "Content-Type": "text/html", | ||
| "Content-Length": "8" | ||
| }, | ||
| "body": ["no need."] | ||
| }, | ||
| "expected": { | ||
| "http_code": 200 | ||
| }, | ||
| "rules": [ | ||
| "SecRuleEngine On", | ||
| "SecAction \"id:1,phase:1,nolog,pass,setvar:'tx.other=1'\"", | ||
| "SecRule REQUEST_URI \"@contains /test\" \"id:2,phase:2,deny,status:403,log,chain\"", | ||
| " SecRule TX:/^score_/ \"@eq 1\" \"t:none\"" | ||
| ] | ||
| } | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — this is accurate. Adding a virtual to Collection grows the
vtable, so a Collection backend built out-of-tree against an older
collection.h would be binary-incompatible with a newer libmodsecurity.
A few points on how I'd weigh it:
m_collections.m_tx_collectionis typed asCollection*, so dispatching through the base class is the natural fit. Thealternatives suggested here - a separate
dynamic_cast-able interface, or anon-virtual helper keyed on the concrete type - add indirection at the call
site for what is otherwise a drop-in. The method is also non-pure (it ships
a default that delegates to the existing string overload), so neither in-tree
nor out-of-tree backends are required to implement anything.
libtool -version-info(MSC_VERSION_INFO = current:revision:age, derived from the release version),
so the mechanism to flag an ABI change is in place. The remaining question is
a release-policy one rather than a code one.
@airween - this is your call: are you comfortable shipping this as an
ABI-affecting change with the corresponding version-info bump, or would you
prefer I keep the base vtable untouched and confine the optimization to the
concrete InMemoryPerProcess path instead? I can also move the new declaration
to the end of the class to minimize in-tree vtable churn if that helps. Happy
to take whichever direction you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recently I've fixed something where the situation was the same like this. Then I solved it with the suggested method and created a helper.
Actually, I've been working on V4, where the collections will be replaced, because there are several issues. Those changes completely brakes the current API (this is why we need to increase the version).
If you deal with it, you can wait with this PR for V4. Otherwise, it would be nice to avoid to break the ABI.
Note: there will be one more release from v3.0 at least, because we have some security issues, so if you solve this, then it can be a part of the current code. I hope I can do that at the end of this month at last.
(Expected release of V4 is in autumn, or later...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworked to avoid the ABI break. The introduced virtual methode in
collection.his removed, and the optimization is inInMemoryPerProcessonly using adynamic_cast.