Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parse_url #2161

Merged
merged 7 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions sentry_sdk/utils.py
Expand Up @@ -1353,8 +1353,8 @@ def from_base64(base64_string):
Components = namedtuple("Components", ["scheme", "netloc", "path", "query", "fragment"])


def sanitize_url(url, remove_authority=True, remove_query_values=True):
# type: (str, bool, bool) -> str
def sanitize_url(url, remove_authority=True, remove_query_values=True, split=False):
# type: (str, bool, bool, bool) -> Union[str, Components]
"""
Removes the authority and query parameter values from a given URL.
"""
Expand Down Expand Up @@ -1383,17 +1383,18 @@ def sanitize_url(url, remove_authority=True, remove_query_values=True):
else:
query_string = parsed_url.query

safe_url = urlunsplit(
Components(
scheme=parsed_url.scheme,
netloc=netloc,
query=query_string,
path=parsed_url.path,
fragment=parsed_url.fragment,
)
components = Components(
scheme=parsed_url.scheme,
netloc=netloc,
query=query_string,
path=parsed_url.path,
fragment=parsed_url.fragment,
)

return safe_url
if split:
return components
else:
return urlunsplit(components)


ParsedUrl = namedtuple("ParsedUrl", ["url", "query", "fragment"])
Expand All @@ -1406,9 +1407,10 @@ def parse_url(url, sanitize=True):
parameters will be sanitized to remove sensitive data. The autority (username and password)
in the URL will always be removed.
"""
url = sanitize_url(url, remove_authority=True, remove_query_values=sanitize)
parsed_url = sanitize_url(
url, remove_authority=True, remove_query_values=sanitize, split=True
)

parsed_url = urlsplit(url)
base_url = urlunsplit(
Components(
scheme=parsed_url.scheme,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_utils.py
Expand Up @@ -3,6 +3,7 @@
import sys

from sentry_sdk.utils import (
Components,
is_valid_sample_rate,
logger,
match_regex_list,
Expand Down Expand Up @@ -69,6 +70,19 @@ def test_sanitize_url(url, expected_result):
assert parts == expected_parts


def test_sanitize_url_and_split():
parts = sanitize_url(
"https://example.com?token=abc&sessionid=123&save=true", split=True
)
assert parts == Components(
scheme="https",
netloc="example.com",
path="",
query="token=[Filtered]&sessionid=[Filtered]&save=[Filtered]",
fragment="",
)


@pytest.mark.parametrize(
("url", "sanitize", "expected_url", "expected_query", "expected_fragment"),
[
Expand Down