Skip to content

Reuse already initialized rule set for each location - #291

Open
k1k wants to merge 2 commits into
owasp-modsecurity:masterfrom
k1k:reuse_rules_across_locations
Open

k1k wants to merge 2 commits into
owasp-modsecurity:masterfrom
k1k:reuse_rules_across_locations

Conversation

@k1k

@k1k k1k commented Nov 16, 2022

Copy link
Copy Markdown

Only merge rule-sets when there are extra rules in the location, otherwise just re-use the rule-set of the parent

Only merge rulesets when there are extra rules in the location,
otherwise just re-use the ruleset of the parent
@airween

airween commented Apr 16, 2024

Copy link
Copy Markdown
Member

Hi @k1k, we've added a CI workflow to this repository. Could you pick up the modifications and send your patch again? It would be fine to run those tests - thanks!

@HanadaLee

Copy link
Copy Markdown

The lazy/shared ruleset approach is reasonable, but it changes the behavior of modsecurity on without any loaded rules. Both parent and child rulesets can remain NULL, ngx_http_modsecurity_create_ctx() then returns NULL, and the rewrite handler turns that into HTTP 500. Previously every location had a valid empty RulesSet.

Please preserve the empty-ruleset behavior for enabled configurations. Also, the DDEBUG dumps currently run before the new NULL checks, while msc_rules_dump() dereferences its argument unconditionally, causing debug builds to crash during configuration merging.

@sonarqubecloud

Copy link
Copy Markdown

@tomsommer

Copy link
Copy Markdown
Contributor

Confirming the issue raised in the review: with this change both p->rules_set and c->rules_set stay NULL for an enabled configuration that has no modsecurity_rules* directive anywhere (for example modsecurity on; at http level with rules meant to be added later, or a server that only inherits), ngx_http_modsecurity_create_ctx() then returns NULL and every request gets a 500. Before the change every location had a valid empty RulesSet.

I have a variant of this idea that keeps that behaviour: rule sets are created lazily in the three modsecurity_rules* handlers (one pool cleanup per created set), merge_conf() shares the parent's pointer when the child has no rules of its own and merges into the child's own set otherwise, and enabled configurations with no rules anywhere share a single empty set owned by the main conf. It comes with a Test::Nginx file covering inheritance, own+parent, nested locations, if/limit_except blocks, modsecurity off and the no-rules case, passes the existing suite on libmodsecurity 3.0.9 and 3.0.14, and survives repeated -s reload under valgrind.

Branch: https://github.com/tomsommer/ModSecurity-nginx/tree/perf/share-rules-set (single commit). Happy for you to cherry-pick it into this PR, or I can open it as a separate PR if the maintainers prefer. Prepared during a performance review of the connector done with Claude Fable 5.1.

@fzipi

fzipi commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Closing in favor of #399, which implements the same idea (share the parent RulesSet with blocks that declare no rules) and also keeps a valid empty RulesSet for enabled configurations with no rules, avoiding the 500 responses raised in the review here. Thanks for the contribution and for your patience.

@fzipi

fzipi commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@airween I don't have permission to close PRs in this repository. Could you close this one, per the comment above? Thanks.

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.

5 participants