Skip to content

Commit 36eeba2

Browse files
authored
fix: Honor X-Real-IP (getsentry#347)
* fix: Honor X-Real-IP * fix: Mypy
1 parent 84db8e1 commit 36eeba2

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

sentry_sdk/integrations/wsgi.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ def _get_environ(environ):
9696
"""
9797
Returns our whitelisted environment variables.
9898
"""
99-
keys = ("SERVER_NAME", "SERVER_PORT")
99+
keys = ["SERVER_NAME", "SERVER_PORT"]
100100
if _should_send_default_pii():
101-
keys += ("REMOTE_ADDR",) # type: ignore
101+
# Add all three headers here to make debugging of proxy setup easier.
102+
keys += ["REMOTE_ADDR", "HTTP_X_FORWARDED_FOR", "HTTP_X_REAL_IP"]
102103

103104
for key in keys:
104105
if key in environ:
@@ -129,16 +130,21 @@ def _get_headers(environ):
129130
def get_client_ip(environ):
130131
# type: (Dict[str, str]) -> Optional[Any]
131132
"""
132-
Naively yank the first IP address in an X-Forwarded-For header
133-
and assume this is correct.
134-
135-
Note: Don't use this in security sensitive situations since this
136-
value may be forged from a client.
133+
Infer the user IP address from various headers. This cannot be used in
134+
security sensitive situations since the value may be forged from a client,
135+
but it's good enough for the event payload.
137136
"""
138137
try:
139138
return environ["HTTP_X_FORWARDED_FOR"].split(",")[0].strip()
140139
except (KeyError, IndexError):
141-
return environ.get("REMOTE_ADDR")
140+
pass
141+
142+
try:
143+
return environ["HTTP_X_REAL_IP"]
144+
except KeyError:
145+
pass
146+
147+
return environ.get("REMOTE_ADDR")
142148

143149

144150
def _capture_exception(hub):

0 commit comments

Comments
 (0)