Files
traefik-llmgateway/cmd/gateway/reload_target_secret_test.go
T
lukaszraczyloandGitHub f1b198bce4 fix(security): audit run-4 fixes and run-3 leftovers (#26)
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
2026-11-29 10:50:36 +00:00

70 lines
1.6 KiB
Go

package main
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
const reloadTargetMarker = "DUMMY-RELOAD-TARGET-0006"
func captureStderr(t *testing.T, fn func()) string {
t.Helper()
orig := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stderr = w
defer func() { os.Stderr = orig }()
fn()
_ = w.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
t.Fatal(err)
}
return buf.String()
}
func TestReloadRejectionLog_OmitsTargetCredential(t *testing.T) {
const tmpl = `providers:
openai:
type: openai
apiKey: test-key
models:
- model-a
mcpServers:
alpha:
url: "%s"
`
for name, raw := range map[string]string{
"mistyped scheme": "htps://mcp.invalid/mcp?api-key=" + reloadTargetMarker,
"unparseable": "https://u:" + reloadTargetMarker + "@mcp.invalid:99x/mcp",
} {
t.Run(name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(path, []byte(strings.Replace(tmpl, "%s", raw, 1)), 0o600); err != nil {
t.Fatal(err)
}
log := newLogger("error")
build := gatewayBuilder(context.Background(), path, "test", log)
sw := &swapHandler{}
sw.cur.Store(newGeneration(okHandler(), func() {}))
r := newReloader(path, []byte("previous"), build, sw, time.Hour, log)
t.Cleanup(sw.closeCurrent)
out := captureStderr(t, func() { r.check(false) })
if !strings.Contains(out, "rejected") {
t.Fatalf("precondition: no rejection logged: %q", out)
}
if strings.Contains(out, reloadTargetMarker) {
t.Errorf("rejection log carries the target credential: %q", out)
}
})
}
}