types: skip malformed derp.urls entries instead of panicking

A failed url.Parse left a nil pointer that was dereferenced at startup.
This commit is contained in:
Kristoffer Dalby
2026-06-05 14:29:43 +00:00
committed by Kristoffer Dalby
parent 5a70a72988
commit bb06b90543
2 changed files with 33 additions and 3 deletions
+5 -3
View File
@@ -712,8 +712,8 @@ func derpConfig() DERPConfig {
urlStrs := viper.GetStringSlice("derp.urls")
urls := make([]url.URL, len(urlStrs))
for index, urlStr := range urlStrs {
urls := make([]url.URL, 0, len(urlStrs))
for _, urlStr := range urlStrs {
urlAddr, err := url.Parse(urlStr)
if err != nil {
log.Error().
@@ -721,9 +721,11 @@ func derpConfig() DERPConfig {
Str("url", urlStr).
Err(err).
Msg("Failed to parse url, ignoring...")
continue
}
urls[index] = *urlAddr
urls = append(urls, *urlAddr)
}
paths := viper.GetStringSlice("derp.paths")
+28
View File
@@ -0,0 +1,28 @@
package types
import (
"testing"
"github.com/spf13/viper"
)
// TestDerpConfigSkipsMalformedURL ensures a malformed derp.urls entry is
// skipped (as the "ignoring..." log promises) rather than dereferencing the
// nil *url.URL that url.Parse returns on error, which crashed the server at
// startup.
func TestDerpConfigSkipsMalformedURL(t *testing.T) {
viper.Reset()
defer viper.Reset()
viper.Set("derp.urls", []string{
"https://controlplane.tailscale.com/derpmap/default",
"://bad",
})
cfg := derpConfig()
if len(cfg.URLs) != 1 {
t.Fatalf("expected the malformed derp.urls entry to be skipped, got %d urls: %v",
len(cfg.URLs), cfg.URLs)
}
}