Skip to content

Commit e1cd85a

Browse files
committed
Be more strict about url scheme parsing
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.
1 parent 8499adc commit e1cd85a

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
@@ -1149,6 +1149,16 @@ def to_bool(value):
11491149

11501150

11511151
def parse_url(url):
1152+
if not (
1153+
url.startswith("redis://")
1154+
or url.startswith("rediss://")
1155+
or url.startswith("unix://")
1156+
):
1157+
raise ValueError(
1158+
"Redis URL must specify one of the following "
1159+
"schemes (redis://, rediss://, unix://)"
1160+
)
1161+
11521162
url = urlparse(url)
11531163
kwargs = {}
11541164

@@ -1175,7 +1185,7 @@ def parse_url(url):
11751185
kwargs["path"] = unquote(url.path)
11761186
kwargs["connection_class"] = UnixDomainSocketConnection
11771187

1178-
elif url.scheme in ("redis", "rediss"):
1188+
else: # implied: url.scheme in ("redis", "rediss"):
11791189
if url.hostname:
11801190
kwargs["host"] = unquote(url.hostname)
11811191
if url.port:
@@ -1191,11 +1201,6 @@ def parse_url(url):
11911201

11921202
if url.scheme == "rediss":
11931203
kwargs["connection_class"] = SSLConnection
1194-
else:
1195-
raise ValueError(
1196-
"Redis URL must specify one of the following "
1197-
"schemes (redis://, rediss://, unix://)"
1198-
)
11991204

12001205
return kwargs
12011206

tests/test_connection_pool.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ def test_invalid_scheme_raises_error(self):
363363
"(redis://, rediss://, unix://)"
364364
)
365365

366+
def test_invalid_scheme_raises_error_when_double_slash_missing(self):
367+
with pytest.raises(ValueError) as cm:
368+
redis.ConnectionPool.from_url("redis:foo.bar.com:12345")
369+
assert str(cm.value) == (
370+
"Redis URL must specify one of the following schemes "
371+
"(redis://, rediss://, unix://)"
372+
)
373+
366374

367375
class TestConnectionPoolUnixSocketURLParsing:
368376
def test_defaults(self):

0 commit comments

Comments
 (0)