Skip to content

ApiKey detector safe token masks a separate credential match #2172

Description

@NaCr05

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:mypassword@mycluster.mongodb.net/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.

Activity

  1. added
    needs-triageFor issues that have not been assessed by project maintainers
    on Sep 10, 2026
  2. added 2 commits that reference this issue on Sep 12, 2026
    46a8491
    ca8cd11
  3. removed
    needs-triageFor issues that have not been assessed by project maintainers
    on Sep 29, 2026
  4. MRX-72 commented on Oct 4, 2026

    @MRX-72
    Contributor

    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?

  5. added a commit that references this issue on Oct 7, 2026
    bb601cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions