Strip the API key from fixed error text and keep typed matches
ProviderError and the new NewRateLimitedError constructor now keep the credential and remove it from their complete Error() text, including fixed words such as "HTTP" and "rate", so errors.As to them succeeds through the adapter's redaction stand-in instead of being refused. The openai adapter builds RateLimitedError with the key. Tests cover the keys "HTTP" (500), "rate" (429) and "openai" (401 and no model), checking Error(), Body(), every unwrap layer, errors.Is and errors.As. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -159,3 +159,28 @@ func TestProviderErrorTruncationMarkCannotCompleteSecret(t *testing.T) {
|
||||
t.Fatalf("secret leaked: %q", pe.Body())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderErrorWithFixedWordSecretNeverLeaksSecret(t *testing.T) {
|
||||
const secret = "HTTP"
|
||||
pe := NewProviderError(500, []byte(`{"error":"HTTP down"}`), secret)
|
||||
if strings.Contains(pe.Error(), secret) || strings.Contains(pe.Body(), secret) {
|
||||
t.Fatalf("secret leaked: Error %q, Body %q", pe.Error(), pe.Body())
|
||||
}
|
||||
if got := NewProviderError(500, nil, secret).Error(); got != "llm: provider error: [REDACTED] 500" {
|
||||
t.Fatalf("Error() without body = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimitedErrorWithFixedWordSecretNeverLeaksSecret(t *testing.T) {
|
||||
for _, secret := range []string{"rate", "retry after"} {
|
||||
for _, wait := range []time.Duration{0, 30 * time.Second} {
|
||||
rl := NewRateLimitedError(wait, secret)
|
||||
if strings.Contains(rl.Error(), secret) {
|
||||
t.Fatalf("secret %q leaked: %q", secret, rl.Error())
|
||||
}
|
||||
if rl.RetryAfter != wait || !errors.Is(rl, ErrRateLimited) {
|
||||
t.Fatalf("NewRateLimitedError(%v) = %+v", wait, rl)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user