From c7577e9ff79305ee982261e67f45971aac7cb551 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Fri, 18 Sep 2026 11:07:36 +0000 Subject: [PATCH] Guard access log lines against unreset injected colors App-supplied extra fields may carry raw ANSI color codes without a reset, bleeding into the next line's IP column and following output. Ensure access log lines begin and end with a reset, adding one only where missing to avoid duplicate resets on well-formed lines. --- fastapi-vue/fastapi_vue/logging.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fastapi-vue/fastapi_vue/logging.py b/fastapi-vue/fastapi_vue/logging.py index ff09811..38a5a0f 100644 --- a/fastapi-vue/fastapi_vue/logging.py +++ b/fastapi-vue/fastapi_vue/logging.py @@ -35,6 +35,8 @@ from .environ import env ANSI_ESCAPE_RE = re.compile(r"\x1b\[[0-9;]*m") +RESET = "\033[0m" + ACCESS_LOG_FMT = "%(client)s %(status)s %(method)s %(host)s%(path)s %(extra)s%(timing)s" ACCESS_LOGGER = "fastapi_vue.access" @@ -122,7 +124,14 @@ class Formatter(logging.Formatter): return _level_prefix(record) + record.getMessage() formatted = super().formatMessage(record) if not self.use_colors: - formatted = strip_ansi(formatted) + return strip_ansi(formatted) + # Guard against app-supplied fields (``extra``) carrying raw color + # codes without a reset: ensure the line begins and ends with a + # reset, but only add one where it's missing to avoid duplicates. + if not formatted.startswith(RESET): + formatted = RESET + formatted + if not formatted.endswith(RESET): + formatted += RESET return formatted