Files
opencode2api/internal/config/secrets.go
T
Jason Xu e2b2a7c281 refactor: harden gateway and document Go/Zen OAuth
Preserve Go and Zen OAuth routing while fixing authentication, secret redaction, streaming, transport, and configuration issues.

Refresh README and management guides in four languages while preserving sponsors, LinuxDo acknowledgements, and Linux/Docker quick start instructions.
2026-11-18 03:01:56 +08:00

148 lines
4.7 KiB
Go

package config
import (
"crypto/sha256"
"encoding/hex"
"net/url"
"sort"
"strings"
"sync/atomic"
"opencode2api/internal/oauth"
)
// MaskValue hides a secret for display: "••••" followed by its last five
// characters, or "••••" alone when the value has five characters or fewer.
func MaskValue(value string) string {
runes := []rune(value)
if len(runes) <= 5 {
return "••••"
}
return "••••" + string(runes[len(runes)-5:])
}
// RedactURL renders a proxy entry or URL for display with any userinfo
// replaced by "***". A pool entry gets a #pool fragment when it has none, and
// an unparsable value becomes "<invalid>".
func RedactURL(raw string) string {
clean, pool := ParseProxyEntry(raw)
if clean != "" {
raw = clean
}
u, err := url.Parse(raw)
if err != nil {
return "<invalid>"
}
if u.User != nil {
u.User = url.User("***")
}
if pool && u.Fragment == "" {
u.Fragment = "pool"
}
return u.String()
}
// Redactor scrubs configured secrets from text such as log lines and error
// messages. It is safe for concurrent use: Replace swaps the whole secret set
// atomically.
type Redactor struct {
replacer atomic.Value
}
// SecretRedactor returns a Redactor with no secrets loaded; Replace loads
// them.
func SecretRedactor() *Redactor {
r := &Redactor{}
r.replacer.Store(strings.NewReplacer())
return r
}
// Replace loads the secrets of cfg: the server, Zen and Go keys, the WebUI
// plaintext password, each upstream URL that carries userinfo (whole, plus its
// username and password), and each entry of RuntimeProxies other than
// "direct" (as written and as its clean URL, plus any username and password).
// Longer secrets are replaced first, so a secret that contains a shorter one
// is scrubbed whole.
func (r *Redactor) Replace(cfg Config) {
values := make([]string, 0, len(cfg.ServerKeys)+len(cfg.ZenKeys)+len(cfg.GoKeys)+2)
values = append(values, cfg.ServerKeys...)
values = append(values, cfg.ZenKeys...)
values = append(values, cfg.GoKeys...)
for _, connection := range []*oauth.Connection{cfg.GoOAuth, cfg.ZenOAuth} {
if connection == nil {
continue
}
values = append(values, connection.Credential.AccessToken, connection.Credential.RefreshToken)
if connection.Proxy != "" {
values = append(values, connection.Proxy)
}
}
if cfg.WebUI.Password != "" {
values = append(values, cfg.WebUI.Password)
}
for _, value := range []string{cfg.Upstream.Zen, cfg.Upstream.Go, cfg.Upstream.Anonymous} {
if parsed, err := url.Parse(value); err == nil && parsed.User != nil {
values = append(values, value, parsed.User.Username())
if password, ok := parsed.User.Password(); ok {
values = append(values, password)
}
}
}
for _, value := range cfg.RuntimeProxies() {
if value != "direct" {
clean, _ := ParseProxyEntry(value)
values = append(values, value, clean)
if parsed, err := url.Parse(clean); err == nil && parsed.User != nil {
values = append(values, parsed.User.Username())
if password, ok := parsed.User.Password(); ok {
values = append(values, password)
}
}
}
}
sort.Slice(values, func(i, j int) bool { return len(values[i]) > len(values[j]) })
replacements := make([]string, 0, 2*len(values))
seen := make(map[string]bool, len(values))
for _, value := range values {
if value == "" || seen[value] {
continue
}
seen[value] = true
replacements = append(replacements, value, "***")
}
r.replacer.Store(strings.NewReplacer(replacements...))
}
// String replaces every non-empty configured secret. One pass prevents a
// short secret from rewriting masking text produced for another secret.
func (r *Redactor) String(value string) string {
replacer, _ := r.replacer.Load().(*strings.Replacer)
if replacer == nil {
return value
}
return replacer.Replace(value)
}
// Fingerprint returns the first 16 hex digits of the SHA-256 of value. It is
// the stable ID of a secret in config, session and usage bookkeeping
// (ServerKeyMetadata is keyed by it), and it doubles as the id of the masked
// secrets, API keys and Playground key choices that the WebUI shows and sends
// back.
func Fingerprint(value string) string {
sum := sha256.Sum256([]byte(value))
return hex.EncodeToString(sum[:])[:16]
}
// KeyDisplayID returns the last five characters of value, or its fingerprint
// when it has five characters or fewer so a short key is never disclosed in
// full. It is intentionally separate from
// Fingerprint: it is meant to be read by a person, and it is the key_id of an
// upstream key in logs, request telemetry and the key status list.
func KeyDisplayID(value string) string {
runes := []rune(value)
if len(runes) <= 5 {
return Fingerprint(value)
}
return string(runes[len(runes)-5:])
}