« Back to Search  |  Reader Mode  |  Switch to Full Retro Site View Links: Stay in Reader  |  Retro Mode: ON  |  Original »
GitHub · Extracted in 863 ms

🐛 FIX: IndexError in html_block/heading terminator rules at EOF (#416) · executablebooks/markdown-it-py@1b7ef65

Source: https://github.com/executablebooks/markdown-it-py/commit/1b7ef6563902ab22deb8d4f3a9ce519bc382d190

Repository navigation

Commit 1b7ef65


Browse files
authored 🐛 FIX: IndexError in html_block/heading terminator rules at EOF (#416 ) ## Summary Fixes #415 . Input that ends on a blockquote marker while a table is open inside that quote raises `IndexError: string index out of range`: ```python from markdown_it import MarkdownIt # html_block.py -- raises IndexError MarkdownIt().enable("table").parse("> | a | b |\n> |---|---|\n>") # heading.py -- same input, raises once html_block is out of the way MarkdownIt("commonmark", {"html": False}).enable("table").parse("> | a | b |\n> |---|---|\n>") ``` ## Root cause With `table` enabled, `html_block` and `heading` run as **terminator rules** on the empty final line that a trailing `>` produces. There, `pos = state.bMarks[startLine] + state.tShift[startLine]` equals `len(state.src)`, and both rules index `state.src[pos]` without guarding that boundary: - `html_block.py`: `if state.src[pos] != "<":` - `heading.py`: `ch = state.src[pos]` -- its `pos >= maximum` check is on the *next* line, after the index In markdown-it (JS) the equivalent `state.src.charCodeAt(pos)` returns `NaN` out of range instead of raising -- the port hazard tracked in #190 . ## Fix Sibling terminator rules already defend against exactly this -- `hr.py` and `blockquote.py` wrap the same index access in `try: ... except IndexError: return False` (added for #185 / #204 ). `html_block.py` and `heading.py` were missed. This applies the same established guard to both. `html_block` runs before `heading`, so it shadows it (disabling `html` moves the traceback to `heading` rather than fixing it) -- hence both rules need the guard, and there is a regression test for each path. ## Tests Added two regression tests in `tests/test_fuzzer.py` (the existing home for crash-regression cases), one per crash path -- asserting the input renders without raising. ## Note on local verification I verified the guard logic in isolation (the old expression raises `IndexError` when `pos == len(src)`; the guarded version returns `False`, i.e. the rule declines, with identical behavior for in-range characters -- matching how `hr.py`/`blockquote.py` already behave). I was not able to run the full pytest suite locally (my environment's Python predates this package's minimum), so I'd appreciate CI confirming the two new tests pass and that the rendered output for that input is sensible. Happy to adjust the tests to assert exact rendered HTML if you'd prefer that over "does not raise". Co-authored-by: saket3395 <sakettulsan95@gmail.com> Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
1 parent c92ed7b commit 1b7ef65

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎markdown_it/rules_block/heading.py‎

Lines changed: 4 additions & 1 deletion
Original file line number Diff line number Diff line change
@@ -19,7 +19,10 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bo
19 19 if state.is_code_block(startLine):
20 20 return False
21 21
22 - ch: str | None = state.src[pos]
22 + try:
23 + ch: str | None = state.src[pos]
24 + except IndexError:
25 + return False
23 26
24 27 if ch != "#" or pos >= maximum:
25 28 return False

‎markdown_it/rules_block/html_block.py‎

Lines changed: 4 additions & 1 deletion
Original file line number Diff line number Diff line change
@@ -44,7 +44,10 @@ def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
44 44 if not state.md.options.get("html", None):
45 45 return False
46 46
47 - if state.src[pos] != "<":
47 + try:
48 + if state.src[pos] != "<":
49 + return False
50 + except IndexError:
48 51 return False
49 52
50 53 lineText = state.src[pos:maximum]

‎tests/test_fuzzer.py‎

Lines changed: 20 additions & 0 deletions
Original file line number Diff line number Diff line change
@@ -23,3 +23,23 @@ def test_fuzzing(raw_input, expected):
23 23 md = MarkdownIt()
24 24 md.parse(raw_input)
25 25 assert md.render(raw_input) == expected
26 +
27 +
28 +# Input that ends on a blockquote marker while a table is open inside the quote
29 +# used to raise ``IndexError: string index out of range`` from the terminator
30 +# rules ``html_block`` and ``heading`` (gh-issue 415). ``table`` must be enabled
31 +# for the terminator rules to run on that line.
32 +GH_415_INPUT = "> | a | b |\n> |---|---|\n>"
33 +
34 +
35 +def test_gh_415_table_in_blockquote_at_eof_html_block() -> None:
36 + # html_block runs first, so with html enabled it is the rule that used to raise
37 + md = MarkdownIt().enable("table")
38 + md.render(GH_415_INPUT) # must not raise IndexError
39 +
40 +
41 +def test_gh_415_table_in_blockquote_at_eof_heading() -> None:
42 + # with html disabled, html_block bails at its options check and heading is
43 + # the terminator rule that used to raise
44 + md = MarkdownIt("commonmark", {"html": False}).enable("table")
45 + md.render(GH_415_INPUT) # must not raise IndexError