Skip to content

fix(modsecurity): avoid ctl:ruleEngine=Off cancelling allow on whitelist rule 9800000 (#4248) - #4258

Open
Mohit20057 wants to merge 1 commit into
bunkerity:devfrom
Mohit20057:fix-4248-whitelist-modsecurity-bypass
Open

Mohit20057 wants to merge 1 commit into
bunkerity:devfrom
Mohit20057:fix-4248-whitelist-modsecurity-bypass

Conversation

@Mohit20057

Copy link
Copy Markdown
Contributor

Summary

In libModSecurity v3 (RuleWithActions::executeActionsAfterFullMatch), non-disruptive actions (ctl:ruleEngine=Off) execute before disruptive actions (allow), and executeAction skips disruptive actions whenever SecRuleEngine is not On. Combining allow with ctl:ruleEngine=Off on rule 9800000 therefore disabled the engine before allow could run, leaving the remaining phase 1 CRS rules (such as 920420) running and logging for whitelisted clients.

This PR updates both src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec and src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec:

  • Keep id:9800000 as phase:1,allow,nolog so allow executes in SecRuleEngine On mode and skips all remaining phase 1 rules as well as phases 2–4.
  • Add fallback rule id:9800001 (phase:1,pass,nolog,ctl:ruleEngine=Off) right after 9800000: skipped by allow when SecRuleEngine is On, and executed when SecRuleEngine is DetectionOnly (where allow does not fire) so phases 2–5 remain bypassed.
  • Add unit tests in tests/unit/modsecurity/test_whitelist_bypass_rule.py covering both templates, DetectionOnly, USE_WHITELIST=no, and USE_MODSECURITY_CRS=no.
  • Add phase 1 bypass checks (920420 via Content-Type: application/octet-stream) in tests/core/modsecurity.yml for per-service and global CRS modes.

Related issue

Fixes #4248

Affected area

core, plugins

Validation

  • pytest tests/unit/modsecurity/test_whitelist_bypass_rule.py tests/unit/gen/test_modsec_rule_id_ledger.py -v β€” 23 passed (5 template rendering tests + 18 rule-ID ledger tests; template tests run on Windows with in-process POSIX stubs for fcntl/pwd/os and ThreadPoolExecutor for Templator)
  • black --check --line-length 160 tests/unit/modsecurity/test_whitelist_bypass_rule.py β€” passed (1 file would be left unchanged)
  • flake8 --max-line-length=160 --ignore=E266,E402,E501,E722,W503 tests/unit/modsecurity/test_whitelist_bypass_rule.py β€” passed
  • codespell tests/unit/modsecurity/test_whitelist_bypass_rule.py tests/core/modsecurity.yml β€” passed
  • prettier --check tests/core/modsecurity.yml β€” passed (All matched files use Prettier code style!)
  • Validated all 14 actions in tests/core/modsecurity.yml against tests/models/action.py (ActionData)
  • Note: live ./tests/scripts/test.sh Docker core dev modsecurity was not run locally (Docker Desktop not running); relies on CI integration matrix

Documentation and changelog

No documentation updates needed β€” no user-facing settings, defaults, or behaviors changed beyond fixing the documented whitelist bypass for ModSecurity phase 1.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change β†’

No actionable comments were generated in the recent review.

Component summary

  • src/common: Two ModSecurity whitelist templates change:
    • confs/server-http/modsecurity-rules.conf.modsec (per-service, lines ~213–218, inside USE_WHITELIST == "yes").
    • confs/http/modsecurity-rules-global-crs.conf.modsec (global CRS, lines ~163–168).
    • Rule 9800000 becomes phase:1,allow,nolog with no ctl.
    • New rule 9800001 becomes phase:1,pass,nolog,ctl:ruleEngine=Off, a fallback for DetectionOnly.
  • tests: tests/unit/modsecurity/test_whitelist_bypass_rule.py (+106) adds rendering tests. tests/core/modsecurity.yml (+29) adds three phase 1 whitelist cases.
  • src (api, ui, bw, linux), .github, docs, examples: not changed.

Security impact

  • With the engine On, whitelisted clients skip the remaining phase 1 rules, as issue #4248 requests.
  • With DetectionOnly, allow does not fire. Rule 9800001 disables the engine, so phases 2–5 are bypassed. Phase 1 rules after 9800001 still run. This is a partial fix for #4248 in that mode.

User-visible behaviour

  • Whitelisted clients produce no phase 1 ModSecurity log noise when the engine is On.

Configuration, schema, packaging

  • No configuration, setting or schema change. No packaging or deployment impact.

Documentation and tests

  • No documentation update.
  • Unit and core test cases added. The author reports 23 passing unit tests locally.
  • The live tests/scripts/test.sh Docker core dev modsecurity run was not performed locally. It depends on CI.

Walkthrough

The whitelist rule is split into an allow rule and a separate rule-engine fallback in the per-service and global CRS configurations. Integration and unit tests cover request outcomes and rendered rules.

Changes

Whitelist bypass

Layer / File(s) Summary
Separate whitelist actions
src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec:213-217, src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec:163-167
Both configurations now use rule 9800000 with allow and a separate rule 9800001 with pass and ctl:ruleEngine=Off. Comments describe the fallback for DetectionOnly.
Whitelist bypass validation
tests/core/modsecurity.yml:121-149, tests/unit/modsecurity/test_whitelist_bypass_rule.py:1-106
Integration tests check that a non-whitelisted request is blocked and that whitelisted requests bypass CRS rule 920420, including with global CRS enabled. Unit tests check rendered rule contents, ordering, and configuration conditions.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Severity of issue fixed: Low

Assessment against linked issues

Objective Addressed Explanation
Skip remaining phase 1 CRS rules for whitelisted clients [#4248] βœ…

Possibly related issues

Merge Risk: Minimal Β· up to 54bcb

Whitelisted clients now bypass the remaining phase 1 rules when the engine is On. A fallback rule covers DetectionOnly. No concrete merge-blocking risk was established, though the live integration test relies on CI.

Pre-merge checks: Passed 2

Check name Status Explanation
Title check Passed The title follows the Conventional Commits format (fix(modsecurity):) and names the main change: whitelist rule 9800000 no longer has ctl:ruleEngine=Off cancelling allow. It is 93 characters, which is…
Description check Passed The description explains the root cause in libModSecurity v3, lists the changed ModSecurity templates, the new fallback rule 9800001 for DetectionOnly, the unit and integration tests added, and the va…
Full details: Title check

Explanation

The title follows the Conventional Commits format (fix(modsecurity):) and names the main change: whitelist rule 9800000 no longer has ctl:ruleEngine=Off cancelling allow. It is 93 characters, which is over the roughly 80-character guideline. The guidance says a scoped Conventional Commits title a few characters over is fine, so this does not warrant a warning.

Full details: Description check

Explanation

The description explains the root cause in libModSecurity v3, lists the changed ModSecurity templates, the new fallback rule 9800001 for DetectionOnly, the unit and integration tests added, and the validation run. It is directly related to the changeset.

  • Autofix Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A whitelist meets phase one at the gate
allow lets matching requests pass
A fallback stands beside the rule
Tests watch the rendered lines
CRS stays quiet for whitelisted traffic

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7d30230 and 54bcb75.

Files selected for processing: 4

  • src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec
  • src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec
  • tests/core/modsecurity.yml
  • tests/unit/modsecurity/test_whitelist_bypass_rule.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Recent review details

Context from checks skipped due to timeout: 2

  • GitHub Check: CodeRabbit
  • GitHub Check: pullfrog
Additional context used

Code guidelines: 3

3 repository guideline sources
Path-based instructions: 9

ModSecurity rules and config must preserve protection without unnecessary false positives: Keep the OWASP Core Rule Set wiring intact unless there is a clearly justified change.

CodeRabbit configuration file

Files:

  • src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec
  • src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec
This tree contains Jinja2-templated NGINX configuration rendered by `src/common/gen/Templator.py`.

CodeRabbit configuration file

Files:

  • src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec
  • src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec
Tests should verify observable behaviour, not incidental implementation details: Prefer Arrange-Act-Assert structure and descriptive test names.

CodeRabbit configuration file

Files:

  • tests/unit/modsecurity/test_whitelist_bypass_rule.py
YAML files (CI, Compose, Kubernetes, app config, docs config) must be structured and reproducible: Use consistent indentation, stable key ordering where the project already has one, and avoid duplicate keys.

CodeRabbit configuration file

Files:

  • tests/core/modsecurity.yml
Follow BunkerWeb's Python standards and security posture: Use snake_case for functions and variables and PascalCase for classes.

CodeRabbit configuration file

Files:

  • tests/unit/modsecurity/test_whitelist_bypass_rule.py
Configuration for BunkerWeb should be driven via environment variables (settings).

CodeRabbit inference engine

Files:

  • tests/core/modsecurity.yml
Core modules in src/common/core/*/ must contain plugin.json, jobs/ folder with Python scripts, Lua code for request-time processing, and confs/ folder with NGINX templates

CodeRabbit inference engine

Files:

  • src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec
  • src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec
Python code must follow snake_case naming for modules and functions, PascalCase for classes, and be formatted with Black at 160 characters per line Python linting with flake8 must ignore error codes: E266, E402, E501, E722, W503

CodeRabbit inference engine

Files:

  • tests/unit/modsecurity/test_whitelist_bypass_rule.py
Python code must use Black for formatting with 160 character line lengths Python code must conform to Flake8 linting with ignores defined in `.pre-commit-config.yaml` Python modules and functions must use snake_case naming convention Python...

CodeRabbit inference engine

Files:

  • tests/unit/modsecurity/test_whitelist_bypass_rule.py
Learnings: 1

Learning: 2026-05-19T13:30:14.616Z

Learnt from: CR
Repo: bunkerity/bunkerweb PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-05-19T13:30:14.616Z
Learning: System tests, fixtures, and helper scripts must be consolidated in `tests/`

Applied to files:

  • tests/core/modsecurity.yml
  • tests/unit/modsecurity/test_whitelist_bypass_rule.py
ast-grep (0.51.3)

tests/unit/modsecurity/test_whitelist_bypass_rule.py

[error] 75-75: Avoid HTML built in strings
Context: render(MULTISITE="no", SERVER_NAME="www.example.com", USE_MODSECURITY="yes", USE_WHITELIST="yes")
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').

(html-string-from-parameters)

[error] 83-83: Avoid HTML built in strings
Context: render(MULTISITE="no", SERVER_NAME="www.example.com", USE_MODSECURITY="yes", SECURITY_MODE="detect", USE_WHITELIST="yes")
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').

(html-string-from-parameters)

[error] 89-89: Avoid HTML built in strings
Context: render(MULTISITE="no", SERVER_NAME="www.example.com", USE_MODSECURITY="yes", USE_WHITELIST="no")
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').

(html-string-from-parameters)

[error] 94-94: Avoid HTML built in strings
Context: render(MULTISITE="no", SERVER_NAME="www.example.com", USE_MODSECURITY="yes", USE_MODSECURITY_CRS="no", USE_WHITELIST="yes")
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').

(html-string-from-parameters)

[error] 101-101: Avoid HTML built in strings
Context: render(MULTISITE="yes", SERVER_NAME="www.example.com", USE_MODSECURITY="yes", USE_MODSECURITY_GLOBAL_CRS="yes")
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').

(html-string-from-parameters)

Additional comments: 2

src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec: 2

217-217: Functional Correctness

I could not verify the claim that rule 9800001 leaves the remaining phase-1 rules running in DetectionOnly. The decisive evidence is the libModSecurity 3.0.17 phase-loop and ctl:ruleEngine=Off handling. The checkout contains only the version pin, and the upstream source read returned 404, so this premise is undecided.

216-216: Functional Correctness

The concern that whitelisted requests can reach phase-5 rules under SecRuleEngine On cannot be decided from this checkout. The rule ordering is clear: 9800000 runs allow on a match and skips 9800001, so the engine is not switched off. Whether allow also skips phase 5 depends on the pinned libModSecurity implementation. That source is not in the checkout, and no version pin was located there. The PR description, which names phases 2–4 only, is not evidence of library behaviour.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ℹ️ No critical issues β€” one scope note on the DetectionOnly path.

Approach: sound β€” splitting the kill switch lets allow run while the engine is On, and the separate ctl:ruleEngine=Off fallback keeps phases 2–5 bypassed in DetectionOnly. The rule order is asserted by the render test.

Reviewed changes

  • Per-service kill switch: 9800000 now carries only allow,nolog; new 9800001 carries pass,nolog,ctl:ruleEngine=Off for the DetectionOnly path, both placed before the CRS include.
  • Global-CRS kill switch: the same split pair added to the shared http rules file, ahead of the CRS include.
  • Unit tests: render the per-service and global templates with USE_WHITELIST/USE_MODSECURITY_CRS/SECURITY_MODE variants and assert the rendered rule lines and their relative order.
  • Integration spec: adds a non-whitelisted baseline (403 + [id "920420"]) and per-service/global whitelisted checks (200 + no 920420) using Content-Type: application/octet-stream.

Verification: I confirmed the ModSecurity v3 premise in upstream source (executeActionsAfterFullMatch runs ctl before the disruptive action, executeAction skips disruptive actions when the engine is not On, and FromNowOnAllowType skips all phases except logging). Rendering both templates through the shipped Configurator/Templator reproduces all five unit-test assertions, and python3 -m py_compile on the new test is clean. No Docker integration run was performed here.

ℹ️ #4248's phase-1 residue remains in DetectionOnly

In DetectionOnly the disruptive allow does not run, so 9800001 switches the engine off β€” but that does not stop the rest of phase 1, because RulesSet::evaluate never re-checks the engine state inside a phase. Whitelisted clients still generate 920420-style log entries in detect mode, which is the symptom named in #4248. This is not a regression (the old combined rule behaved the same way), and the PR body accurately scopes the fallback to phases 2–5, but closing #4248 will imply the field no longer sees these lines.

Technical details
# DetectionOnly keeps evaluating the rest of phase 1

## Affected sites
- `src/common/core/modsecurity/confs/server-http/modsecurity-rules.conf.modsec:217`
- `src/common/core/modsecurity/confs/http/modsecurity-rules-global-crs.conf.modsec:167`
- `tests/unit/modsecurity/test_whitelist_bypass_rule.py:82-86` β€” asserts the rendered fallback text only

## Failure mechanism
- With `SecRuleEngine DetectionOnly`, `RuleWithActions::executeAction` skips the disruptive `allow` on `9800000`
  (`src/rule_with_actions.cc`), so `9800001` runs and sets `transaction->m_secRuleEngine = Disabled`
  (`src/actions/ctl/rule_engine.cc`).
- `RulesSet::evaluate` checks `m_allowType` and engine state only at phase entry, never between rules in the same
  phase (`src/rules_set.cc`). The remaining phase-1 CRS rules therefore still match and call `performLogging`.
- Later phases do early-return on `getRuleEngineState() == Disabled`, so only phases 2–5 are bypassed by the fallback.

## Required outcome
- Either the `DetectionOnly` path also stops the rest of phase 1 for whitelisted clients, or the limitation is stated
  where #4248 is closed so "whitelisted clients skip all ModSecurity rules" is not claimed for detect mode.

## Suggested approach (optional)
- Add a `SECURITY_MODE=detect` action to `tests/core/modsecurity.yml` with `USE_WHITELIST=yes` and
  `WHITELIST_IP=0.0.0.0/0` asserting `not_log: '[id "920420"]'`, so the `9800001` branch is exercised at runtime
  rather than only through rendered text.

Pullfrog  | Fix it βž” | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

This branch has not been deployed

No deployments
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.

1 participant