Skip to content

FIX: Detect shortcut-reference images and HTML img tags in MarkdownInjectionScorer - #3471

Merged
Richard Lundeen (richlundeen) merged 2 commits into
microsoft:mainfrom
aatif101:fix/markdown-injection-image-forms
Oct 29, 2026
Merged

Richard Lundeen (richlundeen) merged 2 commits into
microsoft:mainfrom
aatif101:fix/markdown-injection-image-forms

Conversation

@aatif101

Copy link
Copy Markdown
Contributor

Description

Fixes #3343.

MarkdownInjectionScorer missed two image forms that renderers fetch automatically:

  1. Shortcut-reference images (![logo] + [logo]: url) where the data is in the URL path. These were only flagged when the definition URL contained =.
  2. Raw HTML <img> tags, which no pattern covered.

This PR adds two patterns:

  • Markdown Image (shortcut reference): flags ![ref] only when a matching [ref]: url definition exists, since without one the text renders literally. It handles the definition appearing before or after the image, and labels match case-insensitively, as in CommonMark.
  • HTML Image: flags <img ... src=...> and srcset=. It deliberately skips data-src, which browsers only fetch when page script copies it into src.

As in the issue: if you'd prefer this scorer to stay markdown-only, I'm happy to drop the HTML pattern. It's a single entry.

Known limitation, shared with the existing patterns: matches inside fenced code blocks are still flagged, and reference labels aren't whitespace-normalized.

Tests and Documentation

  • Added 15 cases to tests/unit/score/regex/test_markdown_injection.py:
    • 4 shortcut-reference positives: path exfil, with a title, case-insensitive label, definition first.
    • 6 HTML <img> positives: quote styles, uppercase, extra attributes, srcset.
    • 5 lookalikes that must stay False: bare ![logo], mismatched label, prose mentioning "img src", <image>, data-src.
  • Wrote the tests first and confirmed the positives failed before the fix.
  • Full unit suite passes locally (uv run pytest tests/unit), and pre-commit passes on the changed files.
  • Updated the class docstring. The scoring guide notebook describes this scorer generically ("images, links"), so I didn't change it and didn't run JupyText.

…jectionScorer

MarkdownInjectionScorer missed two image forms that renderers fetch
automatically:

- Shortcut-reference images (![ref] with a [ref]: url definition) whose
  URL carries the data in the path. These were only flagged when the
  definition URL contained "=".
- Raw HTML <img src=...> / <img srcset=...> tags.

Add a pattern for shortcut-reference images that have a matching
definition (either order, case-insensitive labels) and one for HTML img
tags. A bare ![ref] with no definition and <img data-src=...> are not
flagged.

Fixes microsoft#3343

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shortcut-reference fix makes sense, but the new HTML Image pattern can match text inside another attribute. For example, <img alt="example src=https://example.com/p.png"> has no src or srcset attribute and fetches no image, but this pattern reports it as an image injection. The lookbehind only excludes data-src; it does not distinguish attribute values from attribute names.

Please keep this narrow: either drop the HTML entry and keep the Markdown fix, as offered in the description, or make the HTML match skip quoted attribute values and add this negative case. I would not add a new HTML parsing layer for this.

Reviewed with GitHub Copilot.

Drop the HTML image pattern and its tests and documentation. Preserve shortcut-reference Markdown image detection.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen

Copy link
Copy Markdown
Contributor

Mohammad Aatif Shaikh (@aatif101) thx for doing this! It's ready to merge but needs your eula agreement first.

Let us know if you agree to the eula. If we don't hear a response we can close this in a few days and unassign it.

@aatif101

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Oct 29, 2026
Merged via the queue into microsoft:main with commit 3a99575 Oct 29, 2026
65 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.

BUG: MarkdownInjectionScorer misses shortcut-reference images with path-based exfiltration, and raw HTML <img>

2 participants