Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions llm/httpclient/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/url"

Expand Down Expand Up @@ -70,6 +71,25 @@ type AuthConfig struct {
HeaderKey string `json:"header_key,omitempty"`
}

// MarshalLog returns a masked representation for safe logging.
func (a AuthConfig) MarshalLog() string {
if a.APIKey == "" {
return ""
}
return maskAPIKey(a.APIKey)
}

// LogValue implements slog.LogValuer so that slog outputs mask API keys.
func (a AuthConfig) LogValue() slog.Value {
return slog.AnyValue(maskedAuthConfig{
Type: a.Type,
APIKey: a.MarshalLog(),
HeaderKey: a.HeaderKey,
})
}

type maskedAuthConfig struct{ Type, APIKey, HeaderKey string }

const (
AuthTypeBearer = "bearer"
AuthTypeAPIKey = "api_key"
Expand Down Expand Up @@ -124,3 +144,12 @@ func EncodeStreamEventToJSON(event *StreamEvent) ([]byte, error) {
Data: string(event.Data),
})
}

// maskAPIKey returns a masked version of the API key for display.
// Shows first 4 and last 4 characters with **** in between.
func maskAPIKey(key string) string {
if len(key) <= 8 {
return "****"
}
return key[:4] + "****" + key[len(key)-4:]
}