Skip to content

Commit b5ebada

Browse files
vanschelvendvora-h
andauthored
Be more strict about url scheme parsing (#2343)
The error message implied that urls had to start with `scheme://`. However, if the double slash was left out, the url parsed just fine and the part that was ostensibly intended to be the hostname ended up as part of the path, whereas the default (localhost) would be used for the hostname. This commit makes the check as strict as the error message implies by including a check for the double slash. Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
1 parent fde03fb commit b5ebada

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

redis/connection.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,16 @@ def to_bool(value):
11661166

11671167

11681168
def parse_url(url):
1169+
if not (
1170+
url.startswith("redis://")
1171+
or url.startswith("rediss://")
1172+
or url.startswith("unix://")
1173+
):
1174+
raise ValueError(
1175+
"Redis URL must specify one of the following "
1176+
"schemes (redis://, rediss://, unix://)"
1177+
)
1178+
11691179
url = urlparse(url)
11701180
kwargs = {}
11711181

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

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

12091219
if url.scheme == "rediss":
12101220
kwargs["connection_class"] = SSLConnection
1211-
else:
1212-
raise ValueError(
1213-
"Redis URL must specify one of the following "
1214-
"schemes (redis://, rediss://, unix://)"
1215-
)
12161221

12171222
return kwargs
12181223

tests/test_connection_pool.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ def test_invalid_scheme_raises_error(self):
339339
"(redis://, rediss://, unix://)"
340340
)
341341

342+
def test_invalid_scheme_raises_error_when_double_slash_missing(self):
343+
with pytest.raises(ValueError) as cm:
344+
redis.ConnectionPool.from_url("redis:foo.bar.com:12345")
345+
assert str(cm.value) == (
346+
"Redis URL must specify one of the following schemes "
347+
"(redis://, rediss://, unix://)"
348+
)
349+
342350

343351
class TestConnectionPoolUnixSocketURLParsing:
344352
def test_defaults(self):

0 commit comments

Comments
 (0)