Skip to content

Prevent catastrophic backtracking in the XML declaration encoding regex - #617

Open
oToToT wants to merge 1 commit into
kurtmckee:mainfrom
oToToT:fix-xml-encoding-backtracking
Open

oToToT wants to merge 1 commit into
kurtmckee:mainfrom
oToToT:fix-xml-encoding-backtracking

Conversation

@oToToT

@oToToT oToToT commented Nov 20, 2026

Copy link
Copy Markdown

Problem

convert_to_utf8() reads the declared encoding with

RE_XML_PI_ENCODING = re.compile(rb'^<\?.*encoding=[\'"](.*?)[\'"].*\?>')

and 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 many encoding="..." 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 re holds the GIL while it matches, so a caller can't easily time this out from another thread. For example, rss2email's feed-timeout never fires; it just hangs.

Fix

Restrict the pattern to the processing instruction at the start of the document:

^(?=<\?[^>\n]*\?>)<\?[^>\n]*encoding=["']([^"'\n>]*)["']
  • The lookahead checks once that the document starts with a complete <?...?>.
  • After that, nothing can match past the first >. 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 repeated encoding="). The time grows linearly and stays under 50 ms.
  • \n is 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,000 encoding= attributes. It passes in milliseconds with this change. On main it didn't finish within 120 s.
  • test_convert_to_utf8_ignores_encoding_after_xml_declaration: fails on main, which detects iso-8859-1 from an element attribute after the declaration.
  • Full suite: 4299 passed, 8 skipped. pre-commit hooks pass.

🤖 Generated with Claude Code

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
oToToT force-pushed the fix-xml-encoding-backtracking branch from b9c195a to 3d001ad Compare November 20, 2026 14:07
@oToToT
oToToT marked this pull request as ready for review November 20, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant