Skip to content

Commit 666e6fb

Browse files
committed
fix(asgi): Gate query string and client IP behind send_default_pii
Move http.query and client.address attribute collection inside the should_send_default_pii() check so sensitive values are not captured by default. Fixes PY-2514 Fixes #6499
1 parent 359ba7e commit 666e6fb

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

sentry_sdk/integrations/_asgi_common.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
121121
for header, value in headers.items():
122122
attributes[f"http.request.header.{header.lower()}"] = value
123123

124+
if should_send_default_pii():
124125
query = _get_query(asgi_scope)
125126
if query:
126127
attributes["http.query"] = query
@@ -129,9 +130,9 @@ def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
129130
asgi_scope, "http" if ty == "http" else "ws", headers.get("host")
130131
)
131132

132-
client = asgi_scope.get("client")
133-
if client and should_send_default_pii():
134-
ip = _get_ip(asgi_scope)
135-
attributes["client.address"] = ip
133+
client = asgi_scope.get("client")
134+
if client:
135+
ip = _get_ip(asgi_scope)
136+
attributes["client.address"] = ip
136137

137138
return attributes

tests/integrations/asgi/test_asgi.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ def test_invalid_transaction_style(asgi3_app):
164164

165165

166166
@pytest.mark.asyncio
167+
@pytest.mark.parametrize(
168+
"should_send_pii",
169+
[True, False],
170+
)
167171
@pytest.mark.parametrize(
168172
"span_streaming",
169173
[True, False],
@@ -174,9 +178,10 @@ async def test_capture_transaction(
174178
capture_events,
175179
capture_items,
176180
span_streaming,
181+
should_send_pii,
177182
):
178183
sentry_init(
179-
send_default_pii=True,
184+
send_default_pii=should_send_pii,
180185
traces_sample_rate=1.0,
181186
_experiments={
182187
"trace_lifecycle": "stream" if span_streaming else "static",
@@ -203,16 +208,18 @@ async def test_capture_transaction(
203208
assert span["attributes"]["sentry.span.source"] == "url"
204209
assert span["attributes"]["sentry.op"] == "http.server"
205210

206-
assert span["attributes"]["url.full"] == "http://localhost/some_url"
207211
assert span["attributes"]["network.protocol.name"] == "http"
208212
assert span["attributes"]["http.request.method"] == "GET"
209-
assert span["attributes"]["http.query"] == "somevalue=123"
210213
assert span["attributes"]["http.request.header.host"] == "localhost"
211214
assert span["attributes"]["http.request.header.remote-addr"] == "127.0.0.1"
212215
assert (
213216
span["attributes"]["http.request.header.user-agent"] == "ASGI-Test-Client"
214217
)
215218

219+
if should_send_pii:
220+
assert span["attributes"]["url.full"] == "http://localhost/some_url"
221+
assert span["attributes"]["http.query"] == "somevalue=123"
222+
216223
else:
217224
(transaction_event,) = events
218225

0 commit comments

Comments
 (0)