Repository navigation
Conversation
The regex that reads the encoding from the XML declaration is matched against the whole document, and both of its `.*` can span the entire first line. A feed served as a single line that contains many `encoding="..."` attributes (such as KaTeX's MathML output) makes it backtrack catastrophically, so parsing effectively never finishes. Restrict the pattern to the processing instruction at the start of the document, so the work is bounded by the length of the declaration rather than the document. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
oToToT
force-pushed
the
fix-xml-encoding-backtracking
branch
from
November 20, 2026 14:07
b9c195a to
3d001ad
Compare
oToToT
marked this pull request as ready for review
November 20, 2026 14:10
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.
Problem
convert_to_utf8()reads the declared encoding withand matches it against the whole document. Because
.doesn't match a newline, this is cheap for typical feeds. But when a feed is served as a single line, both.*can span the entire document. If that document also contains manyencoding="..."attributes, the pattern backtracks catastrophically.This happens in practice. A Hugo-generated feed served as a single 646 KB line, whose posts contain KaTeX math, has hundreds of
<annotation encoding="application/x-tex">elements. Parsing it didn't finish within 5 minutes. On synthetic single-line input of this shape, matching time grows about 7× each time the input doubles: 12 ms at 5.5 KB, 83 ms at 11 KB, 69 s at 110 KB.Python's
reholds the GIL while it matches, so a caller can't easily time this out from another thread. For example, rss2email'sfeed-timeoutnever fires; it just hangs.Fix
Restrict the pattern to the processing instruction at the start of the document:
<?...?>.>. The work is bounded by the length of the declaration, not the document, and the match takes about 1 µs on a 5.5 MB single-line document. I also timed adversarial inputs aimed at the new pattern up to 5 MB (no>at all, an unterminated quote, many repeatedencoding="). The time grows linearly and stays under 50 ms.\nis still excluded, so a declaration split across lines behaves exactly as before.I fuzzed the old and new patterns against each other on 300,000 random short inputs. Every disagreement was a case where the old pattern's match extended past the end of the XML declaration, i.e. it read an
encoding=attribute from the document body. That is the only intended behavior change, and a test below covers it.Tests
test_convert_to_utf8_single_line_document: a 550 KB single-line document with 10,000encoding=attributes. It passes in milliseconds with this change. Onmainit didn't finish within 120 s.test_convert_to_utf8_ignores_encoding_after_xml_declaration: fails onmain, which detectsiso-8859-1from an element attribute after the declaration.🤖 Generated with Claude Code