Agent Zero AI framework
feat(login): Enhance next URL validation to reject backslash open redirects
neurocisf8730c0608cf
diff --git a/helpers/api.py b/helpers/api.pyindex 9ac198c16..351ba4716 100644--- a/helpers/api.py+++ b/helpers/api.py@@ -1,7 +1,7 @@from abc import abstractmethodimport jsonimport threading-from urllib.parse import urlsplit+from urllib.parse import urlsplit, unquotefrom functools import wrapsfrom pathlib import Pathfrom typing import Union, Dict, Any@@ -109,8 +109,16 @@ def is_safe_next_url(value: str | None) -> bool:return Falseif "\r" in value or "\n" in value:return False+ # Reject raw backslashes (browsers normalize `/\host` to `//host` -> external).+ if "\\" in value:+ return False++ # Decode percent-escapes so encoded backslashes (e.g. `%5C`) are caught too.+ decoded = unquote(value)+ if "\\" in decoded:+ return False- parsed = urlsplit(value)+ parsed = urlsplit(decoded)if parsed.scheme or parsed.netloc:return Falsediff --git a/tests/test_http_auth_csrf.py b/tests/test_http_auth_csrf.pyindex ddb01a4b3..0f95f449e 100644--- a/tests/test_http_auth_csrf.py+++ b/tests/test_http_auth_csrf.py@@ -171,3 +171,23 @@ def test_auth_redirect_includes_original_path_and_query(monkeypatch) -> None:location = response.headers["Location"]assert location.startswith("/login?next=")assert "%2Fplugins%2Fa0_voqualizer%2Fwebui%2Fvoqualizer.html%3Fcontext%3DrlO1iMV7" in location+++def test_is_safe_next_url_rejects_backslash_open_redirects() -> None:+ from helpers.api import is_safe_next_url++ # Raw backslash forms+ assert is_safe_next_url("/\\evil.example") is False+ assert is_safe_next_url("\\/evil.example") is False+ assert is_safe_next_url("/path\\evil") is False++ # Percent-encoded backslash forms+ assert is_safe_next_url("/%5Cevil.example") is False+ assert is_safe_next_url("%5C/evil.example") is False+ assert is_safe_next_url("/%5cevil.example") is False # lowercase hex++ # Mixed / double-encoded edge+ assert is_safe_next_url("/path/%5Cevil") is False++ # Sanity: a legitimate relative path still passes+ assert is_safe_next_url("/plugins/a0_voqualizer/webui/voqualizer.html") is True