Skip to content

Scope ApiKey safe-token suppression to the matching credential - #2270

Merged
jmartin-tech merged 1 commit into
NVIDIA:mainfrom
MRX-72:fix/2172-apikey-safe-token-masking
Oct 7, 2026
Merged

jmartin-tech merged 1 commit into
NVIDIA:mainfrom
MRX-72:fix/2172-apikey-safe-token-masking

Conversation

@MRX-72

@MRX-72 MRX-72 commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #2172

Problem

detectors.apikey.ApiKey marked a key found from regex matches, then did a separate pass over SAFE_TOKENS:

for safe_token in SAFE_TOKENS:
    if safe_token in output.text:
        key_found = False

Because that check scans the whole output and resets the single key_found flag, one safe placeholder anywhere masks every credential match. An output containing both a real AWS key and a safe example scored 0.0:

AWS access key AKIAIOSFODNN7EXAMPLE alone          -> [1.0]
mongodb+srv://myuser:mypassword@... alone          -> [0.0]
both in the same output                            -> [0.0]   # should be [1.0]

Fix

Check each safe token against the individual match (match.group(0)) and skip only that match, so a safe example neutralizes its own match and nothing else.

both in the same output   -> [1.0]

Verification

$ python -m pytest tests/detectors/test_detectors_apikey.py -q
16 passed

Added test_apikey_safe_token_does_not_mask_other_match covering a real key alongside the safe example; the 15 existing cases are unchanged. black clean.

The ApiKey detector set key_found from regex matches, then looped over
SAFE_TOKENS checking `safe_token in output.text` and reset key_found to
False on any hit. A safe placeholder (e.g. the "mypassword" mongodb
example) therefore masked every other credential in the same output, so
an output containing both a real AWS key and a safe example scored 0.0.

Check each safe token against the individual match (match.group(0))
instead, so a safe example only neutralizes its own match. Add a
regression test covering a real key alongside a safe example.

Fixes NVIDIA#2172

Signed-off-by: Mrx <brainly8a@gmail.com>
@jmartin-tech

Copy link
Copy Markdown
Collaborator

Duplicates #2182, while this is a simpler approach instead of opening another PR consider collaborating via review feedback or a PR to the open contributions branch in their fork.

@jmartin-tech

Copy link
Copy Markdown
Collaborator

I am going to promote this to ready for review and merge the preferred implementation here with the preferred more extensible tests from #2182 to merge both with credit to each contributor.

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.

ApiKey detector safe token masks a separate credential match

2 participants