Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions src/sentry/apidocs/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,15 @@ def absolute_url(value: str) -> Url:
"""
Returns ``value`` unchanged as the value of a ``uri`` field.

A value without a scheme and a host, such as a path or an empty string, is invalid.
"""
parts = urlsplit(value)
A value without a scheme and a host, such as a path or an empty string, is invalid,
and so is one ``urlsplit`` can't parse, such as a host in brackets that isn't an IP
address.
"""
try:
parts = urlsplit(value)
except ValueError:
_invalid("uri", "the URL can't be parsed")
return UrlStr(value)
if not parts.scheme or not parts.netloc:
_invalid("uri", "the URL has no scheme or host")
return UrlStr(value)
Expand Down
23 changes: 23 additions & 0 deletions tests/sentry/apidocs/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def test_absolute_url_rejects(url: str) -> None:
absolute_url(url)


@pytest.mark.parametrize(
"url", ["https://[REDACTED]/assets/app.js.map", "https://[not-an-ip]:8000/", "http://[::1/"]
)
def test_absolute_url_rejects_unparseable(url: str) -> None:
with pytest.raises(ValueError, match="can't be parsed"):
absolute_url(url)


def test_uuid_str_formats_a_uuid() -> None:
value = uuid.UUID("6a2c8b1e-5d4f-4c3b-9a8e-7f6d5c4b3a21")
assert uuid_str(value) == "6a2c8b1e-5d4f-4c3b-9a8e-7f6d5c4b3a21"
Expand Down Expand Up @@ -140,3 +148,18 @@ def test_invalid_value_is_logged_outside_tests(caplog: pytest.LogCaptureFixture)
("apidocs.invalid_format", "uri", "the URL has no scheme or host"),
("apidocs.invalid_format", "date-time", "the datetime has no UTC offset"),
]


def test_unparseable_url_is_returned_and_logged_outside_tests(
caplog: pytest.LogCaptureFixture,
) -> None:
url = "https://[REDACTED]/assets/app.js.map"
with (
mock.patch("sentry.apidocs.formats.in_test_environment", return_value=False),
caplog.at_level(logging.WARNING, logger="sentry.apidocs.formats"),
):
assert absolute_url(url) == url

assert [(r.message, r.__dict__["format"], r.__dict__["problem"]) for r in caplog.records] == [
("apidocs.invalid_format", "uri", "the URL can't be parsed"),
]
Loading