Skip to content

http: Sort auto-HTTPS redirect routes by host specificity (fix #7390)#7502

Merged
francislavoie merged 1 commit intocaddyserver:masterfrom
pauloappbr:fix/7390-autohttps-redirect-sorting
Feb 22, 2026
Merged

http: Sort auto-HTTPS redirect routes by host specificity (fix #7390)#7502
francislavoie merged 1 commit intocaddyserver:masterfrom
pauloappbr:fix/7390-autohttps-redirect-sorting

Conversation

@pauloappbr
Copy link
Contributor

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 to srv.Routes before 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.SortFunc immediately before injecting the redirect routes into the server. The sorting logic enforces strict host specificity:

  • Exact matches take precedence over wildcards.
  • Wildcards take precedence over catch-alls (empty host).
  • If both are exact or wildcards, the longer string wins.
  • Ties are broken alphabetically to ensure deterministic generation.

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.1 would sporadically redirect to :10443. Now it consistently redirects to the implicit :443.

*.domain.com:10443 {
	respond * "10443"
}

dev.domain.com {
	respond * "80443"
}

@pauloappbr pauloappbr force-pushed the fix/7390-autohttps-redirect-sorting branch from 2359688 to 4fd42c2 Compare February 20, 2026 20:22
@francislavoie francislavoie added the bug 🐞 Something isn't working label Feb 20, 2026
@francislavoie francislavoie added this to the v2.11.2 milestone Feb 20, 2026
@francislavoie francislavoie merged commit 76b198f into caddyserver:master Feb 22, 2026
29 checks passed
@github-actions github-actions bot mentioned this pull request Mar 6, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected automatic redirection

2 participants