From 29872f168e8cb18ed0fa8e4ac4441e05c29d2109 Mon Sep 17 00:00:00 2001 From: Tyson Smith Date: Mon, 18 Nov 2024 17:00:24 -0800 Subject: [PATCH] fix(sapphire): unquote() is now able to handle bytes --- src/sapphire/test_worker.py | 5 +++++ src/sapphire/worker.py | 7 +++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sapphire/test_worker.py b/src/sapphire/test_worker.py index 7f439930..d332e9c4 100644 --- a/src/sapphire/test_worker.py +++ b/src/sapphire/test_worker.py @@ -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): diff --git a/src/sapphire/worker.py b/src/sapphire/worker.py index a261d4d7..95b9d2dc 100644 --- a/src/sapphire/worker.py +++ b/src/sapphire/worker.py @@ -50,10 +50,9 @@ 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 ( @@ -61,7 +60,7 @@ def parse(cls, raw_data: bytes) -> Request | None: 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