Private
Public Access
Stop nginx from forwarding a client-spoofable X-Forwarded-For (S8)
The example nginx config set X-Forwarded-For to $proxy_add_x_forwarded_for, which APPENDS to whatever X-Forwarded-For a client already sent rather than replacing it. A client could send its own "X-Forwarded-For: 1.2.3.4" and have it forwarded as the first (trusted) entry — exactly what apps.accounts.views._client_address reads to key the login rate limiter, letting an attacker spoof a different address on every request to dodge the per-address throttle. Changed to $remote_addr, nginx's own view of who actually connected, which overwrites rather than appends. docs/install.md's "different host" proxy guidance also gets a concrete ufw example instead of just "firewall that port" prose, explains why that firewall rule isn't optional (gunicorn trusts whoever can reach it directly as if they were the proxy), and documents an optional Unix socket setup for the same-host case for anyone who wants gunicorn unreachable by anything but the proxy, not just this nginx config. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cb5617edb8
commit
ca03a5994b
2 files changed
+44
-7
No files matched your search
@@ -50,7 +50,15 @@ server {
|
|||||||
proxy_pass http://127.0.0.1:8000;
|
proxy_pass http://127.0.0.1:8000;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
# $remote_addr, not $proxy_add_x_forwarded_for (S8): the latter
|
||||||
|
# APPENDS to whatever X-Forwarded-For a client already sent, so a
|
||||||
|
# client could set their own "X-Forwarded-For: 1.2.3.4" and have it
|
||||||
|
# forwarded as the first (trusted) hop — Evection's login rate
|
||||||
|
# limiter (apps.accounts.views._client_address) and anything else
|
||||||
|
# that reads this header take the first entry as the real client
|
||||||
|
# address. Overwriting it with nginx's own view of the connecting
|
||||||
|
# IP is what actually makes that trust meaningful.
|
||||||
|
proxy_set_header X-Forwarded-For $remote_addr;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+35
-6
@@ -233,17 +233,46 @@ sudo apt install certbot python3-certbot-nginx
|
|||||||
sudo certbot --nginx -d your-domain.example
|
sudo certbot --nginx -d your-domain.example
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`127.0.0.1:8000` is still a TCP port any local process can connect to (any
|
||||||
|
other user or container sharing this host, not just nginx). If you want
|
||||||
|
gunicorn reachable by nothing but the proxy, bind it to a Unix socket
|
||||||
|
instead: set `EVECTION_GUNICORN_BIND=unix:/run/evection/gunicorn.sock` in
|
||||||
|
`.env`, add `RuntimeDirectory=evection` under `[Service]` in
|
||||||
|
`evection-gunicorn.service` (creates `/run/evection`, owned by the
|
||||||
|
`evection` user, on every start), and point nginx's `proxy_pass` at
|
||||||
|
`http://unix:/run/evection/gunicorn.sock:;` instead of the TCP address —
|
||||||
|
nginx needs read/write access to that socket, so either run nginx's worker
|
||||||
|
as a user in the `evection` group or adjust the directory's permissions
|
||||||
|
accordingly. This is optional hardening, not required.
|
||||||
|
|
||||||
**Proxy on a different host or VM** (Nginx Proxy Manager, a Caddy box, a
|
**Proxy on a different host or VM** (Nginx Proxy Manager, a Caddy box, a
|
||||||
container host, etc.). Set `EVECTION_GUNICORN_BIND=0.0.0.0:8000` (or to this
|
container host, etc.). Set `EVECTION_GUNICORN_BIND=0.0.0.0:8000` (or to this
|
||||||
host's specific IP) so the proxy can actually reach gunicorn over the
|
host's specific IP) so the proxy can actually reach gunicorn over the
|
||||||
network, then **firewall that port to only accept connections from the
|
network, then **firewall that port to only accept connections from the
|
||||||
proxy's host** — gunicorn itself has no auth in front of it, so it must
|
proxy's host** — gunicorn itself has no auth in front of it, so it must
|
||||||
never be reachable from the open internet directly. Point the proxy's
|
never be reachable from the open internet directly. This isn't optional
|
||||||
upstream/forward target at `this-host:8000`, and make sure it forwards
|
hardening: Evection trusts `X-Forwarded-Proto` and `X-Forwarded-For` from
|
||||||
`X-Forwarded-Proto` (whatever sets up TLS passthrough/termination normally
|
whoever can reach this port at all (see `config/settings/prod.py` and
|
||||||
handles this, but double-check — see the next paragraph for why it matters).
|
`apps/accounts/views.py`'s login rate limiter), so anyone who can reach it
|
||||||
Whichever proxy you use, `docs/deploy/nginx.conf.example`'s `location /`
|
directly can set those headers themselves and be trusted as if they were
|
||||||
block shows the three headers that need forwarding either way.
|
the proxy. With ufw, that's:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo ufw allow from <proxy-host-ip> to any port 8000 proto tcp
|
||||||
|
sudo ufw deny 8000/tcp
|
||||||
|
```
|
||||||
|
|
||||||
|
(order matters — the `allow from` rule must be added before the blanket
|
||||||
|
`deny`). Point the proxy's upstream/forward target at `this-host:8000`,
|
||||||
|
and make sure it **overwrites** `X-Forwarded-For` with its own view of the
|
||||||
|
client's address rather than appending to whatever the client already
|
||||||
|
sent — a proxy that appends lets a client forge a trusted-looking first
|
||||||
|
hop. It must also set `X-Forwarded-Proto` (whatever sets up TLS
|
||||||
|
passthrough/termination normally handles this, but double-check — see the
|
||||||
|
next paragraph for why it matters). Whichever proxy you use,
|
||||||
|
`docs/deploy/nginx.conf.example`'s `location /` block shows the three
|
||||||
|
headers that need forwarding either way, including that overwrite-not-
|
||||||
|
append distinction for `X-Forwarded-For`.
|
||||||
|
|
||||||
Either way, the proxy must set `X-Forwarded-Proto: https` on requests it
|
Either way, the proxy must set `X-Forwarded-Proto: https` on requests it
|
||||||
forwards once it's terminating TLS — Evection's `prod` settings
|
forwards once it's terminating TLS — Evection's `prod` settings
|
||||||
|
|||||||
Reference in new issue
Block a user