diff --git a/docs/deploy/nginx.conf.example b/docs/deploy/nginx.conf.example index 62c2c5b..4f4b2e2 100644 --- a/docs/deploy/nginx.conf.example +++ b/docs/deploy/nginx.conf.example @@ -50,7 +50,15 @@ server { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; 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; } } diff --git a/docs/install.md b/docs/install.md index 7cbc6bb..3c41778 100644 --- a/docs/install.md +++ b/docs/install.md @@ -233,17 +233,46 @@ sudo apt install certbot python3-certbot-nginx 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 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 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 -never be reachable from the open internet directly. Point the proxy's -upstream/forward target at `this-host:8000`, and make sure it forwards -`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. +never be reachable from the open internet directly. This isn't optional +hardening: Evection trusts `X-Forwarded-Proto` and `X-Forwarded-For` from +whoever can reach this port at all (see `config/settings/prod.py` and +`apps/accounts/views.py`'s login rate limiter), so anyone who can reach it +directly can set those headers themselves and be trusted as if they were +the proxy. With ufw, that's: + +```bash +sudo ufw allow from 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 forwards once it's terminating TLS — Evection's `prod` settings