garak icon indicating copy to clipboard operation
garak copied to clipboard

ApiKey detector safe token masks a separate credential match

Open NaCr05 opened this issue 2 months ago • 1 comments

ApiKey detector safe token masks a separate credential match

Steps to reproduce

Run the following against main at commit 3f50ea5ff9cd7050099940647c15c39b07a93392:

from garak.attempt import Attempt, Message
from garak.detectors.apikey import ApiKey

attempt = Attempt(prompt=Message(text="audit prompt"))
attempt.outputs = [
    "The AWS access key is AKIAIOSFODNN7EXAMPLE. "
    "A safe MongoDB example is "
    "mongodb+srv://myuser:[email protected]/mydatabase."
]

print(ApiKey().detect(attempt))

This prints:

[0.0]

The AWS key sample by itself produces [1.0], and the MongoDB example by itself produces [0.0] as expected.

Expected behavior

The safe placeholder should suppress only the credential-pattern match that contains mypassword. A separate API key match in the same output should still produce [1.0].

Current behavior

After any regex match sets key_found = True, the detector searches the entire output for each safe token and resets key_found = False. Consequently, a safe placeholder anywhere in the output masks every other credential match.

garak version

0.17.1.pre1, source commit 3f50ea5ff9cd7050099940647c15c39b07a93392.

Additional Information

  1. Operating system: Windows
  2. Python version: 3.12.5
  3. Install method: editable source checkout
  4. No target, model, API, GPU, or network access is required
  5. A focused regression test reproduces the issue deterministically

I found no open issue or pull request describing this mixed-output behavior. Open PR #1907 covers tests for the API key probe and does not modify this detector or its tests.

NaCr05 avatar Sep 10 '26 07:09 NaCr05

I've opened #2270 with a fix for this.

Root cause: the SAFE_TOKENS pass runs against the whole output and resets the single key_found flag, so one safe placeholder masks every other match. The fix scopes the safe-token check to each individual match (match.group(0)), so a safe example only neutralizes its own match. Added a regression test (real AWS key + the mypassword mongodb example now correctly scores [1.0]); the 15 existing detector tests still pass and black is clean.

It's currently a draft — happy to adjust the approach if you'd prefer the safe-token handling live elsewhere, or if this is out of scope. Could a maintainer confirm it's a good fit?

MRX-72 avatar Oct 04 '26 18:10 MRX-72