Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🗃️ Keep space owners from losing their membership row (#3505)

Luke Vella758a1aea4ca3
diff --git a/apps/web/src/features/space/loaders.test.ts b/apps/web/src/features/space/loaders.test.ts
index 90e0194cd..619c78ef7 100644
--- a/apps/web/src/features/space/loaders.test.ts
+++ b/apps/web/src/features/space/loaders.test.ts
@@ -3,18 +3,13 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("server-only", () => ({}));
const REDIRECT = "NEXT_REDIRECT";
-const NOT_FOUND = "NEXT_NOT_FOUND";
const mockRedirect = vi.fn((url: string) => {
throw Object.assign(new Error(REDIRECT), { digest: `${REDIRECT};${url}` });
});
-const mockNotFound = vi.fn(() => {
- throw Object.assign(new Error(NOT_FOUND), { digest: NOT_FOUND });
-});
vi.mock("next/navigation", () => ({
redirect: (url: string) => mockRedirect(url),
- notFound: () => mockNotFound(),
}));
const mockGetSessionState = vi.fn();
@@ -75,7 +70,6 @@ describe("loadActiveSpace", () => {
await expect(gate()).resolves.toEqual(space);
expect(mockRedirect).not.toHaveBeenCalled();
- expect(mockNotFound).not.toHaveBeenCalled();
});
it("redirects to /setup when the user owns no space at all", async () => {
@@ -91,17 +85,15 @@ describe("loadActiveSpace", () => {
);
});
- // The bug: an owned-but-inaccessible space used to fall through to the
- // /setup redirect, where onboarding created a second space.
- it("renders not-found when the user owns a space but none is active", async () => {
+ // /setup bounces an owner straight back, so redirecting there would loop.
+ it("throws when the user owns a space but none is active", async () => {
authenticatedAs(completeUser);
mockGetActiveSpaceForUser.mockResolvedValue(null);
mockGetOwnedSpace.mockResolvedValue({ id: "space-1" });
const gate = await loadGate();
- await expect(gate()).rejects.toThrow(NOT_FOUND);
- expect(mockNotFound).toHaveBeenCalled();
+ await expect(gate()).rejects.toThrow("Space owner has no membership row");
expect(mockRedirect).not.toHaveBeenCalled();
});
diff --git a/apps/web/src/features/space/loaders.ts b/apps/web/src/features/space/loaders.ts
index 2ee874c32..1e267b3e6 100644
--- a/apps/web/src/features/space/loaders.ts
+++ b/apps/web/src/features/space/loaders.ts
@@ -1,6 +1,6 @@
import "server-only";
-import { notFound, redirect } from "next/navigation";
+import { redirect } from "next/navigation";
import { cache } from "react";
import { getUpcomingEventCount } from "@/features/scheduled-event/data";
@@ -59,14 +59,10 @@ export const loadActiveSpace = cache(async () => {
const onboarding = await resolveOnboarding(user);
if (onboarding.status === "ownedSpaceInactive") {
- // A user can resolve to no *active* space while still owning one
- // (their only other memberships are ineffective, or their own
- // membership row is gone) — /setup would bounce them straight back,
- // so render the not-found page instead of looping. notFound() rather
- // than a thrown AppError: nothing adapts AppError for server
- // components, so it would reach the error boundary as an unexpected
- // error, showing "Something went wrong" and reporting to Sentry.
- notFound();
+ // The owner's own membership is always effective, so this means their
+ // row is gone, which a database trigger forbids. /setup would bounce
+ // them straight back; fail loudly instead of looping.
+ throw new Error("Space owner has no membership row");
}
if (onboarding.status === "incomplete") {
diff --git a/packages/database/prisma/migrations/20261013120000_protect_space_owner_membership/migration.sql b/packages/database/prisma/migrations/20261013120000_protect_space_owner_membership/migration.sql
new file mode 100644
index 000000000..73e657a4f
--- /dev/null
+++ b/packages/database/prisma/migrations/20261013120000_protect_space_owner_membership/migration.sql
@@ -0,0 +1,46 @@
+-- Restore any owner membership that has already gone missing, so every
+-- space satisfies the invariant the trigger below protects.
+INSERT INTO "space_members" ("id", "space_id", "user_id", "role", "created_at", "updated_at", "last_selected_at")
+SELECT gen_random_uuid(), s."id", s."owner_id", 'ADMIN', now(), now(), now()
+FROM "spaces" s
+WHERE NOT EXISTS (
+ SELECT 1 FROM "space_members" m
+ WHERE m."space_id" = s."id" AND m."user_id" = s."owner_id"
+)
+ON CONFLICT ("space_id", "user_id") DO NOTHING;
+
+-- A space's owner must always be a member of it: without the row the owner
+-- is locked out of the app. Rejects deleting the row, or moving it to
+-- another space or user, while the space and its owner both exist. Deleting
+-- the space or the owner's account cascades through here after the parent
+-- row is already gone, so those deletions pass.
+CREATE OR REPLACE FUNCTION prevent_space_owner_membership_removal()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF TG_OP = 'UPDATE'
+ AND NEW."space_id" = OLD."space_id"
+ AND NEW."user_id" = OLD."user_id" THEN
+ RETURN NEW;
+ END IF;
+
+ IF EXISTS (
+ SELECT 1 FROM "spaces" s
+ JOIN "users" u ON u."id" = s."owner_id"
+ WHERE s."id" = OLD."space_id" AND s."owner_id" = OLD."user_id"
+ ) THEN
+ RAISE EXCEPTION 'Cannot remove the owner (%) from space %. Transfer ownership first.',
+ OLD."user_id", OLD."space_id"
+ USING ERRCODE = 'integrity_constraint_violation';
+ END IF;
+
+ IF TG_OP = 'DELETE' THEN
+ RETURN OLD;
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER trg_prevent_space_owner_membership_removal
+BEFORE DELETE OR UPDATE OF "space_id", "user_id" ON "space_members"
+FOR EACH ROW EXECUTE FUNCTION prevent_space_owner_membership_removal();