Skip to content

Record the visitor's IP in download_history, not the ingress pod's - #9

Merged
glani merged 2 commits into
mainfrom
fix/client-ip-priority
Nov 11, 2026
Merged

glani merged 2 commits into
mainfrom
fix/client-ip-priority

Conversation

@glani

@glani glani commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

What the IP is used for

GET /dl (the HMAC-signed download proxy) writes each completed download to download_history with client_ip and user_agent. It is an audit record only; nothing rate-limits or geolocates on it. No other code path in Watchword reads a client IP (auth, MCP handlers and upload proxy do not).

Before

internal/proxy/handler.go stored r.RemoteAddr. In the cluster the request path is Cloudflare -> Hetzner LB -> Traefik -> pod, so this was the Traefik pod IP:port for every download. It was wrong before the Cloudflare-only origin change too. Switching to X-Real-IP would not help: Traefik sets it to its own peer, which is now the Cloudflare edge (before, it was the LB at 10.10.0.9).

After

/dl calls clientip.FromRequest from the new internal/clientip package, the only place that reads a client IP. Sources are tried in this order:

  1. each header listed in CLIENT_IP_HEADERS, default cf-connecting-ip,x-forwarded-for,x-real-ip
  2. r.RemoteAddr, always the final fallback

CLIENT_IP_HEADERS is comma-separated and case-insensitive, and is read once. Unset or empty (or only whitespace and commas) means the default. For every header the first comma-separated entry is used, which for X-Forwarded-For is the visitor. Values that are empty, unknown or not an IP are skipped. Ports are stripped (1.2.3.4:5678, [2001:db8::1]:443), and IPv4-mapped IPv6 is unmapped.

No deployment change is needed. The default is the correct behaviour behind Cloudflare. The origin only accepts traffic from Cloudflare (Traefik ipAllowList), so CF-Connecting-IP can be trusted there. The README has a Client IP table, and docker-compose.yml lists the variable commented out with its default.

Tests

  • internal/clientip/clientip_test.go:
    • default order: CF wins, then XFF first entry (also across repeated header lines), then X-Real-IP
    • custom order, including from the env var (TestFromRequestReadsEnv, TestHeadersFromEnv/custom_order)
    • empty or blank env var means the default (TestHeadersFromEnv/empty_env_var_means_default, .../blank_env_var_means_default, TestEmptyEnvVarUsesDefaultOrder)
    • IPv4 and bracketed IPv6 ports stripped
    • unknown, empty and garbage values skipped
    • socket fallback
  • internal/proxy/handler_stream_test.go: the request carries CF/XFF/X-Real-IP headers and a pod RemoteAddr, and the test expects the recorded IP to be 203.0.113.7.
  • go test ./... passes (golang:1.24-alpine).

No tag or release. Rows already in download_history keep their old values.

🤖 Generated with Claude Code

https://claude.ai/code/session_017KnhgEygVgyfHhD87euSMC

GET /dl stored r.RemoteAddr as client_ip. Behind Cloudflare -> Hetzner LB
-> Traefik that is the Traefik pod IP:port, so every row had the same
useless address. X-Real-IP is no better: Traefik sets it to its own peer
(the Cloudflare edge).

The new internal/clientip package takes the IP from CF-Connecting-IP,
then the first X-Forwarded-For entry, then X-Real-IP, then RemoteAddr.
Values that are empty, "unknown" or not an IP are skipped and ports are
stripped. CLIENT_IP_HEADERS overrides the header order.
The previous commit added internal/clientip but left the handler on
r.RemoteAddr: the handler and test edits were not in it. /dl now calls
clientip.FromRequest, and handler_stream_test sends the proxy headers
with a pod RemoteAddr and expects the visitor's IP.

HeadersFromEnv makes the env handling testable. New tests cover an empty
or blank env var falling back to the default order. The README has a
Client IP table, and docker-compose.yml lists the variable commented out
with its default.

Claude-Session: https://claude.ai/code/session_017KnhgEygVgyfHhD87euSMC
@glani
glani merged commit 87e2433 into main Nov 11, 2026
12 checks passed
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