diff options
| author | Dennis Fink | 2026-11-12 22:44:25 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-12 22:44:25 +0100 |
| commit | 1db6b47f88dde5538efa433328987c70eab74d70 (patch) | |
| tree | 1231cc03e603f8b22f5c35a51475d3acc4694621 | |
| parent | c7993952c585b66acad5275e39dbf91cf662b975 (diff) | |
| download | webmentions-ssg-1db6b47f88dde5538efa433328987c70eab74d70.tar.gz webmentions-ssg-1db6b47f88dde5538efa433328987c70eab74d70.zip | |
fix(core): validate Redis Huey URL schemes
Parse Redis backend flavours explicitly so supported combinations
preserve the correct redis or rediss scheme while malformed or unknown
variants fail fast.
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_extension.py | 15 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/extension.py | 37 |
2 files changed, 32 insertions, 20 deletions
diff --git a/tests/tasks/test_extension.py b/tests/tasks/test_extension.py index 5e9cdfa..19f1191 100644 --- a/tests/tasks/test_extension.py +++ b/tests/tasks/test_extension.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: BSD-3-Clause +import re from collections.abc import Callable from typing import Any @@ -122,6 +123,11 @@ def test_getattr_delegates_to_huey(app: Flask) -> None: "PriorityRedisExpireHuey", {"url": "redis://localhost:6379/0"}, ), + ( + "rediss+priority+expire://localhost:6379/0", + "PriorityRedisExpireHuey", + {"url": "rediss://localhost:6379/0"}, + ), ("sqlite:///var/huey.db", "SqliteHuey", {"filename": "var/huey.db"}), ("file:///var/huey-queue", "FileHuey", {"path": "var/huey-queue"}), ( @@ -168,8 +174,15 @@ def test_backend_from_url( ), ("file:///", "File Huey URL must include a directory path"), ("amqp://localhost", "Unsupported HUEY_URL scheme: 'amqp'"), + ("redis+typo://localhost", "Unsupported HUEY_URL scheme: 'redis+typo'"), + ( + "rediss+priority+typo://localhost", + "Unsupported HUEY_URL scheme: 'rediss+priority+typo'", + ), + ("redis+://localhost", "Unsupported HUEY_URL scheme: 'redis+'"), + ("redisish://localhost", "Unsupported HUEY_URL scheme: 'redisish'"), ], ) def test_backend_from_url_rejects_invalid_url(url: str, message: str) -> None: - with pytest.raises(RuntimeError, match=message): + with pytest.raises(RuntimeError, match=re.escape(message)): Huey.backend_from_url(url) diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py index f7072b2..ee9c442 100644 --- a/webmentions_ssg/tasks/extension.py +++ b/webmentions_ssg/tasks/extension.py @@ -144,27 +144,26 @@ class Huey: parsed = urlsplit(url) scheme = parsed.scheme.lower() - if scheme.startswith(("redis", "rediss")): - fixed_url = urlunsplit(parsed._replace(scheme=scheme.split("+", 1)[0])) - - if scheme.endswith("priority+expire"): - from huey import PriorityRedisExpireHuey - - return PriorityRedisExpireHuey, {"url": fixed_url} - - if scheme.endswith("priority"): - from huey import PriorityRedisHuey - - return PriorityRedisHuey, {"url": fixed_url} - - if scheme.endswith("expire"): - from huey import RedisExpireHuey - - return RedisExpireHuey, {"url": fixed_url} + base_scheme, separator, flavour = scheme.partition("+") + if base_scheme in {"redis", "rediss"}: + from huey import ( + PriorityRedisExpireHuey, + PriorityRedisHuey, + RedisExpireHuey, + RedisHuey, + ) - from huey import RedisHuey + backends = { + "": RedisHuey, + "priority": PriorityRedisHuey, + "expire": RedisExpireHuey, + "priority+expire": PriorityRedisExpireHuey, + } + if flavour not in backends or (separator and not flavour): + raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}") - return RedisHuey, {"url": url} + fixed_url = urlunsplit(parsed._replace(scheme=base_scheme)) + return backends[flavour], {"url": fixed_url} if scheme == "sqlite": from huey import SqliteHuey |
