Skip to content

Commit b898cd9

Browse files
authored
fix(gql): Remove credentials and query values from the transport URL (#8227)
### Description The gql integration copied `transport.url` into `event["request"]["url"]` unchanged, so credentials and query tokens in the URL were sent with every event, whatever `send_default_pii` was set to. The URL now goes through `parse_url`. The authority is filtered in `url`, and the query goes to `query_string`. One change from what I suggested in the issue: I kept `parse_url`'s default sanitizing instead of `sanitize=False`. With `sanitize=False`, a token such as `access_token=...` would only move from `url` to `query_string` and still be sent. Query values are now always `[Filtered]`, with the keys kept, even with `send_default_pii=True`. If you'd rather keep the values when PII is enabled, that's a small change. The URL is passed through `str()` because some transports accept a URL object as well as a string. If parsing fails, `url` is left out of the event. I ran `tests/integrations/gql` through tox on `py3.10-gql-v3.4.1`, `py3.14-gql-v4.4.0` and `py3.14-gql-latest`, plus ruff and mypy. Without the change, the new sanitizing tests fail. #### Issues * resolves: #8216 * resolves: PY-3071
1 parent b7c4236 commit b898cd9

2 files changed

Lines changed: 111 additions & 3 deletions

File tree

‎sentry_sdk/integrations/gql.py‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import sentry_sdk
2+
from sentry_sdk.data_collection import (
3+
_apply_data_collection_filtering_to_query_string,
4+
)
25
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
36
from sentry_sdk.scope import should_send_default_pii
47
from sentry_sdk.utils import (
8+
capture_internal_exceptions,
59
ensure_integration_enabled,
610
event_from_exception,
711
has_data_collection_enabled,
12+
parse_url,
813
parse_version,
914
)
1015

@@ -36,7 +41,7 @@
3641
from typing import TYPE_CHECKING
3742

3843
if TYPE_CHECKING:
39-
from typing import Any, Dict, Tuple, Union
44+
from typing import Any, Dict, Optional, Tuple, Union
4045

4146
from sentry_sdk._types import Event, EventProcessor
4247

@@ -99,9 +104,29 @@ def _request_info_from_transport(
99104
}
100105

101106
try:
102-
request_info["url"] = transport.url
107+
url = transport.url
103108
except AttributeError:
104-
pass
109+
return request_info
110+
111+
with capture_internal_exceptions():
112+
client_options = sentry_sdk.get_client().options
113+
use_data_collection = has_data_collection_enabled(client_options)
114+
115+
# Some transports accept a URL object as well as a string.
116+
# Credentials are always removed from the URL. Query values are
117+
# filtered here only when data_collection is not configured, since
118+
# its url_query_params setting does its own filtering.
119+
parsed_url = parse_url(str(url), sanitize=not use_data_collection)
120+
request_info["url"] = parsed_url.url
121+
122+
query_string: "Optional[str]" = parsed_url.query
123+
if use_data_collection and query_string:
124+
query_string = _apply_data_collection_filtering_to_query_string(
125+
query_string=query_string,
126+
behaviour=client_options["data_collection"]["url_query_params"],
127+
)
128+
if query_string:
129+
request_info["query_string"] = query_string
105130

106131
return request_info
107132

‎tests/integrations/gql/test_gql.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import pytest
24
import responses
35
from gql import Client, __version__, gql
@@ -273,3 +275,84 @@ def test_real_gql_request_with_error_data_collection_variables(
273275
assert event["request"]["data"].get("variables")
274276
else:
275277
assert not event["request"]["data"].get("variables")
278+
279+
280+
@pytest.mark.parametrize(
281+
"init_kwargs,expected_query_string",
282+
[
283+
pytest.param(
284+
{},
285+
"access_token=[Filtered]&v=[Filtered]&page=[Filtered]",
286+
id="no_pii_no_data_collection",
287+
),
288+
pytest.param(
289+
{"send_default_pii": True},
290+
"access_token=[Filtered]&v=[Filtered]&page=[Filtered]",
291+
id="legacy_pii_on",
292+
),
293+
pytest.param(
294+
{"data_collection": {}},
295+
"access_token=%5BFiltered%5D&v=1&page=2",
296+
id="data_collection_defaults",
297+
),
298+
pytest.param(
299+
{"data_collection": {"url_query_params": {"mode": "off"}}},
300+
None,
301+
id="data_collection_query_params_off",
302+
),
303+
pytest.param(
304+
{
305+
"data_collection": {
306+
"url_query_params": {"mode": "denylist", "terms": ["page"]}
307+
}
308+
},
309+
"access_token=%5BFiltered%5D&v=1&page=%5BFiltered%5D",
310+
id="data_collection_denylist",
311+
),
312+
pytest.param(
313+
{
314+
"data_collection": {
315+
"url_query_params": {"mode": "allowlist", "terms": ["page"]}
316+
}
317+
},
318+
"access_token=%5BFiltered%5D&v=%5BFiltered%5D&page=2",
319+
id="data_collection_allowlist",
320+
),
321+
pytest.param(
322+
{
323+
"send_default_pii": True,
324+
"data_collection": {"url_query_params": {"mode": "off"}},
325+
},
326+
None,
327+
id="data_collection_takes_precedence_over_send_default_pii",
328+
),
329+
],
330+
)
331+
@responses.activate
332+
def test_transport_url_is_sanitized(
333+
sentry_init, capture_events, init_kwargs, expected_query_string
334+
):
335+
sentry_init(integrations=[GQLIntegration()], **init_kwargs)
336+
events = capture_events()
337+
338+
responses.add(
339+
method=responses.POST,
340+
url=re.compile(r"https://.*api\.example\.com/graphql.*"),
341+
json={"errors": [{"message": "some error"}]},
342+
status=200,
343+
)
344+
transport = RequestsHTTPTransport(
345+
url="https://user:password@api.example.com/graphql?access_token=secret&v=1&page=2#frag"
346+
)
347+
client = Client(transport=transport)
348+
349+
with pytest.raises(TransportQueryError):
350+
client.execute(gql("query Example { example }"))
351+
352+
(event,) = events
353+
assert event["request"]["url"] == (
354+
"https://[Filtered]:[Filtered]@api.example.com/graphql"
355+
)
356+
assert event["request"].get("query_string") == expected_query_string
357+
assert "password" not in str(event["request"])
358+
assert "secret" not in str(event["request"])

0 commit comments

Comments
 (0)