Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception caused in custom plugin altering parsing rules #376

Open
maxking opened this issue Jan 15, 2024 · 2 comments
Open

Exception caused in custom plugin altering parsing rules #376

maxking opened this issue Jan 15, 2024 · 2 comments

Comments

@maxking
Copy link
Contributor

maxking commented Jan 15, 2024

We have a custom plugin that we use to disable all the parsing rules, except block_quote. The way we have implemented this is:

def plugin_disable_markdown(md):
    """This plugin disables most of the rules in mistune."""
    md.block.rules = ['block_quote']
    md.block.block_quote_rules = ['block_quote']
    md.block.list_rules = ['block_quote']
    md.inline.rules = ['inline_html', 'auto_link']

This causes an exception in the situation when the block_quote nesting depth increases the max_nesting_depth value (6 is the default in mistune).

So, a text like:

>> >> >> foo bar

will raise an exception like below (few lines of traceback is removed, full traceback can be found here)

  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/hyperkitty/templatetags/decorate.py", line 36, in render
    return mark_safe(text_renderer(content))
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/markdown.py", line 110, in __call__
    return self.parse(s)[0]
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/markdown.py", line 85, in parse
    self.block.parse(state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 168, in parse_method
    return func(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 369, in parse_block_quote
    self.parse(child, rules)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 168, in parse_method
    return func(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 369, in parse_block_quote
    self.parse(child, rules)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 168, in parse_method
    return func(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 369, in parse_block_quote
    self.parse(child, rules)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 168, in parse_method
    return func(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 369, in parse_block_quote
    self.parse(child, rules)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 168, in parse_method
    return func(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 369, in parse_block_quote
    self.parse(child, rules)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 168, in parse_method
    return func(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 369, in parse_block_quote
    self.parse(child, rules)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/block_parser.py", line 446, in parse
    end_pos = self.parse_method(m, state)
  File "/opt/mailman/mm/venv/lib/python3.9/site-packages/mistune/core.py", line 167, in parse_method
    func = self._methods[m.lastgroup]
KeyError: None
@maxking
Copy link
Contributor Author

maxking commented Jan 15, 2024

My suspicion is that after 5 levels of parsing blockquote, the block.block_quote_rules removes block_quote so that becomes an empty set of rules []. And since there aren't any rules m.lastgroup has no groups so it returns None value causing the above exception.

IMO, this can be guarded against with a None check of try-except (so control flow is altered only in case of special case of block_quote_rules being empty, which isn't typical).

Happy to submit a patch a try-except to guard works.

@lepture
Copy link
Owner

lepture commented Jan 17, 2024

@maxking A pull request is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants