Skip to content

FIX: Match LDAPInjectionOutputScorer's URL and filter-break default patterns in linear time - #4647

Open
Jaafer Rahmani (bIackr0se) wants to merge 1 commit into
microsoft:mainfrom
bIackr0se:fix/ldap-url-filter-linear
Open

Jaafer Rahmani (bIackr0se) wants to merge 1 commit into
microsoft:mainfrom
bIackr0se:fix/ldap-url-filter-linear

Conversation

@bIackr0se

@bIackr0se Jaafer Rahmani (bIackr0se) commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Four of LDAPInjectionOutputScorer's regexes take superlinear time on long replies that never match: on main (5aec2409f), "ldap://h???*)" followed by 25,000 ( returns False after 72.77 s through the default scorer. This PR rewrites only those default patterns so they run in linear time with the same verdict on every reply.

  • "LDAP URL Filter Injection" and _URL_FILTER_PATTERN started with \bldaps?://[^\s?#]*, so in ldap://ldap://ldap://... every ldap:// began a new re.search scan across the rest of the run for ? separators. Both patterns now start once per run of URL characters ((?<![^\s?#])), at the run's first ldap:// via an atomic group ((?>[^\s?#]*?\bldaps?://)). A later ldap:// in the same run reaches the same ? separators and filter, so it cannot match where the first one failed. The extracted filter groups are the same as on main.
  • _FILTER_BREAK_FRAGMENT (used by the "Filter Break Sequence" default pattern and inside the URL pattern) and the decoded-filter pattern had (?:\(\s*)+, an optional operator, then (?:\(\s*)*. Without an operator, both repeats accepted the same parentheses, so on *)((((... with no attribute the engine tried every way of splitting the run between them. Opening parentheses now match in one run, and a second run is allowed only after an operator ((?:op\s*(?:\(\s*)*)?). This accepts the same strings as main.
  • Inside the URL pattern, the null-byte branch (?:\)|%29)[^()?\s#]*<terminator> rescanned the rest of the filter from each %29. The run now stops at the next %29 ((?:(?!%29)[^()?\s#])*). Because that %29 is itself a closer and starts a run of its own, the verdict is the same.
  • Only pattern text changes, with no new function or dispatch entry. Two default pattern strings change ("Filter Break Sequence" and "LDAP URL Filter Injection"), so patterns_digest of a default LDAPInjectionOutputScorer changes once. Custom patterns= are unaffected. Python 3.11, PyRIT's minimum, supports the atomic group (?>...).
  • main and this PR agreed with 0 differences across 167,626 inputs on every default pattern's re.search result, the scorer's matched pattern names, the list of _URL_FILTER_PATTERN filter groups, and the decoded-filter check. The inputs are every string constant in this scorer's test file, every string of up to three tokens from 40 LDAP and URL tokens, 60,000 random token strings, 40,000 LDAP URLs with injected filters and 1,800 adversarial shapes. The scorer on main flagged 43,345 of these inputs as True, 18,250 of them with "LDAP URL Filter Injection".
  • This PR adds a 10-second timeout test with seven cases (four replies that never match and three that do). Those no-match cases fail on main by timeout. Reverting any of the four pattern edits alone makes at least one of them fail. Four new URL cases pin where a URL may start: ldap://ldap://..., see.ldap://..., a second URL after ?, and xldap://... (which must not match).

Default LDAPInjectionOutputScorer().score_text_async(reply) with in-memory SQLite, Python 3.12:

reply                                  chars   main              this PR
"ldap://" * 3571                       24997   False, 4.49 s     False, 0.022 s
"ldap://h???" + "%29" * 8333           25010   False, 5.03 s     False, 0.020 s
"*)" + "(" * 25000                     25002   False, 19.66 s    False, 0.013 s
"ldap://h???*)" + "(" * 25000          25013   False, 72.77 s    False, 0.024 s

At 1 MB the same four shapes take 0.263 s, 0.572 s, 0.337 s and 0.745 s on this PR (main was not run at that size).

Copilot AI balanced review requested due to automatic review settings November 25, 2026 22:23

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.

🟢 Approval recommended

No active review issues block approval of the LDAP regex optimization.

No open issues 🎉

What changed in this PR

This PR optimizes LDAPInjectionOutputScorer’s default patterns to avoid superlinear matching on long model replies while preserving detection behavior, URL-filter extraction, and custom-pattern handling.

Changes:

  • Reworks URL prefixes, opening-parenthesis runs, and encoded-closer matching to prevent repeated scans and excessive backtracking.
  • Adds timeout regressions for long inputs and URL-boundary cases covering valid and invalid LDAP URL starts.
File Description
tests/​unit/​score/​regex/​test_ldap_injection_output_scorer.py Adds seven performance cases and four URL-start regressions.
pyrit/​score/​true_false/​regex/​ldap_injection_output_scorer.py Rewrites affected default regexes and shares the optimized URL prefix with filter extraction.

Review effort: Balanced

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