Skip to content

Do not check content-type every time when appending response body - #3784

Open
JakubOnderka wants to merge 1 commit into
owasp-modsecurity:v3/masterfrom
JakubOnderka:response-body-perf
Open

JakubOnderka wants to merge 1 commit into
owasp-modsecurity:v3/masterfrom
JakubOnderka:response-body-perf

Conversation

@JakubOnderka

@JakubOnderka JakubOnderka commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

what

  • Cache whether the response body should be inspected in the transaction state.
  • Determine the inspection state when the response Content-Type header is received instead of repeatedly looking it up while processing body chunks.
  • Exclude diagnostic construction of the configured media-type list when logging is disabled.

why

  • Avoid repeated lowercasing and std::set lookups for every response-body chunk.
  • Reject unneeded response data before buffering it, reducing processing overhead for response types that are not inspected.

Summary by CodeRabbit

  • Bug Fixes
    • Response-body MIME filtering now correctly handles Content-Type values that include parameters such as a character set.
    • Response bodies with content types excluded by the configured MIME list are skipped during inspection and buffering.
    • Response-body inspection continues when no MIME filter is configured, including when the response has no Content-Type header.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Transaction now tracks whether response-body inspection is enabled. It sets this state from the configured MIME types and uses it to control response-body processing and buffering. Regression fixtures cover matching content types and the absence of a MIME filter.

Changes: Response body MIME inspection

Layer / File(s) Summary
Set the response-body inspection flag
headers/modsecurity/transaction.h, src/transaction.cc
Transaction adds and initializes an inspection flag. When a Content-Type header arrives, the code removes the value after the first semicolon and checks the lowercase type against the configured MIME types.
Gate response-body handling and verify MIME cases
src/transaction.cc, test/test-cases/regression/config-response_type.json
processResponseBody and appendResponseBody return early when the flag is false. Regression fixtures cover a parameterized Content-Type, a missing Content-Type with no MIME filter, and the existing MIME filter cases.

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

Change: Refactor

Possibly related PRs

Suggested labels: 3.x

Suggested reviewers: airween, zimmerle

Merge Risk: Minimal · up to 9dac0

This change makes response-body handling cheaper by deciding once, when the Content-Type header arrives, whether the body needs inspection. Matching behavior is preserved, and new regression fixtures cover matching types, Content-Type parameters, and the no-filter case. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the content-type check for response body appends is no longer repeated on each append. The change caches the inspection decision in transaction state when the Content-…
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: Title check

Explanation

The title names the main change: the content-type check for response body appends is no longer repeated on each append. The change caches the inspection decision in transaction state when the Content-Type header arrives, which matches the PR objectives and the summarized code changes in src/transaction.cc.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0d571d7d-f6d9-4443-a745-b38d0a220bfa
Commits

Reviewing files that changed from the base of the PR and between 475aece and 9dac088.

Files selected for processing: 3

  • headers/modsecurity/transaction.h
  • src/transaction.cc
  • test/test-cases/regression/config-response_type.json

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

@sonarqubecloud

Copy link
Copy Markdown

@airween airween added the 3.x Related to ModSecurity version 3.x label Nov 29, 2026

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

3.x Related to ModSecurity version 3.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants