Skip to content

Caddy: Allow setting servers.client_ip_headers so that alternatives to X-Forwarded-For (like Cloudflare's Cf-Connecting-Ip) work #4517

Description

@Nezteb

Important notices

Is your feature request related to a problem? Please describe.

I use OPNsense with the Caddy plugin (dynamic DNS enabled through Cloudflare) and have Cloudflare's orange cloud/proxy enabled pointing at my public IP. Because of this, the X-Forwarded-For header is somewhat spoofed.

The Caddy and Authelia docs describe the issue in more detail:

I've found plenty of threads about this on the OPNsense forums, the Caddy forums, the Cloudflare forums, etc. Most threads end with the OP disabling Cloudflare's orange cloud/proxy toggle, but I don't think that should be totally necessary for what I'm trying to do? A few examples:

I tried the header rewrite rule described in the Authelia docs, but Cloudflare still appends their proxy IP to the header.

Cloudflare docs suggest to instead use the Cf-Connecting-Ip header:

However, it seems that the OPNsense Caddy plugin doesn't currently support this.

Describe the solution you'd like

I would like a way to effectively update my /usr/local/etc/caddy/Caddyfile to use client_ip_headers:

{
    # ...
    servers {
        protocols h1 h2 h3
        trusted_proxies static <ips> # https://www.cloudflare.com/ips/
        client_ip_headers Cf-Connecting-Ip X-Forwarded-For
    }
    # ...
}

I can think of two options:

  1. Add a plugin UI element that takes a string like "Cf-Connecting-Ip X-Forwarded-For" and it simply puts it into the Caddyfile as is.
  2. Add some sort of template/import that allows users to still put config in /usr/local/etc/caddy/caddy.d/ that accomplishes this somehow.

Describe alternatives you've considered

I tried to do this by creating a custom config file as mentioned in the docs: https://docs.opnsense.org/manual/how-tos/caddy.html#custom-configuration-files

$ cat /usr/local/etc/caddy/caddy.d/client_ip_headers.global
servers {
        protocols h1 h2 h3
        trusted_proxies static <ips> # https://www.cloudflare.com/ips/
        client_ip_headers Cf-Connecting-Ip X-Forwarded-For
}

However, running caddy validate gives an error:

$ caddy validate --config /usr/local/etc/caddy/Caddyfile
2025/01/31 20:16:39.483 INFO    using config from file  {"file": "/usr/local/etc/caddy/Caddyfile"}
2025/01/31 20:16:39.483 WARN    No files matching import glob pattern   {"pattern": "/usr/local/etc/caddy/caddy.d/*.conf"}
Error: adapting config using caddyfile: cannot have 'servers' global options with duplicate listener addresses:

Reading more into the Caddy docs, I realize that import can't be used to overwrite previous blocks, which makes sense.

Metadata

Metadata

Assignees

Labels

featureAdding new functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions