types: reject unknown policy.mode

A typo booted with no policy, allowing all traffic.

Updates #1926
This commit is contained in:
Kristoffer Dalby committed 2026-11-07 21:16:41 +00:00
1 parent 1c1c4901ca
commit 66a50a1241
3 files changed
+61

No files matched your search

+1
View File
@@ -173,6 +173,7 @@ is not connected, or refuses is "not found". See [the client docs](https://heads
- Invalid booleans stop startup or CLI commands. Use `true` or `false`. Remove ignored `database.gorm.prepare_stmt`; Headscale logs a warning. [#3619](https://github.com/juanfont/headscale/pull/3619)
- Debug hosts must be localhost, an IP, or the configured `server_url`/`metrics_listen_addr` host. `/metrics` accepts other hosts. [#3619](https://github.com/juanfont/headscale/pull/3619)
- `policy.mode` must be `file` or `database`. Any other value (e.g. a typo) now stops startup; before, headscale started with no policy and allowed all traffic.
#### CLI
+17
View File
@@ -968,12 +968,29 @@ func validateServerConfigInto(v *configValidator) {
validateDERPConfig(v)
validateDatabaseConfig(v)
validatePolicyConfig(v)
validateMagicDNSConfig(v)
validateCertificatesConfig(v)
depr.Apply(v)
}
// validatePolicyConfig records a ConfigError for an unknown policy.mode:
// startup would load no policy at all and allow all traffic.
func validatePolicyConfig(v *configValidator) {
m := viper.GetString("policy.mode")
if m == PolicyModeFile || m == PolicyModeDB {
return
}
v.Add(&ConfigError{
Reason: "policy.mode has an unsupported value",
Current: []KV{{"policy.mode", m}},
Allowed: []string{PolicyModeFile, PolicyModeDB},
See: "https://headscale.net/stable/ref/policy/",
})
}
// validateDERPConfig records ConfigErrors when the embedded DERP server
// is enabled without the addresses or paths it needs.
func validateDERPConfig(v *configValidator) {
+43
View File
@@ -1897,3 +1897,46 @@ derp:
})
}
}
func TestPolicyMode(t *testing.T) {
for _, tt := range []struct {
mode string
wantErr bool
}{
{"file", false},
{"database", false},
{"databse", true},
{"File", true},
{`""`, true},
} {
t.Run(tt.mode, func(t *testing.T) {
viper.Reset()
t.Cleanup(viper.Reset)
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`---
noise:
private_key_path: noise_private.key
server_url: http://127.0.0.1:8080
prefixes:
v4: 100.64.0.0/10
database:
type: sqlite3
dns:
magic_dns: false
override_local_dns: false
policy:
mode: `+tt.mode+"\n"), 0o600))
require.NoError(t, LoadConfig(dir, false))
err := validateServerConfig()
if !tt.wantErr {
require.NoError(t, err)
return
}
require.ErrorContains(t, err, "policy.mode has an unsupported value")
})
}
}