derp: fix HomeParams merge panic, allow scores-only files

Maps without HomeParams (Tailscale default, embedded region) hit a nil
dereference. Keep HomeParams nil unless set; drop scores clients ignore
or JSON cannot encode.

Fixes #2918
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 14:11:33 +01:00
1 parent 0f2e68fd1d
commit 6f2f97ca17
3 files changed
+106 -15

No files matched your search

+26 -14
View File
@@ -9,6 +9,7 @@ import (
"hash/crc64"
"io"
"maps"
"math"
"math/rand"
"net/http"
"net/url"
@@ -23,18 +24,19 @@ import (
"tailscale.com/tailcfg"
)
var errEmptyDERPMapFile = errors.New("DERP map file has no regions (YAML keys are lowercased Go field names, e.g. regionid)")
var errEmptyDERPMapFile = errors.New("DERP map file has no regions or homeparams (YAML keys are lowercased Go field names, e.g. regionid)")
// loadDERPMapFromPath reads a DERP map file in the format its extension names
// (see [util.UnmarshalByExt]). A map with no regions is an error: unknown keys
// decode to nothing, e.g. YAML written with JSON's field names.
// (see [util.UnmarshalByExt]). A map with neither regions nor HomeParams is an
// error: unknown keys decode to nothing, e.g. YAML written with JSON's field
// names. HomeParams alone is valid, to score regions from other sources.
func loadDERPMapFromPath(path string) (*tailcfg.DERPMap, error) {
derpMap, err := util.ReadFileByExt[tailcfg.DERPMap](path)
if err != nil {
return nil, fmt.Errorf("reading DERP map: %w", err)
}
if len(derpMap.Regions) == 0 {
if len(derpMap.Regions) == 0 && derpMap.HomeParams == nil {
return nil, fmt.Errorf("%w: %s", errEmptyDERPMapFile, path)
}
@@ -73,20 +75,28 @@ func loadDERPMapFromURL(addr url.URL) (*tailcfg.DERPMap, error) {
return &derpMap, err
}
// mergeDERPMaps naively merges a list of [tailcfg.DERPMap] values into a single
// [tailcfg.DERPMap], it will _only_ look at the Regions, an integer.
// If a region exists in two of the given [tailcfg.DERPMap] values, the region
// form the _last_ [tailcfg.DERPMap] will be preserved.
// mergeDERPMaps merges a list of [tailcfg.DERPMap] values into a single
// [tailcfg.DERPMap], looking only at Regions and HomeParams.RegionScore.
// If a region or score exists in two of the given [tailcfg.DERPMap] values, the
// one from the _last_ [tailcfg.DERPMap] will be preserved. HomeParams stays nil
// unless a source sets it.
// An empty [tailcfg.DERPMap] list will result in a [tailcfg.DERPMap] with no regions.
func mergeDERPMaps(derpMaps []*tailcfg.DERPMap) *tailcfg.DERPMap {
result := tailcfg.DERPMap{
OmitDefaultRegions: false,
HomeParams: &tailcfg.DERPHomeParams{},
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{},
}
for _, derpMap := range derpMaps {
maps.Copy(result.HomeParams.RegionScore, derpMap.HomeParams.RegionScore)
if hp := derpMap.HomeParams; hp != nil {
if result.HomeParams == nil {
result.HomeParams = &tailcfg.DERPHomeParams{
RegionScore: map[tailcfg.DERPRegionID]float64{},
}
}
maps.Copy(result.HomeParams.RegionScore, hp.RegionScore)
}
// Clone each region: copying the pointer would let a later in-place
// shuffle alias regions shared with the source map or a previously
@@ -103,10 +113,12 @@ func mergeDERPMaps(derpMaps []*tailcfg.DERPMap) *tailcfg.DERPMap {
}
}
for id, regionScore := range result.HomeParams.RegionScore {
if regionScore <= 0 {
delete(result.HomeParams.RegionScore, id)
}
if result.HomeParams != nil {
// Clients ignore non-positive scores, and NaN or Inf would fail the
// JSON encoding of every MapResponse.
maps.DeleteFunc(result.HomeParams.RegionScore, func(_ tailcfg.DERPRegionID, s float64) bool {
return s <= 0 || math.IsNaN(s) || math.IsInf(s, 0)
})
}
return &result
+73
View File
@@ -1,9 +1,14 @@
package derp
import (
"math"
"os"
"path/filepath"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tailscale.com/tailcfg"
)
@@ -43,3 +48,71 @@ func TestMergeDERPMapsNullRemovesRegion(t *testing.T) {
assert.NotContains(t, merged.Regions, tailcfg.DERPRegionID(1))
assert.Contains(t, merged.Regions, tailcfg.DERPRegionID(2))
}
// TestGetDERPMapCarriesHomeParams pins that RegionScore set in a derp.paths
// file reaches clients (#2918). A file may hold scores alone: region 1 comes
// from another source and 999 is the embedded region app.go adds after the
// merge. Without scores HomeParams stays nil, so the MapResponse is unchanged
// for everyone who does not opt in.
func TestGetDERPMapCarriesHomeParams(t *testing.T) {
base := &tailcfg.DERPMap{
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{
1: {RegionID: 1, RegionCode: "nyc"},
},
}
path := filepath.Join(t.TempDir(), "derp.yaml")
require.NoError(t, os.WriteFile(path, []byte(`homeparams:
regionscore:
1: 4
999: 0.5
`), 0o600))
got, err := GetDERPMap(types.DERPConfig{DERPMap: base})
require.NoError(t, err)
assert.Nil(t, got.HomeParams)
got, err = GetDERPMap(types.DERPConfig{DERPMap: base, Paths: []string{path}})
require.NoError(t, err)
require.NotNil(t, got.HomeParams)
assert.Equal(t,
map[tailcfg.DERPRegionID]float64{1: 4, 999: 0.5},
got.HomeParams.RegionScore,
)
}
// TestMergeDERPMapsHomeParams mirrors region merging: a later map's score for
// a region replaces an earlier one, and other scores survive maps that set
// none. Scores clients ignore or JSON cannot encode are dropped.
func TestMergeDERPMapsHomeParams(t *testing.T) {
first := &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{
RegionScore: map[tailcfg.DERPRegionID]float64{1: 2, 2: 3},
}}
none := &tailcfg.DERPMap{Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{
1: {RegionID: 1},
}}
empty := &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{}}
invalid := &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{
RegionScore: map[tailcfg.DERPRegionID]float64{
3: 0, 4: -1, 5: math.NaN(), 6: math.Inf(1),
},
}}
second := &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{
RegionScore: map[tailcfg.DERPRegionID]float64{1: 0.5},
}}
merged := mergeDERPMaps([]*tailcfg.DERPMap{first, none, empty, invalid, second})
require.NotNil(t, merged.HomeParams)
assert.Equal(t,
map[tailcfg.DERPRegionID]float64{1: 0.5, 2: 3},
merged.HomeParams.RegionScore,
)
merged.HomeParams.RegionScore[2] = 9
assert.Equal(t,
map[tailcfg.DERPRegionID]float64{1: 2, 2: 3},
first.HomeParams.RegionScore,
"merged scores must not alias a source map",
)
}
+7 -1
View File
@@ -403,6 +403,9 @@ func TestLoadDERPMapFromPath(t *testing.T) {
},
},
},
HomeParams: &tailcfg.DERPHomeParams{
RegionScore: map[tailcfg.DERPRegionID]float64{999: 0.5},
},
}
tailcfgJSON, err := json.Marshal(want)
@@ -418,6 +421,9 @@ func TestLoadDERPMapFromPath(t *testing.T) {
hostname: derp.test
derpport: 443
insecurefortests: true
homeparams:
regionscore:
999: 0.5
`
tests := []struct {
@@ -431,7 +437,7 @@ func TestLoadDERPMapFromPath(t *testing.T) {
{
name: "flow-style yaml",
file: "derp.yaml",
content: "{regions: {999: {regionid: 999, regioncode: test, nodes: [{name: 999a, regionid: 999, hostname: derp.test, derpport: 443, insecurefortests: true}]}}}",
content: "{regions: {999: {regionid: 999, regioncode: test, nodes: [{name: 999a, regionid: 999, hostname: derp.test, derpport: 443, insecurefortests: true}]}}, homeparams: {regionscore: {999: 0.5}}}",
},
{name: "tailcfg json", file: "derp.json", content: string(tailcfgJSON)},
{name: "tailcfg json with comments and trailing comma", file: "derp.json", content: "// test map\n" + strings.TrimSuffix(string(tailcfgJSON), "}") + ",}"},