Repository navigation
intercept: fix replace_status being silently dropped - #7810
Merged
mholt merged 4 commits intoJun 12, 2026
Merged
Conversation
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.
dunglas
approved these changes
Jun 8, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Member
|
@oksusucha, can you please sign the CLA? |
Contributor
Author
|
Done, just signed it. |
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7805
Problem
replace_statusinintercepthas no effect. The status code replacement never reaches the client — it has been silently a no-op since the directive was introduced.The
shouldBufferclosure correctly identifies the matching response and records the replacement status, but then returnsfalse. 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
WriteHeadermethod 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
shouldBuffernow returnstruewhenreplace_statusmatches, so the response gets buffered.next.ServeHTTPreturns, a new branch handles thereplace_status-only case (no routes): it writes the replacement status code and flushes the buffered body to the client.WriteHeadermethod and resolvedTODOcomment since the substitution now happens post-ServeHTTP.Tests
Integration tests added:
TestInterceptReplaceStatusWithMatcher— upstream 500, expect client sees 200TestInterceptReplaceStatusWithoutMatcher— upstream 403, expect 200TestInterceptReplaceStatusNotMatched— upstream 200 with a 5xx matcher, passes through unchangedManual verification using the exact config from the issue:
Before (master):
status=500. After (this branch):status=200. Bodyboomunchanged in both cases.Existing
TestIntercept(handle_response path) passes. Full./modules/caddyhttp/...suite passes.Notes
replace_statusnow buffers the full body. This is the same trade-offhandle_responsemakes, 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.replace_statusandhandle_responseshare the same matcher,replace_statuswins 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.