caddy: http.request.tls.client.* placeholder broken for Caddy 2.6.3+

Let me first say thanks for this awesome product ! The bug I am about to “complain” is rather a first world problem^^

I am using following matcher to decide whenever I am on a “safe” client (with installed client certificate) or some “public” machine (fallback to Authelia):

        @missing_mTLS_cert {
                expression {tls_client_subject} == null
        }
        
        forward_auth @missing_mTLS_cert ... {
                ....
        }        

Everything was working fine until 2.6.3 … Starting from this version it become unstable… Now even with a valid certificate it randomly fails (placeholder not set (?)). forcing me to login with Authelia …

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 37 (20 by maintainers)

Most upvoted comments

I’m unable to replicate this. Just to confirm, are we using some version of Caddy other than 2.7.6? E.g., given this Dockerfile, should I get the same version of Quic as you?

FROM caddy:builder AS builder

RUN xcaddy build --with github.com/caddy-dns/cloudflare

FROM caddy:latest

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Thanks for the details! I’ll try to circle back to this soon! Got just about 80 notifications in my backlog still… 😅

Hi folks, I got very good progress and I am able to consistently replicate the issue with Curl and Caddy 2.7.6. Thanks to @francislavoie for linking to HTTP3 version of Curl in the thread here.

It seems the issue always occurs in HTTP3/QUIC. It does not occur over HTTP1 or HTTP2. Previously, I stated “[i]t seems to only occur with browsers (Firefox, Chrome) and only sometimes.” This is false, wrong and absolute misinformation. The only reason why it occurs “sometimes” is because Firefox and Chrome tend to switch between HTTP2 and HTTP3 seemingly in a random fashion. I’m not quite sure what gets them to switch between the two, but I reckon that’s out of scope of this issue.

I can confirm that this behavior is always consistent, given the variables below.

Given the following Caddyfile:

{
    log {
        level debug
    }
}

(client_auth) {
    tls {
        dns cloudflare [SECRET REDACTED]
        client_auth {
            mode verify_if_given
            trusted_ca_cert_file /intermediate.crt
        }
    }
}

caddytest.homelab.memetichenry.com {
    import client_auth
    bind 10.12.180.18
    reverse_proxy 127.0.0.1:5000 {
        header_up tls-client-certificate {http.request.tls.client.certificate_der_base64}
        header_up soundness-check are-you-still-there
    }
}

and the following Docker Compose file:

services:
  caddy:
    build: Cloudflare_Caddy
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:Z
      - ./certs/intermediate.crt:/intermediate.crt:Z
      - ./caddy_data:/data:Z
      - ./caddy_config:/config:Z
    network_mode: "host"
    restart: unless-stopped

Where “Cloudflare_Caddy” is simply Caddy with the official Cloudflare module, the following curl command:

curl -E ./client.crt --key ./client.key -vs -D/dev/stdout --http3 https://caddytest.homelab.memetichenry.com

Will always generate the following reverse proxy request, taken from the Caddy log:

{
  "level": "debug",
  "ts": 1706345885.550976,
  "logger": "http.handlers.reverse_proxy",
  "msg": "upstream roundtrip",
  "upstream": "127.0.0.1:5000",
  "duration": 0.00240038,
  "request": {
    "remote_ip": "10.12.180.2",
    "remote_port": "39728",
    "client_ip": "10.12.180.2",
    "proto": "HTTP/3.0",
    "method": "GET",
    "host": "caddytest.homelab.memetichenry.com",
    "uri": "/",
    "headers": {
      "X-Forwarded-For": [
        "10.12.180.2"
      ],
      "X-Forwarded-Proto": [
        "https"
      ],
      "X-Forwarded-Host": [
        "caddytest.homelab.memetichenry.com"
      ],
      "Tls-Client-Certificate": [
        "{http.request.tls.client.certificate_der_base64}"
      ],
      "Soundness-Check": [
        "are-you-still-there"
      ],
      "User-Agent": [
        "curl/8.2.1-DEV"
      ],
      "Accept": [
        "*/*"
      ]
    },
    "tls": {
      "resumed": false,
      "version": 772,
      "cipher_suite": 4867,
      "proto": "h3",
      "server_name": "caddytest.homelab.memetichenry.com"
    }
  },
  "headers": {
    "Server": [
      "Werkzeug/3.0.1 Python/3.9.18"
    ],
    "Date": [
      "Sat, 27 Jan 2024 08:58:05 GMT"
    ],
    "Content-Type": [
      "application/json"
    ],
    "Content-Length": [
      "552"
    ]
  },
  "status": 200
}

I used a simple Python program written by ChatGPT to send back the HTTP headers, and I can confirm they match the log (meaning the client certificate is never passed to backend).

If, however, you disable HTTP3 via servers{protocols h1 h2}, the client certificate is passed normally:

{
  "level": "debug",
  "ts": 1706346834.37273,
  "logger": "http.handlers.reverse_proxy",
  "msg": "upstream roundtrip",
  "upstream": "127.0.0.1:5000",
  "duration": 0.002871964,
  "request": {
    "remote_ip": "10.12.180.2",
    "remote_port": "45112",
    "client_ip": "10.12.180.2",
    "proto": "HTTP/2.0",
    "method": "GET",
    "host": "caddytest.homelab.memetichenry.com",
    "uri": "/",
    "headers": {
      "Accept": [
        "*/*"
      ],
      "Soundness-Check": [
        "are-you-still-there"
      ],
      "Tls-Client-Certificate": [
        "MIIByzCCAXKgAwIBAgIRANKDWKTP49v869jRCdFzmw0wCgYIKoZIzj0EAwIwKjEoMCYGA1UEAxMfQ2FkZHkgQ2xpZW50IENBIEludGVybWVkaWF0ZSBDQTAeFw0yNDAxMjYxOTIzMTZaFw00NDAxMjYxOTIzMTZaMBcxFTATBgNVBAMTDENhZGR5IENsaWVudDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABGFabTn1YK+af8aONdSGO6X7zfzx+kq15N0U33Q6iEl/Y7WNiX2PnU43RapRvepLH+uQbct/JoFDZYWIUAc+jeqjgYswgYgwDgYDVR0PAQH/BAQDAgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAdBgNVHQ4EFgQURXgJ57eglfL2YD63w++Y3k0ZeU4wHwYDVR0jBBgwFoAU5SOl0DJU/th60T+B7kpijKJLBtwwFwYDVR0RBBAwDoIMQ2FkZHkgQ2xpZW50MAoGCCqGSM49BAMCA0cAMEQCIAm1JC6gJDR2qWUWAHWfcFUB96mcVdmO4lJMd+U3TtByAiBHRGtADANutoSpcrGj1me4/ef1ltex4zbZZK9unJBWlg=="
      ],
      "X-Forwarded-For": [
        "10.12.180.2"
      ],
      "X-Forwarded-Proto": [
        "https"
      ],
      "X-Forwarded-Host": [
        "caddytest.homelab.memetichenry.com"
      ],
      "User-Agent": [
        "curl/8.2.1-DEV"
      ]
    },
    "tls": {
      "resumed": false,
      "version": 772,
      "cipher_suite": 4867,
      "proto": "h2",
      "server_name": "caddytest.homelab.memetichenry.com",
      "client_common_name": "Caddy Client",
      "client_serial": "279819867913103031473390418487099497229"
    }
  },
  "headers": {
    "Content-Type": [
      "application/json"
    ],
    "Content-Length": [
      "1124"
    ],
    "Server": [
      "Werkzeug/3.0.1 Python/3.9.18"
    ],
    "Date": [
      "Sat, 27 Jan 2024 09:13:54 GMT"
    ]
  },
  "status": 200
}

Attached here certificates.zip are the intermediate CA, client certificate and associated key files for ease of testing. The file client.p12 is provided in case either of you want to test a web browser, both Chrome and Firefox only support P12 files to the best of my knowledge. The certificate keys are plaintext. The certificates and associated keys were generated with the following commands, using Smallstep CLI:

step-cli certificate create --insecure --no-password --not-after=175320h --profile=root-ca "Caddy Client CA Root CA" root.crt root.key
step-cli certificate create --insecure --no-password --not-after=175320h --profile=
intermediate-ca --ca ./root.crt --ca-key ./root.key "Caddy Client CA Intermediate CA" intermediate.crt intermediate.key 
step-cli certificate create --insecure --no-password --not-after=175320h --profile=leaf --ca ./intermediate.crt --ca-key ./intermediate.key "Caddy Client" client.crt client.key
step-cli certificate p12 --insecure --no-password client.p12 client.crt client.key

Please let me know if you need any more information, and again, my apologies for the wait.

No rush 😃

@mholt

😦 I have not. Let me try to work this sometime this week. Sorry again for the delay.

Dear all:

Thank you all for this thread. For me, the issue is that I was using {http.request.tls.client.certificate} instead of {http.request.tls.client.certificate_der_base64}.

So thank you for your time, @mholt

Greetings and best wishes!

Notice that I’ve changed the placeholder to {http.request.tls.client.certificate_der_base64} to get a value valid for headers.

Yeah long story, I used a mod by someone in a different issue to get around this since Caddy doesn’t wrap certs in escape characters–it was unrelated to the issue so I didn’t want to bog down my already-wordy paragraphs with extraneous details.

The extra Client cert: {http.request.tls.client.certificate_der_base64} at the end is normal because of the --next https://localhost request which makes a second request without client auth.

This is a lack of an understanding of how curl processes the --next request on my side then.

Let’s put a pin in this for now. I will do more testing on my side and see if I can recreate the issue I am having in the web browsers I tested with curl, instead. The workaround for now is to disable HTTP3, which allows users to successfully use require_and_verify. I will also see if this behavior occurs with other web servers (Nginx, Apache, etc.) to see if it’s an issue that is only related to web browsers.

I think the issue should be left as open with needs info. I will update as soon as I can, likely after Hacker Summer Camp. Sorry for the delays on my end.

ALCON,

I am having the same issue where Caddy v2.6.4 only sometimes passes the client certificate to the backend server (in my case, Keycloak, but it shouldn’t matter whether it’s Keycloak, Authelia, or Authentik). Note that this issue only occurs if H3 is enabled and verify_if_given is used. If require_and_verify is used, this issue doesn’t occur. Below is my Caddyfile

{
    log {
        level debug
    }
    servers {
        protocols h1 h2 h3
    }
}

(client_auth) {
    tls {
        client_auth {
            mode verify_if_given
            trusted_ca_cert_file /certs/ca/root_ca.crt
            trusted_ca_cert_file /certs/ca/intermediate_ca.crt
        }
    }
}

keycloak.secure.local {
    import client_auth
    reverse_proxy 127.0.0.1:8080 {
        header_up SSL_CLIENT_CERT {http.request.tls.client.certificate_pem}
    }
}

Attached here is my debug log. Note that I removed the following from the logs, do let me know if it is an issue:

  1. The client certificate itself has been removed. All instances that contain certificate data have been replaced with “[CERT_DATA]”
  2. Any references to the server certificate. I can’t release this at this time, but if it’s required I’ll stand up a separate environment with releasable server certificate data.

Another difference is I cannot recreate the issue with curl. It seems to only occur with browsers (Firefox, Chrome) and only sometimes. I know this is unhelpful. I will also try the latest alpha version, as well, and comment here.

@francislavoie You are right! I don’t see any http3 requests when testing with 2.6.2.

2 6 2

The behavior changed with 2.6.3+

2 7 0 b (302 is the Authelia redirection)

but afterwards with http3 requests i only see placeholder strings “{http.request.tls.client…}” returned.

Hmm. So you’re saying it seems broken for H3 requests but it works fine for H1/H2? Okay. Are you sure it worked with H3 with prior versions? I’m trying to understand what you’re actually describing as broken and when.

Please try v2.7.0-beta.1, we’ve made a lot of changes since 2.6.4, some of which may have already fixed it.