Skip to content
Merged
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
206 changes: 83 additions & 123 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,38 +545,29 @@ async def hello(request):
assert error_event["contexts"]["trace"]["trace_id"] == trace_id


_BREADCRUMB_QUERY_CASES = [
pytest.param({}, "query=value", id="data_collection_default"),
pytest.param(
{"url_query_params": {"mode": "allowlist", "terms": []}},
"query=%5BFiltered%5D",
id="data_collection_allowlist_empty",
),
pytest.param(
{"url_query_params": {"mode": "off"}},
None,
id="data_collection_off",
),
]


@pytest.mark.asyncio
@pytest.mark.parametrize(
"pii_options,url_expected,query_expected",
[
({}, False, False),
(
{
"data_collection": {
"url_query_params": {"mode": "denylist", "terms": []}
}
},
True,
True,
),
(
{
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": []}
}
},
True,
False,
),
],
)
@pytest.mark.parametrize("data_collection, query_expected", _BREADCRUMB_QUERY_CASES)
async def test_crumb_capture(
sentry_init,
aiohttp_raw_server,
aiohttp_client,
capture_events,
pii_options,
url_expected,
data_collection,
query_expected,
):
def before_breadcrumb(crumb, hint):
Expand All @@ -586,7 +577,7 @@ def before_breadcrumb(crumb, hint):
sentry_init(
integrations=[AioHttpIntegration()],
before_breadcrumb=before_breadcrumb,
**pii_options,
data_collection=data_collection,
)

async def handler(request):
Expand All @@ -613,15 +604,12 @@ async def handler(request):
"reason": "OK",
}

if url_expected:
if query_expected:
expected["url"] = f"http://127.0.0.1:{raw_server.port}/?query=value"
expected["http.query"] = "query=value"
else:
expected["url"] = (
f"http://127.0.0.1:{raw_server.port}/?query=%5BFiltered%5D"
)
expected["http.query"] = "query=%5BFiltered%5D"
expected["url"] = f"http://127.0.0.1:{raw_server.port}/"
if query_expected is None:
assert "http.query" not in crumb["data"]
else:
expected["url"] += "?" + query_expected
expected["http.query"] = query_expected

assert crumb["data"] == ApproxDict(expected)

Expand All @@ -636,30 +624,7 @@ async def handler(request):
(500, "error", "Internal Server Error"),
],
)
@pytest.mark.parametrize(
"pii_options,url_expected,query_expected",
[
({}, False, False),
(
{
"data_collection": {
"url_query_params": {"mode": "denylist", "terms": []}
}
},
True,
True,
),
(
{
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": []}
}
},
True,
False,
),
],
)
@pytest.mark.parametrize("data_collection, query_expected", _BREADCRUMB_QUERY_CASES)
@pytest.mark.asyncio
async def test_crumb_capture_client_error(
sentry_init,
Expand All @@ -669,11 +634,10 @@ async def test_crumb_capture_client_error(
status_code,
level,
reason,
pii_options,
url_expected,
data_collection,
query_expected,
):
sentry_init(integrations=[AioHttpIntegration()], **pii_options)
sentry_init(integrations=[AioHttpIntegration()], data_collection=data_collection)

async def handler(request):
return web.Response(status=status_code)
Expand Down Expand Up @@ -703,15 +667,12 @@ async def handler(request):
"reason": reason,
}

if url_expected:
if query_expected:
expected["url"] = f"http://127.0.0.1:{raw_server.port}/?query=value"
expected["http.query"] = "query=value"
else:
expected["url"] = (
f"http://127.0.0.1:{raw_server.port}/?query=%5BFiltered%5D"
)
expected["http.query"] = "query=%5BFiltered%5D"
expected["url"] = f"http://127.0.0.1:{raw_server.port}/"
if query_expected is None:
assert "http.query" not in crumb["data"]
else:
expected["url"] += "?" + query_expected
expected["http.query"] = query_expected

assert crumb["data"] == ApproxDict(expected)

Expand Down Expand Up @@ -1475,9 +1436,8 @@ async def hello(request):

(server_span,) = [item.payload for item in items]

# send_default_pii defaults to False, so _filter_headers substitutes
# sensitive headers with SENSITIVE_DATA_SUBSTITUTE ("[Filtered]"). The
# original token must not leak.
# Data collection always substitutes sensitive headers with
# SENSITIVE_DATA_SUBSTITUTE ("[Filtered]"). The original token must not leak.
assert (
server_span["attributes"]["http.request.header.authorization"]
== SENSITIVE_DATA_SUBSTITUTE
Expand Down Expand Up @@ -1796,14 +1756,27 @@ async def hello(request):


@pytest.mark.asyncio
@pytest.mark.parametrize("send_pii", [True, False])
@pytest.mark.parametrize(
"data_collection, expect_query",
[
pytest.param({}, True, id="data_collection_default"),
pytest.param(
{"url_query_params": {"mode": "off"}}, False, id="data_collection_off"
),
],
)
async def test_outgoing_client_span(
sentry_init, aiohttp_raw_server, aiohttp_client, capture_items, send_pii
sentry_init,
aiohttp_raw_server,
aiohttp_client,
capture_items,
data_collection,
expect_query,
):
sentry_init(
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_pii,
data_collection=data_collection,
)

async def handler(request):
Expand Down Expand Up @@ -1844,15 +1817,15 @@ async def hello(request):
assert inner_client_span["attributes"]["http.response.status_code"] == 200
assert inner_client_span["status"] == "ok"

if send_pii:
if expect_query:
assert inner_client_span["attributes"]["url.query"] == "foo=bar"
else:
assert "url.query" not in inner_client_span["attributes"]

url_full = inner_client_span["attributes"]["url.full"]

assert url_full.startswith("http://127.0.0.1:")
assert "?foo=bar" in url_full

assert inner_client_span["attributes"]["url.path"] == "/"
url_full = inner_client_span["attributes"]["url.full"]
assert url_full.startswith("http://127.0.0.1:")
assert ("?foo=bar" in url_full) == expect_query
assert inner_client_span["attributes"]["url.path"] == "/"


@pytest.mark.asyncio
Expand Down Expand Up @@ -1888,14 +1861,20 @@ async def handler(request):


@pytest.mark.asyncio
@pytest.mark.parametrize("send_default_pii", [True, False])
@pytest.mark.parametrize(
"data_collection, expect_user_info",
[
pytest.param({}, True, id="data_collection_default"),
pytest.param({"user_info": False}, False, id="data_collection_user_info_off"),
],
)
async def test_user_ip_address_on_all_spans(
sentry_init, aiohttp_client, capture_items, send_default_pii
sentry_init, aiohttp_client, capture_items, data_collection, expect_user_info
):
sentry_init(
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
data_collection=data_collection,
)

async def hello(request):
Expand All @@ -1918,7 +1897,7 @@ async def hello(request):
assert server_span["attributes"]["sentry.segment.name.source"] == "component"
assert "sentry.segment.name.source" not in child_span["attributes"]

if send_default_pii:
if expect_user_info:
assert server_span["attributes"]["user.ip_address"] == "127.0.0.1"
assert child_span["attributes"]["user.ip_address"] == "127.0.0.1"
else:
Expand All @@ -1928,39 +1907,27 @@ async def hello(request):

_QUERY_PARAM_DATA_COLLECTION_CASES = [
pytest.param(
{"data_collection": {}},
{},
"toy=tennisball&color=red&auth=%5BFiltered%5D",
id="data_collection_denylist_default",
),
pytest.param(
{
"data_collection": {
"url_query_params": {"mode": "denylist", "terms": ["toy"]}
}
},
{"url_query_params": {"mode": "denylist", "terms": ["toy"]}},
"toy=%5BFiltered%5D&color=red&auth=%5BFiltered%5D",
id="data_collection_denylist_custom_terms",
),
pytest.param(
{
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["toy"]}
}
},
{"url_query_params": {"mode": "allowlist", "terms": ["toy"]}},
"toy=tennisball&color=%5BFiltered%5D&auth=%5BFiltered%5D",
id="data_collection_allowlist",
),
pytest.param(
{
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["auth"]}
}
},
{"url_query_params": {"mode": "allowlist", "terms": ["auth"]}},
"toy=%5BFiltered%5D&color=%5BFiltered%5D&auth=%5BFiltered%5D",
id="data_collection_allowlist_sensitive_term",
),
pytest.param(
{"data_collection": {"url_query_params": {"mode": "off"}}},
{"url_query_params": {"mode": "off"}},
None,
id="data_collection_off",
),
Expand All @@ -1969,16 +1936,15 @@ async def hello(request):

@pytest.mark.asyncio
@pytest.mark.parametrize(
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
"data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
)
async def test_server_url_query_data_collection(
sentry_init, aiohttp_client, capture_items, init_kwargs, expected_query
sentry_init, aiohttp_client, capture_items, data_collection, expected_query
):
init_kwargs = dict(init_kwargs)
sentry_init(
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
**init_kwargs,
data_collection=data_collection,
)

async def hello(request):
Expand All @@ -2005,21 +1971,20 @@ async def hello(request):

@pytest.mark.asyncio
@pytest.mark.parametrize(
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
"data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
)
async def test_client_url_query_data_collection(
sentry_init,
aiohttp_raw_server,
aiohttp_client,
capture_items,
init_kwargs,
data_collection,
expected_query,
):
init_kwargs = dict(init_kwargs)
sentry_init(
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
**init_kwargs,
data_collection=data_collection,
)

async def handler(request):
Expand Down Expand Up @@ -2052,13 +2017,12 @@ async def hello(request):

@pytest.mark.asyncio
@pytest.mark.parametrize(
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
"data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
)
async def test_server_url_query_data_collection_event_processor(
sentry_init, aiohttp_client, capture_events, init_kwargs, expected_query
sentry_init, aiohttp_client, capture_events, data_collection, expected_query
):
init_kwargs = dict(init_kwargs)
sentry_init(integrations=[AioHttpIntegration()], **init_kwargs)
sentry_init(integrations=[AioHttpIntegration()], data_collection=data_collection)

async def hello(request):
1 / 0
Expand All @@ -2078,11 +2042,7 @@ async def hello(request):
assert event["request"]["url"] == "http://{host}/".format(host=host)
assert event["request"]["method"] == "GET"

if "data_collection" not in init_kwargs:
assert (
event["request"]["query_string"] == "toy=tennisball&color=red&auth=secret"
)
elif expected_query is None:
if expected_query is None:
assert "query_string" not in event["request"]
else:
assert event["request"]["query_string"] == expected_query
Expand Down
Loading