Never quote caller-supplied text in New errors
A managed-header error named the canonical header, which equals the API key when the key is itself a managed header name such as Authorization. The unknown structured-output error quoted the mode after redacting the whole key, which still leaked any fragment of the key. Both now return fixed messages, so no New error interpolates caller input. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -652,6 +652,38 @@ func TestNewConfigErrorsNeverLeakKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewManagedHeaderErrorNeverLeaksKey uses an API key that is itself a
|
||||
// managed header name, so naming the rejected header would print the key.
|
||||
func TestNewManagedHeaderErrorNeverLeaksKey(t *testing.T) {
|
||||
for _, key := range []string{"Authorization", "Content-Type", "Host"} {
|
||||
_, err := New(Config{
|
||||
BaseURL: "https://example.com/v1",
|
||||
APIKey: key,
|
||||
StructuredOutput: JSONSchema,
|
||||
Headers: map[string]string{key: "x"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Errorf("key %q: New() = nil error", key)
|
||||
continue
|
||||
}
|
||||
for _, secret := range []string{key, "Bearer " + key} {
|
||||
if strings.Contains(strings.ToLower(err.Error()), strings.ToLower(secret)) {
|
||||
t.Errorf("key %q: error leaks %q: %v", key, secret, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewModeErrorNeverLeaksKeyFragment uses a structured output mode that is
|
||||
// a fragment of the API key, which redacting the whole key would not catch.
|
||||
func TestNewModeErrorNeverLeaksKeyFragment(t *testing.T) {
|
||||
_, err := New(Config{BaseURL: "https://example.com/v1", APIKey: testKey, StructuredOutput: StructuredOutput(testKey[:20])})
|
||||
if err == nil {
|
||||
t.Fatal("New() = nil error")
|
||||
}
|
||||
assertNoSecret(t, err)
|
||||
}
|
||||
|
||||
func TestAcceptHeader(t *testing.T) {
|
||||
for name, tc := range map[string]struct {
|
||||
headers map[string]string
|
||||
|
||||
Reference in New Issue
Block a user