Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions headers/modsecurity/collection/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ typedef struct Variable_t Variables;

#ifdef __cplusplus
namespace modsecurity {
namespace Utils {
class Regex;
}
namespace variables {
class KeyExclusions;
}
Expand Down Expand Up @@ -69,6 +72,17 @@ class Collection {
std::vector<const VariableValue *> *l,
variables::KeyExclusions &ke) = 0;

/*
* Resolve using a regular expression that was already compiled (e.g. the
* one held by a VariableRegex). This avoids recompiling - and JIT'ing - the
* same pattern on every transaction. The default implementation delegates
* to the string overload using the pattern text, so backends that do not
* override it keep their previous behaviour.
*/
virtual void resolveRegularExpression(Utils::Regex *r,
std::vector<const VariableValue *> *l,
variables::KeyExclusions &ke);

Copy link
Copy Markdown
Author

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:

  • I kept it virtual on purpose. m_collections.m_tx_collection is typed as
    Collection*, so dispatching through the base class is the natural fit. The
    alternatives suggested here - a separate dynamic_cast-able interface, or a
    non-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.
  • libmodsecurity is already SONAME-versioned via 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.

Copy link
Copy Markdown
Member

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...)

Copy link
Copy Markdown
Author

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.h is removed, and the optimization is in InMemoryPerProcess only using a dynamic_cast.

Comment thread
chweidling marked this conversation as resolved.
Outdated


/* storeOrUpdateFirst */
virtual bool storeOrUpdateFirst(const std::string &key,
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ UTILS = \


COLLECTION = \
collection/collection.cc \
collection/collections.cc \
collection/backend/collection_data.cc \
collection/backend/in_memory-per_process.cc \
Expand Down
10 changes: 9 additions & 1 deletion src/collection/backend/in_memory-per_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,22 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var,

void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) {
// Callers that do not hold a compiled regex (e.g. the compartment-prefixed
// overloads) still pay the compilation cost here.
Utils::Regex r(var, true);
resolveRegularExpression(&r, l, ke);
}


void InMemoryPerProcess::resolveRegularExpression(Utils::Regex *r,
std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) {
std::list<std::string> expiredVars;

{
const std::shared_lock lock(m_mutex); // read lock (shared access)

for (const auto& x : m_map) {
const auto ret = Utils::regex_search(x.first, r);
const auto ret = Utils::regex_search(x.first, *r);
if (ret <= 0) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions src/collection/backend/in_memory-per_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class InMemoryPerProcess :
void resolveRegularExpression(const std::string& var,
std::vector<const VariableValue *> *l,
variables::KeyExclusions &ke) override;
void resolveRegularExpression(Utils::Regex *r,
std::vector<const VariableValue *> *l,
variables::KeyExclusions &ke) override;

/* store */
virtual void store(const std::string &key, std::string &compartment,
Expand Down
39 changes: 39 additions & 0 deletions src/collection/collection.cc
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.
Comment thread
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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Concatenate this namespace with the nested one.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AZ6w4kNrmwt6SnoGNzNO&open=AZ6w4kNrmwt6SnoGNzNO&pullRequest=3573
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
7 changes: 2 additions & 5 deletions src/variables/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,14 @@ class Tx_NoDictElement : public Variable {
class Tx_DictElementRegexp : public VariableRegex {
public:
explicit Tx_DictElementRegexp(const std::string &dictElement)
: VariableRegex("TX", dictElement),
m_dictElement(dictElement) { }
: VariableRegex("TX", dictElement) { }

void evaluate(Transaction *t,
RuleWithActions *rule,
std::vector<const VariableValue *> *l) override {
t->m_collections.m_tx_collection->resolveRegularExpression(
m_dictElement, l, m_keyExclusion);
&m_r, l, m_keyExclusion);
}

std::string m_dictElement;
};


Expand Down
78 changes: 78 additions & 0 deletions test/test-cases/regression/variable-tx-regex-precompiled.json
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\""
]
}
]
Loading