Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 22 additions & 0 deletions tailcat_derpmap_null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
}
}