Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): scope folder access checks to the folder's org and keep member access
Richie McIlroyae7bdac5bb0d
diff --git a/apps/web/__tests__/unit/folder-access.test.ts b/apps/web/__tests__/unit/folder-access.test.tsnew file mode 100644index 000000000..eff4d9f69--- /dev/null+++ b/apps/web/__tests__/unit/folder-access.test.ts@@ -0,0 +1,100 @@+import { Organisation, Space, User } from "@cap/web-domain";+import { beforeEach, describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ organizationAccess: vi.fn(),+ spaceAccess: vi.fn(),+}));++vi.mock("@/actions/organization/authorization", () => ({+ getOrganizationAccess: mocks.organizationAccess,+}));+vi.mock("@/actions/organization/space-authorization", () => ({+ getSpaceAccess: mocks.spaceAccess,+}));++import { canViewFolder } from "@/actions/folders/folder-access";++const userId = User.UserId.make("user-1");+const otherUserId = User.UserId.make("user-2");+const organizationId = Organisation.OrganisationId.make("org-1");+const spaceId = Space.SpaceId.make("space-1");++describe("canViewFolder", () => {+ beforeEach(() => {+ mocks.organizationAccess.mockReset();+ mocks.spaceAccess.mockReset();+ });++ it("limits personal folders to their creator", async () => {+ const folder = { organizationId, spaceId: null, createdById: userId };++ await expect(canViewFolder(userId, folder)).resolves.toBe(true);+ await expect(canViewFolder(otherUserId, folder)).resolves.toBe(false);+ expect(mocks.organizationAccess).not.toHaveBeenCalled();+ expect(mocks.spaceAccess).not.toHaveBeenCalled();+ });++ it("allows organization members into organization folders", async () => {+ const folder = {+ organizationId,+ spaceId: organizationId,+ createdById: otherUserId,+ };++ mocks.organizationAccess.mockResolvedValueOnce({ role: "member" });+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);++ mocks.organizationAccess.mockResolvedValueOnce(null);+ await expect(canViewFolder(userId, folder)).resolves.toBe(false);+ expect(mocks.organizationAccess).toHaveBeenCalledWith(+ userId,+ organizationId,+ );+ expect(mocks.spaceAccess).not.toHaveBeenCalled();+ });++ it("requires an organization or space role for space folders", async () => {+ const folder = { organizationId, spaceId, createdById: otherUserId };++ mocks.spaceAccess.mockResolvedValueOnce({+ organizationId,+ organizationRole: null,+ spaceRole: "member",+ });+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);++ mocks.spaceAccess.mockResolvedValueOnce({+ organizationId,+ organizationRole: "admin",+ spaceRole: null,+ });+ await expect(canViewFolder(userId, folder)).resolves.toBe(true);++ mocks.spaceAccess.mockResolvedValueOnce({+ organizationId,+ organizationRole: null,+ spaceRole: null,+ });+ await expect(canViewFolder(userId, folder)).resolves.toBe(false);++ mocks.spaceAccess.mockResolvedValueOnce(null);+ await expect(canViewFolder(userId, folder)).resolves.toBe(false);+ });++ it("rejects space folders whose space belongs to another organization", async () => {+ mocks.spaceAccess.mockResolvedValueOnce({+ organizationId: Organisation.OrganisationId.make("org-2"),+ organizationRole: "owner",+ spaceRole: "admin",+ });++ await expect(+ canViewFolder(userId, {+ organizationId,+ spaceId,+ createdById: otherUserId,+ }),+ ).resolves.toBe(false);+ });+});diff --git a/apps/web/actions/folders/add-videos.ts b/apps/web/actions/folders/add-videos.tsindex 2bd4b2b6d..f64a59ced 100644--- a/apps/web/actions/folders/add-videos.ts+++ b/apps/web/actions/folders/add-videos.ts@@ -12,6 +12,7 @@ import {import type { Folder, Space, Video } from "@cap/web-domain";import { and, eq, inArray } from "drizzle-orm";import { revalidatePath } from "next/cache";+import { getOrganizationAccess } from "@/actions/organization/authorization";import { getSpaceAccess } from "@/actions/organization/space-authorization";export async function addVideosToFolder(@@ -33,26 +34,32 @@ export async function addVideosToFolder(const [folder] = await db().select({id: folders.id,+ organizationId: folders.organizationId,spaceId: folders.spaceId,- createdById: folders.createdById,}).from(folders)- .where(eq(folders.id, folderId));+ .where(and(eq(folders.id, folderId), eq(folders.spaceId, spaceId)));if (!folder) {throw new Error("Folder not found");}- // Verify the caller can edit the destination folder. Mirrors- // FoldersPolicy.canEdit: personal folders (no space) are creator-only,- // space folders require space-admin or org admin/owner access.- if (folder.spaceId === null) {- if (folder.createdById !== user.id) {+ if (spaceId === user.activeOrganizationId) {+ const access = await getOrganizationAccess(+ user.id,+ user.activeOrganizationId,+ );+ if (!access || folder.organizationId !== user.activeOrganizationId) {throw new Error("Folder not found");}} else {- const access = await getSpaceAccess(user.id, folder.spaceId);- if (!access?.canManage) {+ const access = await getSpaceAccess(user.id, spaceId);+ if (+ !access ||+ access.organizationId !== folder.organizationId ||+ !access.organizationRole ||+ (!access.canManage && !access.spaceRole)+ ) {throw new Error("Folder not found");}}diff --git a/apps/web/actions/folders/folder-access.ts b/apps/web/actions/folders/folder-access.tsnew file mode 100644index 000000000..ce33352fc--- /dev/null+++ b/apps/web/actions/folders/folder-access.ts@@ -0,0 +1,27 @@+import type { Organisation, Space, User } from "@cap/web-domain";+import { getOrganizationAccess } from "@/actions/organization/authorization";+import { getSpaceAccess } from "@/actions/organization/space-authorization";++export async function canViewFolder(+ userId: User.UserId,+ folder: {+ organizationId: Organisation.OrganisationId;+ spaceId: Space.SpaceIdOrOrganisationId | null;+ createdById: User.UserId;+ },+) {+ if (folder.spaceId === null) return folder.createdById === userId;++ if (folder.spaceId === folder.organizationId) {+ return (+ (await getOrganizationAccess(userId, folder.organizationId)) !== null+ );+ }++ const access = await getSpaceAccess(userId, folder.spaceId);+ return Boolean(+ access &&+ access.organizationId === folder.organizationId &&+ (access.organizationRole || access.spaceRole),+ );+}diff --git a/apps/web/actions/folders/get-folder-videos.ts b/apps/web/actions/folders/get-folder-videos.tsindex ad7881147..edcce6fb9 100644--- a/apps/web/actions/folders/get-folder-videos.ts+++ b/apps/web/actions/folders/get-folder-videos.ts@@ -5,7 +5,7 @@ import { getCurrentUser } from "@cap/database/auth/session";import { folders, sharedVideos, spaceVideos } from "@cap/database/schema";import type { Folder, Space, Video } from "@cap/web-domain";import { eq } from "drizzle-orm";-import { getSpaceAccess } from "@/actions/organization/space-authorization";+import { canViewFolder } from "@/actions/folders/folder-access";export async function getFolderVideoIds(folderId: Folder.FolderId,@@ -22,40 +22,19 @@ export async function getFolderVideoIds(throw new Error("Folder ID is required");}- // Ensure the caller can see this folder before disclosing its contents.const [folder] = await db().select({- spaceId: folders.spaceId,organizationId: folders.organizationId,+ spaceId: folders.spaceId,createdById: folders.createdById,}).from(folders).where(eq(folders.id, folderId));- if (!folder) {+ if (!folder || !(await canViewFolder(user.id, folder))) {throw new Error("Folder not found");}- if (folder.spaceId === null) {- // Personal folders are creator-only (mirrors FoldersPolicy.canEdit and- // add-videos.ts); org membership must NOT grant access to another user's- // personal folder.- if (folder.createdById !== user.id) {- throw new Error("Folder not found");- }- } else {- // getSpaceAccess returns a non-null object even for non-members (with- // both roles null), so a bare `!access` check would NOT block them.- // Require an actual org or space role to view the folder's contents.- const access = await getSpaceAccess(user.id, folder.spaceId);- if (- !access ||- (access.organizationRole === null && access.spaceRole === null)- ) {- throw new Error("Folder not found");- }- }-const isAllSpacesEntry = user.activeOrganizationId === spaceId;const rows = isAllSpacesEntrydiff --git a/apps/web/actions/organizations/get-organization-videos.ts b/apps/web/actions/organizations/get-organization-videos.tsindex 39ead8c4b..7fde37a1e 100644--- a/apps/web/actions/organizations/get-organization-videos.ts+++ b/apps/web/actions/organizations/get-organization-videos.ts@@ -21,7 +21,6 @@ export async function getOrganizationVideoIds(throw new Error("Organization ID is required");}- // Only members/owner of the organization may see its shared videos.const access = await getOrganizationAccess(user.id, organizationId);if (!access) {throw new Error("Organization not found");diff --git a/apps/web/actions/spaces/get-space-videos.ts b/apps/web/actions/spaces/get-space-videos.tsindex 5c3581907..a53dc3ac0 100644--- a/apps/web/actions/spaces/get-space-videos.ts+++ b/apps/web/actions/spaces/get-space-videos.ts@@ -22,21 +22,17 @@ export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {const isAllSpacesEntry = user.activeOrganizationId === spaceId;- // Only members/owner of the space (or organization) may see its videos.if (isAllSpacesEntry) {- const access = await getOrganizationAccess(user.id, spaceId);+ const access = await getOrganizationAccess(+ user.id,+ user.activeOrganizationId,+ );if (!access) {throw new Error("Space not found");}} else {- // getSpaceAccess returns a non-null object even for non-members (with- // both roles null), so a bare `!access` check would NOT block them.- // Require an actual org or space role to view the space's videos.const access = await getSpaceAccess(user.id, spaceId);- if (- !access ||- (access.organizationRole === null && access.spaceRole === null)- ) {+ if (!access || (!access.organizationRole && !access.spaceRole)) {throw new Error("Space not found");}}diff --git a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx b/apps/web/app/(org)/dashboard/folder/[id]/page.tsxindex 87f7a4e09..c0701a7ee 100644--- a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx+++ b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx@@ -4,7 +4,7 @@ import { makeCurrentUserLayer } from "@cap/web-backend";import { Folder } from "@cap/web-domain";import { Effect } from "effect";import { notFound } from "next/navigation";-import { getSpaceAccess } from "@/actions/organization/space-authorization";+import { canViewFolder } from "@/actions/folders/folder-access";import {getChildFolders,getFolderBreadcrumb,@@ -32,32 +32,13 @@ const FolderPage = async (props: PageProps<"/dashboard/folder/[id]">) => {const user = await getCurrentUser();if (!user || !user.activeOrganizationId) return notFound();- // Ensure the folder belongs to a space the caller can access before- // disclosing its contents (mirrors FoldersPolicy: personal folders are- // creator-only, space folders require space/org membership). A missing folder- // surfaces as notFound() rather than an unhandled 500.- const folderForAccess = await getFolderById(folderId).pipe(+ const folder = await getFolderById(folderId).pipe(Effect.provide(makeCurrentUserLayer(user)),- Effect.catchAll(() => Effect.succeed(null)),+ Effect.catchAllCause(() => Effect.succeed(null)),runPromise,);- if (!folderForAccess) return notFound();-- if (folderForAccess.spaceId === null) {- if (folderForAccess.createdById !== user.id) return notFound();- } else {- // getSpaceAccess returns a non-null object even for non-members (both roles- // null), so check for an actual role. Using space access (not org-only)- // keeps legitimate space members who aren't org members from being blocked.- const access = await getSpaceAccess(user.id, folderForAccess.spaceId);- if (- !access ||- (access.organizationRole === null && access.spaceRole === null)- ) {- return notFound();- }- }+ if (!folder || !(await canViewFolder(user.id, folder))) return notFound();return Effect.gen(function* () {const [childFolders, breadcrumb, videosData, share] = yield* Effect.all(@@ -68,7 +49,6 @@ const FolderPage = async (props: PageProps<"/dashboard/folder/[id]">) => {variant: "user",}),Effect.gen(function* () {- const folder = yield* getFolderById(folderId);// Mirrors FoldersPolicy.canEdit for personal folders: only the// creator may manage sharing; the Pro gate uses the folder's own// organization, which is what the server enforces on writes.