Skip to content

Commit ce40abf

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 ce40abf

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

redis/connection.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,13 @@ def to_bool(value):
11491149

11501150

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

@@ -1175,7 +1182,7 @@ def parse_url(url):
11751182
kwargs["path"] = unquote(url.path)
11761183
kwargs["connection_class"] = UnixDomainSocketConnection
11771184

1178-
elif url.scheme in ("redis", "rediss"):
1185+
else: # implied: url.scheme in ("redis", "rediss"):
11791186
if url.hostname:
11801187
kwargs["host"] = unquote(url.hostname)
11811188
if url.port:
@@ -1191,11 +1198,6 @@ def parse_url(url):
11911198

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

12001202
return kwargs
12011203

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)