Skip to content

Commit e902991

Browse files
committed
fix(detectors): support 84-character Azure OpenAI API keys
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 #4389
1 parent 79acbf4 commit e902991

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

pkg/detectors/azure_openai/azure_openai.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
// TODO: Investigate custom `azure-api.net` endpoints.
3232
// https://github.com/openai/openai-python#microsoft-azure-openai
3333
azureUrlPat = regexp.MustCompile(`(?i)([a-z0-9-]+\.openai\.azure\.com)`)
34-
azureKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"api[_.-]?key", "openai[_.-]?key"}) + `\b(?-i:([a-f0-9]{32}))\b`)
34+
azureKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"api[_.-]?key", "openai[_.-]?key"}) + `\b(?-i:([a-zA-Z0-9]{32}|[a-zA-Z0-9]{84}))\b`)
3535

3636
invalidServices = simple.NewCache[struct{}]()
3737
)
@@ -76,7 +76,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7676
for token := range tokens {
7777
s1 := detectors.Result{
7878
DetectorType: s.Type(),
79-
Redacted: token[:3] + "..." + token[25:],
79+
Redacted: token[:3] + "..." + token[len(token)-4:],
8080
Raw: []byte(token),
8181
SecretParts: map[string]string{"key": token},
8282
}

pkg/detectors/azure_openai/azure_openai_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ func TestAzureOpenAI_Pattern(t *testing.T) {
7979
}`,
8080
want: []string{"57d2de35873840b5ad59d742e90e974e"},
8181
},
82+
{
83+
name: "84-character key - environment variable",
84+
input: `export OPENAI_API_BASE=https://myservice-east.openai.azure.com/
85+
export OPENAI_API_KEY=uQ9XsjB7aM2eVt5rL1pZcW6yGk4nF8oHd3RzXaYbT7vUjKmQeP5fNwL9oS2tH1rJ3pZxDkMvYeWq0bAs`,
86+
want: []string{"uQ9XsjB7aM2eVt5rL1pZcW6yGk4nF8oHd3RzXaYbT7vUjKmQeP5fNwL9oS2tH1rJ3pZxDkMvYeWq0bAs"},
87+
},
88+
{
89+
name: "84-character key - curl command",
90+
input: `curl -X POST "https://prod-openai.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2025-01-01-preview" \
91+
-H "Content-Type: application/json" \
92+
-H "api-key: Rk7mTz3nWx9pLq2vBs5yJd8cFg1hNa6oUi4eXwYrKbQjVm0tPl5sDf3gHn7kMz9aRcWx2bYu4eL"`,
93+
want: []string{"Rk7mTz3nWx9pLq2vBs5yJd8cFg1hNa6oUi4eXwYrKbQjVm0tPl5sDf3gHn7kMz9aRcWx2bYu4eL"},
94+
},
95+
{
96+
name: "84-character key - Python SDK",
97+
input: `from openai import AzureOpenAI
98+
client = AzureOpenAI(
99+
azure_endpoint="https://team-ai.openai.azure.com/",
100+
api_key="Ht5mNr9wXz3pLq7vBs2yJd6cFg8hKa1oUi4eTxYrQbMjVn0kPl5sDf3gRn7wMz9aXcWx2bYu4eLk0q",
101+
)`,
102+
want: []string{"Ht5mNr9wXz3pLq7vBs2yJd6cFg8hKa1oUi4eTxYrQbMjVn0kPl5sDf3gRn7wMz9aXcWx2bYu4eLk0q"},
103+
},
104+
{
105+
name: "invalid - 50 character key (wrong length)",
106+
input: `OPENAI_API_KEY=uQ9XsjB7aM2eVt5rL1pZcW6yGk4nF8oHd3RzXaYbT7vUjKm
107+
https://test.openai.azure.com/`,
108+
want: []string{},
109+
},
82110
}
83111

84112
for _, test := range tests {

0 commit comments

Comments
 (0)