Skip to content

Commit d68b0d0

Browse files
author
bhanugoudm041
committed
Fix O(n^2) CPU exhaustion in parse_link_text (GHSA patch)
1 parent 067f908 commit d68b0d0

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

src/mistune/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def parse_link_text(src: str, pos: int) -> Union[Tuple[str, int], Tuple[None, No
115115
while pos < len(src):
116116
m = _INLINE_SQUARE_BRACKET_RE.search(src, pos)
117117
if not m:
118+
pos = len(src) # ← CHANGE 1: record we scanned to end
118119
break
119120

120121
pos = m.end()
@@ -130,7 +131,7 @@ def parse_link_text(src: str, pos: int) -> Union[Tuple[str, int], Tuple[None, No
130131
if found:
131132
text = src[start_pos : pos - 1]
132133
return text, pos
133-
return None, None
134+
return None, pos # ← CHANGE 2: return pos instead of None
134135

135136

136137
def parse_link_label(src: str, start_pos: int) -> Union[Tuple[str, int], Tuple[None, None]]:

src/mistune/inline_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ def parse_link(self, m: Match[str], state: InlineState) -> Optional[int]:
128128
if label is None:
129129
text, end_pos = parse_link_text(state.src, pos)
130130
if text is None:
131-
return None
131+
# ← CHANGE: emit skipped content as literal text
132+
# so nothing is dropped, then jump ahead to end_pos
133+
skipped = state.src[m.start():end_pos]
134+
state.append_token({"type": "text", "raw": skipped})
135+
return end_pos
132136

133137
assert end_pos is not None
134138

0 commit comments

Comments
 (0)