fix(detectors): support 84-character Azure OpenAI API keys#4976
fix(detectors): support 84-character Azure OpenAI API keys#4976mangod12 wants to merge 1 commit into
Conversation
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
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
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"}, |
There was a problem hiding this comment.
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)
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`) |
There was a problem hiding this comment.
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}.
Reviewed by Cursor Bugbot for commit e902991. Configure here.


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}))\brestricts 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 theRedactedfield to use relative indexing (len(token)-4) instead of hardcodedtoken[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 alphanumericazure_openai_test.go— added 4 test cases: 84-char env var, curl command, Python SDK, and invalid 50-char keyKey Formats
[a-f0-9]{32}— lowercase hex (e.g.,3397348fcdcb4a5fbeb6cceb5a6a284f)[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-previewwithApi-Keyheader.Test plan
make test-community(Go not available locally — CI will verify)Checklist
make test-community)?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.