From d8ca9d029b371c5dcb7baba8c41efcc3d6aed4b5 Mon Sep 17 00:00:00 2001 From: Paulo Almeida Date: Fri, 13 Nov 2026 14:55:37 +0000 Subject: [PATCH] fix(config): reject misplaced known YAML keys instead of silently dropping them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The key normalizer looked keys up in a flat, schema-wide canonical map. A key that exists anywhere in the schema but not at the location used (e.g. `dashboard:` at the root — only valid inside preset templates — or `proxyAccessLog` under `log:` — a top-level field) passed the flat lookup, failed fieldTypeByCanonical, and was silently dropped. v2.3.4 rejected these via yaml KnownFields(true); the normalizer regressed that guarantee: configs booted with the misplaced settings silently ignored (worse than failing: adminAllowLocalhost could appear enabled while actually doing nothing). processStructField now reports such keys as unknown-field issues at their line/column, with suggestions scoped to the struct expected at that location — same treatment as truly unknown keys. Reported with repro in #562. --- internal/config/keynormalizer.go | 10 ++++++ internal/config/keynormalizer_test.go | 51 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/internal/config/keynormalizer.go b/internal/config/keynormalizer.go index 11f3f157..6897cd7e 100644 --- a/internal/config/keynormalizer.go +++ b/internal/config/keynormalizer.go @@ -379,6 +379,16 @@ func processStructField(keyNode, valNode *yaml.Node, typ reflect.Type, lookup *k } fieldType, ok := lookup.fieldTypeByCanonical(typ, canonical) if !ok { + // Known key used at the wrong level (e.g. `dashboard:` at the root, + // which is only valid inside preset templates). v2.3.4 rejected this + // via yaml KnownFields(true); dropping it silently would apply none + // of its settings. Report it as an unknown field at this location. + *issues = append(*issues, keyIssue{ + Line: keyNode.Line, + Column: keyNode.Column, + Original: original, + Suggestions: suggest(original, lookup.validFields(typ)), + }) return } childPath := canonical diff --git a/internal/config/keynormalizer_test.go b/internal/config/keynormalizer_test.go index 2131409b..64895641 100644 --- a/internal/config/keynormalizer_test.go +++ b/internal/config/keynormalizer_test.go @@ -168,6 +168,57 @@ func TestNormalize_RejectsTrulyUnknown(t *testing.T) { assert.Contains(t, msg, "line") } +func TestNormalize_RejectsMisplacedKnownKeys(t *testing.T) { + t.Parallel() + + // Regression: a key that exists somewhere in the config schema but not at + // the location used was silently dropped instead of rejected. v2.3.4 + // rejected these via yaml KnownFields(true); the key normalizer must keep + // that guarantee (see processStructField). + cases := []struct { + name string + yaml string + want string + }{ + { + name: "top-level dashboard section (only valid inside preset templates)", + yaml: "dashboard:\n adminAllowLocalhost: true\n", + want: "dashboard", + }, + { + name: "proxyAccessLog nested under log (field is top-level)", + yaml: "log:\n level: info\n proxyAccessLog: true\n", + want: "proxyAccessLog", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := unmarshalNormalized([]byte(tc.yaml), &Data{}, zerolog.Nop()) + if err == nil { + t.Fatalf("misplaced known key should be rejected, got nil error") + } + if !strings.Contains(err.Error(), "unknown field") || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("error should report unknown field %q, got: %v", tc.want, err) + } + }) + } + + t.Run("correct top-level placement decodes all values", func(t *testing.T) { + validYaml := "adminAllowLocalhost: true\nproxyAccessLog: true\nlog:\n level: info\n json: true\n" + cfg := &Data{} + if err := unmarshalNormalized([]byte(validYaml), cfg, zerolog.Nop()); err != nil { + t.Fatalf("valid top-level settings should decode: %v", err) + } + if !cfg.AdminAllowLocalhost || !cfg.ProxyAccessLog { + t.Fatalf("top-level settings did not apply: adminAllowLocalhost=%v proxyAccessLog=%v", + cfg.AdminAllowLocalhost, cfg.ProxyAccessLog) + } + if cfg.Log.Level != "info" || !cfg.Log.JSON { + t.Fatalf("log settings did not apply: level=%q json=%v", cfg.Log.Level, cfg.Log.JSON) + } + }) +} + func TestNormalize_ReportedLineAndColumn(t *testing.T) { yamlIn := "tailscale:\n dataDir: /tmp\n bogus: 1\n" cfg := &Data{}