Skip to content

Commit 7592cf2

Browse files
committed
use legacy postfix
1 parent a1bb857 commit 7592cf2

8 files changed

Lines changed: 140 additions & 24 deletions

File tree

‎sentry_sdk/integrations/_asgi_common.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import sentry_sdk
66
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
7-
from sentry_sdk.integrations._wsgi_common import _filter_headers
7+
from sentry_sdk.integrations._wsgi_common import _filter_headers_legacy
88
from sentry_sdk.scope import should_send_default_pii
99
from sentry_sdk.utils import has_data_collection_enabled
1010

@@ -121,7 +121,7 @@ def _get_request_data(
121121

122122
headers = _get_headers(asgi_scope)
123123

124-
request_data["headers"] = _filter_headers(
124+
request_data["headers"] = _filter_headers_legacy(
125125
headers,
126126
use_annotated_value=False,
127127
)
@@ -175,7 +175,7 @@ def _get_request_attributes(
175175

176176
headers = _get_headers(asgi_scope)
177177

178-
filtered_headers = _filter_headers(headers, use_annotated_value=False)
178+
filtered_headers = _filter_headers_legacy(headers, use_annotated_value=False)
179179
for header, value in filtered_headers.items():
180180
attributes[f"http.request.header.{header.lower()}"] = value
181181

‎sentry_sdk/integrations/_wsgi_common.py‎

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import sentry_sdk
55
from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE
66
from sentry_sdk.data_collection import _apply_key_value_collection_filtering
7-
from sentry_sdk.utils import AnnotatedValue
7+
from sentry_sdk.scope import should_send_default_pii
8+
from sentry_sdk.utils import AnnotatedValue, has_data_collection_enabled
89

910
try:
1011
from django.http.request import RawPostDataException
@@ -34,6 +35,22 @@
3435
"TRACE",
3536
)
3637

38+
SENSITIVE_ENV_KEYS = (
39+
"REMOTE_ADDR",
40+
"HTTP_X_FORWARDED_FOR",
41+
"HTTP_SET_COOKIE",
42+
"HTTP_COOKIE",
43+
"HTTP_AUTHORIZATION",
44+
"HTTP_PROXY_AUTHORIZATION",
45+
"HTTP_X_API_KEY",
46+
"HTTP_X_FORWARDED_FOR",
47+
"HTTP_X_REAL_IP",
48+
)
49+
50+
SENSITIVE_HEADERS = tuple(
51+
x[len("HTTP_") :] for x in SENSITIVE_ENV_KEYS if x.startswith("HTTP_")
52+
)
53+
3754

3855
def request_body_within_bounds(
3956
client: "Optional[sentry_sdk.client.BaseClient]", content_length: int
@@ -185,6 +202,63 @@ def env(self) -> "Dict[str, Any]":
185202
raise NotImplementedError()
186203

187204

205+
class LegacyRequestExtractor(RequestExtractor):
206+
def extract_into_event(self, event: "Event") -> None:
207+
client = sentry_sdk.get_client()
208+
if not client.is_active():
209+
return
210+
211+
data: "Optional[Union[AnnotatedValue, Dict[str, Any]]]" = None
212+
content_length = self.content_length()
213+
request_info = event.get("request", {})
214+
215+
# Prior to data collection being implemented we unconditionally attached
216+
# the request body, which is why we default to True here.
217+
attach_request_body = True
218+
if has_data_collection_enabled(client.options):
219+
cookies = _apply_key_value_collection_filtering(
220+
items=dict(self.cookies()),
221+
behaviour=client.options["data_collection"]["cookies"],
222+
)
223+
if cookies:
224+
request_info["cookies"] = cookies
225+
226+
attach_request_body = (
227+
"incoming_request" in client.options["data_collection"]["http_bodies"]
228+
)
229+
elif should_send_default_pii():
230+
request_info["cookies"] = dict(self.cookies())
231+
232+
if attach_request_body:
233+
if not request_body_within_bounds(client, content_length):
234+
data = AnnotatedValue.removed_because_over_size_limit()
235+
else:
236+
# First read the raw body data
237+
# It is important to read this first because if it is Django
238+
# it will cache the body and then we can read the cached version
239+
# again in parsed_body() (or json() or wherever).
240+
raw_data = None
241+
try:
242+
raw_data = self.raw_data()
243+
except _RAW_DATA_EXCEPTIONS:
244+
# If DjangoRestFramework is used it already read the body for us
245+
# so reading it here will fail. We can ignore this.
246+
pass
247+
248+
parsed_body = self.parsed_body()
249+
if parsed_body is not None:
250+
data = parsed_body
251+
elif raw_data:
252+
data = AnnotatedValue.removed_because_raw_data()
253+
else:
254+
data = None
255+
256+
if data is not None:
257+
request_info["data"] = data
258+
259+
event["request"] = deepcopy(request_info)
260+
261+
188262
def _is_json_content_type(ct: "Optional[str]") -> bool:
189263
mt = (ct or "").split(";", 1)[0]
190264
return (
@@ -210,3 +284,42 @@ def _filter_headers(
210284
filtered[key] = SENSITIVE_DATA_SUBSTITUTE
211285

212286
return filtered
287+
288+
289+
def _filter_headers_legacy(
290+
headers: "Mapping[str, str]",
291+
use_annotated_value: bool = True,
292+
) -> "Mapping[str, Union[AnnotatedValue, str]]":
293+
client_options = sentry_sdk.get_client().options
294+
295+
if has_data_collection_enabled(client_options):
296+
data_collection_configuration = client_options["data_collection"]
297+
filtered = _apply_key_value_collection_filtering(
298+
items=headers,
299+
behaviour=data_collection_configuration["http_headers"]["request"],
300+
)
301+
302+
for key in filtered:
303+
if isinstance(key, str) and key.lower() in ("cookie", "set-cookie"):
304+
filtered[key] = SENSITIVE_DATA_SUBSTITUTE
305+
306+
return filtered
307+
308+
else:
309+
if should_send_default_pii():
310+
return headers
311+
312+
substitute: "Union[AnnotatedValue, str]" = (
313+
SENSITIVE_DATA_SUBSTITUTE
314+
if not use_annotated_value
315+
else AnnotatedValue.removed_because_over_size_limit()
316+
)
317+
318+
return {
319+
k: (
320+
v
321+
if k.upper().replace("-", "_") not in SENSITIVE_HEADERS
322+
else substitute
323+
)
324+
for k, v in headers.items()
325+
}

‎sentry_sdk/integrations/django/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from sentry_sdk.integrations._wsgi_common import (
1717
DEFAULT_HTTP_METHODS_TO_CAPTURE,
18-
RequestExtractor,
18+
LegacyRequestExtractor,
1919
)
2020
from sentry_sdk.integrations.logging import ignore_logger_for_events
2121
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
@@ -703,7 +703,7 @@ def sentry_patched_response_for_exception(
703703
exception_handler.response_for_exception = sentry_patched_response_for_exception
704704

705705

706-
class DjangoRequestExtractor(RequestExtractor):
706+
class DjangoRequestExtractor(LegacyRequestExtractor):
707707
def __init__(self, request: "Union[WSGIRequest, ASGIRequest]") -> None:
708708
try:
709709
drf_request = request._sentry_drf_request_backref()
@@ -747,7 +747,7 @@ def parsed_body(self) -> "Optional[Dict[str, Any]]":
747747
try:
748748
return self.request.data
749749
except Exception:
750-
return RequestExtractor.parsed_body(self)
750+
return LegacyRequestExtractor.parsed_body(self)
751751

752752

753753
def _set_user_info(request: "WSGIRequest", event: "Event") -> None:

‎sentry_sdk/integrations/flask.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
66
from sentry_sdk.integrations._wsgi_common import (
77
DEFAULT_HTTP_METHODS_TO_CAPTURE,
8-
RequestExtractor,
8+
LegacyRequestExtractor,
99
)
1010
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1111
from sentry_sdk.scope import should_send_default_pii
@@ -185,7 +185,7 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None:
185185
scope.add_event_processor(evt_processor)
186186

187187

188-
class FlaskRequestExtractor(RequestExtractor):
188+
class FlaskRequestExtractor(LegacyRequestExtractor):
189189
def env(self) -> "Dict[str, str]":
190190
return self.request.environ
191191

‎sentry_sdk/integrations/gcp.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sentry_sdk.consts import OP
1010
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
1111
from sentry_sdk.integrations import Integration
12-
from sentry_sdk.integrations._wsgi_common import _filter_headers
12+
from sentry_sdk.integrations._wsgi_common import _filter_headers_legacy
1313
from sentry_sdk.integrations.cloud_resource_context import CLOUD_PROVIDER
1414
from sentry_sdk.scope import Scope, should_send_default_pii
1515
from sentry_sdk.traces import SegmentNameSource
@@ -86,7 +86,7 @@ def sentry_func(
8686
header_attributes: "dict[str, Any]" = {}
8787
if hasattr(gcp_event, "headers"):
8888
headers = gcp_event.headers
89-
for header, header_value in _filter_headers(
89+
for header, header_value in _filter_headers_legacy(
9090
headers, use_annotated_value=False
9191
).items():
9292
header_attributes[f"http.request.header.{header.lower()}"] = (
@@ -244,7 +244,7 @@ def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]":
244244
request["query_string"] = query_string
245245

246246
if hasattr(gcp_event, "headers"):
247-
request["headers"] = _filter_headers(gcp_event.headers)
247+
request["headers"] = _filter_headers_legacy(gcp_event.headers)
248248

249249
if hasattr(gcp_event, "data"):
250250
if has_data_collection_enabled(client_options):

‎sentry_sdk/integrations/quart.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sentry_sdk.consts import SPANDATA
99
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
1010
from sentry_sdk.integrations import DidNotEnable, Integration
11-
from sentry_sdk.integrations._wsgi_common import _filter_headers
11+
from sentry_sdk.integrations._wsgi_common import _filter_headers_legacy
1212
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1313
from sentry_sdk.scope import should_send_default_pii
1414
from sentry_sdk.traces import SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE
@@ -187,7 +187,7 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None:
187187
segment.set_attribute("http.request.method", request_websocket.method)
188188
header_attributes: "dict[str, Any]" = {}
189189

190-
for header, header_value in _filter_headers(
190+
for header, header_value in _filter_headers_legacy(
191191
dict(request_websocket.headers), use_annotated_value=False
192192
).items():
193193
header_attributes[f"http.request.header.{header.lower()}"] = header_value
@@ -282,7 +282,7 @@ def inner(event: "Event", hint: "dict[str, Any]") -> "Event":
282282
request_info["url"] = request.url
283283
request_info["query_string"] = request.query_string
284284
request_info["method"] = request.method
285-
request_info["headers"] = _filter_headers(dict(request.headers))
285+
request_info["headers"] = _filter_headers_legacy(dict(request.headers))
286286

287287
client_options = sentry_sdk.get_client().options
288288
if has_data_collection_enabled(client_options):

‎sentry_sdk/integrations/sanic.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
_apply_data_collection_filtering_to_query_string,
1111
)
1212
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
13-
from sentry_sdk.integrations._wsgi_common import RequestExtractor, _filter_headers
13+
from sentry_sdk.integrations._wsgi_common import (
14+
LegacyRequestExtractor,
15+
_filter_headers_legacy,
16+
)
1417
from sentry_sdk.scope import should_send_default_pii
1518
from sentry_sdk.traces import SegmentNameSource
1619
from sentry_sdk.utils import (
@@ -64,7 +67,7 @@ def setup_once() -> None:
6467
_setup_sanic()
6568

6669

67-
class SanicRequestExtractor(RequestExtractor):
70+
class SanicRequestExtractor(LegacyRequestExtractor):
6871
def content_length(self) -> int:
6972
if self.request.body is None:
7073
return 0
@@ -251,7 +254,7 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]":
251254
if request.method:
252255
attributes[SPANDATA.HTTP_REQUEST_METHOD] = request.method.upper()
253256

254-
headers = _filter_headers(dict(request.headers), use_annotated_value=False)
257+
headers = _filter_headers_legacy(dict(request.headers), use_annotated_value=False)
255258
for header, value in headers.items():
256259
attributes[f"{SPANDATA.HTTP_REQUEST_HEADER}.{header.lower()}"] = value
257260

@@ -340,7 +343,7 @@ def sanic_processor(event: "Event", hint: "Optional[Hint]") -> "Optional[Event]"
340343
or client_options["data_collection"]["user_info"]
341344
):
342345
request_info["env"] = {"REMOTE_ADDR": request.remote_addr}
343-
request_info["headers"] = _filter_headers(dict(request.headers))
346+
request_info["headers"] = _filter_headers_legacy(dict(request.headers))
344347

345348
return event
346349

‎sentry_sdk/integrations/tornado.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
77
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
88
from sentry_sdk.integrations._wsgi_common import (
9-
RequestExtractor,
10-
_filter_headers,
9+
LegacyRequestExtractor,
10+
_filter_headers_legacy,
1111
_is_json_content_type,
1212
request_body_within_bounds,
1313
)
@@ -147,7 +147,7 @@ def _get_request_attributes(request: "Any") -> "Dict[str, Any]":
147147
if request.method:
148148
attributes[SPANDATA.HTTP_REQUEST_METHOD] = request.method.upper()
149149

150-
headers = _filter_headers(dict(request.headers), use_annotated_value=False)
150+
headers = _filter_headers_legacy(dict(request.headers), use_annotated_value=False)
151151
for header, value in headers.items():
152152
attributes[f"{SPANDATA.HTTP_REQUEST_HEADER}.{header.lower()}"] = value
153153

@@ -280,7 +280,7 @@ def tornado_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
280280
or client_options["data_collection"]["user_info"]
281281
):
282282
request_info["env"] = {"REMOTE_ADDR": request.remote_ip}
283-
request_info["headers"] = _filter_headers(dict(request.headers))
283+
request_info["headers"] = _filter_headers_legacy(dict(request.headers))
284284

285285
if has_data_collection_enabled(client_options):
286286
if client_options["data_collection"]["user_info"]:
@@ -305,7 +305,7 @@ def tornado_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
305305
return tornado_processor
306306

307307

308-
class TornadoRequestExtractor(RequestExtractor):
308+
class TornadoRequestExtractor(LegacyRequestExtractor):
309309
def content_length(self) -> int:
310310
if self.request.body is None:
311311
return 0

0 commit comments

Comments
 (0)