fix: avoid quadratic inline footnote membership checks - #478
Open
susyimes wants to merge 4 commits into
Open
Conversation
Inline footnote parsing does O(n) list membership per reference (quadratic DoS on n footnote references) Defect id: footnote-defs-membership-quadratic Verified against upstream tag v3.3.4 and HEAD (triage: unfixed_at_head). Generated-by: blackhole-agent upstream-publication plane (autonomous stewardship mission)
susyimes
pushed a commit
to susyimes/blackhole-agent
that referenced
this pull request
Aug 9, 2026
…3.4 publication - lepture/mistune#478; PyPI pub...
There was a problem hiding this comment.
Pull request overview
This PR addresses a potential algorithmic complexity DoS in the footnotes plugin by eliminating repeated O(n) list membership checks during inline footnote reference parsing, and adds a regression test/repro to prevent reintroduction.
Changes:
- Optimize
parse_inline_footnoteto use a dict (footnote_indexes) for O(1) membership checks instead of scanning thefootnoteslist. - Add a standalone complexity repro script and a regression test that runs it.
- Extend the test suite with a performance-focused guard against superlinear behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/mistune/plugins/footnotes.py |
Switch inline footnote membership checks from list to dict index to avoid quadratic behavior. |
tests/footnote_defs_quadratic.py |
Add standalone doubling-ladder repro to detect superlinear growth. |
tests/test_contribution_footnote_defs_membership_quadratic.py |
Add pytest regression test that executes the repro against the local src tree. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #478 +/- ##
=======================================
Coverage 90.86% 90.86%
=======================================
Files 36 36
Lines 3713 3713
Branches 696 696
=======================================
Hits 3374 3374
Misses 208 208
Partials 131 131 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
added 2 commits
August 31, 2026 15:37
…ole/footnote-defs-membership-quadratic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Inline footnote parsing already maintained a
footnote_indexesmapping, but still checkedkey not in notes. That scanned a growing list for every new reference and made documents with many distinct footnotes quadratic.This PR:
footnoteslist and rendered numbering;Regression coverage
tests/test_security_footnotes.pysupplies a list whose__contains__raises. The patched implementation passes because it checksfootnote_indexes; temporarily restoring the oldkey not in notesline makes the test fail at that exact membership operation.This replaces the former wall-clock doubling-ladder test, which could false-positive under host load.
Verification
uv run --locked --group lint ruff check .— passeduv run --locked --group lint mypy— passed, 38 source filesuv run --locked --group test pytest --cov=mistune --cov-branch --cov-report=xml— 766 passedProvenance and sealed evidence
This contribution was originally prepared by blackhole-agent and was amended after review feedback. The evidence below was regenerated after synchronizing with upstream
mainand matches the reopened PR exactly.a1b50bc12e066e5707ff797f821829bfcdab03b5019af68ae9847f7adebc07a12fb762c9d50a0baae8ae33bfda9f74f623dead368778d23c83d731fa25e0875d9ae6c3b31f6622ca4373ba290d1c2ec067b557558d29411329e7af26src/mistune/plugins/footnotes.pyblob SHA256:3f1229e3a3fe221dd1038a765184d1626559bbd78acdab40d4845f721b96b474tests/test_security_footnotes.pyblob SHA256:790d457821de4913e6b95b7755ab65b4875983c92ce2a9da9bb5bca45919bcfcThe PR diff digest is the SHA256 of GitHub's
application/vnd.github.diffresponse for PR #478; it matches the locally generated base-to-head diff byte-for-byte.