Skip to content

Commit d82c586

Browse files
authored
Merge pull request #1266 from Mihir7027/fix/redis-url-null-password
fix(api): omit Redis auth segment when password is not configured
2 parents 465b8b3 + c88e864 commit d82c586

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

app/controllers/v1/video.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848
_max_concurrent_tasks = config.app.get("max_concurrent_tasks", 5)
4949
_max_queued_tasks = config.app.get("max_queued_tasks", 100)
5050

51-
redis_url = f"redis://:{_redis_password}@{_redis_host}:{_redis_port}/{_redis_db}"
51+
52+
def _build_redis_url(host: str, port: int, db: int, password: str | None) -> str:
53+
auth = f":{password}@" if password else ""
54+
return f"redis://{auth}{host}:{port}/{db}"
55+
56+
57+
redis_url = _build_redis_url(_redis_host, _redis_port, _redis_db, _redis_password)
5258
# 根据配置选择合适的任务管理器
5359
if _enable_redis:
5460
task_manager = RedisTaskManager(

test/services/test_controller_video.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,5 +581,28 @@ def test_download_video_encodes_content_disposition_filename(self):
581581
)
582582

583583

584+
class TestBuildRedisUrl(unittest.TestCase):
585+
def test_no_password_omits_auth_segment(self):
586+
"""None and empty-string passwords must not embed a literal 'None' or ':@'."""
587+
from app.controllers.v1.video import _build_redis_url
588+
589+
self.assertEqual(
590+
_build_redis_url("localhost", 6379, 0, None),
591+
"redis://localhost:6379/0",
592+
)
593+
self.assertEqual(
594+
_build_redis_url("localhost", 6379, 0, ""),
595+
"redis://localhost:6379/0",
596+
)
597+
598+
def test_password_is_included_in_url(self):
599+
from app.controllers.v1.video import _build_redis_url
600+
601+
self.assertEqual(
602+
_build_redis_url("redis-host", 6380, 1, "s3cr3t"),
603+
"redis://:s3cr3t@redis-host:6380/1",
604+
)
605+
606+
584607
if __name__ == "__main__":
585608
unittest.main()

0 commit comments

Comments
 (0)