ad6cdab418
The web proxy binds the wildcard and drops connections that are not loopback or Tailscale at both ends. A LAN listener is opt-in (web.lan) and refuses sign-in and signed-in pages over plain HTTP unless web.lan_plain_login is set. Existing installs keep their bind address with a warning. Without the LAN the mDNS advert carries only the tailnet and public routes (lan=0).
108 lines
3.9 KiB
Go
108 lines
3.9 KiB
Go
package discovery
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/mdns"
|
|
)
|
|
|
|
func service(ip string) *mdns.ServiceEntry {
|
|
return &mdns.ServiceEntry{AddrV4: net.ParseIP(ip), Host: "server.local.", Port: 8083,
|
|
InfoFields: []string{"name=Linux desktop", "path=/connect", "tls=0", "auth=password", "v=release"}}
|
|
}
|
|
|
|
func TestBrowseCollectsDeduplicatedValidAdvertisements(t *testing.T) {
|
|
values, err := browse(context.Background(), func(ctx context.Context, p *mdns.QueryParam) error {
|
|
if p.Service != ServiceType || p.Timeout.Seconds() > 4 {
|
|
t.Fatal("unbounded or wrong service query")
|
|
}
|
|
for _, entry := range []*mdns.ServiceEntry{service("192.168.1.3"), service("192.168.1.2"), service("192.168.1.3"), service("127.0.0.1"), nil} {
|
|
p.Entries <- entry
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil || len(values) != 2 {
|
|
t.Fatalf("values=%+v err=%v", values, err)
|
|
}
|
|
if values[0].Endpoint != "ws://192.168.1.2:8083/connect" || values[0].Name != "Linux desktop" || values[0].Auth != "password" {
|
|
t.Fatalf("wrong record: %+v", values[0])
|
|
}
|
|
}
|
|
|
|
func TestBrowseHonorsCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, err := browse(ctx, func(ctx context.Context, p *mdns.QueryParam) error { <-ctx.Done(); return ctx.Err() })
|
|
if err == nil {
|
|
t.Fatal("canceled browse must fail")
|
|
}
|
|
}
|
|
|
|
func TestDiscoveryRejectsMalformedAndUnsafeAdvertisements(t *testing.T) {
|
|
for _, ip := range []string{"0.0.0.0", "127.0.0.1", "224.0.0.1", "169.254.3.1", "::", "::1", "fe80::1"} {
|
|
if _, ok := discovered(service(ip)); ok {
|
|
t.Fatalf("accepted address %s", ip)
|
|
}
|
|
}
|
|
for _, fields := range [][]string{{"path=//evil/connect", "auth=password", "tls=0"}, {"path=/connect", "auth=unknown", "tls=0"}, {"path=/connect", "auth=password", "tls=garbage"}} {
|
|
item := service("192.168.1.2")
|
|
item.InfoFields = fields
|
|
if _, ok := discovered(item); ok {
|
|
t.Fatalf("accepted invalid TXT %+v", fields)
|
|
}
|
|
}
|
|
item := service("192.168.1.2")
|
|
item.Port = 65536
|
|
if _, ok := discovered(item); ok {
|
|
t.Fatal("accepted invalid port")
|
|
}
|
|
}
|
|
|
|
func TestBrowseListsEveryRouteToTheSameMachine(t *testing.T) {
|
|
item := service("192.168.1.2")
|
|
item.InfoFields = append(item.InfoFields, "ts=100.85.25.33, 10.0.0.9,100.70.1.1", "public=https://box.tail1.ts.net:8443")
|
|
found, ok := discovered(item)
|
|
if !ok {
|
|
t.Fatal("rejected a valid advertisement with extra routes")
|
|
}
|
|
want := []FoundAddress{
|
|
{Kind: "lan", Endpoint: "ws://192.168.1.2:8083/connect"},
|
|
{Kind: "tailscale", Endpoint: "ws://100.85.25.33:8083/connect"},
|
|
{Kind: "tailscale", Endpoint: "ws://100.70.1.1:8083/connect"},
|
|
{Kind: "public", Endpoint: "wss://box.tail1.ts.net:8443/connect"},
|
|
}
|
|
if !slices.Equal(found.Addresses, want) || found.Endpoint != want[0].Endpoint {
|
|
t.Fatalf("addresses = %+v", found.Addresses)
|
|
}
|
|
plain, _ := discovered(service("192.168.1.2"))
|
|
if !slices.Equal(plain.Addresses, want[:1]) {
|
|
t.Fatalf("a LAN-only advert must list its one route: %+v", plain.Addresses)
|
|
}
|
|
bad := service("192.168.1.2")
|
|
bad.InfoFields = append(bad.InfoFields, "ts=evil.example", "public=javascript:alert(1)")
|
|
if found, _ := discovered(bad); len(found.Addresses) != 1 {
|
|
t.Fatalf("unsafe routes accepted: %+v", found.Addresses)
|
|
}
|
|
}
|
|
|
|
func TestBrowseOmitsTheLANRouteWhenTheAdvertSaysLan0(t *testing.T) {
|
|
item := service("100.85.25.33")
|
|
item.InfoFields = append(item.InfoFields, "lan=0", "ts=100.85.25.33", "public=https://box.tail1.ts.net:8443")
|
|
found, ok := discovered(item)
|
|
want := []FoundAddress{
|
|
{Kind: "tailscale", Endpoint: "ws://100.85.25.33:8083/connect"},
|
|
{Kind: "public", Endpoint: "wss://box.tail1.ts.net:8443/connect"},
|
|
}
|
|
if !ok || !slices.Equal(found.Addresses, want) || found.Endpoint != want[0].Endpoint {
|
|
t.Fatalf("found = %+v (%v)", found, ok)
|
|
}
|
|
bare := service("100.85.25.33")
|
|
bare.InfoFields = append(bare.InfoFields, "lan=0")
|
|
if _, ok := discovered(bare); ok {
|
|
t.Fatal("an advert with no usable route was accepted")
|
|
}
|
|
}
|