Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(hub): warn when TRUSTED_AUTH_HEADER is set without TRUSTED_PROXY_IPS (#2812)

Pavlo Sukhanovdcd4f14807df
diff --git a/internal/hub/api.go b/internal/hub/api.go
index 715b3e384..0061f40e4 100644
--- a/internal/hub/api.go
+++ b/internal/hub/api.go
@@ -85,6 +85,9 @@ func (h *Hub) registerMiddlewares(se *core.ServeEvent) {
if trustedHeader, _ := utils.GetEnv("TRUSTED_AUTH_HEADER"); trustedHeader != "" {
// only honor the header from these peers, if set
trustedProxies, restricted := parseTrustedProxies()
+ if !restricted {
+ slog.Warn("TRUSTED_AUTH_HEADER is set without TRUSTED_PROXY_IPS: the header is trusted from any client that can reach the hub", "header", trustedHeader)
+ }
se.Router.BindFunc(func(e *core.RequestEvent) error {
if restricted && !isTrustedProxy(trustedProxies, e.Request.RemoteAddr) {
return e.Next()
diff --git a/internal/hub/api_test.go b/internal/hub/api_test.go
index 88fdbded3..7584847e6 100644
--- a/internal/hub/api_test.go
+++ b/internal/hub/api_test.go
@@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
+ "log/slog"
"net/http"
"net/http/httptest"
"sort"
@@ -1525,6 +1526,76 @@ func TestTrustedHeaderProxyAllowlist(t *testing.T) {
}
}
+func TestTrustedHeaderWithoutProxyWarning(t *testing.T) {
+ var hubs []*beszelTests.TestHub
+
+ defer func() {
+ for _, hub := range hubs {
+ hub.Cleanup()
+ }
+ }()
+
+ testAppFactory := func(t testing.TB) *pbTests.TestApp {
+ hub, _ := beszelTests.NewTestHub(t.TempDir())
+ hubs = append(hubs, hub)
+ hub.StartHub()
+ return hub.TestApp
+ }
+
+ testCases := []struct {
+ name string
+ env map[string]string
+ expectWarning []string
+ expectNone []string
+ }{
+ {
+ name: "header not set",
+ expectNone: []string{"TRUSTED_AUTH_HEADER"},
+ },
+ {
+ name: "trusted header without allowlist",
+ env: map[string]string{"TRUSTED_AUTH_HEADER": "X-Beszel-Trusted"},
+ expectWarning: []string{"TRUSTED_AUTH_HEADER is set without TRUSTED_PROXY_IPS", "header=X-Beszel-Trusted"},
+ },
+ {
+ name: "trusted header with allowlist",
+ env: map[string]string{"TRUSTED_AUTH_HEADER": "X-Beszel-Trusted", "TRUSTED_PROXY_IPS": "10.0.0.5"},
+ expectNone: []string{"TRUSTED_AUTH_HEADER"},
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ for key, value := range tc.env {
+ t.Setenv(key, value)
+ }
+
+ var logs bytes.Buffer
+ previous := slog.Default()
+ slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelWarn})))
+ t.Cleanup(func() { slog.SetDefault(previous) })
+
+ scenario := beszelTests.ApiScenario{
+ Name: "GET /api/health",
+ Method: http.MethodGet,
+ URL: "/api/health",
+ ExpectedStatus: 200,
+ ExpectedContent: []string{"API is healthy"},
+ TestAppFactory: testAppFactory,
+ AfterTestFunc: func(t testing.TB, app *pbTests.TestApp, res *http.Response) {
+ for _, want := range tc.expectWarning {
+ require.Contains(t, logs.String(), want)
+ }
+ for _, unwanted := range tc.expectNone {
+ require.NotContains(t, logs.String(), unwanted)
+ }
+ },
+ }
+ scenario.Test(t)
+ })
+ }
+}
+
func TestUpdateEndpoint(t *testing.T) {
t.Setenv("CHECK_UPDATES", "true")