Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Scheme validation to reject schemes with non-alphanumeric characters #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 7 additions & 2 deletions furl/furl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/test_furl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]/path')
assert f.scheme is None

f = furl.furl('sch2323eme://[email protected]/path')
assert f.scheme == 'sch2323eme'
assert f.username == 'user'
assert f.host == 'host.com'


# TODO(grun): Test more odd URLs.

def test_hosts(self):
Expand Down