Repository navigation
fix(detectors): scope safe-token suppression to its own match - #2182
Conversation
A safe placeholder anywhere in the output reset the detector-wide key_found flag, masking unrelated credentials (verified: AWS sample key + MongoDB safe example scored [0.0], each alone scores correctly). Track qualifying match spans and drop only the span containing the safe token. Fixes NVIDIA#2172. Tests: mixed safe+real regression case; existing match/no-match and None-group cases unchanged (verified against real REGEX_DICTS). Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
|
Reproduced on Windows against the real detector, building the attempt exactly as At the merge-base a single At |
…#2182 A second reproduction showed the masking is broader than NVIDIA#2172's example: a bare "mypassword" with no URL around it also zeroes an unrelated AWS key, and two keys sitting beside the safe example are masked together. The added cases pin both, and keep the two undetected controls so that scoping the suppression cannot be satisfied by dropping it. Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
|
@MohammedAlkindi this was worth running, and the two extra shapes you found are now pinned on the branch —
The first three fail on the merge-base here with exactly the scores you measured ( Your point about the bare token being the broader case is the useful one: Build note in case it helps anyone else reproduce: the detector and this test file import fine from a plain checkout with For context on review load: CI on this PR has not run yet (fork runs need a maintainer to approve them, so the head currently shows only the DCO check), so a second platform's measurement is the only non-Linux evidence this has. Thanks for taking the time to check it properly rather than just commenting. |
|
Ran Worth saying since you raised it: the head still shows only the DCO check, so that run is the only platform result this PR has. Happy to re-run on any case you want pinned before a maintainer approves the workflow. |
|
@MohammedAlkindi the situation changed after my last note here, and it changes in your favour: a maintainer has approved the workflows, so CI has now run on So the Windows evidence is now doubled - the job matrix plus your run on Python 3.13.5 with the five-case matrix holding - which is more than this file had before the fix. Nothing further to ask of you, and thank you again for measuring rather than commenting. |
|
@MohammedAlkindi picking this back up now that CI runs on the branch. The state as I see it: Two open questions, both of which are yours or a maintainer's to answer rather
|
|
I am merging the preferred implementation in #2270 with the preferred more extensible tests here to merge both with credit to each contributor. |
Fixes #2172.
Root cause
ApiKey.detectkept one detector-widekey_foundflag: after any regex match set it, the safe-token loop searched the entire output and reset the flag. A safe placeholder (mypassword) anywhere therefore masked every other credential match — verified: AWS sample key + MongoDB safe example scores[0.0], while each alone scores[1.0]/[0.0]correctly.Fix
Track the spans of qualifying matches and drop only the span that contains the safe token. Unrelated matches (disjoint spans) still report. Diff scope: detector +1 test, small.
Test
test_apikey_safe_token_masks_only_its_own_match(mixed safe+real →[1.0]).test_apikey_match/test_apikey_no_match/test_apikey_nonegroupclasses re-verified unchanged against the realREGEX_DICTS(pure-logic run: aws/stripe/sqeuence still[1.0], refusals and safe-only still[0.0]). Full suite left to CI.Platform evidence
ca8cd1178: 17 check-runs, all success - build (ubuntu-latest, 3.11), build (ubuntu-latest, 3.12), build (ubuntu-latest, 3.13); build (ubuntu-24.04-arm, 3.11), build (ubuntu-24.04-arm, 3.12), build (ubuntu-24.04-arm, 3.13); build_macos (3.11), build_macos (3.12), build_macos (3.13); build_windows (3.11), build_windows (3.12), build_windows (3.13);docs;DCO.tests/detectors/test_detectors_apikey.pyon Windows with Python 3.13.5: 20 passed in 0.34s, with the five-case matrix holding, including the two-keys and bare-token shapes he found.