package app import ( "os" "path/filepath" "testing" ) func TestConfigurationSafety(t *testing.T) { c := DefaultConfig() c.Listen = "0.0.0.0:8080" if c.Validate() == nil { t.Fatal("plain HTTP public listener accepted") } dir := t.TempDir() p := filepath.Join(dir, "config.toml") os.WriteFile(p, []byte("unknown_setting = true\n"), 0600) if _, e := LoadConfig(p); e == nil { t.Fatal("unknown configuration silently accepted") } os.WriteFile(p, []byte("data_dir = 'var'\n"), 0600) loaded, e := LoadConfig(p) if e != nil { t.Fatal(e) } if loaded.DataDir != filepath.Join(dir, "var") { t.Fatal("relative directory not resolved from configuration") } } func TestLANHTTPRequiresExplicitPrivateAddress(t *testing.T) { for _, test := range []struct { name, listen, publicURL string allow, valid bool }{ {"disabled", "192.168.1.204:8080", "http://192.168.1.204:8080", false, false}, {"explicit LAN", "192.168.1.204:8080", "http://192.168.1.204:8080", true, true}, {"wildcard IPv4", "0.0.0.0:8080", "http://192.168.1.204:8080", true, false}, {"public listener", "8.8.8.8:8080", "http://192.168.1.204:8080", true, false}, {"public HTTP URL", "192.168.1.204:8080", "http://8.8.8.8:8080", true, true}, {"private IPv6", "[fd00::204]:8080", "http://[fd00::204]:8080", true, true}, {"wildcard IPv6", "[::]:8080", "http://[fd00::204]:8080", true, false}, {"loopback", "127.0.0.1:8080", "http://127.0.0.1:8080", false, true}, } { t.Run(test.name, func(t *testing.T) { c := DefaultConfig() c.Listen, c.PublicURL, c.AllowLANHTTP = test.listen, test.publicURL, test.allow if err := c.Validate(); (err == nil) != test.valid { t.Fatalf("valid=%v, expected %v; error=%v", err == nil, test.valid, err) } }) } c := DefaultConfig() c.Listen, c.PublicURL, c.AllowLANHTTP, c.SecureCookies = "192.168.1.204:8080", "http://192.168.1.204:8080", true, true if c.Validate() == nil { t.Fatal("Secure cookies accepted for LAN HTTP") } }