mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 04:53:36 +00:00
middleware: Do not trust X-Forwarded-For; use X-Real-Ip, set from nginx.
The `X-Forwarded-For` header is a list of proxies' IP addresses; each proxy appends the remote address of the host it received its request from to the list, as it passes the request down. A naïve parsing, as SetRemoteAddrFromForwardedFor did, would thus interpret the first address in the list as the client's IP. However, clients can pass in arbitrary `X-Forwarded-For` headers, which would allow them to spoof their IP address. `nginx`'s behavior is to treat the addresses as untrusted unless they match an allowlist of known proxies. By setting `real_ip_recursive on`, it also allows this behavior to be applied repeatedly, moving from right to left down the `X-Forwarded-For` list, stopping at the right-most that is untrusted. Rather than re-implement this logic in Django, pass the first untrusted value that `nginx` computer down into Django via `X-Real-Ip` header. This allows consistent IP addresses in logs between `nginx` and Django. Proxied calls into Tornado (which don't use UWSGI) already passed this header, as Tornado logging respects it.
This commit is contained in:
committed by
Alex Vandiver
parent
5aefb5e656
commit
07779ea879
@@ -172,7 +172,7 @@ MIDDLEWARE = (
|
||||
# With the exception of it's dependencies,
|
||||
# our logging middleware should be the top middleware item.
|
||||
"zerver.middleware.TagRequests",
|
||||
"zerver.middleware.SetRemoteAddrFromForwardedFor",
|
||||
"zerver.middleware.SetRemoteAddrFromRealIpHeader",
|
||||
"zerver.middleware.RequestContext",
|
||||
"zerver.middleware.LogRequests",
|
||||
"zerver.middleware.JsonErrorHandler",
|
||||
|
||||
Reference in New Issue
Block a user