Repository navigation
Record the visitor's IP in download_history, not the ingress pod's - #9
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What the IP is used for
GET /dl(the HMAC-signed download proxy) writes each completed download todownload_historywithclient_ipanduser_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.gostoredr.RemoteAddr. In the cluster the request path is Cloudflare -> Hetzner LB -> Traefik -> pod, so this was the Traefik podIP:portfor every download. It was wrong before the Cloudflare-only origin change too. Switching toX-Real-IPwould 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
/dlcallsclientip.FromRequestfrom the newinternal/clientippackage, the only place that reads a client IP. Sources are tried in this order:CLIENT_IP_HEADERS, defaultcf-connecting-ip,x-forwarded-for,x-real-ipr.RemoteAddr, always the final fallbackCLIENT_IP_HEADERSis 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 forX-Forwarded-Foris the visitor. Values that are empty,unknownor 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-IPcan be trusted there. The README has a Client IP table, anddocker-compose.ymllists the variable commented out with its default.Tests
internal/clientip/clientip_test.go:TestFromRequestReadsEnv,TestHeadersFromEnv/custom_order)TestHeadersFromEnv/empty_env_var_means_default,.../blank_env_var_means_default,TestEmptyEnvVarUsesDefaultOrder)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 be203.0.113.7.go test ./...passes (golang:1.24-alpine).No tag or release. Rows already in
download_historykeep their old values.🤖 Generated with Claude Code
https://claude.ai/code/session_017KnhgEygVgyfHhD87euSMC