From d6b2318f95c8025e282f4fbbf8bc5056acee70c5 Mon Sep 17 00:00:00 2001 From: 321215665 <321215665@users.noreply.github.com> Date: Thu, 3 Dec 2026 20:08:44 +0000 Subject: [PATCH] Fix access log format literal % escape producing an invalid format compile_format() replaced known atoms with %s via FORMAT_RE, then ran CLEANUP_RE to escape any remaining % as a doubled %%. A literal "%%" (the documented escape for a percent sign) was re-escaped into "%%%" -- an invalid percent-format. Every request then logged "Error in logging" with a ValueError: incomplete format. The same ordering also let the character right after a literal "%%" be consumed as the start of an atom, so "%%P" logged the process id instead of the literal text "%P". Protect literal "%%" before atom processing and restore it as a valid percent-escape after the cleanup pass. Fixes #14390 --- aiohttp/web_log.py | 8 ++++++++ tests/test_web_log.py | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/aiohttp/web_log.py b/aiohttp/web_log.py index e75f48f31e5..ec2401f0d4a 100644 --- a/aiohttp/web_log.py +++ b/aiohttp/web_log.py @@ -107,6 +107,12 @@ def compile_format(self, log_format: str) -> tuple[str, list[KeyMethod]]: # can repeat the same key more than once methods = list() + # Protect literal "%%" (an escaped percent sign) before atom processing + # so the second "%" is not mistaken for the start of an atom and is not + # re-escaped into an invalid "%%%" format by the cleanup pass below. + sentinel = "\x00" + log_format = log_format.replace("%%", sentinel) + for atom in self.FORMAT_RE.findall(log_format): if atom[1] == "": format_key1 = self.LOG_FORMAT_MAP[atom[0]] @@ -121,6 +127,8 @@ def compile_format(self, log_format: str) -> tuple[str, list[KeyMethod]]: log_format = self.FORMAT_RE.sub(r"%s", log_format) log_format = self.CLEANUP_RE.sub(r"%\1", log_format) + # Restore protected "%%" as a percent-format escape for a literal "%". + log_format = log_format.replace(sentinel, "%%") return log_format, methods @staticmethod diff --git a/tests/test_web_log.py b/tests/test_web_log.py index 0d0fac192c2..3840bd0e630 100644 --- a/tests/test_web_log.py +++ b/tests/test_web_log.py @@ -24,10 +24,27 @@ def test_access_logger_format() -> None: log_format = '%T "%{ETag}o" %X {X} %%P' mock_logger = mock.Mock() access_logger = AccessLogger(mock_logger, log_format) - expected = '%s "%s" %%X {X} %%%s' + expected = '%s "%s" %%X {X} %%P' assert expected == access_logger._log_format +def test_access_logger_format_literal_percent() -> None: + """A literal ``%%`` compiles to a valid percent-escape and logs correctly. + + Regression test for https://github.com/aio-libs/aiohttp/issues/14390 + """ + mock_logger = mock.Mock() + access_logger = AccessLogger(mock_logger, "%a %s 100%%") + assert "%s %s 100%%" == access_logger._log_format + + request = mock.Mock(remote="127.0.0.1", headers={}) + response = mock.Mock(status=200, headers={}) + access_logger.log(request, response, 0.1) + # The escaped "%%" renders as a single "%" and no error is raised. + mock_logger.info.assert_called_once() + mock_logger.exception.assert_not_called() + + @pytest.mark.skipif( IS_PYPY, reason="""