Skip to content

link-patterns extra replaces matches inside inline HTML tags and destroys email auto-links #752

Description

@rahul05ranjan

Summary

With the link-patterns extra enabled, a pattern that matches inside an inline HTML tag (its attributes) or inside a <...> auto-link token gets replaced there, corrupting the HTML and destroying email auto-links. The same input renders correctly without the extra.

Repro

import re
import markdown2

lp = [(re.compile(r"\b(word)\b"), r"/w/\1")]
print(markdown2.markdown('See <span title="word">x</span>', extras=["link-patterns"], link_patterns=lp))
print(markdown2.markdown('<word@example.com>', extras=["link-patterns"], link_patterns=lp))
print(markdown2.markdown('<img src="/x.png" alt="word">', extras=["link-patterns"], link_patterns=lp))

Output with the extra:

<p>See <span title="<a href="/w/word">word</a>">x</span></p>
<p>&lt;<a href="/w/word">word</a>@example.com&gt;</p>
<p><img src="/x.png" alt="<a href="/w/word">word</a>"></p>

Expected (matches what plain markdown2 does for the markup, while prose matches are linkified):

<p>See <span title="word">x</span></p>
<p><a href="mailto:word@example.com">word@example.com</a></p>
<p><img src="/x.png" alt="word"></p>

Three distinct symptoms:

  1. <span title="word">x</span> — an anchor gets inserted inside the title attribute value, producing invalid HTML (and, depending on the pattern, allowing attribute content to be rewritten inside a raw tag).
  2. <word@example.com> — the mailto auto-link is destroyed entirely: the match splits the token so the auto-linker no longer recognizes it, and the angle brackets end up escaped.
  3. <img src="/x.png" alt="word"> — the alt attribute is corrupted the same way.

Root cause

LinkPatterns.run (lib/markdown2.py) guards matches with is_inside_link, which checks only _auto_link_re (<http://...> style auto-links) and _basic_link_re (markdown [...](...) syntax) — #385/#446 covered those. It does not check the other tokens that markdown2's own tokenizer _sorta_html_tokenize_re classifies as inline HTML markup: tags, comments, processing instructions and <word@example.com> style auto-link tokens. _do_auto_links runs after _do_links, so at the time the extra runs the email auto-link is still the raw <word@example.com> text and a match inside it destroys it.

Environment

python-markdown2 2.5.6 (current master, 6e89cff), Python 3.13, Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions