From e35af45066d65edb2e00f10e21a97885e297d575 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 10 Nov 2026 08:04:54 +0000 Subject: [PATCH] config: reject empty list items A resolver became the local one, an allowed user one without an email. Updates #1926 --- hscontrol/config/default_test.go | 18 ++++++++++++++++++ hscontrol/config/load_test.go | 3 +++ hscontrol/config/schema.go | 2 +- hscontrol/config/validate.go | 14 ++++++++++++-- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/hscontrol/config/default_test.go b/hscontrol/config/default_test.go index 6cc6477c5..fcb3a88a9 100644 --- a/hscontrol/config/default_test.go +++ b/hscontrol/config/default_test.go @@ -232,6 +232,24 @@ func TestValidateGoData(t *testing.T) { }, wantErr: "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", }, + { + name: "empty resolver", + set: func(c *types.Config) { c.DNSConfig.Challenge.Resolvers = []string{""} }, + wantErr: "dns.challenge.resolvers[0] is empty", + }, + { + name: "empty derp url", + set: func(c *types.Config) { + c.DERP.URLs = []types.HTTPURL{"https://derp.example.com/map.json", ""} + }, + wantErr: "derp.urls[1] is empty", + }, + { + // Would admit a user whose provider sends no email. + name: "empty allowed user", + set: func(c *types.Config) { c.OIDC.AllowedUsers = []string{""} }, + wantErr: "oidc.allowed_users[0] is empty", + }, { name: "url", set: func(c *types.Config) { diff --git a/hscontrol/config/load_test.go b/hscontrol/config/load_test.go index 958c9cfdd..649906853 100644 --- a/hscontrol/config/load_test.go +++ b/hscontrol/config/load_test.go @@ -4081,6 +4081,7 @@ dns: extra_records: [{name: a.example.com, type: A, value: 100.64.0.1}] extra_records_path: /var/lib/headscale/records.json challenge: + resolvers: [""] provider: name: bogus tls: @@ -4117,6 +4118,7 @@ oidc: name: "every rule, in order", file: everyRule, want: []string{ + "dns.challenge.resolvers[0] is empty", "OIDC configuration is invalid", "dns.extra_records and dns.extra_records_path are mutually exclusive", "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", @@ -4149,6 +4151,7 @@ oidc: "database:\n type: \"\"\n", "database:\n type: sqlite\n sqlite:\n path: \"\"\n", ).Replace(everyRule), want: []string{ + "dns.challenge.resolvers[0] is empty", "OIDC configuration is invalid", "dns.extra_records and dns.extra_records_path are mutually exclusive", "tls.acme.hostname and tls.cert_path/tls.key_path are mutually exclusive", diff --git a/hscontrol/config/schema.go b/hscontrol/config/schema.go index 3b9455c6b..d4ad53d4d 100644 --- a/hscontrol/config/schema.go +++ b/hscontrol/config/schema.go @@ -146,7 +146,7 @@ var leafTypes = []leafType{ }, { Kinds: []reflect.Kind{reflect.Slice}, Label: "list", - Format: "A list. A variable holds it as a JSON array or, unless its values are objects, separated by spaces.", + Format: "A list. A variable holds it as a JSON array or, unless its values are objects, separated by spaces. An empty item is an error.", Sample: "`[9.9.9.9, 2620:fe::fe]`", Nix: "lib.types.listOf", }, { diff --git a/hscontrol/config/validate.go b/hscontrol/config/validate.go index 90f0fa669..13bf6b907 100644 --- a/hscontrol/config/validate.go +++ b/hscontrol/config/validate.go @@ -248,7 +248,7 @@ func Validate(c *types.Config) error { // validateValues parses every value whose type parses text, and each // element of a list of them, from its own text, as the loader did: a -// Config built in Go was never parsed. +// Config built in Go was never parsed. An empty text in a list is an error. func validateValues(c *types.Config, v *configValidator) { for _, f := range types.ConfigFields(c) { if f.Value.Kind() != reflect.Slice { @@ -258,7 +258,17 @@ func validateValues(c *types.Config, v *configValidator) { } for i := range f.Value.Len() { - validateValue(v, fmt.Sprintf("%s[%d]", f.Path, i), f.Value.Index(i)) + key, elem := fmt.Sprintf("%s[%d]", f.Path, i), f.Value.Index(i) + + // No list gives "" a meaning: a resolver would be the local one, + // an allowed user one without an email. + if elem.Kind() == reflect.String && elem.Len() == 0 { + v.Add(&types.ConfigError{Reason: key + " is empty", Hint: "remove the empty entry"}) + + continue + } + + validateValue(v, key, elem) } } }