Repository navigation
fix(pii): scan for email addresses in linear time - #396
Merged
Merged
Conversation
Owner
|
@bIackr0se thx for the patch! |
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.
What this changes
On
main, theemailpattern inagentic_security/refusal_classifier/pii_detector.pyscans long runs of local-part characters such asa.a.a.ora-a-a-in time that grows with the square of the run length. With[detectors] pii = true,fuzzer.process_promptcallsPIIDetector.detected_typessynchronously on every target answer viarefusal_heuristic(response.json()).The pattern on
mainis\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b.re.searchtries 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:
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.cominstead of atj.PIIDetectoronly checks whether the pattern matches, so its verdicts are unchanged.How it was tested
test_email_scan_is_linear_on_long_address_like_runsnext totest_api_token_scan_is_linear_on_dotted_valuesintests/unit/refusal_classifier/test_pii_detector.py. The test scansa.repeated,a-repeated, anda@followed bya.repeated (200 KB per run), asserting no detection and under 1 s each. It fails onmain(the first input alone took 33 s) and passes on this branch.test_email_local_part_needs_a_word_characterpins the verdicts the lookahead keeps:--john@example.com,..jane@example.comand_john@example.comare detected,..@example.com,-@example.comand%+@example.comare not. It passes with the pattern frommainand with this branch, and fails if the lookahead is removed.refusal_heuristicon an OpenAI-shaped chat completion body with onlypiienabled, comparingmain(a908eb1) against this branch across 3 alternating runs per side on Python 3.15.2 (Apple M4). A 40 KB answer ofa.repeated took 1.37 to 1.38 s onmainand 0.004 s on this branch. A 160 KB answer ofa.repeated took 22.10 to 22.26 s onmainand 0.017 s on this branch. Every run returnedFalseon both sides (no email present).mainagainst 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 overa 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.pypassed (13 passed).pytestsuite passed (573 passed, 5 skipped).pre-commit run --all-filesran clean.Checklist
pytestpasses and new behaviour has a test. Tests don't call paid APIs.pre-commit run --all-filesis clean.