Skip to content

fix(detectors): support 84-character Azure OpenAI API keys#4976

Open
mangod12 wants to merge 3 commits into
trufflesecurity:mainfrom
mangod12:fix/azure-openai-84char-keys
Open

fix(detectors): support 84-character Azure OpenAI API keys#4976
mangod12 wants to merge 3 commits into
trufflesecurity:mainfrom
mangod12:fix/azure-openai-84char-keys

Conversation

@mangod12
Copy link
Copy Markdown

@mangod12 mangod12 commented May 21, 2026

Description

Azure OpenAI now issues API keys that are 84 alphanumeric characters in addition to the original 32-character lowercase hex format. The existing detector only matched [a-f0-9]{32}, silently missing the newer keys.

Root cause: The regex \b(?-i:([a-f0-9]{32}))\b restricts to lowercase hex and exactly 32 characters.

Fix: Expand to \b(?-i:([a-zA-Z0-9]{32}|[a-zA-Z0-9]{84}))\b — matches both formats. Also fixed the Redacted field to use relative indexing (len(token)-4) instead of hardcoded token[25:], which works for both key lengths.

Changes

  • azure_openai.go — expanded key regex to match 32-char and 84-char keys with mixed-case alphanumeric
  • azure_openai_test.go — added 4 test cases: 84-char env var, curl command, Python SDK, and invalid 50-char key

Key Formats

  • 32-char (original): [a-f0-9]{32} — lowercase hex (e.g., 3397348fcdcb4a5fbeb6cceb5a6a284f)
  • 84-char (new): [a-zA-Z0-9]{84} — mixed-case alphanumeric (e.g., uQ9XsjB7aM2eVt5rL1pZcW6yGk4nF8oHd3Rz...)

Verification

Same endpoint works for both key formats — GET /openai/deployments?api-version=2023-03-15-preview with Api-Key header.

Test plan

  • 4 new pattern tests (3 valid 84-char, 1 invalid length)
  • All 6 existing 32-char tests unchanged
  • make test-community (Go not available locally — CI will verify)

Checklist

  • Tests passing (make test-community)?
  • Lint passing (make lint)?

Fixes #4389


Note

Low Risk
Low risk: regex broadening and redaction logic updates are localized to the Azure OpenAI detector, with added tests to reduce false negatives/regressions.

Overview
Updates the Azure OpenAI detector to also match new 84-character alphanumeric API keys (in addition to the existing 32-char hex keys).

Fixes result redaction to use len(token)-4 instead of hardcoded slicing so both key lengths redact correctly, and expands pattern tests to cover multiple 84-char key appearances plus an invalid-length negative case.

Reviewed by Cursor Bugbot for commit 944342b. Bugbot is set up for automated code reviews on this repo. Configure here.

Azure OpenAI now issues API keys that are 84 alphanumeric characters
in addition to the original 32-character lowercase hex format. The
existing regex only matched `[a-f0-9]{32}`, missing the newer keys.

Changes:
- Expand key pattern to match both 32-char and 84-char keys
- Accept mixed-case alphanumeric characters (not just lowercase hex)
- Fix Redacted field to use relative indexing for both key lengths
- Add test cases for 84-char keys (env var, curl, Python SDK, invalid)

Fixes trufflesecurity#4389
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit e902991. Configure here.

Comment thread pkg/detectors/azure_openai/azure_openai_test.go Outdated
Comment thread pkg/detectors/azure_openai/azure_openai.go Outdated
mangod12 and others added 2 commits May 22, 2026 18:13
…r regex

- All three 84-character test keys were wrong length (80, 75, 78).
  Replaced with exactly 84-character alphanumeric strings.
- Restored 32-char key pattern to [a-f0-9]{32} (lowercase hex only)
  to avoid false positives. Only the new 84-char format uses the
  broader [a-zA-Z0-9] character class.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace random 84-char test keys with repeating-pattern keys.
Avoids GitGuardian false positives while still validating the
84-character key regex.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

No detector for 84-character Azure OpenAI secret keys

2 participants