fix: prevent ReDoS in HTML tokenizer via bounded substring matching (issue #707)#708
Closed
vanhci wants to merge 1 commit into
Closed
fix: prevent ReDoS in HTML tokenizer via bounded substring matching (issue #707)#708vanhci wants to merge 1 commit into
vanhci wants to merge 1 commit into
Conversation
Replace _sorta_html_tokenize_re.split(text) with a hand-written tokenizer (_sorta_html_tokenize) that first locates <...> boundaries using simple string operations, then validates each bounded substring with the existing regex in .match() mode. This eliminates catastrophic backtracking on malformed HTML fragments like repeated <p m="1"< sequences, where the regex engine would previously attempt exponentially many attribute-split combinations across the full input. Also replaces the re.findall call in _tag_is_closed with a linear str.find loop to avoid the same class of ReDoS. Benchmark: 60KB trigger payload goes from 2.2s to 0.05s (40x speedup). All existing tests pass. Fixes #707
Crozzers
reviewed
May 24, 2026
| # here. | ||
| escaped = [] | ||
| is_html_markup = False | ||
| for token in self._sorta_html_tokenize_re.split(text): |
Contributor
There was a problem hiding this comment.
I think the regex split is also used in _hash_html_spans. Worth replacing that as well if tests pass?
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
Fix a ReDoS (Regular Expression Denial of Service) vulnerability in the HTML tokenizer that allows a crafted ~60KB Markdown input to monopolize the Python render process for 2+ seconds.
Problem
_sorta_html_tokenize_re.split(text)applies the full tokenizer regex across the entire input. When the input contains repeated malformed tag fragments like<p m="1"<p m="1"<..., the regex engine attempts exponentially many attribute-split combinations during backtracking, causing O(2^n) CPU time.Fix
Two changes, both replacing regex-heavy approaches with linear string operations:
_sorta_html_tokenize()(new method): Replaces_sorta_html_tokenize_re.split(text)with a hand-written tokenizer that:<characters viastr.find()(O(n) linear scan)<and the next>.match()mode on the bounded substring_tag_is_closed(): Replacesre.findall('<%s(?:.*?)>' % tag_name, text)with astr.find()loop to avoid the same class of ReDoS.Benchmark
All existing tests pass. Normal Markdown rendering is unaffected.
Related
Fixes #707