Reject status-only response matchers in handle_response#7709
Reject status-only response matchers in handle_response#7709suryaanshah wants to merge 1 commit into
Conversation
| if len(args) == 2 { | ||
| return d.Errf("configuring 'handle_response' for status code replacement is no longer supported. Use 'replace_status' instead.") | ||
| if isStatusReplacementPattern(matcher, subroute) { | ||
| return d.Errf("use 'replace_status' instead of 'handle_response' for status changes") |
There was a problem hiding this comment.
Must use tabs
| return d.Errf("use 'replace_status' instead of 'handle_response' for status changes") | |
| return d.Errf("use 'replace_status' instead of 'handle_response' for status changes") |
|
I think this is wrong? It's not about it being a status matcher, it's about not having routes inside it. I think we should just change the condition to reject more than a single argument, and stop referencing |
|
@francislavoie if we do reject >1 arguments (which was a suggestion copilot gave me but I rejected it) gives no errors Hence I thought a different approach was needed. |
|
How can we verify if it doesn't have routes inside it according to you? This was one approach not ideal though I agree |
|
But... that's valid. Matching on 500 status is the whole point of it, so you can render a different error page or something. I think you misunderstood the issue. We used to have only See the examples in the docs https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#examples: |
|
So you're suggesting to
Is that accurate? PS. Thanks for quick responses, my first time contributing to this repo so pardon for misunderstanding |
Caddy PR #6529: Status Matcher Fix
Assistance Disclosure
I used GitHub Copilot to understand the issue and repository structure. It generated the code changes, which I verified for correctness.
Issue Summary
This pull request addresses Caddy issue #6529, where the configuration parser incorrectly enforced a two-argument check for
handle_statusinstead ofreplace_statuswhen replacing HTTP status codes. This failed for abstracted status replacements or multi-argument usage.Proposed Solution
The fix introduces a helper function that returns
trueiflen(m.StatusCode) > 0andlen(m.Headers) == 0, accurately detecting matchers dedicated solely to status codes.Key Logic
len(m.StatusCode) > 0confirms at least one status rule (e.g.,@bad status 500setsStatusCode = [500]).len(m.Headers) == 0ensures no additional header matching (e.g.,@bad status 500 header Foo barsetsHeaders = {"Foo": ["bar"]}, returningfalse).This catches pure status matchers like
@bad status 500used inhandle_response @bad { ... }.Accuracy Rationale
The check precisely answers: "Is this matcher only about status?" by verifying status rules exist without header interference. It aligns with Caddy's matcher syntax for directives like
reverse_proxyandhandle_response.Limitations
This simple heuristic covers status codes and headers only. Future
ResponseMatcherexpansions may require updates to the helper.