fix(webui): the wildcard listen address is not loopback

IsLoopback(":8484") returned true: the empty host fell through the
switch as loopback, so a wildcard bind skipped the non-loopback token
requirement and served every interface unauthenticated. An empty host is
the wildcard, not loopback.
This commit is contained in:
sam committed 2026-10-10 22:09:05 +11:00
1 parent 5124bfd4cf
commit a6ac25876a
2 files changed
+32 -2

No files matched your search

+3 -2
View File
@@ -48,8 +48,9 @@ func IsLoopback(listen string) bool {
if err != nil {
host = strings.TrimSpace(listen)
}
switch host {
case "", "localhost":
// An empty host is the wildcard, not loopback: ":8484" binds every
// interface, so it must not pass the loopback exemption.
if host == "localhost" {
return true
}
ip := net.ParseIP(host)
+29
View File
@@ -0,0 +1,29 @@
package webui
import "testing"
// TestIsLoopback pins the loopback exemption that lets a bind skip the
// dashboard token: the wildcard host must not qualify, or `-listen :8484`
// would serve every interface unauthenticated.
func TestIsLoopback(t *testing.T) {
tests := []struct {
listen string
want bool
}{
{":8484", false},
{"0.0.0.0:8484", false},
{"[::]:8484", false},
{"192.168.1.5:8484", false},
{"127.0.0.1:8484", true},
{"127.0.0.2:8484", true},
{"[::1]:8484", true},
{"localhost:8484", true},
}
for _, tt := range tests {
t.Run(tt.listen, func(t *testing.T) {
if got := IsLoopback(tt.listen); got != tt.want {
t.Fatalf("IsLoopback(%q) = %v, want %v", tt.listen, got, tt.want)
}
})
}
}