Skip to content

Commit 713735b

Browse files
fix(deploy): report malformed Redis URLs instead of crashing
Codex review of #90: with REDIS_PASSWORD set, apply_redis_password() splits REDIS_URL with urlsplit() before the audit parses it. A malformed IPv6 authority such as redis://[broken makes urlsplit raise ValueError, so the guard died with a traceback instead of returning the actionable problem line the rest of the audit produces. Resolve the Redis URL through a helper that catches the split error and reports 'REDIS_URL is not a valid connection URL.' A failed split also suppresses the follow-on 'has no password' message, since there is no URL to inspect. Add regression tests for the unsplittable authority and for a bad port, and verified by hand that the guard exits 1 with the problem line and no traceback.
1 parent 59c1499 commit 713735b

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎scripts/check_deploy_credentials.py‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ def _password_in_url(url: str) -> str | None:
121121
return None
122122

123123

124+
def _resolve_redis_url(url: str, password: str | None) -> tuple[str, list[str]]:
125+
"""Complete a passwordless Redis URL, reporting a malformed value instead of raising.
126+
127+
``apply_redis_password`` splits the URL with ``urlsplit``, which raises ``ValueError`` on a
128+
malformed authority (``redis://[broken``). That call happens before ``_parse_url``, so an
129+
unguarded value would abort the guard with a traceback instead of the actionable problem
130+
line the rest of the audit produces. A failed split also means there is no URL to inspect,
131+
so the caller must not additionally report a missing password.
132+
"""
133+
try:
134+
return apply_redis_password(url, password), []
135+
except ValueError:
136+
return "", ["REDIS_URL is not a valid connection URL."]
137+
138+
124139
def audit_credentials(env: Mapping[str, str]) -> list[str]:
125140
"""Check the deployment's datastore credentials.
126141
@@ -170,7 +185,8 @@ def audit_credentials(env: Mapping[str, str]) -> list[str]:
170185
or ""
171186
)
172187
raw_redis = (env.get("REDIS_URL") or "").strip()
173-
redis_url = apply_redis_password(raw_redis, env.get("REDIS_PASSWORD"))
188+
redis_url, redis_problems = _resolve_redis_url(raw_redis, env.get("REDIS_PASSWORD"))
189+
problems.extend(redis_problems)
174190

175191
app_role: str | None = None
176192
app_password: str | None = None
@@ -215,7 +231,10 @@ def audit_credentials(env: Mapping[str, str]) -> list[str]:
215231
)
216232

217233
if not redis_url:
218-
problems.append("REDIS_URL has no password; Redis must not be reachable unauthenticated.")
234+
if not redis_problems:
235+
problems.append(
236+
"REDIS_URL has no password; Redis must not be reachable unauthenticated."
237+
)
219238
else:
220239
parsed_redis, parse_problems = _parse_url("REDIS_URL", redis_url)
221240
problems.extend(parse_problems)

‎tests/unit/test_deploy_credentials.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ def test_malformed_urls_are_reported_instead_of_raising(url: str) -> None:
162162
assert "DATABASE_URL is not a valid connection URL." in problems
163163

164164

165+
@pytest.mark.parametrize("url", ["redis://[broken", "redis://host:notaport/0"])
166+
def test_malformed_redis_url_is_reported_instead_of_raising(url: str) -> None:
167+
"""URL splitting happens before parsing, so it needs the same guard as the databases.
168+
169+
``redis://[broken`` makes ``urlsplit`` raise ``ValueError`` while ``REDIS_PASSWORD`` is
170+
being embedded. That used to abort the audit with a traceback before any problem line was
171+
printed, which is the opposite of what a fail-closed deployment guard should do.
172+
"""
173+
problems = audit_credentials(_env(REDIS_URL=url))
174+
assert problems == ["REDIS_URL is not a valid connection URL."]
175+
176+
165177
def test_problem_messages_never_echo_the_secret() -> None:
166178
"""The guard reports variable names, never the credential it rejected."""
167179
secret = "hunter2"

0 commit comments

Comments
 (0)