Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/config/keynormalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions internal/config/keynormalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Loading