Trusted Proxy and Client IP Resolution#
Overview#
Tinyauth relies on the upstream reverse proxy (Traefik, Caddy, Nginx, Envoy) to forward the real client IP in headers like X-Real-IP or X-Forwarded-For. Because Tinyauth itself sits behind a Docker bridge or load balancer, it cannot determine the actual end-user IP from the raw TCP connection. The TrustedProxies configuration tells Gin which upstream IP addresses are authoritative sources of those headers.
Without trusted proxies configured, all IP-based access controls are silently disabled β IP allow/block rules pass through without effect, and IP bypass rules never fire.
Configuration#
Auth.TrustedProxies is a []string slice in AuthConfig accepting individual IPv4/IPv6 addresses or CIDR ranges.
- Env var:
TINYAUTH_AUTH_TRUSTEDPROXIES(comma-separated) - YAML key:
auth.trustedProxies - Default: empty (feature disabled)
Docker hostname limitation: Gin only accepts IPv4/IPv6 IPs and CIDRs β not Docker service hostnames. For Docker Compose deployments, configure the subnet CIDR of the shared network instead (e.g.,
172.20.0.0/16).
For NGINX Proxy Manager, the docs recommend setting TINYAUTH_AUTH_TRUSTEDPROXIES to the NGINX instance IP and running NGINX in network_mode: host for correct IP passthrough.
Startup Behavior#
During router setup in setupRouter():
- If
Auth.TrustedProxiesis non-empty β callsengine.SetTrustedProxies(...)and setsruntime.TrustedProxiesConfigured = true. - If empty β calls
engine.SetTrustedProxies(nil)(disabling Gin's proxy trust entirely) and logs a warning:"Trusted proxies are not configured, IP access controls will NOT work".
The TrustedProxiesConfigured boolean is stored in RuntimeConfig and propagated into every ACLContext at request time.
How the Policy Engine Uses It#
At the start of each auth request, proxyHandler calls c.ClientIP() (Gin's method, which reads the forwarded header only when the upstream IP is trusted) and builds an ACLContext containing the resolved IP and the TrustedProxiesConfigured flag.
The flag gates two IP-specific rules evaluated in order before any user authentication:
RuleIPBypassed#
IPBypassedRule β If TrustedProxiesConfigured is false, immediately returns EffectDeny (i.e., bypass never activates). Otherwise, merges the global Auth.IP.Bypass list with the per-app ip.bypass list and returns EffectAllow if the client IP matches any entry.
RuleIPAllowed#
IPAllowedRule β If TrustedProxiesConfigured is false, immediately returns EffectAllow (passes everything through, avoiding false blocks behind Docker bridges). Otherwise:
- Merges global
Auth.IP.Block+ per-appip.blockβ if the IP matches any entry βEffectDeny. - Merges global
Auth.IP.Allow+ per-appip.allowβ if the IP matches any entry βEffectAllow. - If an allow list exists but the IP isn't in it β
EffectDeny. - If neither list has entries β
EffectAllow.
Merge semantics: Global lists and per-app lists are concatenated at evaluation time β a global block applies even if the per-app allow list would otherwise permit the IP.
IP Filtering Configuration Reference#
| Scope | Field | Description |
|---|---|---|
| Global | auth.ip.allow | Allow only listed IPs/CIDRs (all apps) |
| Global | auth.ip.block | Block listed IPs/CIDRs (all apps) |
| Global | auth.ip.bypass | Skip auth entirely for listed IPs/CIDRs |
| Per-app | ip.allow | Merged with global allow list |
| Per-app | ip.block | Merged with global block list |
| Per-app | ip.bypass | Merged with global bypass list |
Global IP config is defined in IPConfig; per-app config in AppIP.
Key Files#
| File | Role |
|---|---|
internal/model/config.go | AuthConfig.TrustedProxies, IPConfig, AppIP struct definitions |
internal/model/runtime.go | RuntimeConfig.TrustedProxiesConfigured flag |
internal/bootstrap/router_bootstrap.go | Calls SetTrustedProxies, sets the runtime flag |
internal/controller/proxy_controller.go | Reads c.ClientIP(), builds ACLContext, runs IP rules |
internal/service/policy_engine.go | ACLContext struct, PolicyEngine.Evaluate() |
internal/service/access_controls_rules.go | IPAllowedRule and IPBypassedRule implementations |