Skip to content
Draft
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
21 changes: 15 additions & 6 deletions sentry_sdk/integrations/_asgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,12 @@
)

client = asgi_scope.get("client")
if client and should_send_default_pii():
request_data["env"] = {"REMOTE_ADDR": _get_ip(asgi_scope)}
if client:
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:

Check warning on line 153 in sentry_sdk/integrations/_asgi_common.py

View check run for this annotation

@sentry/warden / warden: code-review

client_options may be unbound when scope type is not http/websocket

In both `_get_request_data` and `_get_request_attributes`, `client_options = sentry_sdk.get_client().options` is assigned only inside the `if ty in ("http", "websocket")` block, but is later referenced unconditionally when `client`/`asgi_scope_client` is truthy. If a scope with `ty` other than http/websocket carries a `client` entry, `has_data_collection_enabled(client_options)` raises `NameError`. In practice only http/websocket ASGI scopes carry a `client` key (lifespan scopes do not), so this path is largely unreachable with spec-compliant servers, but it is a latent regression: the prior code branched on `should_send_default_pii()` and did not depend on `client_options`.
Comment on lines +152 to +153

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client_options may be unbound when scope type is not http/websocket

In both _get_request_data and _get_request_attributes, client_options = sentry_sdk.get_client().options is assigned only inside the if ty in ("http", "websocket") block, but is later referenced unconditionally when client/asgi_scope_client is truthy. If a scope with ty other than http/websocket carries a client entry, has_data_collection_enabled(client_options) raises NameError. In practice only http/websocket ASGI scopes carry a client key (lifespan scopes do not), so this path is largely unreachable with spec-compliant servers, but it is a latent regression: the prior code branched on should_send_default_pii() and did not depend on client_options.

Evidence
  • client_options is assigned at line 128 (and 181) inside if ty in ("http", "websocket").
  • Lines 151-156 (_get_request_data) and 234-239 (_get_request_attributes) reference has_data_collection_enabled(client_options) at the outer indent, guarded only by if client: / if asgi_scope_client:, not by scope type.
  • Per ASGI, only http/websocket scopes include a client key, so client being truthy while ty is neither is not expected from compliant servers, making the NameError path hard to reach in practice.
  • The regression is that the old branch used should_send_default_pii(), which needed no client_options, so a malformed/non-standard scope with a client value would now raise instead of behaving gracefully.
Also found at 1 additional location
  • sentry_sdk/integrations/_asgi_common.py:233-235

Identified by Warden code-review · DVQ-38F

request_data["env"] = {"REMOTE_ADDR": _get_ip(asgi_scope)}
elif should_send_default_pii():
request_data["env"] = {"REMOTE_ADDR": _get_ip(asgi_scope)}

return request_data

Expand Down Expand Up @@ -226,9 +230,14 @@
else url_without_query_string
)

client = asgi_scope.get("client")
if client and should_send_default_pii():
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip
asgi_scope_client = asgi_scope.get("client")
if asgi_scope_client:
if has_data_collection_enabled(client_options):

Check warning on line 235 in sentry_sdk/integrations/_asgi_common.py

View check run for this annotation

@sentry/warden / warden: code-review

[DVQ-38F] client_options may be unbound when scope type is not http/websocket (additional location)

In both `_get_request_data` and `_get_request_attributes`, `client_options = sentry_sdk.get_client().options` is assigned only inside the `if ty in ("http", "websocket")` block, but is later referenced unconditionally when `client`/`asgi_scope_client` is truthy. If a scope with `ty` other than http/websocket carries a `client` entry, `has_data_collection_enabled(client_options)` raises `NameError`. In practice only http/websocket ASGI scopes carry a `client` key (lifespan scopes do not), so this path is largely unreachable with spec-compliant servers, but it is a latent regression: the prior code branched on `should_send_default_pii()` and did not depend on `client_options`.
if client_options["data_collection"]["user_info"]:
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip
elif should_send_default_pii():
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip

return attributes
Loading
Loading