Python-Markdown / markdown

A Python implementation of John Gruber’s Markdown with Extension support.
https://python-markdown.github.io/
BSD 3-Clause "New" or "Revised" License
3.74k stars 858 forks source link

Fix edge-case crash in InlineProcessor #1406

Closed oprypin closed 10 months ago

oprypin commented 10 months ago

If an inlineprocessor returns an AtomicString (even though that is pointless, a plain string is atomic in that context), there can be an exception in 2 separate places. The added test case was crashing before this change.

  File "tests/test_apis.py", line 722, in testInlineProcessorDoesntCrashWithAtomicString
    new = self.inlineprocessor.run(tree)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "markdown/treeprocessors.py", line 384, in run
    lst = self.__processPlaceholders(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "markdown/treeprocessors.py", line 223, in __processPlaceholders
    if child.tail:
       ^^^^^^^^^^
AttributeError: 'AtomicString' object has no attribute 'tail'
  File "tests/test_apis.py", line 722, in testInlineProcessorDoesntCrashWithAtomicString
    new = self.inlineprocessor.run(tree)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "markdown/treeprocessors.py", line 387, in run
    self.__handleInline(text), child
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "markdown/treeprocessors.py", line 136, in __handleInline
    data, matched, startIndex = self.__applyPattern(
                                ^^^^^^^^^^^^^^^^^^^^
  File "markdown/treeprocessors.py", line 310, in __applyPattern
    if not isinstance(node.text, util.AtomicString):
                      ^^^^^^^^^
AttributeError: 'AtomicString' object has no attribute 'text'