Skip to content

feat(proxy): add proxy.trustedProxies for forwarded-header trust - #241

Merged
vishr merged 2 commits into
mainfrom
feat/proxy-trusted-proxies
Nov 27, 2026
Merged

vishr merged 2 commits into
mainfrom
feat/proxy-trusted-proxies

Conversation

@vishr

@vishr vishr commented Nov 27, 2026

Copy link
Copy Markdown
Member

What this changes

Adds proxy.trustedProxies: [CIDR, ...] to the project file. When a CDN or another reverse proxy sits in front of the managed Traefik, every request's peer is the front proxy, and Traefik overwrites the X-Forwarded-For the front proxy wrote. Listing the front proxy's prefixes makes the managed proxy keep those forwarded headers instead.

  • Rendering: for the Onebox-rendered static configuration, the list is written as entryPoints.<name>.forwardedHeaders.trustedIPs on every entrypoint: web, websecure, and each proxy.entrypoints listener. This covers both the default-managed case and proxy.config directories that hold only dynamic files.

  • Validation: each entry goes through netip.ParsePrefix and must be in canonical masked form. 10.20.0.5/24 is refused with "write 10.20.0.0/24". IPv4 and IPv6 are accepted. These are refused:

    • bare addresses (the error suggests /32 or /128)
    • 0.0.0.0/0 and ::/0
    • IPv4-mapped IPv6 prefixes, because ::ffff:0:0/96 matches every IPv4 peer under Go's net comparison
    • zoned addresses

    The list is sorted (IPv4 first) and deduplicated in the spec, so the order it is written in never changes the bytes.

  • Conflicts: using the field without a managed proxy is an error. That covers proxy.managed: false, kind: None, and projects with no routes and no explicit managed: true. Using it with a project-owned traefik.yml/traefik.yaml in proxy.config is also an error, mirroring dnsChallenge.

  • Preview: ob preview prints # proxy trustedProxies <effective list>: …. The JSON envelope gains proxy.trustedProxies. Both appear only when the field is declared, so preview output without it is unchanged.

  • Docs: generated field reference, plus a new "Trusted proxies in front of the managed proxy" section next to "PROXY protocol on TCP routes". The section covers:

    • when to use the field
    • the precondition: the host must accept proxy traffic only from the listed edge, or clients can forge X-Forwarded-For
    • walking X-Forwarded-For right to left
    • adding the front proxy to the app's own trusted list
    • ignoring X-Real-Ip

    Examples use the documentation ranges 192.0.2.0/24 and 2001:db8::/32.

Out of scope: PROXY protocol on entrypoints, an insecure mode, and per-entrypoint lists.

Why this is correct

  • Byte-identical when absent: TestStaticConfigWithoutTrustedProxiesIsUnchanged pins, by sha256, the static config and the proxy Identity (config and compose) taken from main before this change. Both nil and empty lists reproduce them, so upgrading restarts no one's proxy.
  • Golden: TestTrustedProxiesRenderIntoEveryEntrypoint compares the full rendered file, including an additional entrypoint, and parses it back as YAML.
  • Identity and plan: declaring the field changes only Identity.Config; Compose is unchanged, so the change is a restart and not a recreate. TestPlanProxyRestartsTraefikWhenTrustedProxiesChange shows the plan line restart Traefik (configuration changed), and unchanged when the field is absent.
  • Validation table: internal/app/trusted_proxies_test.go covers the canonical-form cases, /0 for v4 and v6, bare v4 and v6 addresses, duplicates, sort order, IPv4-mapped, zone, the empty list, the unmanaged/no-proxy conflicts and the authored camelCase spelling. Error paths are checked as spec.proxy.trustedProxies[i].
  • Custom static conflict: TestTrustedProxiesRefuseProjectOwnedStaticConfig covers traefik.yml and traefik.yaml.
  • Preview: TestPreviewShowsTrustedProxies covers the text and JSON output, and checks that a project without the field never mentions it.
  • Real Traefik (e2e, opt-in): TestTrustedProxiesKeepForwardedHeadersOnlyFromTrustedPeers stages the managed config and runs traefik:v3.7 on a fixed-subnet Docker network with a busybox CGI backend.
    • A peer inside the trusted /28 sends X-Forwarded-For: 203.0.113.7, and the backend sees 203.0.113.7, <peer>.
    • A peer outside it sends the same header, and the backend sees only <peer>.
    • I confirmed the test fails when the peer is moved out of the trusted range.

Ran: just check (passes), golangci-lint run ./... (0 issues), and ONEBOX_E2E=1 go test ./e2e -run TestTrustedProxiesKeepForwardedHeadersOnlyFromTrustedPeers (passes). Not run: the full just e2e suite.

Effect on the safety envelope

  • Projects that don't declare the field: none. Their static bytes and proxy identity are pinned unchanged.
  • Projects that declare it: the managed Traefik is restarted on the next deploy, through the existing proxy: restart Traefik (configuration changed) plan line.
  • The field is shipped behaviour, not schema-only, so the capability status is unchanged.

Notes for review:

  • Shared hosts: Onebox allows one application owner per host (HostOwnerMismatchError / HostEnvironmentMismatchError refuse any other app or environment before the proxy is staged), and proxy cannot be overridden per environment. Two co-hosted apps with different lists therefore can't happen today, and I added no new conflict code. If co-hosting ever arrives, this field must join whatever host-proxy agreement check is built for it.
  • Port 8080: a separate, pre-existing issue turned up while writing the e2e test. The managed static config enables ping: {} without an entryPoint, so Traefik opens its internal traefik entrypoint on :8080. A proxy.entrypoints listener on port 8080 then fails to bind. Validation does not reserve 8080 today. It belongs in its own PR.

Checklist

  • just check passes locally.
  • Tests cover the new behaviour, including the failure paths.
  • Generated documentation is current (just check verifies this).
  • I have accepted the CLA, or will when the bot asks on my first pull request.

🤖 Generated with Claude Code

vishr and others added 2 commits November 26, 2026 19:49
Behind a CDN or another reverse proxy, the managed Traefik sees the front
proxy as every request's peer and overwrites the X-Forwarded-For it wrote,
so applications cannot learn the client address. proxy.trustedProxies lists
the front proxy's CIDR prefixes, and Onebox renders them as
forwardedHeaders.trustedIPs on every entrypoint in its generated static
configuration: web, websecure and each proxy.entrypoints listener.

Entries must be canonical IPv4 or IPv6 prefixes. Bare addresses, prefixes
with host bits set (the error names the canonical form), IPv4-mapped IPv6
prefixes and 0.0.0.0/0 or ::/0 are refused; the list is sorted and
deduplicated so its order never changes the configuration. The field
requires a Onebox-managed proxy and cannot be combined with a project-owned
traefik.yml, mirroring proxy.dnsChallenge.

An absent or empty list renders the static configuration byte for byte as
before, pinned by digest, so upgrading does not restart anyone's proxy.
Declaring it changes only the configuration identity, which the plan
reports as "restart Traefik (configuration changed)". ob preview prints the
effective list. An opt-in e2e test runs the real Traefik image and checks
that a trusted peer keeps its X-Forwarded-For while an untrusted one has it
replaced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refusing /0 alone let a pair such as 0.0.0.0/1 and 128.0.0.0/1 trust every
peer. Trusted prefixes must now be at least /8 for IPv4 and /16 for IPv6;
no CDN publishes ranges anywhere near that wide.

The no-managed-proxy error blamed an operator-owned proxy even when the
cause was that no route is declared or that proxy.kind is none; each case
now says what it is and what to do.

The docs now state that trusted peers also control X-Forwarded-Proto and
X-Forwarded-Host, including that X-Forwarded-Proto: https on the web
entrypoint suppresses the HTTPS redirect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vishr
vishr merged commit c5b1176 into main Nov 27, 2026
6 checks passed
@vishr
vishr deleted the feat/proxy-trusted-proxies branch November 27, 2026 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant