@@ -96,9 +96,10 @@ def _get_environ(environ):
96
96
"""
97
97
Returns our whitelisted environment variables.
98
98
"""
99
- keys = ( "SERVER_NAME" , "SERVER_PORT" )
99
+ keys = [ "SERVER_NAME" , "SERVER_PORT" ]
100
100
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" ]
102
103
103
104
for key in keys :
104
105
if key in environ :
@@ -129,16 +130,21 @@ def _get_headers(environ):
129
130
def get_client_ip (environ ):
130
131
# type: (Dict[str, str]) -> Optional[Any]
131
132
"""
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.
137
136
"""
138
137
try :
139
138
return environ ["HTTP_X_FORWARDED_FOR" ].split ("," )[0 ].strip ()
140
139
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" )
142
148
143
149
144
150
def _capture_exception (hub ):
0 commit comments