http: Sort auto-HTTPS redirect routes by host specificity (fix #7390)#7502
Merged
francislavoie merged 1 commit intocaddyserver:masterfrom Feb 22, 2026
Merged
Conversation
2359688 to
4fd42c2
Compare
francislavoie
approved these changes
Feb 20, 2026
4 tasks
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.
Assistance Disclosure
I used an AI assistant (Gemini) to help analyze the Caddyfile routing issue, identify the non-deterministic map iteration, and draft the initial slices.SortFunc sorting logic. I manually reviewed the code, integrated it, verified the fix locally by building from source, and ran the Caddy test suite to ensure correctness and no regressions.
Description
This PR fixes an issue where automatic HTTP->HTTPS redirects were being misrouted when a wildcard domain with an explicit port and an exact host domain coexisted in the configuration.
The Problem
During
automaticHTTPSPhase1, domains are grouped by address using a map (domainsByAddr). When iterating over this map to build and append redirect routes (redirServersLoop), the order of the generated routes was non-deterministic. If a wildcard redirect route was appended tosrv.Routesbefore an exact match route, the Caddy router would evaluate the wildcard first, resulting in incorrect port redirection (e.g., returning HTTP 308 to port :10443 instead of the standard :443 for the exact host).The Solution
Added a
slices.SortFuncimmediately before injecting the redirect routes into the server. The sorting logic enforces strict host specificity:This ensures the HTTP router evaluates the most specific redirect rules first, regardless of map iteration order.
Related Issue
Fixes #7390
How to test
A Caddyfile with the following configuration can reproduce the issue. Before this PR,
curl -I -H "Host: dev.domain.com" http://127.0.0.1would sporadically redirect to :10443. Now it consistently redirects to the implicit :443.