Skip to content

fix: cap delayed response-body buffering to bound worker memory - #21

Merged
fzipi merged 3 commits into
corazawaf:mainfrom
ppomes:fix/cap-delayed-body
Jul 31, 2026
Merged

fix: cap delayed response-body buffering to bound worker memory#21
fzipi merged 3 commits into
corazawaf:mainfrom
ppomes:fix/cap-delayed-body

Conversation

@ppomes

@ppomes ppomes commented Jul 30, 2026

Copy link
Copy Markdown
Member

Ports coraza-nginx #71. While the header delay holds a response for phase-4 inspection, the output filter reads every body bucket into memory before forwarding — a large download or a long stream would buffer without limit.

The filter now tracks the buffered bytes and, once they pass CORAZA_MAX_DELAYED_BODY (1 MiB, build-overridable), stops delaying: it flushes the headers + everything buffered so far and lets the rest stream. A later phase-4 match can then no longer render a clean error page (headers are on the wire) — the same trade-off the SSE and 101 paths accept.

The check is per-bucket in the read loop, not just on the pending brigade, so it also bounds a single response drained in one filter pass (e.g. a CGI/streamed source), not only multi-brigade proxied responses.

Test (paced 4 MiB CGI, both MPMs): the response arrives intact (200, full 4 MiB) and the cap logs an early flush; without the fix that flush never happens. Suite 144/0.

Summary by CodeRabbit

  • New Features

    • Added a bulk-response endpoint for testing and validating large downloads.
  • Bug Fixes

    • Prevented excessive memory use when inspecting delayed response bodies by enforcing a 1 MiB buffering limit.
    • Automatically forwards responses when the limit is exceeded, allowing large downloads to complete successfully.
  • Tests

    • Added coverage for delayed 4 MiB responses, complete byte delivery, early header flushing, container logs, and download-size validation.

@ppomes
ppomes requested a review from fzipi July 30, 2026 17:49

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/mod_coraza_filter_out.c`:
- Around line 205-227: Update the cap-exceeded branch in
coraza_append_response_body to remove the output filter with
ap_remove_output_filter(f) before flushing and returning. Preserve the existing
pending-brigade prepend and downstream pass, ensuring subsequent response
brigades bypass Coraza inspection and continue streaming normally.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a014a005-08cd-4f3c-bb5a-da06626971fa

📥 Commits

Reviewing files that changed from the base of the PR and between 9edb2ab and 9685f71.

📒 Files selected for processing (5)
  • Dockerfile
  • src/mod_coraza.h
  • src/mod_coraza_filter_out.c
  • test.sh
  • tests/cgi-bin/bulk

Comment thread src/mod_coraza_filter_out.c
@corazawaf corazawaf deleted a comment from coderabbitai Bot Jul 30, 2026
@ppomes

ppomes commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b89da713-3bb7-4757-ab18-8d529b989151

📥 Commits

Reviewing files that changed from the base of the PR and between d8e01e7 and 36ecb79.

📒 Files selected for processing (1)
  • test.sh
🚧 Files skipped from review as they are similar to previous changes (1)
  • test.sh

📝 Walkthrough

Walkthrough

Adds a configurable 1 MiB cap for delayed response buffering. When the cap is exceeded, buffered data is flushed and inspection stops. A paced 4 MiB CGI endpoint, Docker wiring, and integration checks validate delivery and early header flushing.

Changes

Delayed response cap

Layer / File(s) Summary
Bound delayed-body buffering
src/mod_coraza.h, src/mod_coraza_filter_out.c
Adds configurable buffering limits and byte tracking. When the cap is exceeded, the filter forwards buffered output downstream.
Provide the delayed CGI endpoint
tests/cgi-bin/bulk, Dockerfile
Adds a paced 4 MiB CGI response and exposes it through /bulk-delayed.
Validate body delivery and flush logging
test.sh
Verifies HTTP 200 responses, exact 4 MiB delivery, and early delayed-response header flushing.

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

Possibly related PRs

Suggested reviewers: fzipi

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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: capping delayed response-body buffering to limit worker memory usage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test.sh`:
- Around line 338-353: Update check_size to capture curl’s exit status and HTTP
response status alongside size_download, and only report PASS when curl
succeeds, the HTTP status is successful, and the downloaded size matches
expected. Apply the same validation to the separate size assertion around the
later response measurement, ensuring failures increment FAIL and include the
relevant status/error details.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d487b5f7-10b2-4294-954a-61a83183f0f5

📥 Commits

Reviewing files that changed from the base of the PR and between 9edb2ab and d8e01e7.

📒 Files selected for processing (5)
  • Dockerfile
  • src/mod_coraza.h
  • src/mod_coraza_filter_out.c
  • test.sh
  • tests/cgi-bin/bulk

Comment thread test.sh
@fzipi
fzipi merged commit c2d5af7 into corazawaf:main Jul 31, 2026
3 checks passed
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.

2 participants