fix: recover the customer address behind the host reverse proxy
Some checks failed
Build and deploy / Validate source (push) Successful in 7s
Build and deploy / Integration suite on a real stack (push) Failing after 51s
Build and deploy / Secret scan and release gate (push) Successful in 9s
Build and deploy / Publish images and notify Portainer (push) Has been skipped
Some checks failed
Build and deploy / Validate source (push) Successful in 7s
Build and deploy / Integration suite on a real stack (push) Failing after 51s
Build and deploy / Secret scan and release gate (push) Successful in 9s
Build and deploy / Publish images and notify Portainer (push) Has been skipped
The production gateway does not face the internet: nginx-proxy-manager owns 80/443 on the host and proxies to it. So $remote_addr inside the gateway is that proxy, and overwriting X-Forwarded-For with it discarded the customer address the proxy had already recorded. Every request would have been attributed to one internal address, which is exactly the fault 2.1 set out to fix, reintroduced in production only. Use real_ip to take the customer address from the proxy's header, trusting only private networks. A request that reaches the published port directly from the internet is not trusted, so its header is ignored and $remote_addr stays the real peer: the anti-spoofing property is kept. Also downgrade 2.9. TLS is not missing, it is terminated by that proxy. The gap is that the repository never says so, which would break every session cookie if the stack moved to a host without one. Validated with nginx -t against the rendered production configuration. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,17 @@ server {
|
||||
# unavailable or still creating its database schema.
|
||||
resolver 127.0.0.11 ipv6=off valid=10s;
|
||||
set $api_upstream api:8000;
|
||||
|
||||
# This gateway sits behind the host's reverse proxy, so $remote_addr is that
|
||||
# proxy, not the customer. Recover the real address from the header it sets,
|
||||
# and only when the connection comes from a private network: a request that
|
||||
# reaches the published port directly from the internet is not trusted, so
|
||||
# its X-Forwarded-For is ignored and $remote_addr stays the actual peer.
|
||||
set_real_ip_from 10.0.0.0/8;
|
||||
set_real_ip_from 172.16.0.0/12;
|
||||
set_real_ip_from 192.168.0.0/16;
|
||||
real_ip_header X-Forwarded-For;
|
||||
real_ip_recursive on;
|
||||
root /usr/share/nginx/html;
|
||||
index ${WEB_INDEX};
|
||||
|
||||
@@ -29,6 +40,7 @@ server {
|
||||
proxy_set_header X-Forwarded-Proto https;
|
||||
# Overwrite, never append: $proxy_add_x_forwarded_for keeps any header the
|
||||
# client sent, and the leftmost value would then be attacker-controlled.
|
||||
# After real_ip above, $remote_addr is the customer even behind the proxy.
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_connect_timeout 5s;
|
||||
proxy_read_timeout 30s;
|
||||
|
||||
Reference in New Issue
Block a user