Generated Caddyfile cannot pass the real client address when a CDN or proxy sits in front of Caddy

Open
#82 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
bash, fastapi, python

Research direction

Start with setup-scripts/debian13/06-caddy.sh and its existing bats tests, then read setup.env.example and the proxy guidance in setup-scripts/debian13/README.md. Check how the generated Caddyfile handles all three reverse_proxy blocks and how the installer validates Caddy before replacing the file. Done means the optional settings preserve the default output, validate trusted proxy configuration, and meet the listed tests and documentation criteria.

Written by the indexing model from the issue text.

Description

enhancement P2 server

Problem

The anonymous passkey routes are rate-limited per client address, and sessions store the same address. The key is request.client.host (server/orcaster_server/auth/router.py:105, server/orcaster_server/auth/router.py:181, server/orcaster_server/auth/router.py:411). uvicorn rewrites it from X-Forwarded-For only for trusted peers, which by default is the loopback address (server/orcaster_server/auth/rate_limit.py:27-30). In the setup from setup-scripts/debian13, that peer is Caddy.

The generated Caddyfile has a plain reverse_proxy and no trusted_proxies or client_ip_headers (setup-scripts/debian13/06-caddy.sh:79-116). By default Caddy ignores forwarded headers from peers it does not trust, which stops spoofing. When Caddy faces the internet directly, this is correct.

The docs describe putting a CDN, WAF or another proxy in front of the server (setup-scripts/debian13/README.md:362, setup-scripts/debian13/README.md:366-370, docs/user-manual.md:4283). In that layout, Caddy sees the edge address as the client and writes it into X-Forwarded-For. All visitors that reach the server through the same edge address then share one rate-limit bucket. One abusive client can cause 429 for all of them, and session records show edge addresses.

Proposed change

  1. Add two optional settings to setup-scripts/debian13/setup.env.example, for example TRUSTED_PROXY_RANGES (space-separated CIDR list of the edge) and CLIENT_IP_HEADER (default X-Forwarded-For).
  2. When TRUSTED_PROXY_RANGES is set, 06-caddy.sh writes the full chain, not only the trust list:
    • A global servers block with trusted_proxies static <ranges>, client_ip_headers <header> and trusted_proxies_strict. Without strict mode, Caddy takes the leftmost X-Forwarded-For value, which the client controls. With strict mode, Caddy reads the header from right to left and takes the first address that is not a trusted proxy.
    • header_up X-Forwarded-For {client_ip} in each of the three reverse_proxy blocks (setup-scripts/debian13/06-caddy.sh:101, :108, :114). By default, Caddy appends the edge address to the header it forwards, and uvicorn takes the rightmost untrusted address, so it would still see the edge. With header_up, uvicorn gets one value: the client address that Caddy resolved.
    • uvicorn keeps trusting only the loopback address (server/orcaster_server/auth/rate_limit.py:27-30). Caddy is the only trusted peer of uvicorn, and the edge is trusted only by Caddy.
    • The settings need Caddy 2.8 or later (client_ip_headers, trusted_proxies_strict and {client_ip}). The installer keeps an already installed Caddy (setup-scripts/debian13/06-caddy.sh:27), so the script checks caddy version and stops with a clear error on an older one, before the Caddyfile is replaced.
    • With a header other than X-Forwarded-For, the edge must set that header itself and never pass a client value through. The docs say so.
  3. Document the settings in setup-scripts/debian13/README.md next to the existing notes about a proxy in front. State that the operator must keep the ranges current, and that the limit needs these settings whenever Caddy does not face the internet directly.

Compatibility

  • With the new settings unset, the generated Caddyfile is byte-identical to today. Existing deployments see no change.
  • The settings are new and optional. No upgrade step is needed.
  • A deployment that later enables them will see rate-limit keys and session addresses change from the edge address to the client address, as long as the edge adds the client address to the header and its ranges are current. A client that connects to Caddy directly from outside the ranges keeps its own address as the key.

Acceptance criteria

  • With TRUSTED_PROXY_RANGES unset, the generated Caddyfile is byte-identical to the current output (bats test).
  • With it set, the Caddyfile contains trusted_proxies static with the given ranges, client_ip_headers with the given header, trusted_proxies_strict, and header_up X-Forwarded-For {client_ip} in all three reverse_proxy blocks, and caddy validate passes (bats test).
  • With it set and a Caddy older than 2.8, the script stops with a clear error and the current Caddyfile stays intact (bats test).
  • Through a trusted edge that appends the client address, request.client.host equals the real client address (integration test or recorded manual check).
  • Through a trusted edge, a client-supplied leftmost X-Forwarded-For value does not change the rate-limit key (test).
  • An invalid range is rejected before the Caddyfile is replaced, and the current Caddyfile stays intact (bats test).
  • Existing deployments keep working without config changes.
  • setup-scripts/debian13/README.md and setup.env.example document both settings and when they are needed.
  • CHANGELOG entry under Unreleased.
Dominant language
Python
Stars
0
Forks
0
Avg merge
1d 1h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AInvirion/orcaster

All issues in AInvirion/orcaster

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.