Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

SENSITIVE_DATA_SUBSTITUTE = "[Filtered]"
BLOB_DATA_SUBSTITUTE = "[Blob substitute]"
OVER_SIZE_LIMIT_SUBSTITUTE = (
"[Value removed due to size of field exceeding configured maximum size.]"
)
UNPARSABLE_RAW_DATA_SUBSTITUTE = "[Value removed due to being unparsable.]"


class AnnotatedValue:
Expand Down Expand Up @@ -47,6 +51,8 @@ def __len__(self: "AnnotatedValue") -> int:
@classmethod
def removed_because_raw_data(cls) -> "AnnotatedValue":
"""The value was removed because it could not be parsed. This is done for request body values that are not json nor a form."""
# This is the legacy approach - we want to transition over to `substituted_because_raw_data` after we completely transition
# to span-first
return AnnotatedValue(
value="",
metadata={
Expand All @@ -59,12 +65,29 @@ def removed_because_raw_data(cls) -> "AnnotatedValue":
},
)

@classmethod
def substituted_because_raw_data(cls) -> "AnnotatedValue":
"""The value was replaced because it could not be parsed. This is done for request body values that are not json nor a form."""
return AnnotatedValue(
value=UNPARSABLE_RAW_DATA_SUBSTITUTE,
metadata={
"rem": [ # Remark
[
"!raw", # Unparsable raw data
"s", # The fields original value was substituted
]
]
},
)

@classmethod
def removed_because_over_size_limit(cls, value: "Any" = "") -> "AnnotatedValue":
"""
The actual value was removed because the size of the field exceeded the configured maximum size,
for example specified with the max_request_body_size sdk option.
"""
# This is the legacy approach - we want to transition over to `substituted_because_over_size_limit` after we completely transition
# to span-first
return AnnotatedValue(
value=value,
metadata={
Expand All @@ -77,6 +100,26 @@ def removed_because_over_size_limit(cls, value: "Any" = "") -> "AnnotatedValue":
},
)

@classmethod
def substituted_because_over_size_limit(
cls, value: "Any" = OVER_SIZE_LIMIT_SUBSTITUTE
) -> "AnnotatedValue":
"""
The actual value was replaced because the size of the field exceeded the configured maximum size,
for example specified with the max_request_body_size sdk option.
"""
return AnnotatedValue(
value=value,
metadata={
"rem": [ # Remark
[
"!config", # Because of configured maximum size
"s", # The fields original value was substituted
]
]
},
)

@classmethod
def substituted_because_contains_sensitive_data(cls) -> "AnnotatedValue":
"""The actual value was removed because it contained sensitive information."""
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/_asgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
if ty in ("http", "websocket"):
request_data["method"] = asgi_scope.get("method")

request_data["headers"] = headers = _filter_headers(_get_headers(asgi_scope))
request_data["headers"] = headers = _filter_headers(
_get_headers(asgi_scope),
)
Comment thread
sentry[bot] marked this conversation as resolved.
request_data["query_string"] = _get_query(asgi_scope)

request_data["url"] = _get_url(
Expand Down
10 changes: 5 additions & 5 deletions sentry_sdk/integrations/_wsgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ def _filter_headers(
if should_send_default_pii():
return headers

substitute: "Union[AnnotatedValue, str]"
if use_annotated_value:
substitute = AnnotatedValue.removed_because_over_size_limit()
else:
substitute = SENSITIVE_DATA_SUBSTITUTE
substitute: "Union[AnnotatedValue, str]" = (
SENSITIVE_DATA_SUBSTITUTE
if not use_annotated_value
else AnnotatedValue.removed_because_over_size_limit()
)
Comment thread
ericapisani marked this conversation as resolved.

return {
k: (v if k.upper().replace("-", "_") not in SENSITIVE_HEADERS else substitute)
Expand Down
Loading