mirror of
https://github.com/juanfont/headscale.git
synced 2026-07-07 16:40:21 +09:00
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:
committed by
Kristoffer Dalby
parent
5a70a72988
commit
bb06b90543
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user