From f10e58de66a12550292c5dc4b6767e46badeb6a2 Mon Sep 17 00:00:00 2001 From: JohnJamesUtley Date: Tue, 25 Apr 2023 16:59:22 -0400 Subject: [PATCH] Fixed Scheme validation to reject schemes with non-alphanumeric characters --- furl/furl.py | 9 +++++++-- tests/test_furl.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/furl/furl.py b/furl/furl.py index cd6d710..c2f5c08 100644 --- a/furl/furl.py +++ b/furl/furl.py @@ -223,9 +223,11 @@ def is_valid_encoded_query_value(value): return is_valid_encoded_query_value.regex.match(value) is not None -@static_vars(regex=re.compile(r'[a-zA-Z][a-zA-Z\-\.\+]*')) +@static_vars(regex=re.compile(r'[a-zA-Z][a-zA-Z0-9\-\.\+]*')) def is_valid_scheme(scheme): - return is_valid_scheme.regex.match(scheme) is not None + if is_valid_scheme.regex.match(scheme) is None: + return False + return is_valid_scheme.regex.match(scheme).group() == scheme @static_vars(regex=re.compile('[%s]' % re.escape(INVALID_HOST_CHARS))) @@ -1423,6 +1425,9 @@ def scheme(self): def scheme(self, scheme): if callable_attr(scheme, 'lower'): scheme = scheme.lower() + if scheme: + if not is_valid_scheme(scheme): + raise ValueError("Invalid Scheme") self._scheme = scheme @property diff --git a/tests/test_furl.py b/tests/test_furl.py index bc268c8..4fa9f2d 100644 --- a/tests/test_furl.py +++ b/tests/test_furl.py @@ -1630,6 +1630,16 @@ def test_odd_urls(self): assert f.netloc == '' and str(f.path) == '//path' assert f.url == '////path' + # Check that schemes with invalid characters are rejected + f = furl.furl('sch^eme://user@host.com/path') + assert f.scheme is None + + f = furl.furl('sch2323eme://user@host.com/path') + assert f.scheme == 'sch2323eme' + assert f.username == 'user' + assert f.host == 'host.com' + + # TODO(grun): Test more odd URLs. def test_hosts(self):