revert: restore simple O(n*m) proximity check
The input lists are bounded by the number of phrase patterns (5+5), so the sorted-scan optimization added complexity for no practical benefit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -112,33 +112,18 @@ JAILBREAK_PHRASES: tuple[re.Pattern[str], ...] = (
|
|||||||
PROXIMITY_CHARS = 500
|
PROXIMITY_CHARS = 500
|
||||||
|
|
||||||
|
|
||||||
def _nearby(
|
def _min_distance(
|
||||||
a_matches: list[re.Match[str]],
|
a_matches: list[re.Match[str]],
|
||||||
b_matches: list[re.Match[str]],
|
b_matches: list[re.Match[str]],
|
||||||
threshold: int,
|
|
||||||
) -> int | None:
|
) -> int | None:
|
||||||
"""Return the smallest char gap between any a–b pair, or None if
|
|
||||||
both lists are empty. O(n log n) via sort + linear scan."""
|
|
||||||
if not a_matches or not b_matches:
|
if not a_matches or not b_matches:
|
||||||
return None
|
return None
|
||||||
events = sorted(
|
best = None
|
||||||
[(m.start(), m.end(), "a") for m in a_matches]
|
for a in a_matches:
|
||||||
+ [(m.start(), m.end(), "b") for m in b_matches],
|
for b in b_matches:
|
||||||
)
|
gap = max(0, max(a.start(), b.start()) - min(a.end(), b.end()))
|
||||||
best: int | None = None
|
|
||||||
prev_end: int | None = None
|
|
||||||
prev_tag: str | None = None
|
|
||||||
for start, end, tag in events:
|
|
||||||
if prev_tag is not None and prev_tag != tag and prev_end is not None:
|
|
||||||
gap = max(0, start - prev_end)
|
|
||||||
if best is None or gap < best:
|
if best is None or gap < best:
|
||||||
best = gap
|
best = gap
|
||||||
if best == 0:
|
|
||||||
return 0
|
|
||||||
if best <= threshold:
|
|
||||||
return best
|
|
||||||
prev_end = end if prev_end is None else max(prev_end, end)
|
|
||||||
prev_tag = tag
|
|
||||||
return best
|
return best
|
||||||
|
|
||||||
|
|
||||||
@@ -147,7 +132,7 @@ def scan_naive_injection(text: str) -> ScanResult | None:
|
|||||||
jailbreak_hits = [m for p in JAILBREAK_PHRASES for m in p.finditer(text)]
|
jailbreak_hits = [m for p in JAILBREAK_PHRASES for m in p.finditer(text)]
|
||||||
|
|
||||||
if disclosure_hits and jailbreak_hits:
|
if disclosure_hits and jailbreak_hits:
|
||||||
dist = _nearby(disclosure_hits, jailbreak_hits, PROXIMITY_CHARS)
|
dist = _min_distance(disclosure_hits, jailbreak_hits)
|
||||||
if dist is not None and dist <= PROXIMITY_CHARS:
|
if dist is not None and dist <= PROXIMITY_CHARS:
|
||||||
return ScanResult(
|
return ScanResult(
|
||||||
severity="block",
|
severity="block",
|
||||||
|
|||||||
Reference in New Issue
Block a user