From 8938aa9271d259becf819718ee548653cae00dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E6=98=B1=E6=88=90?= <300126469+yuchengshen931122-creator@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:01:15 +0800 Subject: [PATCH] fix(uri): treat the scheme case-insensitively `uri` compared scheme prefixes on the raw value, so `HTTP://EXAMPLE.COM` was rejected although `url` accepts it. RFC 3986 3.1: schemes are case-insensitive. Lower-case the scheme before dispatching. Co-Authored-By: Claude Opus 5.5 --- src/validators/uri.py | 4 ++++ tests/test_uri.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/validators/uri.py b/src/validators/uri.py index 21e5fa46..171a3241 100644 --- a/src/validators/uri.py +++ b/src/validators/uri.py @@ -92,6 +92,10 @@ def uri(value: str, /): if not value or re.search(r"[\x00-\x20\x7f]", value): return False + # schemes are case-insensitive (RFC 3986, section 3.1) + scheme, colon, rest = value.partition(":") + value = scheme.lower() + colon + rest + # TODO: work on various validations # url diff --git a/tests/test_uri.py b/tests/test_uri.py index f33865ec..c8eee3c1 100644 --- a/tests/test_uri.py +++ b/tests/test_uri.py @@ -73,3 +73,19 @@ def test_rejects_raw_ascii_controls_and_spaces_in_uri(value: str, invalid_char: def test_accepts_percent_encoded_characters_in_uri(value: str): """Test accepts percent encoded characters in URI.""" assert uri(value) + + +@pytest.mark.parametrize( + "value", + [ + "HTTP://EXAMPLE.COM", + "Https://example.com/path", + "MAILTO:user@example.com", + "FILE:///tmp/readme.txt", + "Telnet://example.com:23", + "URN:isbn:9780140328721", + ], +) +def test_accepts_scheme_in_any_case(value: str): + """Test schemes are case-insensitive (RFC 3986 3.1).""" + assert uri(value)