Skip to content

Commit 064ed6a

Browse files
defensive access
1 parent 6a30714 commit 064ed6a

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

sentry_sdk/integrations/starlette.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,19 @@ def _serialize_cached_request_body_attribute(
537537
"""
538538
Returns a stringified JSON representation of the request body if the request body is cached on the ASGI scope and within size bounds.
539539
"""
540+
scope_state = request.scope.get("state", {})
540541
if (
541542
"content-length" not in request.headers
542-
or _SCOPE_STATE_JSON_REQUEST_BODY_KEY not in request.scope["state"]
543-
and _SCOPE_STATE_FORMDATA_REQUEST_BODY_KEY not in request.scope["state"]
543+
or _SCOPE_STATE_JSON_REQUEST_BODY_KEY not in scope_state
544+
and _SCOPE_STATE_FORMDATA_REQUEST_BODY_KEY not in scope_state
544545
):
545546
return None
546547

547-
content_length = int(request.headers["content-length"])
548+
try:
549+
content_length = int(request.headers["content-length"])
550+
except ValueError:
551+
return None
552+
548553
if content_length and not request_body_within_bounds(client, content_length):
549554
return OVER_SIZE_LIMIT_SUBSTITUTE
550555

0 commit comments

Comments
 (0)