Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7541460
ref(integrations): route HTTP header filtering through data collectio…
ericapisani Jul 8, 2026
5e9f610
Merge branch 'master' into py-2584-update-wsgi-filter-headers
ericapisani Jul 9, 2026
a590c8e
Make changes to adjust to the data collection option being within the…
ericapisani Jul 9, 2026
16ed0c8
fix test
ericapisani Jul 9, 2026
486f0bc
make sure host header is not filtered when being added to the url
ericapisani Jul 9, 2026
7d14a81
add test coverage for edge case around host header being dropped but …
ericapisani Jul 9, 2026
bb98334
add aws lambda test coverage
ericapisani Jul 9, 2026
61d1618
fix(aws-lambda): Remove vendored deps from new embedded-sdk test fixt…
ericapisani Jul 9, 2026
951c408
add test coverage within the wsgi test file
ericapisani Jul 9, 2026
a19be39
feat(integrations): apply data_collection cookie filtering to wsgi, s…
ericapisani Jul 10, 2026
620e222
fix(integrations): omit cookies field when data_collection cookies mo…
ericapisani Jul 10, 2026
ec6f520
update tornado test
ericapisani Jul 10, 2026
4dd3ba6
ref(integrations): route HTTP header filtering through data collectio…
ericapisani Jul 8, 2026
d0bae90
Make changes to adjust to the data collection option being within the…
ericapisani Jul 9, 2026
a6c4688
fix test
ericapisani Jul 9, 2026
d7b378b
make sure host header is not filtered when being added to the url
ericapisani Jul 9, 2026
52c2da1
add test coverage for edge case around host header being dropped but …
ericapisani Jul 9, 2026
aa645d7
add aws lambda test coverage
ericapisani Jul 9, 2026
8b6e3ad
fix(aws-lambda): Remove vendored deps from new embedded-sdk test fixt…
ericapisani Jul 9, 2026
d717172
add test coverage within the wsgi test file
ericapisani Jul 9, 2026
c7f590f
Merge branch 'py-2584-update-wsgi-filter-headers' of github.com:getse…
ericapisani Jul 14, 2026
293a291
feat(integrations): apply data_collection cookie filtering to wsgi, s…
ericapisani Jul 10, 2026
530e83c
fix(integrations): omit cookies field when data_collection cookies mo…
ericapisani Jul 10, 2026
326d796
update tornado test
ericapisani Jul 10, 2026
e9f0e20
Merge branch 'master' into py-2584-update-wsgi-filter-headers
ericapisani Jul 15, 2026
48f7c7b
Merge branch 'py-2581-cookies' of github.com:getsentry/sentry-python …
ericapisani Jul 15, 2026
621d79b
Merge branch 'py-2584-update-wsgi-filter-headers' into py-2581-cookies
ericapisani Jul 15, 2026
3d7bddb
test(aiohttp): Expect single span in streaming passthrough test
ericapisani Jul 15, 2026
fad2292
Merge branch 'py-2584-update-wsgi-filter-headers' into py-2581-cookies
ericapisani Jul 15, 2026
40d9f38
feat(wsgi): Apply data_collection filtering to URL query strings
ericapisani Jul 15, 2026
e6e7148
Merge branch 'py-2581-cookies' into py-2583-query-parameters
ericapisani Jul 15, 2026
1f9bce7
lint
ericapisani Jul 15, 2026
fad0c9a
Do not encode what is added to event/span attributes in order to conf…
ericapisani Jul 16, 2026
21cf40b
feat(asgi): Apply data_collection filtering to URL query strings
ericapisani Jul 16, 2026
3f0bc2a
lint
ericapisani Jul 16, 2026
19f4778
Merge branch 'master' into py-2583-query-params-asgi
ericapisani Jul 22, 2026
657c186
test(asgi): Update query param assertions for encoded query strings
ericapisani Jul 22, 2026
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
53 changes: 50 additions & 3 deletions sentry_sdk/integrations/_asgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
from enum import Enum
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import has_data_collection_enabled

if TYPE_CHECKING:
from typing import Any, Dict, Optional, Union
Expand Down Expand Up @@ -75,7 +78,7 @@ def _get_url(
return path


def _get_query(asgi_scope: "Any") -> "Any":
def _get_query(asgi_scope: "Any") -> "Optional[str]":
"""
Extract querystring from the ASGI scope, in the format that the Sentry protocol expects.
"""
Expand Down Expand Up @@ -122,7 +125,20 @@ def _get_request_data(
use_annotated_value=False,
)

request_data["query_string"] = _get_query(asgi_scope)
client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
qs = _get_query(asgi_scope)
if qs:
filtered_query_string = (
_apply_data_collection_filtering_to_query_string(
query_string=qs,
behaviour=client_options["data_collection"]["url_query_params"],
)
Comment thread
ericapisani marked this conversation as resolved.
)
if filtered_query_string:
request_data["query_string"] = filtered_query_string
else:
request_data["query_string"] = _get_query(asgi_scope)

request_data["url"] = _get_url(
asgi_scope,
Expand Down Expand Up @@ -158,7 +174,38 @@ def _get_request_attributes(
for header, value in filtered_headers.items():
attributes[f"http.request.header.{header.lower()}"] = value

if should_send_default_pii():
client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
filtered_query_string = None
query = _get_query(asgi_scope)

if query:
filtered_query_string = (
_apply_data_collection_filtering_to_query_string(
query_string=query,
behaviour=client_options["data_collection"]["url_query_params"],
)
)
if filtered_query_string:
attributes["http.query"] = filtered_query_string

path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path)
attributes["url.path"] = path

url_without_query_string = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
headers.get("host"),
path=path,
)

attributes["url.full"] = (
f"{url_without_query_string}?{filtered_query_string}"
if filtered_query_string is not None
else url_without_query_string
)

elif should_send_default_pii():
query = _get_query(asgi_scope)
if query:
attributes["http.query"] = query
Expand Down
209 changes: 209 additions & 0 deletions tests/integrations/asgi/test_asgi.py
Comment thread
ericapisani marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,215 @@ def test_get_request_attributes_url_with_headers_off(sentry_init):
assert attributes["url.full"] == "http://example.com/foo?somevalue=123"


QUERY_STRING = "token=abc&theme=dark&lang=en&session=xyz"


def _http_scope(query_string=QUERY_STRING):
return {
"type": "http",
"method": "GET",
"scheme": "http",
"server": ("example.com", 80),
"path": "/foo",
"query_string": query_string.encode("latin-1"),
"headers": [(b"host", b"example.com")],
}


@pytest.mark.parametrize(
"init_kwargs, expected_query_string",
[
pytest.param(
{"send_default_pii": True},
QUERY_STRING,
id="send_default_pii_true",
),
pytest.param(
{"send_default_pii": False},
QUERY_STRING,
id="send_default_pii_false",
),
pytest.param(
{},
QUERY_STRING,
id="defaults",
),
pytest.param(
{"_experiments": {"data_collection": {}}},
"token=%5BFiltered%5D&theme=dark&lang=en&session=%5BFiltered%5D",
id="data_collection_denylist_default",
Comment thread
cursor[bot] marked this conversation as resolved.
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "denylist", "terms": ["theme"]}
}
}
},
"token=%5BFiltered%5D&theme=%5BFiltered%5D&lang=en&session=%5BFiltered%5D",
id="data_collection_denylist_custom_terms",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["theme"]}
}
}
},
"token=%5BFiltered%5D&theme=dark&lang=%5BFiltered%5D&session=%5BFiltered%5D",
id="data_collection_allowlist",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["token"]}
}
}
},
"token=%5BFiltered%5D&theme=%5BFiltered%5D&lang=%5BFiltered%5D&session=%5BFiltered%5D",
id="data_collection_allowlist_sensitive_term",
),
pytest.param(
{
"_experiments": {
"data_collection": {"url_query_params": {"mode": "off"}}
}
},
None,
id="data_collection_off",
),
# data_collection wins over send_default_pii: filtering still applies.
pytest.param(
{
"send_default_pii": True,
"_experiments": {
"data_collection": {"url_query_params": {"mode": "off"}}
},
},
None,
id="data_collection_wins_over_send_default_pii",
),
],
)
def test_get_request_data_query_string_data_collection(
sentry_init, init_kwargs, expected_query_string
):
sentry_init(**init_kwargs)

request_data = _get_request_data(_http_scope(), _RootPathInPath.EXCLUDED)

if expected_query_string is None:
assert "query_string" not in request_data
else:
assert request_data["query_string"] == expected_query_string


def test_get_request_data_query_string_empty_legacy_is_none(sentry_init):
# Legacy path: the query string is always set even when empty (``None``).
sentry_init(send_default_pii=True)

request_data = _get_request_data(
_http_scope(query_string=""), _RootPathInPath.EXCLUDED
)

assert request_data["query_string"] is None


def test_get_request_data_empty_query_string_dropped_with_data_collection(sentry_init):
sentry_init(_experiments={"data_collection": {}})

request_data = _get_request_data(
_http_scope(query_string=""), _RootPathInPath.EXCLUDED
)

assert "query_string" not in request_data


@pytest.mark.parametrize(
"init_kwargs, expected_query, expected_url_full",
[
pytest.param(
{"send_default_pii": True},
QUERY_STRING,
"http://example.com/foo?" + QUERY_STRING,
id="send_default_pii_true",
),
pytest.param(
{"send_default_pii": False},
None,
None,
id="send_default_pii_false",
),
pytest.param(
{},
None,
None,
id="defaults",
),
pytest.param(
{"_experiments": {"data_collection": {}}},
"token=%5BFiltered%5D&theme=dark&lang=en&session=%5BFiltered%5D",
"http://example.com/foo?token=%5BFiltered%5D&theme=dark&lang=en&session=%5BFiltered%5D",
id="data_collection_denylist_default",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["theme"]}
}
}
},
"token=%5BFiltered%5D&theme=dark&lang=%5BFiltered%5D&session=%5BFiltered%5D",
"http://example.com/foo?token=%5BFiltered%5D&theme=dark&lang=%5BFiltered%5D&session=%5BFiltered%5D",
id="data_collection_allowlist",
),
pytest.param(
{
"_experiments": {
"data_collection": {"url_query_params": {"mode": "off"}}
}
},
None,
"http://example.com/foo",
id="data_collection_off",
),
pytest.param(
{
"send_default_pii": True,
"_experiments": {
"data_collection": {"url_query_params": {"mode": "off"}}
},
},
None,
"http://example.com/foo",
id="data_collection_wins_over_send_default_pii",
),
],
)
def test_get_request_attributes_query_data_collection(
sentry_init, init_kwargs, expected_query, expected_url_full
):
sentry_init(**init_kwargs)

attributes = _get_request_attributes(_http_scope(), _RootPathInPath.EXCLUDED)

if expected_query is None:
assert "http.query" not in attributes
else:
assert attributes["http.query"] == expected_query

if expected_url_full is None:
assert "url.full" not in attributes
assert "url.path" not in attributes
else:
assert attributes["url.full"] == expected_url_full
assert attributes["url.path"] == "/foo"


@pytest.mark.asyncio
@pytest.mark.parametrize(
"request_url,transaction_style,expected_transaction_name,expected_transaction_source",
Expand Down
3 changes: 0 additions & 3 deletions tests/integrations/django/test_data_scrubbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
from django.core.urlresolvers import reverse


NO_COOKIES = object()


@pytest.fixture
def client():
return Client(application)
Expand Down
3 changes: 0 additions & 3 deletions tests/integrations/litestar/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,6 @@ async def __call__(self, scope, receive, send):

COOKIE_HEADER = "jwt=tokenval; theme=dark; lang=en; identity=alice"

# Sentinel meaning "the request payload should have no ``cookies`` key at all",
# as opposed to an empty ``{}`` dict.


@pytest.mark.parametrize(
"init_kwargs, expected_cookies",
Expand Down
Loading
Loading