mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 11:57:07 +09:00
types: reject unknown policy.mode
A typo booted with no policy, allowing all traffic. Updates #1926
This commit is contained in:
1 parent
ae6ea2cce4
commit
bd3495d8d2
2 files changed
+60
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user