diff --git a/markdown_it/rules_block/heading.py b/markdown_it/rules_block/heading.py index afcf9ed4..c58c1c54 100644 --- a/markdown_it/rules_block/heading.py +++ b/markdown_it/rules_block/heading.py @@ -19,7 +19,10 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bo if state.is_code_block(startLine): return False - ch: str | None = state.src[pos] + try: + ch: str | None = state.src[pos] + except IndexError: + return False if ch != "#" or pos >= maximum: return False diff --git a/markdown_it/rules_block/html_block.py b/markdown_it/rules_block/html_block.py index 3d43f6ee..fe7e464e 100644 --- a/markdown_it/rules_block/html_block.py +++ b/markdown_it/rules_block/html_block.py @@ -44,7 +44,10 @@ def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool) -> if not state.md.options.get("html", None): return False - if state.src[pos] != "<": + try: + if state.src[pos] != "<": + return False + except IndexError: return False lineText = state.src[pos:maximum] diff --git a/tests/test_fuzzer.py b/tests/test_fuzzer.py index 7286f8ea..42c6600b 100644 --- a/tests/test_fuzzer.py +++ b/tests/test_fuzzer.py @@ -23,3 +23,23 @@ def test_fuzzing(raw_input, expected): md = MarkdownIt() md.parse(raw_input) assert md.render(raw_input) == expected + + +# Input that ends on a blockquote marker while a table is open inside the quote +# used to raise ``IndexError: string index out of range`` from the terminator +# rules ``html_block`` and ``heading`` (gh-issue 415). ``table`` must be enabled +# for the terminator rules to run on that line. +GH_415_INPUT = "> | a | b |\n> |---|---|\n>" + + +def test_gh_415_table_in_blockquote_at_eof_html_block() -> None: + # html_block runs first, so with html enabled it is the rule that used to raise + md = MarkdownIt().enable("table") + md.render(GH_415_INPUT) # must not raise IndexError + + +def test_gh_415_table_in_blockquote_at_eof_heading() -> None: + # with html disabled, html_block bails at its options check and heading is + # the terminator rule that used to raise + md = MarkdownIt("commonmark", {"html": False}).enable("table") + md.render(GH_415_INPUT) # must not raise IndexError