Skip to content

fix(pii): scan for email addresses in linear time - #396

Merged
msoedov merged 1 commit into
msoedov:mainfrom
bIackr0se:fix/pii-email-linear
Nov 26, 2026
Merged

msoedov merged 1 commit into
msoedov:mainfrom
bIackr0se:fix/pii-email-linear

Conversation

@bIackr0se

Copy link
Copy Markdown
Contributor

What this changes

On main, the email pattern in agentic_security/refusal_classifier/pii_detector.py scans long runs of local-part characters such as a.a.a. or a-a-a- in time that grows with the square of the run length. With [detectors] pii = true, fuzzer.process_prompt calls PIIDetector.detected_types synchronously on every target answer via refusal_heuristic(response.json()).

160 KB target answer ("a." repeated), pii detector only
main a908eb1   refusal_heuristic=False   22.1 s
this branch    refusal_heuristic=False   0.017 s

The pattern on main is \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b. re.search tries it from every word boundary. Inside a run of local-part characters, each try reads to the end of the run before failing.

This PR updates that pattern to:

(?<![A-Za-z0-9._%+-])(?=[A-Za-z0-9._%+-]*?\b[A-Za-z0-9._%+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b

The lookbehind starts the local part only at the first character of its run, so each run is tried once. The lookahead keeps main's requirement that the local part contain a word boundary, so punctuation standing alone before the @, as in ..@example.com, stays unmatched. The domain and TLD part is unchanged.

One difference is visible to code that calls the regex directly: a match can now start at the beginning of the run, at the first dot in ..jane@example.com instead of at j. PIIDetector only checks whether the pattern matches, so its verdicts are unchanged.

How it was tested

  • This branch adds test_email_scan_is_linear_on_long_address_like_runs next to test_api_token_scan_is_linear_on_dotted_values in tests/unit/refusal_classifier/test_pii_detector.py. The test scans a. repeated, a- repeated, and a@ followed by a. repeated (200 KB per run), asserting no detection and under 1 s each. It fails on main (the first input alone took 33 s) and passes on this branch.
  • test_email_local_part_needs_a_word_character pins the verdicts the lookahead keeps: --john@example.com, ..jane@example.com and _john@example.com are detected, ..@example.com, -@example.com and %+@example.com are not. It passes with the pattern from main and with this branch, and fails if the lookahead is removed.
  • Scanner-path runs called refusal_heuristic on an OpenAI-shaped chat completion body with only pii enabled, comparing main (a908eb1) against this branch across 3 alternating runs per side on Python 3.15.2 (Apple M4). A 40 KB answer of a. repeated took 1.37 to 1.38 s on main and 0.004 s on this branch. A 160 KB answer of a. repeated took 22.10 to 22.26 s on main and 0.017 s on this branch. Every run returned False on both sides (no email present).
  • A differential comparison of main against this branch produced the same match or no-match result on all 2,696,780 tested inputs (8,071 matched on both sides). The inputs cover every string up to 7 characters over a 1 _ . - @ space é, 300,000 random strings of 1 to 120 characters, and 35 hand-picked addresses and near misses. Of the matching inputs, 2,175 start at a different character on this branch.
  • tests/unit/refusal_classifier/test_pii_detector.py passed (13 passed).
  • The default pytest suite passed (573 passed, 5 skipped).
  • pre-commit run --all-files ran clean.

Checklist

  • No real API key, token or customer endpoint anywhere in the code, tests, docs or this description (CONTRIBUTING.md).
  • pytest passes and new behaviour has a test. Tests don't call paid APIs.
  • pre-commit run --all-files is clean.
  • Docs updated if behaviour changed.

@msoedov

msoedov commented Nov 26, 2026

Copy link
Copy Markdown
Owner

@bIackr0se thx for the patch!

@msoedov
msoedov merged commit 50efbf6 into msoedov:main Nov 26, 2026
2 checks passed
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