Skip to content

Be more strict about url scheme parsing #2343

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

Merged
merged 2 commits into from
Aug 21, 2022
Merged
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
17 changes: 11 additions & 6 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,16 @@ def to_bool(value):


def parse_url(url):
if not (
url.startswith("redis://")
or url.startswith("rediss://")
or url.startswith("unix://")
):
raise ValueError(
"Redis URL must specify one of the following "
"schemes (redis://, rediss://, unix://)"
)

url = urlparse(url)
kwargs = {}

Expand All @@ -1192,7 +1202,7 @@ def parse_url(url):
kwargs["path"] = unquote(url.path)
kwargs["connection_class"] = UnixDomainSocketConnection

elif url.scheme in ("redis", "rediss"):
else: # implied: url.scheme in ("redis", "rediss"):
if url.hostname:
kwargs["host"] = unquote(url.hostname)
if url.port:
Expand All @@ -1208,11 +1218,6 @@ def parse_url(url):

if url.scheme == "rediss":
kwargs["connection_class"] = SSLConnection
else:
raise ValueError(
"Redis URL must specify one of the following "
"schemes (redis://, rediss://, unix://)"
)

return kwargs

Expand Down
8 changes: 8 additions & 0 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ def test_invalid_scheme_raises_error(self):
"(redis://, rediss://, unix://)"
)

def test_invalid_scheme_raises_error_when_double_slash_missing(self):
with pytest.raises(ValueError) as cm:
redis.ConnectionPool.from_url("redis:foo.bar.com:12345")
assert str(cm.value) == (
"Redis URL must specify one of the following schemes "
"(redis://, rediss://, unix://)"
)


class TestConnectionPoolUnixSocketURLParsing:
def test_defaults(self):
Expand Down