Skip to content

intercept: fix replace_status being silently dropped - #7810

Merged
mholt merged 4 commits into
caddyserver:masterfrom
oksusucha:fix/intercept-replace-status
Jun 12, 2026
Merged

mholt merged 4 commits into
caddyserver:masterfrom
oksusucha:fix/intercept-replace-status

Conversation

@oksusucha

Copy link
Copy Markdown
Contributor

Fixes #7805

Problem

replace_status in intercept has no effect. The status code replacement never reaches the client — it has been silently a no-op since the directive was introduced.

The shouldBuffer closure correctly identifies the matching response and records the replacement status, but then returns false. This puts the response into stream mode, which writes the original status to the wire immediately. There is no second chance to substitute it.

On top of that, the WriteHeader method that was supposed to apply the substitution uses a value receiver, so it operates on a stale copy of the struct that never sees the replacement status set by the closure.

Fix

  • shouldBuffer now returns true when replace_status matches, so the response gets buffered.
  • After next.ServeHTTP returns, a new branch handles the replace_status-only case (no routes): it writes the replacement status code and flushes the buffered body to the client.
  • Removed the dead WriteHeader method and resolved TODO comment since the substitution now happens post-ServeHTTP.

Tests

Integration tests added:

  • TestInterceptReplaceStatusWithMatcher — upstream 500, expect client sees 200
  • TestInterceptReplaceStatusWithoutMatcher — upstream 403, expect 200
  • TestInterceptReplaceStatusNotMatched — upstream 200 with a 5xx matcher, passes through unchanged

Manual verification using the exact config from the issue:

$ curl -sk -o /tmp/body -w "status=%{http_code}\n" https://localhost:9443/

Before (master): status=500. After (this branch): status=200. Body boom unchanged in both cases.

Existing TestIntercept (handle_response path) passes. Full ./modules/caddyhttp/... suite passes.

Notes

  • replace_status now buffers the full body. This is the same trade-off handle_response makes, and is required because HTTP needs the status line before the body. Typical use case is small error pages so this should be fine in practice.
  • 1xx responses (including WebSocket 101) are unaffected — they bypass shouldBuffer and go directly to the wire.
  • If replace_status and handle_response share the same matcher, replace_status wins due to parse ordering. This was pre-existing behavior.

Assistance Disclosure

AI (Claude) was used to help analyze the root cause and draft the initial test cases. I reviewed the fix, verified every code path, and confirmed the before/after behavior manually against master and the patched binary.

oksusucha added 3 commits June 8, 2026 13:40
Add integration tests that verify replace_status actually substitutes
the HTTP status code sent to the client. Currently these tests fail
because replace_status is silently a no-op due to value-receiver
boxing and shouldBuffer returning false.

Tests added:
- TestInterceptReplaceStatusWithMatcher: 500 -> 200 with @err matcher
- TestInterceptReplaceStatusWithoutMatcher: 403 -> 200 unconditionally
- TestInterceptReplaceStatusNotMatched: 200 passes through unchanged
Fix caddyserver#7805: replace_status was silently a no-op because:
1. shouldBuffer returned false when a replacement status was set,
   causing the original status to be streamed directly to the wire
2. The value-receiver WriteHeader method operated on a stale copy

The fix:
- shouldBuffer now returns true when replace_status matches, so
  the response is buffered instead of streamed
- After next.ServeHTTP returns, if routes are nil (replace_status
  only), write the substituted status and buffered body to the client

The interceptedResponseHandler.WriteHeader substitution branch is no
longer needed for this path since substitution happens post-ServeHTTP.
The value-receiver WriteHeader on interceptedResponseHandler was
unreachable dead code — the substitution is now handled post-ServeHTTP
via buffering. Remove it along with the TODO comment that noted
status code replacement was unfinished.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Jun 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mohammed90

Copy link
Copy Markdown
Member

@oksusucha, can you please sign the CLA?

@oksusucha

Copy link
Copy Markdown
Contributor Author

@mohammed90

Done, just signed it.

@mholt
mholt merged commit 16235cc into caddyserver:master Jun 12, 2026
24 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.

intercept: replace_status is silently dropped — WriteHeader checks state before the recorder callback can set it

5 participants