Skip to content

Do not run the connector on subrequests - #394

Open
tomsommer wants to merge 1 commit into
owasp-modsecurity:masterfrom
tomsommer:fix/subrequest-filters
Open

tomsommer wants to merge 1 commit into
owasp-modsecurity:masterfrom
tomsommer:fix/subrequest-filters

Conversation

@tomsommer

@tomsommer tomsommer commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

what

  • The header filter, the body filter and the LOG-phase handler now return early for subrequests (r != r->main). No change to the access handler: nginx core already skips the ACCESS phase for subrequests.
  • New test tests/modsecurity-subrequest.t (auth_request + log_subrequest on): the main response's headers are inspected in phase 3, the auth subrequest's response is not, and exactly one audit record is written per request.

why

  • Subrequests share the parent request's pool, and ngx_http_modsecurity_get_module_ctx() falls back to walking the pool cleanup list when a request has no context of its own. A subrequest therefore gets the main request's transaction.
  • Two observable bugs follow:
    • With auth_request, the auth subrequest's response headers were fed to phase 3 as the main request's response headers and ctx->processed was set, so the real response headers were never inspected (the new test fails on master with the rule for the main response header absent and the rule for the subrequest's status present).
    • With log_subrequest on, the LOG phase also runs for the subrequest, so msc_process_logging() ran twice on one transaction: phase 5 and the audit log executed twice (the new test counts two audit records on master, one after the fix).
  • It also removes a per-subrequest transaction lookup and the copies that came with it.
  • Behaviour change, stated in the commit message: subrequests are never inspected, even if the subrequest's location has modsecurity on while the parent has it off. A subrequest produces no response of its own, so there is nothing for the WAF to protect there. The body-filter check is unreachable with the module's enforced filter order (the postpone filter forwards subrequest output as r->main) and is kept as a cheap safeguard should that order ever change.

references


Origin: this change comes from a performance review of the connector done with Claude Fable 5.1 (Anthropic). The patch and its test were verified by building the module against nginx master with libmodsecurity 3.0.14 (PCRE2) and, with upstream CI's flags (--without-pcre2 --with-http_v2_module --with-http_auth_request_module), against libmodsecurity 3.0.9 (PCRE1), then running the full tests/modsecurity*.t suite in both builds (16 files, 256 tests, all passing).

Summary by CodeRabbit

  • Bug Fixes

    • Improved subrequest handling so response bodies and headers are forwarded without duplicate security processing.
    • Prevented duplicate audit logging for subrequests, ensuring one audit record is generated for the main request.
  • Tests

    • Added coverage for authentication subrequests, response handling, and audit-log behavior.

Subrequests share the parent's pool, so the cleanup-list fallback in
ngx_http_modsecurity_get_module_ctx() returns the main request's
transaction for them.

With auth_request the auth subrequest's response headers were fed to
phase 3 as the "response headers" of the main request and
ctx->processed was set, so the real response headers were never
inspected.  With log_subrequest on, the subrequest also runs the LOG
phase, so msc_process_logging() was called on the main request's
transaction once for the subrequest and once for the main request:
phase 5 and the audit log ran twice for a single transaction.

Skip subrequests in the header filter, in the body filter and in the
log handler; the main request's own headers, body and logging phase
are still processed.  The body filter cannot currently see a
subrequest, because the module is ordered after the postpone filter
and that one forwards subrequest output as r->main, but the check is
kept so the module stays correct if the filter order ever changes.

No check is needed in the access handler: ngx_http_core_access_phase()
already skips the whole ACCESS phase for subrequests.

Behaviour change: subrequests are no longer inspected at all, even
when the parent location has modsecurity off and the subrequest's
location has it on.  A subrequest never produces a response of its
own, so there is nothing for the WAF to protect there.
@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 4d53d059-1e94-4b80-a4dc-fcb440aa191e

📥 Commits

Reviewing files that changed from the base of the PR and between 9eb44fd and 883d6ff.

📒 Files selected for processing (4)
  • src/ngx_http_modsecurity_body_filter.c
  • src/ngx_http_modsecurity_header_filter.c
  • src/ngx_http_modsecurity_log.c
  • tests/modsecurity-subrequest.t

Included review availability: Your plan provides up to 4 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Subrequests now bypass ModSecurity body filtering, header inspection, and audit logging. Main requests retain the existing processing path. A new auth_request test verifies response handling and audit record counts.

Changes

Subrequest bypass

Layer / File(s) Summary
Subrequest processing guards
src/ngx_http_modsecurity_body_filter.c, src/ngx_http_modsecurity_header_filter.c, src/ngx_http_modsecurity_log.c
Subrequests forward body and header processing to nginx and return from the logging handler without running ModSecurity processing.
Subrequest behavior test
tests/modsecurity-subrequest.t
The auth_request test verifies the main response, excludes the subrequest response rule, and checks for one audit record.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: the connector no longer processes subrequests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

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