Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): add ownership checks to folder, space and org video actions

Richie McIlroyd2348b1647b1
diff --git a/apps/web/actions/folders/add-videos.ts b/apps/web/actions/folders/add-videos.ts
index 817da6191..debaeaa41 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 { getSpaceAccess } from "@/actions/organization/space-authorization";
export async function addVideosToFolder(
folderId: Folder.FolderId,
@@ -30,7 +31,11 @@ export async function addVideosToFolder(
}
const [folder] = await db()
- .select({ id: folders.id, spaceId: folders.spaceId })
+ .select({
+ id: folders.id,
+ spaceId: folders.spaceId,
+ createdById: folders.createdById,
+ })
.from(folders)
.where(eq(folders.id, folderId));
@@ -38,6 +43,20 @@ export async function addVideosToFolder(
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) {
+ throw new Error("You don't have permission to manage this folder");
+ }
+ } else {
+ const access = await getSpaceAccess(user.id, folder.spaceId);
+ if (!access?.canManage) {
+ throw new Error("You don't have permission to manage this folder");
+ }
+ }
+
const userVideos = await db()
.select({ id: videos.id })
.from(videos)
diff --git a/apps/web/actions/folders/get-folder-videos.ts b/apps/web/actions/folders/get-folder-videos.ts
index 704f6aeb2..84e02b019 100644
--- a/apps/web/actions/folders/get-folder-videos.ts
+++ b/apps/web/actions/folders/get-folder-videos.ts
@@ -2,9 +2,11 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
-import { sharedVideos, spaceVideos } from "@cap/database/schema";
+import { folders, sharedVideos, spaceVideos } from "@cap/database/schema";
import type { Folder, Space, Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
export async function getFolderVideoIds(
folderId: Folder.FolderId,
@@ -21,6 +23,41 @@ 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,
+ createdById: folders.createdById,
+ })
+ .from(folders)
+ .where(eq(folders.id, folderId));
+
+ if (!folder) {
+ throw new Error("Folder not found");
+ }
+
+ if (folder.spaceId === null) {
+ const access = await getOrganizationAccess(
+ user.id,
+ folder.organizationId,
+ );
+ if (!access && 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 = isAllSpacesEntry
diff --git a/apps/web/actions/folders/moveVideoToFolder.ts b/apps/web/actions/folders/moveVideoToFolder.ts
index 59d8de51d..47424b72e 100644
--- a/apps/web/actions/folders/moveVideoToFolder.ts
+++ b/apps/web/actions/folders/moveVideoToFolder.ts
@@ -11,6 +11,8 @@ import {
import type { Folder, Space, Video } from "@cap/web-domain";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
+import { requireOrganizationSettingsManager } from "@/actions/organization/authorization";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
export async function moveVideoToFolder({
videoId,
folderId,
@@ -54,6 +56,11 @@ export async function moveVideoToFolder({
}
if (spaceId && !isAllSpacesEntry) {
+ const access = await getSpaceAccess(user.id, spaceId);
+ if (!access?.canManage) {
+ throw new Error("You don't have permission to manage this space");
+ }
+
await db()
.update(spaceVideos)
.set({
@@ -63,6 +70,11 @@ export async function moveVideoToFolder({
and(eq(spaceVideos.videoId, videoId), eq(spaceVideos.spaceId, spaceId)),
);
} else if (spaceId && isAllSpacesEntry) {
+ await requireOrganizationSettingsManager(
+ user.id,
+ user.activeOrganizationId,
+ );
+
await db()
.update(sharedVideos)
.set({
@@ -80,7 +92,7 @@ export async function moveVideoToFolder({
.set({
folderId: folderId === null ? null : folderId,
})
- .where(eq(videos.id, videoId));
+ .where(and(eq(videos.id, videoId), eq(videos.ownerId, user.id)));
}
// Always revalidate the main caps page
diff --git a/apps/web/actions/organizations/get-organization-videos.ts b/apps/web/actions/organizations/get-organization-videos.ts
index 2e584388f..39ead8c4b 100644
--- a/apps/web/actions/organizations/get-organization-videos.ts
+++ b/apps/web/actions/organizations/get-organization-videos.ts
@@ -5,6 +5,7 @@ import { getCurrentUser } from "@cap/database/auth/session";
import { sharedVideos } from "@cap/database/schema";
import type { Organisation } from "@cap/web-domain";
import { and, eq, isNull } from "drizzle-orm";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
export async function getOrganizationVideoIds(
organizationId: Organisation.OrganisationId,
@@ -20,6 +21,12 @@ 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");
+ }
+
const videoIds = await db()
.select({
videoId: sharedVideos.videoId,
diff --git a/apps/web/actions/spaces/get-space-videos.ts b/apps/web/actions/spaces/get-space-videos.ts
index ddbcc42b8..5c3581907 100644
--- a/apps/web/actions/spaces/get-space-videos.ts
+++ b/apps/web/actions/spaces/get-space-videos.ts
@@ -5,6 +5,8 @@ import { getCurrentUser } from "@cap/database/auth/session";
import { sharedVideos, spaceVideos } from "@cap/database/schema";
import type { Space } from "@cap/web-domain";
import { and, eq, isNull } from "drizzle-orm";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
+import { getSpaceAccess } from "@/actions/organization/space-authorization";
export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
try {
@@ -20,6 +22,25 @@ 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);
+ 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)
+ ) {
+ throw new Error("Space not found");
+ }
+ }
+
const videoIds = isAllSpacesEntry
? await db()
.select({
diff --git a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
index 249507304..0601cc08c 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
@@ -4,6 +4,7 @@ import { makeCurrentUserLayer } from "@cap/web-backend";
import { Folder } from "@cap/web-domain";
import { Effect } from "effect";
import { notFound } from "next/navigation";
+import { getOrganizationAccess } from "@/actions/organization/authorization";
import {
getChildFolders,
getFolderBreadcrumb,
@@ -31,6 +32,24 @@ 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/org the caller can access before
+ // disclosing its contents (mirrors FoldersPolicy: personal folders are
+ // creator-only, space/org folders require org membership/ownership).
+ const folderForAccess = await getFolderById(folderId).pipe(
+ Effect.provide(makeCurrentUserLayer(user)),
+ runPromise,
+ );
+
+ if (folderForAccess.spaceId === null) {
+ if (folderForAccess.createdById !== user.id) return notFound();
+ } else {
+ const access = await getOrganizationAccess(
+ user.id,
+ folderForAccess.organizationId,
+ );
+ if (!access) return notFound();
+ }
+
return Effect.gen(function* () {
const [childFolders, breadcrumb, videosData, share] = yield* Effect.all(
[