Skip to content

Fix access log format literal percent escape producing an invalid format - #14586

Open
321215665 wants to merge 1 commit into
aio-libs:masterfrom
321215665:fix/access-log-percent-escape
Open

321215665 wants to merge 1 commit into
aio-libs:masterfrom
321215665:fix/access-log-percent-escape

Conversation

@321215665

Copy link
Copy Markdown

Description

A literal %% in the access log access_log_format is documented as "the percent sign", but in practice a format containing it never logged anything: every request instead logged Error in logging with a ValueError: incomplete format.

AccessLogger.compile_format() replaced known atoms with %s via FORMAT_RE, then ran CLEANUP_RE to escape any remaining % as %%. A literal %% fell between the two passes: CLEANUP_RE matched the first % plus the following % and re-escaped them into %%%, which is an invalid percent-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.

Fix

Protect literal %% before atom processing and restore it as a valid percent-escape after the cleanup pass. Now %a %s 100%% compiles to %s %s 100%% and logs ... 100% with no error, and %%P logs the literal text %P.

test_access_logger_format asserted the old (buggy) compiled form for %%P; its expectation is updated, and a regression test for the literal-percent crash is added.

Fixes #14390

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 aio-libs#14390
@321215665
321215665 requested a review from asvetlov as a code owner December 3, 2026 20:10
@agentscanapp

agentscanapp Bot commented Dec 3, 2026

Copy link
Copy Markdown

Automation signals

Activity patterns show signs of automation.

View full analysis →

Evidence
  • Fork spike: 8 forks within 24 hours

This is an automated analysis by AgentScan

@greptile-apps

greptile-apps Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • The log format string is only set by the application developer or operator (via run_app or gunicorn config), never by a remote client, so the new sentinel logic never touches attacker input.
  • Request-controlled values like headers and the request path enter the log line only as %-format arguments at log time, not as format text, so the \x00 sentinel cannot collide with logged data or enable format-string injection.
  • The fix removes a ValueError path (the invalid %%% compile) rather than adding one, and log-time formatting errors are still caught by the try/except in log(), so a bad operator format cannot crash request handling.
  • The compiled format and its method list stay consistent (e.g. %a %s 100%% yields two %s and two methods), and the _FORMAT_CACHE still stores a matching compiled result.
  • No dependencies, auth, or trust-boundary code changed; the second changed file is a test.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.13%. Comparing base (abbb10c) to head (d6b2318).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14586   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         135      135           
  Lines       54630    54642   +12     
  Branches     2808     2808           
=======================================
+ Hits        54158    54170   +12     
  Misses        357      357           
  Partials      115      115           
Flag Coverage Δ
Autobahn 21.53% <30.76%> (+<0.01%) ⬆️
CI-GHA 99.06% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.75% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.53% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.38% <100.00%> (-0.01%) ⬇️
Py-3.11 98.32% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.40% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.39% <100.00%> (-0.01%) ⬇️
Py-3.14 98.45% <100.00%> (+<0.01%) ⬆️
Py-3.15 98.45% <100.00%> (+<0.01%) ⬆️
Py-3.15t 98.30% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.12 97.00% <100.00%> (+0.01%) ⬆️
VM-macos-latest 98.38% <100.00%> (-0.01%) ⬇️
VM-ubuntu-latest 98.75% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 97.53% <100.00%> (+<0.01%) ⬆️
cython-coverage 84.04% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 Tier: apexRetriggerView in Greptile

[Low impact] The change is limited to literal-percent handling in built-in access logs.

Safe to merge, with a non-blocking edge case affecting null separators in custom log formats.

Reviews (1) · Last reviewed commit: "Fix access log format literal % escape p..." · Reviewed by Greptile

Comment thread aiohttp/web_log.py
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, "%%")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Null separators become percent signs

If an app uses null characters to separate fields in log_format, this replacement changes them to percent signs too. For example, "%a\x00%s" now writes % between the address and status, so readers that split on null characters lose the field boundaries. Choose a marker absent from the input, or handle %% without a marker.

@codspeed

codspeed Bot commented Dec 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 50 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing 321215665:fix/access-log-percent-escape (d6b2318) with master (abbb10c)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The %% escape in the access log format makes every access log line fail with "Error in logging"

1 participant