From a96ecb2dc9ac3bf37ffb683854d2bda493f1679f Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 3 Nov 2026 00:29:13 +0000 Subject: [PATCH] test(admin): guard that every Config string key is redaction-classified A secret field added under a new JSON key would pass through the config endpoint unredacted because the redactor matches keys by name. The test fails until each string key is redacted or declared non-secret. --- admin_config_guard_test.go | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 admin_config_guard_test.go diff --git a/admin_config_guard_test.go b/admin_config_guard_test.go new file mode 100644 index 0000000..5957220 --- /dev/null +++ b/admin_config_guard_test.go @@ -0,0 +1,62 @@ +package traefikllmgateway + +import ( + "reflect" + "strings" + "testing" +) + +// nonSecretConfigKeys lists every string-valued JSON key in Config reviewed as +// non-secret. A new key must be classified here or in the redaction sets. +var nonSecretConfigKeys = map[string]bool{ + "agents": true, "allowedCIDRs": true, "backoff": true, "cacheTTL": true, + "card": true, "discoveryInterval": true, "file": true, "group": true, + "groups": true, "maxOpenDuration": true, "mcpServers": true, + "metadataPath": true, "modelAliases": true, "models": true, "name": true, + "openDuration": true, "passthroughPaths": true, "path": true, + "probeInterval": true, "providers": true, "requestTimeout": true, + "ttl": true, "type": true, +} + +func collectStringKeys(t reflect.Type, seen map[reflect.Type]bool, out map[string]bool) { + for t.Kind() == reflect.Pointer || t.Kind() == reflect.Slice || t.Kind() == reflect.Array || t.Kind() == reflect.Map { + t = t.Elem() + } + if t.Kind() != reflect.Struct || seen[t] { + return + } + seen[t] = true + for i := 0; i < t.NumField(); i++ { + f := t.Field(i) + tag := strings.Split(f.Tag.Get("json"), ",")[0] + if tag == "-" || !f.IsExported() { + continue + } + if tag == "" { + tag = f.Name + } + ft := f.Type + for ft.Kind() == reflect.Pointer || ft.Kind() == reflect.Slice || ft.Kind() == reflect.Array || ft.Kind() == reflect.Map { + ft = ft.Elem() + } + if ft.Kind() == reflect.String { + out[tag] = true + } + collectStringKeys(ft, seen, out) + } +} + +func TestRedaction_EveryStringKeyClassified(t *testing.T) { + keys := map[string]bool{} + collectStringKeys(reflect.TypeOf(Config{}), map[reflect.Type]bool{}, keys) + for k := range keys { + if !redactedConfigKeys[k] && !hiddenConfigKeys[k] && !baseURLLikeConfigKeys[k] && !nonSecretConfigKeys[k] { + t.Errorf("Config string key %q is unclassified: add it to the redaction sets or nonSecretConfigKeys", k) + } + } + for k := range nonSecretConfigKeys { + if redactedConfigKeys[k] || hiddenConfigKeys[k] || baseURLLikeConfigKeys[k] { + t.Errorf("key %q is both redacted and declared non-secret", k) + } + } +}