diff --git a/internal/hub/api.go b/internal/hub/api.go index 4c1085a8..e8067ebe 100644 --- a/internal/hub/api.go +++ b/internal/hub/api.go @@ -180,6 +180,10 @@ func (info *UpdateInfo) getUpdate(e *core.RequestEvent) error { // GetUniversalToken handles the universal token API endpoint (create, read, delete) func (h *Hub) getUniversalToken(e *core.RequestEvent) error { + if e.Auth.IsSuperuser() { + return e.ForbiddenError("Superusers cannot use universal tokens", nil) + } + tokenMap := universalTokenMap.GetMap() userID := e.Auth.Id query := e.Request.URL.Query() diff --git a/internal/hub/api_test.go b/internal/hub/api_test.go index df7cdd80..03ef8b66 100644 --- a/internal/hub/api_test.go +++ b/internal/hub/api_test.go @@ -45,6 +45,12 @@ func TestApiRoutesAuthentication(t *testing.T) { readOnlyUser, err := beszelTests.CreateUserWithRole(hub, "readonly@example.com", "password123", "readonly") require.NoError(t, err, "Failed to create readonly user") readOnlyUserToken, err := readOnlyUser.NewAuthToken() + require.NoError(t, err, "Failed to create readonly user auth token") + + superuser, err := beszelTests.CreateSuperuser(hub, "superuser@example.com", "password123") + require.NoError(t, err, "Failed to create superuser") + superuserToken, err := superuser.NewAuthToken() + require.NoError(t, err, "Failed to create superuser auth token") // Create test system system, err := beszelTests.CreateRecord(hub, "systems", map[string]any{ @@ -197,6 +203,19 @@ func TestApiRoutesAuthentication(t *testing.T) { ExpectedContent: []string{"\"permanent\":true", "permanent-token-123"}, TestAppFactory: testAppFactory, }, + { + Name: "GET /universal-token - superuser should fail", + Method: http.MethodGet, + URL: "/api/beszel/universal-token", + Headers: map[string]string{ + "Authorization": superuserToken, + }, + ExpectedStatus: 403, + ExpectedContent: []string{"Superusers cannot use universal tokens"}, + TestAppFactory: func(t testing.TB) *pbTests.TestApp { + return hub.TestApp + }, + }, { Name: "GET /universal-token - with readonly auth should fail", Method: http.MethodGet, diff --git a/internal/tests/hub.go b/internal/tests/hub.go index ef048151..c68587e6 100644 --- a/internal/tests/hub.go +++ b/internal/tests/hub.go @@ -77,6 +77,16 @@ func CreateUser(app core.App, email string, password string) (*core.Record, erro return user, app.Save(user) } +// Helper function to create a test superuser for config tests +func CreateSuperuser(app core.App, email string, password string) (*core.Record, error) { + superusersCollection, _ := app.FindCachedCollectionByNameOrId(core.CollectionNameSuperusers) + superuser := core.NewRecord(superusersCollection) + superuser.Set("email", email) + superuser.Set("password", password) + + return superuser, app.Save(superuser) +} + func CreateUserWithRole(app core.App, email string, password string, roleName string) (*core.Record, error) { user, err := CreateUser(app, email, password) if err != nil {