Skip to content

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

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

fix(detectors): support 84-character Azure OpenAI API keys#4976
mangod12 wants to merge 1 commit 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

Medium Risk
Moderate risk: expands secret-matching regex (potential false positives/negatives) and changes redaction logic, but scope is limited to the Azure OpenAI detector with added tests.

Overview
Azure OpenAI secret detection now matches both 32- and 84-character API keys. The detector regex was broadened from 32-char hex to 32/84-char alphanumeric keys, and result redaction was updated to use len(token) so it doesn’t assume a fixed length.

Tests were extended with additional fixtures for 84-character keys (env var, curl, SDK) plus a negative case for an invalid-length key.

Reviewed by Cursor Bugbot for commit e902991. 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.

name: "84-character key - environment variable",
input: `export OPENAI_API_BASE=https://myservice-east.openai.azure.com/
export OPENAI_API_KEY=uQ9XsjB7aM2eVt5rL1pZcW6yGk4nF8oHd3RzXaYbT7vUjKmQeP5fNwL9oS2tH1rJ3pZxDkMvYeWq0bAs`,
want: []string{"uQ9XsjB7aM2eVt5rL1pZcW6yGk4nF8oHd3RzXaYbT7vUjKmQeP5fNwL9oS2tH1rJ3pZxDkMvYeWq0bAs"},
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Test keys are not 84 characters, tests will fail

High Severity

The three "84-character" test keys are actually 80, 75, and 78 characters respectively — none is 84 characters. The regex [a-zA-Z0-9]{84} requires exactly 84 alphanumeric characters at a \b word boundary, so none of these strings will match. Since word boundaries don't occur between alphanumeric characters, a partial 32-char match is also impossible. These tests will fail in CI. The PR author noted Go wasn't available locally and tests were not run.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e902991. Configure here.

// https://github.com/openai/openai-python#microsoft-azure-openai
azureUrlPat = regexp.MustCompile(`(?i)([a-z0-9-]+\.openai\.azure\.com)`)
azureKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"api[_.-]?key", "openai[_.-]?key"}) + `\b(?-i:([a-f0-9]{32}))\b`)
azureKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"api[_.-]?key", "openai[_.-]?key"}) + `\b(?-i:([a-zA-Z0-9]{32}|[a-zA-Z0-9]{84}))\b`)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

32-char regex broadened from hex to all alphanumeric

Medium Severity

The 32-character key pattern was changed from [a-f0-9]{32} (lowercase hex) to [a-zA-Z0-9]{32} (any alphanumeric). The PR description explicitly states the 32-char format is "lowercase hex," yet the regex now matches any 32-character alphanumeric string, significantly increasing false positives. The 32-char alternative in the alternation needs to remain [a-f0-9]{32}.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e902991. Configure here.

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