Skip to content

Enforce max_line_size on fragmented request target and reason in C parser - #12826

Merged
bdraco merged 3 commits into
masterfrom
c-parser-fragmented-line-limit
Jun 7, 2026
Merged

bdraco merged 3 commits into
masterfrom
c-parser-fragmented-line-limit

Conversation

@bdraco

@bdraco bdraco commented Jun 7, 2026

Copy link
Copy Markdown
Member

What do these changes do?

The C HTTP parser receives the request target and response reason phrase through on_url and on_status callbacks, which can fire multiple times for a single line when it is split across reads. Each callback only compared its own fragment against max_line_size, so a line split into small enough pieces could accumulate past the limit without raising LineTooLong. This checks the accumulated buffer length plus the new fragment instead, so the limit is enforced on the whole line, matching the pure-Python parser.

Are there changes in behavior for the user?

A request target or response reason phrase that exceeds max_line_size is now rejected with LineTooLong even when it arrives split across multiple reads; previously the C parser accepted it. Lines within the limit are unaffected.

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

No.

Related issue number

N/A

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes N/A
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt N/A, already listed
  • Add a new news fragment into the CHANGES/ folder

@bdraco
bdraco requested review from asvetlov and webknjaz as code owners June 7, 2026 00:21
@bdraco
bdraco marked this pull request as draft June 7, 2026 00:21
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jun 7, 2026
@bdraco bdraco 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 Jun 7, 2026
@codecov

codecov Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.14%. Comparing base (69344c6) to head (adbf5e5).
⚠️ Report is 516 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12826      +/-   ##
==========================================
+ Coverage   98.94%   99.14%   +0.20%     
==========================================
  Files         131      128       -3     
  Lines       47099    46424     -675     
  Branches     2435     2435              
==========================================
- Hits        46600    46027     -573     
+ Misses        376      274     -102     
  Partials      123      123              
Flag Coverage Δ
Autobahn 22.39% <20.00%> (-0.01%) ⬇️
CI-GHA 98.91% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.66% <100.00%> (-0.01%) ⬇️
OS-Windows 97.04% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.92% <100.00%> (-0.01%) ⬇️
Py-3.10 98.15% <100.00%> (-0.01%) ⬇️
Py-3.11 98.40% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.49% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.47% <100.00%> (+<0.01%) ⬆️
Py-3.14 98.49% <100.00%> (+<0.01%) ⬆️
Py-3.14t 97.55% <100.00%> (-0.01%) ⬇️
Py-pypy-3.11 97.41% <100.00%> (-0.01%) ⬇️
VM-macos 97.92% <100.00%> (-0.01%) ⬇️
VM-ubuntu 98.66% <100.00%> (-0.01%) ⬇️
VM-windows 97.04% <100.00%> (+<0.01%) ⬆️
cython-coverage ?

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.

@aiolibsbot

Copy link
Copy Markdown
Contributor

PR Review — Enforce max_line_size on fragmented request target and reason in C parser

Correct, minimal fix that closes a real bypass of max_line_size in the C parser. Merge-ready.

  • len(pyparser._buf) + length > pyparser._max_line_size is the right invariant: _buf is reset on message begin (line 774) and drained in _on_status_complete (lines 690-694, 760-763), so it accumulates exactly the URL or reason fragments and nothing else.
  • Error-message construction (pyparser._buf + at[:length], truncated to 100 chars) is unchanged, so the existing LineTooLong payload still reflects the full accumulated line.
  • Tests cover both on_url and on_status fragmentation paths with fragment sizes that stay individually under the limit but accumulate past it — exactly the regression window the patch closes.
  • Changelog fragment CHANGES/12826.bugfix.rst is well-scoped and attributes correctly. Behavior now matches the pure-Python parser, as the PR description claims.


Checklist

  • Logic correctly enforces the limit on the accumulated buffer
  • Error message format preserved
  • Buffer lifetime safe (reset between messages, not shared with headers)
  • Tests cover the fragmented case for both URL and status
  • Changelog fragment present and correctly attributed
  • No untested branches introduced

Automated review by Kōan (Claude) HEAD=6ba0839

@codspeed

codspeed Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 72 untouched benchmarks
⏩ 72 skipped benchmarks1


Comparing c-parser-fragmented-line-limit (adbf5e5) with master (69344c6)

Open in CodSpeed

Footnotes

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

@bdraco
bdraco marked this pull request as ready for review June 7, 2026 00:55
@bdraco
bdraco merged commit 36df6c1 into master Jun 7, 2026
48 of 51 checks passed
@bdraco
bdraco deleted the c-parser-fragmented-line-limit branch June 7, 2026 01:31
@patchback

patchback Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Backport to 3.14: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 36df6c1 on top of patchback/backports/3.14/36df6c138d10c5776a25c69b698c41153d84d571/pr-12826

Backporting merged PR #12826 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.14/36df6c138d10c5776a25c69b698c41153d84d571/pr-12826 upstream/3.14
  4. Now, cherry-pick PR Enforce max_line_size on fragmented request target and reason in C parser #12826 contents into that branch:
    $ git cherry-pick -x 36df6c138d10c5776a25c69b698c41153d84d571
    If it'll yell at you with something like fatal: Commit 36df6c138d10c5776a25c69b698c41153d84d571 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 36df6c138d10c5776a25c69b698c41153d84d571
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Enforce max_line_size on fragmented request target and reason in C parser #12826 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.14/36df6c138d10c5776a25c69b698c41153d84d571/pr-12826
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback

patchback Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Backport to 3.15: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 36df6c1 on top of patchback/backports/3.15/36df6c138d10c5776a25c69b698c41153d84d571/pr-12826

Backporting merged PR #12826 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.15/36df6c138d10c5776a25c69b698c41153d84d571/pr-12826 upstream/3.15
  4. Now, cherry-pick PR Enforce max_line_size on fragmented request target and reason in C parser #12826 contents into that branch:
    $ git cherry-pick -x 36df6c138d10c5776a25c69b698c41153d84d571
    If it'll yell at you with something like fatal: Commit 36df6c138d10c5776a25c69b698c41153d84d571 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 36df6c138d10c5776a25c69b698c41153d84d571
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Enforce max_line_size on fragmented request target and reason in C parser #12826 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.15/36df6c138d10c5776a25c69b698c41153d84d571/pr-12826
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

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

Labels

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.

2 participants