From 7f6327e94ecfb1068d28fa4fc783b5b34fadbb69 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Thu, 26 Feb 2026 11:30:08 +0100 Subject: [PATCH] Handle missing Accept header in host logs (#6594) * Handle missing Accept header in host logs Avoid indexing request headers directly in the host advanced logs handler when Accept is absent, preventing KeyError crashes on valid requests without that header. Fixes SUPERVISOR-1939. * Add pytest --- supervisor/api/host.py | 6 ++++-- tests/api/test_host.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/supervisor/api/host.py b/supervisor/api/host.py index cc657e3e0..bc43aa6e3 100644 --- a/supervisor/api/host.py +++ b/supervisor/api/host.py @@ -240,7 +240,9 @@ class APIHost(CoreSysAttributes): f"Cannot determine CONTAINER_LOG_EPOCH of {identifier}, latest logs not available." ) from err - if ACCEPT in request.headers and request.headers[ACCEPT] not in [ + accept_header = request.headers.get(ACCEPT) + + if accept_header and accept_header not in [ CONTENT_TYPE_TEXT, CONTENT_TYPE_X_LOG, "*/*", @@ -250,7 +252,7 @@ class APIHost(CoreSysAttributes): "supported for now." ) - if "verbose" in request.query or request.headers[ACCEPT] == CONTENT_TYPE_X_LOG: + if "verbose" in request.query or accept_header == CONTENT_TYPE_X_LOG: log_formatter = LogFormatter.VERBOSE if "no_colors" in request.query: diff --git a/tests/api/test_host.py b/tests/api/test_host.py index a3a38b5e2..7c933cd63 100644 --- a/tests/api/test_host.py +++ b/tests/api/test_host.py @@ -374,6 +374,11 @@ async def test_advanced_logs_formatters( await api_client.get("/host/logs/identifiers/test", headers=headers) journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE, False) + journal_logs_reader.reset_mock() + + await api_client.get("/host/logs/identifiers/test", skip_auto_headers={"Accept"}) + journal_logs_reader.assert_called_once_with(ANY, LogFormatter.PLAIN, False) + async def test_advanced_logs_errors(coresys: CoreSys, api_client: TestClient): """Test advanced logging API errors."""