Skip to content

Fix the %% escape in the access log format - #14391

Open
ArnabDutta01 wants to merge 3 commits into
aio-libs:masterfrom
ArnabDutta01:fix/access-log-percent-escape
Open

ArnabDutta01 wants to merge 3 commits into
aio-libs:masterfrom
ArnabDutta01:fix/access-log-percent-escape

Conversation

@ArnabDutta01

Copy link
Copy Markdown

What do these changes do?

A literal %% in the access log format is documented as "The percent sign", but CLEANUP_RE turned it into %%%, so every log line failed with Error in logging (ValueError: incomplete format). The % that follows %% was also read as the start of an atom, so %%P logged the process id instead of %P.

compile_format() now splits the format on %%, looks up atoms only inside the pieces, and joins the compiled pieces back with %%.

Are there changes in behavior for the user?

Formats that contain %% now log a single % instead of failing on every request. %% followed by an atom letter (for example %%P) now logs the literal text (%P). It used to log % followed by the atom's value. test_access_logger_format asserted that old compiled form, so I updated its expectation.

Is it a substantial burden for the maintainers to support this?

No. The change is a few lines inside AccessLogger.compile_format() and does not change the public API.

Related issue number

Fixes #14390

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (N/A, the docs already describe %% as the percent sign)
  • If you provide code modifications, please add yourself to
    CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep the list in alphabetical order, the file is sorted by name.
  • Add a new news fragment into the CHANGES/ folder
Local test run

Without the fix (aiohttp/web_log.py from master, new tests applied):

$ PYTHONPATH=. AIOHTTP_NO_EXTENSIONS=1 pytest tests/test_web_log.py --no-cov -q
FAILED tests/test_web_log.py::test_access_logger_literal_percent[%%%% %s-%% 200]
FAILED tests/test_web_log.py::test_access_logger_literal_percent[%% %s-% 200]
FAILED tests/test_web_log.py::test_access_logger_literal_percent[%%%s-%200]
FAILED tests/test_web_log.py::test_access_logger_literal_percent[%%s %s-%s 200]
FAILED tests/test_web_log.py::test_access_logger_format - assert '%s "%s" %%X...
FAILED tests/test_web_log.py::test_access_logger_literal_percent[%s 100%%-200 100%]
6 failed, 16 passed in 3.97s

With the fix:

$ PYTHONPATH=. AIOHTTP_NO_EXTENSIONS=1 pytest tests/test_web_log.py tests/test_web_runner.py tests/test_worker.py --no-cov -n auto -q
84 passed, 2 skipped in 4.33s

$ pre-commit run --files aiohttp/web_log.py tests/test_web_log.py CHANGES/NNNN.bugfix.rst CONTRIBUTORS.txt
(all hooks passed, checked with the fragment renamed to a numeric name)

$ mypy aiohttp/web_log.py tests/test_web_log.py
(no errors in the changed files; the 8 remaining errors are in other files and are the same on master)

Drafted with Claude Code (Claude Opus 5.5); reviewed by @ArnabDutta01.

A literal "%%" in the access log format was not treated as an escape:
the cleanup step turned it into "%%%", so every log line failed with
"Error in logging". The "%" that followed it was also taken as the start
of an atom, so "%%P" logged the process id instead of "%P".

Split the format on "%%" before looking up atoms and join the compiled
parts back with "%%".

Signed-off-by: ArnabDutta01 <rickkdutta@gmail.com>
@agentscanapp

agentscanapp Bot commented Nov 16, 2026

Copy link
Copy Markdown

Mixed activity

Activity patterns show a mix of organic and automated signals.

View full analysis →

Evidence
  • Multiple forks: 6 repositories forked in a single 24-hour window

This is an automated analysis by AgentScan

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Nov 16, 2026
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.13%. Comparing base (fe09949) to head (7c06dba).
⚠️ Report is 50 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #14391      +/-   ##
==========================================
+ Coverage   99.12%   99.13%   +0.01%     
==========================================
  Files         135      135              
  Lines       53661    54638     +977     
  Branches     2737     2808      +71     
==========================================
+ Hits        53191    54166     +975     
- Misses        356      357       +1     
- Partials      114      115       +1     
Flag Coverage Δ
Autobahn 21.53% <46.15%> (-0.13%) ⬇️
CI-GHA 99.06% <100.00%> (+0.01%) ⬆️
OS-Linux 98.75% <100.00%> (+0.01%) ⬆️
OS-Windows 97.53% <100.00%> (+0.04%) ⬆️
OS-macOS 98.38% <100.00%> (+0.02%) ⬆️
Py-3.11 98.32% <100.00%> (+0.02%) ⬆️
Py-3.12 98.40% <100.00%> (+0.02%) ⬆️
Py-3.13 98.39% <100.00%> (+0.01%) ⬆️
Py-3.14 98.45% <100.00%> (+0.02%) ⬆️
Py-3.15 98.45% <100.00%> (+0.02%) ⬆️
Py-3.15t 98.30% <100.00%> (+0.01%) ⬆️
Py-pypy-3.12 96.99% <100.00%> (+0.02%) ⬆️
VM-macos-latest 98.38% <100.00%> (+0.02%) ⬆️
VM-ubuntu-latest 98.75% <100.00%> (+0.01%) ⬆️
VM-windows-latest 97.53% <100.00%> (+0.04%) ⬆️
cython-coverage 84.04% <100.00%> (+0.10%) ⬆️

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.

@codspeed

codspeed Bot commented Nov 16, 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 ArnabDutta01:fix/access-log-percent-escape (7c06dba) with master (b5acd43)

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. ↩

@ArnabDutta01
ArnabDutta01 marked this pull request as ready for review November 16, 2026 06:58
@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 Tier: apexRetriggerView in Greptile

[Medium impact] The PR appears safe to merge.

Reviews (3) · Last reviewed commit: "Mention the %%P change in the release no..." · Reviewed by Greptile

Comment thread CHANGES/14390.bugfix.rst Resolved
@Polandia94 Polandia94 added backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot labels Nov 16, 2026
Signed-off-by: ArnabDutta01 <rickkdutta@gmail.com>

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This now matches the %% row in docs/logging.rst, and I'm approving it.

Splitting on %% before the atom lookup also fixes %%P, not only the %%% crash. The old test_access_logger_format expectation recorded the bug, so changing it is right.

# exported d3e6278, Python 3.12, AIOHTTP_NO_EXTENSIONS=1
$ pytest tests/test_web_log.py --no-cov -q
22 passed in 2.72s
# aiohttp/web_log.py from base fe09949, tests kept
6 failed, 16 passed in 2.06s
$ pre-commit run --all-files    # every hook passed
$ mypy                          # mypy 2.4.1: 9 errors, all in worker.py, compression_utils.py, test_worker.py

non-blocking: %%P used to log % plus the pid and now logs %P, but CHANGES/14390.bugfix.rst only mentions the crash. Could you add a clause there for anyone whose format relied on the old output?

Signed-off-by: ArnabDutta01 <rickkdutta@gmail.com>
@ArnabDutta01

ArnabDutta01 commented Dec 3, 2026 •

Copy link
Copy Markdown
Author

Thanks for testing it. I added a sentence about %%P (and %% before any other format letter) to CHANGES/14390.bugfix.rst in 7c06dba.

@ArnabDutta01
ArnabDutta01 force-pushed the fix/access-log-percent-escape branch from e8773dc to 7c06dba Compare December 3, 2026 14:26
@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 access log format string comes only from operator config, never from a remote peer, so the new %% split/join logic never handles attacker input.
  • Attacker-controlled request data (path, headers, status) still flows only as %-format arguments in log(), not into the format string, so no format-string injection is possible.
  • The atom lookup and %s substitution run the same regex over the same parts, so the method count always matches the placeholder count; any residual mismatch would raise inside the existing try/except and only log an error.
  • Unknown %X sequences are still escaped per part, so they cannot consume arguments as unintended format specifiers at log time.
  • The compiled-format cache stores only the format and method list, so no request data leaks across requests through it.

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

Labels

agentscan:mixed-signals backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

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"

3 participants