Caddy upgrade http before basic auth prompt

1. The problem I’m having:

Consider the Caddyfile attached below. The problem is that if I go to http://foo.com, the website will serve me a prompt for basic authentication before upgrading my connection to https. Since this has the danger of sending credentials in plaintext, some browsers, like Safari, will refuse to offer the basic auth prompt http sites at all (only offering it to https sites). This can lead to some browsers, like Safari, entirely unable to access the web-page (unless they manually upgrade http to https):

Safari Can’t Connect to the Server
Safari can’t open the page “foo.com” because Safari can’t connect to the server “foo.com

The question is: how do I have caddy upgrade http to https before prompting for basic auth?

2. Error messages and/or full log output:

N/A

No errors, I suspect this is intended / expected behavior. I’m not reporting a bug, but asking how to change the behavior.

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

a. System environment:

Docker

b. Command:

docker compose up

c. Service/unit/compose file:

services:

  caddy:
    image: "caddy"
    container_name: "caddy"
    restart: unless-stopped
    ports:
      - 443:443
    volumes:
      - ./caddy/Caddyfile:/srv/Caddyfile:Z,ro  # z probably overrides ro :/
      - ./caddy/data:/data:Z
    command: ["caddy", "run"]

Dockerfile:

FROM caddy:builder AS builder
RUN xcaddy build  --with github.com/caddy-dns/cloudflare
FROM caddy
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

d. My complete Caddy config:

foo.com {
        basicauth {
                foo $2a$14$WkjDjpbJ48qPK2JeUECpO.D6PSJas2RzDB1V72d9Nk0HqNYrkl5Wm
        }
        tls myemail@gmail.com
        reverse_proxy myhost:12345
}

5. Links to relevant resources:

That’s not true. Caddy upgrades to HTTPS before any handler takes any action. Try it. You’ll see the redirect to HTTPS before you see the basic auth prompt.

2 Likes

Don’t you need a listener on port 80 to perform redirect to https on port 443?

Sites configured on Caddy are served on HTTPS automatically and includes an implicit listener on port 80 to redirect any HTTP request on port 80 to HTTPS on port 443. Please read more about Caddy Automatic HTTPS feature here:

3 Likes

Upon attempting to spin up a public test site to demonstrate this I noticed a firewall blocking port 80 which explains that. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.