Lightweight server monitoring with historical data, docker stats, and alerts.
fix(hub): warn when no user matches the automatic login email (#2824)
Pavlo Sukhanov83a494424594
diff --git a/internal/hub/api.go b/internal/hub/api.goindex 0061f40e4..85fbcb706 100644--- a/internal/hub/api.go+++ b/internal/hub/api.go@@ -2,6 +2,8 @@ package hubimport ("context"+ "database/sql"+ "errors""fmt""log/slog""net"@@ -67,6 +69,11 @@ func (h *Hub) registerMiddlewares(se *core.ServeEvent) {}isAuthRefresh := e.Request.URL.Path == "/api/collections/users/auth-refresh" && e.Request.Method == http.MethodPoste.Auth, err = e.App.FindAuthRecordByEmail("users", email)+ // log on the auth refresh only: the login page sends one per attempt,+ // while this middleware runs on every request+ if isAuthRefresh && errors.Is(err, sql.ErrNoRows) {+ slog.Warn("No user matches the automatic login email, showing the login form", "email", email)+ }if err != nil || !isAuthRefresh {return e.Next()}diff --git a/internal/hub/api_test.go b/internal/hub/api_test.goindex 7584847e6..46e4f86f2 100644--- a/internal/hub/api_test.go+++ b/internal/hub/api_test.go@@ -1596,6 +1596,114 @@ func TestTrustedHeaderWithoutProxyWarning(t *testing.T) {}}+func TestTrustedHeaderUnknownUserWarning(t *testing.T) {+ var hubs []*beszelTests.TestHub++ defer func() {+ for _, hub := range hubs {+ hub.Cleanup()+ }+ }()++ t.Setenv("TRUSTED_AUTH_HEADER", "X-Beszel-Trusted")++ testAppFactory := func(t testing.TB) *pbTests.TestApp {+ hub, _ := beszelTests.NewTestHub(t.TempDir())+ hubs = append(hubs, hub)+ hub.StartHub()+ return hub.TestApp+ }++ const warning = "No user matches the automatic login email"++ testCases := []struct {+ name string+ method string+ url string+ headerEmail string+ createUser bool+ expectedStatus int+ expectedContent []string+ expectWarning bool+ }{+ {+ name: "auth refresh with unknown email",+ method: http.MethodPost,+ url: "/api/collections/users/auth-refresh",+ headerEmail: "[email protected]",+ expectedStatus: 401,+ expectedContent: []string{"requires valid"},+ expectWarning: true,+ },+ {+ name: "auth refresh with known email",+ method: http.MethodPost,+ url: "/api/collections/users/auth-refresh",+ headerEmail: "[email protected]",+ createUser: true,+ expectedStatus: 200,+ expectedContent: []string{"\"token\":"},+ },+ {+ name: "auth refresh without header",+ method: http.MethodPost,+ url: "/api/collections/users/auth-refresh",+ expectedStatus: 401,+ expectedContent: []string{"requires valid"},+ },+ {+ name: "other request with unknown email",+ method: http.MethodGet,+ url: "/api/health",+ headerEmail: "[email protected]",+ expectedStatus: 200,+ expectedContent: []string{"API is healthy"},+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ 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) })++ headers := map[string]string{}+ if tc.headerEmail != "" {+ headers["X-Beszel-Trusted"] = tc.headerEmail+ }++ scenario := beszelTests.ApiScenario{+ Name: tc.method + " " + tc.url,+ Method: tc.method,+ URL: tc.url,+ Headers: headers,+ ExpectedStatus: tc.expectedStatus,+ ExpectedContent: tc.expectedContent,+ TestAppFactory: testAppFactory,+ BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {+ if tc.createUser {+ // the users collection only authenticates verified users+ user, err := beszelTests.CreateUser(app, tc.headerEmail, "password123")+ require.NoError(t, err)+ user.SetVerified(true)+ require.NoError(t, app.Save(user))+ }+ },+ AfterTestFunc: func(t testing.TB, app *pbTests.TestApp, res *http.Response) {+ if tc.expectWarning {+ require.Contains(t, logs.String(), warning)+ require.Contains(t, logs.String(), "email="+tc.headerEmail)+ } else {+ require.NotContains(t, logs.String(), warning)+ }+ },+ }+ scenario.Test(t)+ })+ }+}+func TestUpdateEndpoint(t *testing.T) {t.Setenv("CHECK_UPDATES", "true")