Skip to content

Commit 61db0dc

Browse files
authored
fix(aiohttp): Gate server request query_string via data_collection option (#7300)
Filter the request event's query_string through the url_query_params data_collection behaviour, mirroring the existing gating for the client span's url.query attribute. Fixes PY-2752 Fixes #7299
1 parent fd25a90 commit 61db0dc

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

sentry_sdk/integrations/aiohttp.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,21 @@ def aiohttp_processor(
564564
request.path,
565565
)
566566

567-
request_info["query_string"] = request.query_string
567+
if has_data_collection_enabled(client_options):
568+
if request.query_string:
569+
filtered_query_string = (
570+
_apply_data_collection_filtering_to_query_string(
571+
query_string=request.query_string,
572+
behaviour=client_options["data_collection"][
573+
"url_query_params"
574+
],
575+
)
576+
)
577+
if filtered_query_string:
578+
request_info["query_string"] = filtered_query_string
579+
else:
580+
request_info["query_string"] = request.query_string
581+
568582
request_info["method"] = request.method
569583

570584
# REMOTE_ADDR was unconditionally set pre-data collection, so it

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,44 @@ async def hello(request):
22732273
assert inner_client_span["attributes"]["url.query"] == expected_query
22742274

22752275

2276+
@pytest.mark.asyncio
2277+
@pytest.mark.parametrize(
2278+
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
2279+
)
2280+
async def test_server_url_query_data_collection_event_processor(
2281+
sentry_init, aiohttp_client, capture_events, init_kwargs, expected_query
2282+
):
2283+
init_kwargs = dict(init_kwargs)
2284+
sentry_init(integrations=[AioHttpIntegration()], **init_kwargs)
2285+
2286+
async def hello(request):
2287+
1 / 0
2288+
2289+
app = web.Application()
2290+
app.router.add_get("/", hello)
2291+
2292+
events = capture_events()
2293+
2294+
client = await aiohttp_client(app)
2295+
resp = await client.get("/?toy=tennisball&color=red&auth=secret")
2296+
assert resp.status == 500
2297+
2298+
(event,) = events
2299+
2300+
host = event["request"]["headers"]["Host"]
2301+
assert event["request"]["url"] == "http://{host}/".format(host=host)
2302+
assert event["request"]["method"] == "GET"
2303+
2304+
if "data_collection" not in init_kwargs.get("_experiments", {}):
2305+
assert (
2306+
event["request"]["query_string"] == "toy=tennisball&color=red&auth=secret"
2307+
)
2308+
elif expected_query is None:
2309+
assert "query_string" not in event["request"]
2310+
else:
2311+
assert event["request"]["query_string"] == expected_query
2312+
2313+
22762314
@pytest.mark.asyncio
22772315
@pytest.mark.parametrize(
22782316
"init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES

0 commit comments

Comments
 (0)