refactor(gateway): share sensitive-key markers with webhookguard
Resolves infra-misc-3. WIRE: webhookguard.SensitiveKeyMarkers is now the single exported marker list, and gateway.sensitiveParameterKey consumes it instead of inlining an identical copy. webhookguard/redact.go documented that the two lists must match "exactly so the two redaction paths agree"; that agreement is now structural (one slice, two consumers) rather than a comment nothing enforces. A new test asserts both consumers observe the same set, so a future edit cannot silently desynchronise chat-transcript redaction from webhook-capture redaction.
This commit is contained in:
3 files changed
+34
-12
No files matched your search
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/webhookguard"
|
||||
)
|
||||
|
||||
// toolSummaryMaxRunes bounds a rendered tool summary. Tool output is
|
||||
@@ -190,13 +192,7 @@ func redactToolParameters(value any, sensitive bool) any {
|
||||
|
||||
func sensitiveParameterKey(key string) bool {
|
||||
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(key))
|
||||
for _, marker := range []string{
|
||||
"token", "secret", "password", "passwd", "pwd", "passphrase",
|
||||
"api_key", "apikey", "api_secret", "client_secret", "private_key", "secret_key",
|
||||
"signing_key", "authorization", "bearer", "credential", "cookie", "session",
|
||||
"access_token", "refresh_token", "id_token", "bot_token", "webhook_secret",
|
||||
"x_api_key", "x_auth_token", "jwt", "oauth", "csrf", "otp", "signature",
|
||||
} {
|
||||
for _, marker := range webhookguard.SensitiveKeyMarkers {
|
||||
if strings.Contains(normalized, marker) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -1,10 +1,35 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/webhookguard"
|
||||
)
|
||||
|
||||
func TestRedactionConsumersShareMarkerSet(t *testing.T) {
|
||||
if len(webhookguard.SensitiveKeyMarkers) == 0 {
|
||||
t.Fatal("webhookguard.SensitiveKeyMarkers is empty, want a non-empty shared set")
|
||||
}
|
||||
for _, marker := range webhookguard.SensitiveKeyMarkers {
|
||||
if !sensitiveParameterKey(marker) {
|
||||
t.Errorf("sensitiveParameterKey(%q) = false, want true (shared marker set)", marker)
|
||||
}
|
||||
payload, err := json.Marshal(map[string]string{marker: "secret"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := webhookguard.RedactPayload(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("RedactPayload(%q): %v", marker, err)
|
||||
}
|
||||
if string(got) == string(payload) {
|
||||
t.Errorf("webhookguard did not redact shared marker key %q", marker)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummarizeToolParameters(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -10,14 +10,15 @@ import (
|
||||
// redactedValue replaces a value whose key (or shape) looks sensitive.
|
||||
const redactedValue = "[redacted]"
|
||||
|
||||
// sensitiveKeyMarkers is the key-name heuristic, matching
|
||||
// internal/gateway/stream.go's sensitiveParameterKey exactly so the two
|
||||
// redaction paths agree on what counts as sensitive. It is intentionally
|
||||
// SensitiveKeyMarkers is the key-name heuristic shared by both redaction
|
||||
// paths -- webhook-capture redaction here and chat-transcript
|
||||
// summarization in internal/gateway/stream.go's sensitiveParameterKey --
|
||||
// so they agree on what counts as sensitive. It is intentionally
|
||||
// conservative: a value whose key matches is redacted wholesale, so a marker
|
||||
// that is too broad (e.g. bare "key" or "auth", which would match "Key: 42"
|
||||
// or "author") would destroy the schema-by-example mapping use case the
|
||||
// captured payload exists to serve.
|
||||
var sensitiveKeyMarkers = []string{
|
||||
var SensitiveKeyMarkers = []string{
|
||||
"token", "secret", "password", "passwd", "pwd", "passphrase",
|
||||
"api_key", "apikey", "api_secret", "client_secret", "private_key", "secret_key",
|
||||
"signing_key", "authorization", "bearer", "credential", "cookie", "session",
|
||||
@@ -107,7 +108,7 @@ func sensitiveValueShape(s string) bool {
|
||||
|
||||
func sensitiveKey(key string) bool {
|
||||
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(key))
|
||||
for _, marker := range sensitiveKeyMarkers {
|
||||
for _, marker := range SensitiveKeyMarkers {
|
||||
if strings.Contains(normalized, marker) {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user