Repository navigation
FIX: Match LDAPInjectionOutputScorer's URL and filter-break default patterns in linear time - #4647
Open
Jaafer Rahmani (bIackr0se) wants to merge 1 commit into
Open
Jaafer Rahmani (bIackr0se) wants to merge 1 commit into
Jaafer Rahmani (bIackr0se) wants to merge 1 commit into
Conversation
…atterns in linear time
Contributor
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Four of
LDAPInjectionOutputScorer's regexes take superlinear time on long replies that never match: onmain(5aec2409f),"ldap://h???*)"followed by 25,000(returnsFalseafter 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._URL_FILTER_PATTERNstarted with\bldaps?://[^\s?#]*, so inldap://ldap://ldap://...everyldap://began a newre.searchscan across the rest of the run for?separators. Both patterns now start once per run of URL characters ((?<![^\s?#])), at the run's firstldap://via an atomic group ((?>[^\s?#]*?\bldaps?://)). A laterldap://in the same run reaches the same?separators and filter, so it cannot match where the first one failed. The extractedfiltergroups are the same as onmain._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 asmain.(?:\)|%29)[^()?\s#]*<terminator>rescanned the rest of the filter from each%29. The run now stops at the next%29((?:(?!%29)[^()?\s#])*). Because that%29is itself a closer and starts a run of its own, the verdict is the same.patterns_digestof a defaultLDAPInjectionOutputScorerchanges once. Custompatterns=are unaffected. Python 3.11, PyRIT's minimum, supports the atomic group(?>...).mainand this PR agreed with 0 differences across 167,626 inputs on every default pattern'sre.searchresult, the scorer's matched pattern names, the list of_URL_FILTER_PATTERNfilter 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 onmainflagged 43,345 of these inputs asTrue, 18,250 of them with "LDAP URL Filter Injection".mainby 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?, andxldap://...(which must not match).Default
LDAPInjectionOutputScorer().score_text_async(reply)with in-memory SQLite, Python 3.12: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).