Repository navigation
FIX: Detect shortcut-reference images and HTML img tags in MarkdownInjectionScorer - #3471
Conversation
…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>
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
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>
|
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. |
|
@microsoft-github-policy-service agree |
Description
Fixes #3343.
MarkdownInjectionScorermissed two image forms that renderers fetch automatically:![logo]+[logo]: url) where the data is in the URL path. These were only flagged when the definition URL contained=.<img>tags, which no pattern covered.This PR adds two patterns:
Markdown Image (shortcut reference): flags![ref]only when a matching[ref]: urldefinition 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=...>andsrcset=. It deliberately skipsdata-src, which browsers only fetch when page script copies it intosrc.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
tests/unit/score/regex/test_markdown_injection.py:<img>positives: quote styles, uppercase, extra attributes,srcset.False: bare![logo], mismatched label, prose mentioning "img src",<image>,data-src.uv run pytest tests/unit), andpre-commitpasses on the changed files.