diff --git a/tailcat.go b/tailcat.go index 52e0723c2..d2f9272c8 100644 --- a/tailcat.go +++ b/tailcat.go @@ -1412,6 +1412,15 @@ func decodeDERPMap(data []byte) *tailcfg.DERPMap { if err := json.Unmarshal(data, dm); err != nil { return nil } + // JSON nulls decode to nil pointers, which callers of + // [FetchDERPMap] would dereference. Drop them, as Expand does. + for rid, r := range dm.Regions { + if r == nil { + delete(dm.Regions, rid) + continue + } + r.Nodes = slices.DeleteFunc(r.Nodes, func(n *tailcfg.DERPNode) bool { return n == nil }) + } return dm } diff --git a/tailcat_derpmap_null_test.go b/tailcat_derpmap_null_test.go index 30fc25c49..fbbcd19d1 100644 --- a/tailcat_derpmap_null_test.go +++ b/tailcat_derpmap_null_test.go @@ -3,9 +3,12 @@ package tailcat import ( "context" "encoding/json" + "net/http" + "net/http/httptest" "testing" "tailscale.com/tailcfg" + "tailscale.com/types/logger" ) func TestExpandSkipsNullDERPRegions(t *testing.T) { @@ -21,3 +24,22 @@ func TestExpandSkipsNullDERPRegions(t *testing.T) { t.Fatal("Expand modified the caller's DERP map") } } + +func TestFetchDERPMapSkipsNulls(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`{"Regions":{"1":null,"2":{"RegionID":2,"Nodes":[null,{"Name":"2a","RegionID":2,"HostName":"a.example.com"}]}}}`)) + })) + defer ts.Close() + + dm, err := FetchDERPMap(context.Background(), DERPMapURL(ts.URL), DERPMapCache(new(memDERPMapCache))) + if err != nil { + t.Fatal(err) + } + if len(dm.Regions) != 1 { + t.Fatalf("got %d regions, want 1 (null region dropped)", len(dm.Regions)) + } + r := dm.Regions[2] + if r == nil || len(r.Nodes) != 1 || r.Nodes[0] == nil || r.Nodes[0].Name != "2a" { + t.Fatalf("region 2 = %v, want only its non-null node", logger.AsJSON(r)) + } +}