"""Tests for the Markdown → sanitized HTML pipeline. We care about three things: 1. Safe inline markup (``**bold**``, ``*italic*``, links, lists) round-trips into the expected HTML tags. 2. Dangerous constructs (``world" html = md.render(src) # bleach strip=True drops the tag; the (potentially dangerous) # content can remain as text but cannot execute. assert "" not in html def test_iframe_and_style_tags_are_stripped(md: MarkdownService) -> None: """Disallowed block-level tags are removed from the output.""" html = md.render( "\n\n\n\nsafe" ) assert " None: """Raw ```` links lose the dangerous href. We construct the link as raw HTML (rather than ``[text](url)`` Markdown syntax, which commonmark silently refuses to turn into an anchor for the unknown ``javascript:`` protocol) so the bleach allowlist actually has an anchor to filter. The assertion is that the ``javascript:`` URL does not make it into the sanitized output. """ html = md.render('click') assert "javascript:" not in html def test_allowed_link_and_image_attributes_survive(md: MarkdownService) -> None: """Safe link/image attributes are preserved.""" html = md.render( '[hello](https://example.com "Example")\n\n' '![alt text](https://example.com/a.png "Caption")' ) assert 'href="https://example.com"' in html assert 'title="Example"' in html assert 'alt="alt text"' in html assert 'src="https://example.com/a.png"' in html def test_inline_event_handler_attribute_is_stripped(md: MarkdownService) -> None: """``onclick`` and similar inline handlers never survive sanitization.""" html = md.render('x') assert "onclick" not in html def test_table_tags_are_stripped(md: MarkdownService) -> None: """Tables are not in the bleach allowlist, so their tags are stripped. Documents the intentional policy: the Markdown parser is the commonmark preset with NO table plugin, and the bleach allowlist has no table tags — widening either without the other would be a policy mismatch. If a future phase wants tables, this test should flip to assert the opposite along with the matching allowlist change. """ src = "| a | b |\n|---|---|\n| 1 | 2 |\n" html = md.render(src) assert " None: """``render_markdown_safe`` produces the same output as the class.""" src = "Hello **there**." assert render_markdown_safe(src) == md.render(src)