Skip to content

Commit

Permalink
prevent very large headline prefixes
Browse files Browse the repository at this point in the history
for example: `<h9999999>` could crash the conversion.

fixes #143
  • Loading branch information
AlexVonB committed Nov 24, 2024
1 parent fe8a821 commit 9595618
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ def convert_hn(self, n, el, text, convert_as_inline):
if convert_as_inline:
return text

# prevent MemoryErrors in case of very large n
n = max(1, min(6, n))

style = self.options['heading_style'].lower()
text = text.strip()
if style == UNDERLINED and n <= 2:
Expand Down
1 change: 1 addition & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def test_hn():
assert md('<h4>Hello</h4>') == '\n#### Hello\n\n'
assert md('<h5>Hello</h5>') == '\n##### Hello\n\n'
assert md('<h6>Hello</h6>') == '\n###### Hello\n\n'
assert md('<h10>Hello</h10>') == md('<h6>Hello</h6>')


def test_hn_chained():
Expand Down

0 comments on commit 9595618

Please sign in to comment.