caddyhttp: Fix edgecase with HTTP->HTTPS logic #4243
Merged
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.
Followup to #4033, see https://caddy.community/t/how-to-set-priority-in-caddyfile/13002/8 for context
As it turns out, my earlier change for HTTP->HTTPS redirect was not enough, because it left one edgecase.
If none of the routes in the HTTP server had host matchers, then it would return the index of the last route, even if the only route was a catch-all (which is defined as having no host matcher).
This means that a config like this would not work correctly, and
Foo
would be returned on requests tohttp://bar.localhost/
What we want is for
http://bar.localhost/
to return a redirect tohttps://bar.localhost/
instead (and this can be overridden by usingauto_https disable_redirects
if you want it otherwise).So the fix I went with is to keep track of whether we actually found a host matcher during the loop, and if we didn't, then return
0
as the index to insert the redirect routes, i.e. before any user-defined catch-all.P.S. Integration tests don't actually run in CI because they're flaky and slow, but I ran them all again with my change and the new test to cover this case, and it passes.