Require Compose Basic auth for dashboard summary API

This commit is contained in:
Dymas committed 2026-10-02 11:02:04 +02:00
1 parent 0d24c3d073
commit 142b75cd8b
5 files changed
+94 -10

No files matched your search

+4
View File
@@ -1,5 +1,9 @@
# Changelog
## 0.53.7 - 2026-10-02
- Required HTTP Basic authentication for `/api/watchlist/homepage`, including the optional download summary and local requests. The route uses `KAIZOKU_AUTH_USERNAME` and `KAIZOKU_AUTH_PASSWORD` from the container environment and responds with a standard Basic challenge when credentials are missing or invalid.
## 0.53.6 - 2026-10-02
- Added an optional `include_downloads=1` field to `/api/watchlist/homepage` so dashboards can show running and pending download jobs with current progress alongside watchlist counts. The default response remains unchanged.
+1 -1
View File
@@ -52,7 +52,7 @@ http://127.0.0.1:8421/
For Uptime Kuma or another HTTP availability monitor, use `http://IP:PORT/status`. `GET` returns HTTP 200 with `OK`; `HEAD` also returns HTTP 200. The endpoint needs no authentication and reports whether Kaizoku's HTTP server is responding. It does not check provider or download health. For a monitor on another machine, bind Kaizoku to a reachable address and set `KAIZOKU_ALLOW_REMOTE=1`; the normal app and API routes still require configured credentials for remote clients.
For a dashboard, request `GET /api/watchlist/homepage?include_downloads=1`. The usual `watchlist`, `planned`, `finished`, `dropped`, and `total` counts remain, and `downloads` adds `total`, `running`, `pending`, and a `jobs` array. Each job includes `id`, `title`, `status`, `episodes`, `mode`, `provider`, and current `progress` fields such as `percent`, `episode`, and `message`. The array contains only running and pending downloads, including the live progress of the current job; completed jobs and Jellyfin handoffs are excluded. Without the query option, the response has the original counts only. This API route uses the normal remote authentication; HTTP Basic auth works for a dashboard client.
For a dashboard, request `GET /api/watchlist/homepage?include_downloads=1`. The usual `watchlist`, `planned`, `finished`, `dropped`, and `total` counts remain, and `downloads` adds `total`, `running`, `pending`, and a `jobs` array. Each job includes `id`, `title`, `status`, `episodes`, `mode`, `provider`, and current `progress` fields such as `percent`, `episode`, and `message`. The array contains only running and pending downloads, including the live progress of the current job; completed jobs and Jellyfin handoffs are excluded. Without the query option, the response has the original counts only. This route always requires HTTP Basic auth, even for local requests. It uses the `KAIZOKU_AUTH_USERNAME` and `KAIZOKU_AUTH_PASSWORD` supplied to Docker Compose; set both before starting the container. A missing or invalid Basic header receives HTTP 401 with a `WWW-Authenticate` challenge.
```sh
curl -u 'admin:your-password' 'http://IP:PORT/api/watchlist/homepage?include_downloads=1'
+1 -1
View File
@@ -1 +1 @@
0.53.6
0.53.7
+29 -1
View File
@@ -570,6 +570,30 @@ class Handler(BaseHTTPRequestHandler):
if not Handler._remote_credentials_valid(self):
raise PermissionError("Remote username or password missing or invalid.")
def ensure_dashboard_basic_auth(self):
client_host = Handler._effective_client_host(self)
if not client_address_is_local(client_host) and not remote_access_allowed():
raise PermissionError(
"Remote access is disabled. Set KAIZOKU_ALLOW_REMOTE=1 to allow non-local clients."
)
if not env_value("KAIZOKU_AUTH_USERNAME") or not env_value("KAIZOKU_AUTH_PASSWORD"):
self.error(HTTPStatus.SERVICE_UNAVAILABLE, "Set KAIZOKU_AUTH_USERNAME and KAIZOKU_AUTH_PASSWORD.")
return False
username, password = Handler._basic_auth_credentials(self)
if not remote_access_credentials_match(username, password):
Handler.write_response_bytes(
self,
b'{"error":"Basic authentication required."}',
HTTPStatus.UNAUTHORIZED,
{
"Content-Type": "application/json",
"Cache-Control": "no-store",
"WWW-Authenticate": 'Basic realm="Kaizoku dashboard", charset="UTF-8"',
},
)
return False
return True
def serve_status(self):
client_host = Handler._effective_client_host(self)
if not client_address_is_local(client_host) and not remote_access_allowed():
@@ -619,7 +643,11 @@ class Handler(BaseHTTPRequestHandler):
if remote_access_allowed() and remote_access_credentials_configured():
Handler._render_remote_login_page(self, parsed)
return
self.ensure_client_access()
if parsed.path == "/api/watchlist/homepage":
if not Handler.ensure_dashboard_basic_auth(self):
return
else:
self.ensure_client_access()
if parsed.path == "/":
self.html(Handler._context(self).index_html)
elif parsed.path in {"/favicon.png", "/favicon.ico"}:
+59 -7
View File
@@ -148,7 +148,7 @@ class NetworkGuardTests(unittest.TestCase):
self.assertEqual(handler.error_status, HTTPStatus.FORBIDDEN)
def test_homepage_download_option_keeps_remote_auth(self):
def test_homepage_api_requires_basic_auth_from_remote_and_local_clients(self):
handler = DummyHandler("/api/watchlist/homepage?include_downloads=1")
handler.client_address = ("192.168.1.25", 8421)
handler.command = "GET"
@@ -159,7 +159,51 @@ class NetworkGuardTests(unittest.TestCase):
):
APP.Handler.do_GET(handler)
self.assertEqual(handler.error_status, HTTPStatus.FORBIDDEN)
local_handler = DummyHandler("/api/watchlist/homepage")
APP.Handler.do_GET(local_handler)
cookie_handler = DummyHandler("/api/watchlist/homepage")
cookie_handler.headers["Cookie"] = (
"kaizoku_session=" + http_handler.remote_access_session_value()
)
APP.Handler.do_GET(cookie_handler)
for response in (handler, local_handler, cookie_handler):
self.assertEqual(response.response_status, HTTPStatus.UNAUTHORIZED)
self.assertIn("Basic", dict(response.response_headers)["WWW-Authenticate"])
def test_homepage_api_accepts_only_compose_basic_credentials(self):
handler = DummyHandler("/api/watchlist/homepage?include_downloads=1")
handler.client_address = ("192.168.1.25", 8421)
handler.command = "GET"
handler.headers["Authorization"] = "Basic " + base64.b64encode(b"demo:secret-pass").decode("ascii")
with mock.patch.dict(
os.environ,
{"KAIZOKU_ALLOW_REMOTE": "1", "KAIZOKU_AUTH_USERNAME": "demo", "KAIZOKU_AUTH_PASSWORD": "secret-pass"},
):
APP.Handler.do_GET(handler)
self.assertEqual(handler.json_status, HTTPStatus.OK)
self.assertIn("downloads", handler.json_payload)
wrong_handler = DummyHandler("/api/watchlist/homepage")
wrong_handler.headers["Authorization"] = "Basic " + base64.b64encode(b"demo:wrong").decode("ascii")
with mock.patch.dict(
os.environ,
{"KAIZOKU_AUTH_USERNAME": "demo", "KAIZOKU_AUTH_PASSWORD": "secret-pass"},
):
APP.Handler.do_GET(wrong_handler)
self.assertEqual(wrong_handler.response_status, HTTPStatus.UNAUTHORIZED)
def test_homepage_api_requires_compose_credentials_to_be_configured(self):
handler = DummyHandler("/api/watchlist/homepage")
handler.headers["Authorization"] = "Basic " + base64.b64encode(b"demo:secret-pass").decode("ascii")
with mock.patch.dict(os.environ, {"KAIZOKU_AUTH_USERNAME": "", "KAIZOKU_AUTH_PASSWORD": ""}):
APP.Handler.do_GET(handler)
self.assertEqual(handler.error_status, HTTPStatus.SERVICE_UNAVAILABLE)
def test_runtime_bootstrap_is_deferred_until_initialize(self):
self.assertTrue(IMPORT_RUNTIME_WAS_DEFERRED)
@@ -517,16 +561,22 @@ class QueueApiTests(unittest.TestCase):
}
try:
handler = DummyHandler("/api/watchlist/homepage?include_downloads=1")
APP.Handler.do_GET(handler)
default_handler = DummyHandler("/api/watchlist/homepage")
basic = "Basic " + base64.b64encode(b"demo:secret-pass").decode("ascii")
handler.headers["Authorization"] = basic
default_handler.headers["Authorization"] = basic
with mock.patch.dict(
os.environ,
{"KAIZOKU_AUTH_USERNAME": "demo", "KAIZOKU_AUTH_PASSWORD": "secret-pass"},
):
APP.Handler.do_GET(handler)
APP.Handler.do_GET(default_handler)
downloads = handler.json_payload["downloads"]
self.assertEqual((downloads["total"], downloads["running"], downloads["pending"]), (2, 1, 1))
self.assertEqual([job["id"] for job in downloads["jobs"]], [running["id"], pending["id"]])
self.assertEqual(downloads["jobs"][0]["progress"]["percent"], 42)
self.assertNotIn("log", downloads["jobs"][0])
self.assertNotIn("download_dir", downloads["jobs"][0])
default_handler = DummyHandler("/api/watchlist/homepage")
APP.Handler.do_GET(default_handler)
self.assertNotIn("downloads", default_handler.json_payload)
finally:
self.queue.current_job = None
@@ -4456,10 +4506,12 @@ class HandlerRouteTests(unittest.TestCase):
def test_watchlist_homepage_route_returns_summary_counts(self):
handler = DummyHandler("/api/watchlist/homepage")
handler.headers["Authorization"] = "Basic " + base64.b64encode(b"demo:secret-pass").decode("ascii")
payload = {"watchlist": 1, "planned": 0, "finished": 2, "dropped": 0, "total": 3}
handler.handler_context = mock.Mock(get_watchlist_homepage_summary=mock.Mock(return_value=payload))
APP.Handler.do_GET(handler)
with mock.patch.dict(os.environ, {"KAIZOKU_AUTH_USERNAME": "demo", "KAIZOKU_AUTH_PASSWORD": "secret-pass"}):
APP.Handler.do_GET(handler)
self.assertEqual(handler.json_payload, payload)
self.assertEqual(handler.json_status, HTTPStatus.OK)