From 9395fadbf752ea6f56adb2b2088250b09cb89a83 Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Tue, 24 Nov 2026 09:47:44 +0100 Subject: [PATCH] fix(configcmd): redact secret fields inside list values "config show" prints a list as one value, and redaction only looked at the leaf's own key, so the password of every entry of a list (yamocir's bootstrap.admins) was printed in clear. The same rule now applies inside lists and maps of a leaf: a field is secret by its default name or by a Spec.SecretKeys key without the list index ("admins.password"). References (env:, file:, secret://) stay visible as before. Co-Authored-By: Claude Opus 5.5 --- README.md | 3 ++- configcmd/configcmd.go | 2 ++ configcmd/layered_test.go | 50 +++++++++++++++++++++++++++++++++++++++ configcmd/show.go | 38 ++++++++++++++++++++++++++--- 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 12735e0..ae8cd27 100644 --- a/README.md +++ b/README.md @@ -377,7 +377,8 @@ the file every value comes from (`Effective.Show`; secrets -- keys `secrets`, `secret`, `token`, `password`, plus the program's own `Spec.SecretKeys` such as `database.url` (a key covers everything below it) -- are redacted unless `--show-secrets`, references like `env:X` -stay visible), and +stay visible; inside a list the same rule applies to every entry's +fields, a Spec key naming them without the index: `admins.password`), and `configcmd.Doctor(report, spec, explicit)` is the config part of a `doctor` command (paths, layers, "config file found", unknown options). diff --git a/configcmd/configcmd.go b/configcmd/configcmd.go index d4fcb3c..eb06fd1 100644 --- a/configcmd/configcmd.go +++ b/configcmd/configcmd.go @@ -120,6 +120,8 @@ type Spec struct { // ("database.url", "oidc.client_secret"): "config show" redacts them // like the keys it always treats as secret (a last segment secrets, // secret, token or password). A key also covers everything below it. + // Fields of list entries are named without the index + // ("admins.password" for every entry of the list admins). SecretKeys []string // Options are passed through to core/config; tests use them to diff --git a/configcmd/layered_test.go b/configcmd/layered_test.go index 86d5a51..cd891bf 100644 --- a/configcmd/layered_test.go +++ b/configcmd/layered_test.go @@ -248,3 +248,53 @@ func statExistsForTest(path string) bool { _, err := os.Stat(path) return err == nil } + +// TestValuesRedactionInLists: a list leaf is printed as one value, so +// the secret fields of its entries are redacted inside it -- by default +// name and by a Spec key without the index -- and the rest stays. +func TestValuesRedactionInLists(t *testing.T) { + merged, origins := coreconfig.Merge(coreconfig.Source{Name: "f", Data: map[string]any{ + "admins": []any{ + map[string]any{"username": "admin", "password": "changeme"}, + map[string]any{"username": "ops", "password": "env:OPS_PW", "tags": []any{"a"}}, + }, + "ids": []any{map[string]any{"name": "gc", "nested": map[string]any{"token": "t0k"}, "url": "pg://u:pw@h"}}, + "plain": []any{"x", "y"}, + }}) + e := &Effective{Name: "p", Merged: merged, Origins: origins, SecretKeys: []string{"ids.url"}} + got := map[string]any{} + for _, v := range e.Values(nil, false) { + got[v.Key] = v.Value + } + want := map[string]any{ + "admins": []any{ + map[string]any{"username": "admin", "password": ""}, + map[string]any{"username": "ops", "password": "env:OPS_PW", "tags": []any{"a"}}, + }, + "ids": []any{map[string]any{"name": "gc", "nested": map[string]any{"token": ""}, "url": ""}}, + "plain": []any{"x", "y"}, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("Values = %v\nwant %v", got, want) + } + if merged["admins"].([]any)[0].(map[string]any)["password"] != "changeme" { + t.Error("the merged configuration was changed") + } + for _, v := range e.Values(nil, true) { + if v.Key == "admins" && !strings.Contains(renderValue(v.Value), "changeme") { + t.Errorf("show-secrets: %v", v.Value) + } + } + + // Through "config show" from a file, YAML and JSON. + root := t.TempDir() + s := testSpec(root) + writeFile(t, filepath.Join(root, "etc", "yamase", "yamase-test.yaml"), + minimalTemplate+"users:\n - name: a\n password: hunter2\n") + for _, args := range [][]string{{"show"}, {"show", "--json"}} { + code, out, errOut := run(t, s, "", args...) + if code != ExitOK || strings.Contains(out, "hunter2") || !strings.Contains(out, "") || !strings.Contains(out, `"name"`) { + t.Errorf("%v: exit %d\n%s%s", args, code, out, errOut) + } + } +} diff --git a/configcmd/show.go b/configcmd/show.go index 6a15cfa..9f34bd8 100644 --- a/configcmd/show.go +++ b/configcmd/show.go @@ -30,7 +30,10 @@ const DefaultOrigin = "default" // default it does not read is left out. Secret values (keys secrets, // secret, token, password, and e.SecretKeys) are replaced by "" unless // showSecrets; a reference that only says where a secret is (env:, -// file:, secret://) is shown. +// file:, secret://) is shown. A leaf that is a list is printed as one +// value, so the same rule is applied inside it: a field of a list entry +// is secret by its name or by its key without the index ("admins.password" +// for the field password of every entry of the list admins). func (e *Effective) Values(used func(string) bool, showSecrets bool) []Value { var out []Value walkLeaves("", e.Merged, func(key string, v any) { @@ -42,8 +45,12 @@ func (e *Effective) Values(used func(string) bool, showSecrets bool) []Value { if !u && source == DefaultOrigin { return } - if !showSecrets && e.isSecret(key) { - v = redact(v) + if !showSecrets { + if e.isSecret(key) { + v = redact(v) + } else { + v = e.redactWithin(key, v) + } } out = append(out, Value{Key: key, Value: v, Source: source, Used: u}) }) @@ -131,6 +138,31 @@ func (e *Effective) isSecret(key string) bool { return false } +// redactWithin redacts the secret fields of the maps inside a list (or +// a map) leaf at key, by the same rule as isSecret. A list entry adds no +// segment to the key. v is copied, never changed. +func (e *Effective) redactWithin(key string, v any) any { + switch x := v.(type) { + case []any: + out := make([]any, len(x)) + for i, el := range x { + out[i] = e.redactWithin(key, el) + } + return out + case map[string]any: + out := make(map[string]any, len(x)) + for k, el := range x { + if sub := key + "." + k; e.isSecret(sub) { + out[k] = redact(el) + } else { + out[k] = e.redactWithin(sub, el) + } + } + return out + } + return v +} + // redact replaces secret values, keeping references that only say where // the secret is. func redact(v any) any {