Skip to content

md_html: escape raw HTML in image alt attribute#376

Merged
mity merged 1 commit into
mity:masterfrom
iliaal:fix-image-alt-raw-html-escape
Jun 28, 2026
Merged

md_html: escape raw HTML in image alt attribute#376
mity merged 1 commit into
mity:masterfrom
iliaal:fix-image-alt-raw-html-escape

Conversation

@iliaal

@iliaal iliaal commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

The HTML renderer opens the alt="..." attribute in render_open_img_span() and renders the image label into it. enter_span_callback() already suppresses nested span tags while image_nesting_level > 0, but text_callback() still emitted MD_TEXT_HTML verbatim, so a raw HTML span in an image label broke out of the attribute:

![<x title=" y" onerror="alert(1)">](/url)

rendered as:

<p><img src="/url" alt="<x title=" y" onerror="alert(1)">"></p>

where onerror becomes a live attribute on the generated img.

This routes MD_TEXT_HTML through render_html_escaped() when inside an image label, matching the renderer's documented intent (only plain text falls into alt), the MD_FLAG_NOHTML output, and cmark-gfm, which escapes raw HTML in the same plain-text context. Behaviour outside image labels is unchanged. Adds a regression test.

The image label is rendered into the alt="..." attribute opened by
render_open_img_span(). enter_span_callback() already suppresses nested
span tags while inside an image, but text_callback() still emitted
MD_TEXT_HTML verbatim, so a raw HTML span in the label broke out of the
attribute. For example

    ![<x title=" y" onerror="alert(1)">](/url)

rendered as

    <img src="/url" alt="<x title=" y" onerror="alert(1)">">

where onerror becomes a live attribute on the generated img element.

Route MD_TEXT_HTML through render_html_escaped() when image_nesting_level
is greater than zero, matching the documented intent that only plain
text falls into alt (and cmark-gfm, which escapes raw HTML in the same
context). Behaviour outside image labels is unchanged.
@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.91%. Comparing base (755ce49) to head (a524e1a).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #376      +/-   ##
==========================================
+ Coverage   78.89%   78.91%   +0.02%     
==========================================
  Files           5        5              
  Lines        3790     3794       +4     
  Branches     1270     1271       +1     
==========================================
+ Hits         2990     2994       +4     
  Misses        297      297              
  Partials      503      503              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mity
mity merged commit 65c6c9d into mity:master Jun 28, 2026
8 checks passed
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

Successfully merging this pull request may close these issues.

2 participants