Skip to content

Commit

Permalink
fix(sapphire): unquote() is now able to handle bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Nov 19, 2024
1 parent 87a448b commit 29872f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/sapphire/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,18 @@ def test_response_01(req, method, scheme, path):
@mark.parametrize(
"req",
[
b"",
b"a",
# Invalid IPv6 URL
b"GET http://[test/ HTTP/1.1",
# Missing Path
b"GET HTTP/1.1",
# Invalid Path
b"GET a a a a a HTTP/1.1",
# Invalid characters under NFKC normalization
b"GET http://%E2%84%80/ HTTP/1.1",
# Missing method
b" / HTTP/1.1",
],
)
def test_response_02(req):
Expand Down
7 changes: 3 additions & 4 deletions src/sapphire/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@ def parse(cls, raw_data: bytes) -> Request | None:

# TODO: parse headers if needed

raw_url = req_match.group("url")
try:
url_str = req_match.group("url").decode("ascii", errors="replace")
# unquote() accepts str | bytes as of Python 3.9
url = urlparse(unquote(url_str))
url = urlparse(unquote(raw_url))
except ValueError as exc:
msg = str(exc)
if (
"contains invalid characters under NFKC normalization" not in msg
and "Invalid IPv6 URL" not in msg
and "does not appear to be an IPv4 or IPv6 address" not in msg
):
LOG.error("Failed to parse URL: %r", url_str)
LOG.error("Failed to parse URL: %r", raw_url)
raise
LOG.debug("failed to parse url from request")
return None
Expand Down

0 comments on commit 29872f1

Please sign in to comment.