Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: only list a space's videos to people who can open the space

Paddyea89d4412cfc
diff --git a/apps/web/__tests__/unit/folder-access.test.ts b/apps/web/__tests__/unit/folder-access.test.ts
index eff4d9f69..1a112e1e9 100644
--- a/apps/web/__tests__/unit/folder-access.test.ts
+++ b/apps/web/__tests__/unit/folder-access.test.ts
@@ -54,7 +54,7 @@ describe("canViewFolder", () => {
expect(mocks.spaceAccess).not.toHaveBeenCalled();
});
- it("requires an organization or space role for space folders", async () => {
+ it("requires a space role or organization admin for space folders", async () => {
const folder = { organizationId, spaceId, createdById: otherUserId };
mocks.spaceAccess.mockResolvedValueOnce({
@@ -82,6 +82,22 @@ describe("canViewFolder", () => {
await expect(canViewFolder(userId, folder)).resolves.toBe(false);
});
+ it("rejects an organization member who is not in the space", async () => {
+ mocks.spaceAccess.mockResolvedValueOnce({
+ organizationId,
+ organizationRole: "member",
+ spaceRole: null,
+ });
+
+ await expect(
+ canViewFolder(userId, {
+ organizationId,
+ spaceId,
+ createdById: otherUserId,
+ }),
+ ).resolves.toBe(false);
+ });
+
it("rejects space folders whose space belongs to another organization", async () => {
mocks.spaceAccess.mockResolvedValueOnce({
organizationId: Organisation.OrganisationId.make("org-2"),
diff --git a/apps/web/__tests__/unit/get-space-video-ids.test.ts b/apps/web/__tests__/unit/get-space-video-ids.test.ts
new file mode 100644
index 000000000..50c69754a
--- /dev/null
+++ b/apps/web/__tests__/unit/get-space-video-ids.test.ts
@@ -0,0 +1,104 @@
+import { Organisation, Space } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { getSpaceVideoIds } from "@/actions/spaces/get-space-videos";
+
+const mocks = vi.hoisted(() => ({
+ query: vi.fn(),
+ user: vi.fn(),
+ organizationAccess: vi.fn(),
+ spaceAccess: vi.fn(),
+}));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ return { db: () => drizzle(mocks.query) };
+});
+vi.mock("@cap/database/auth/session", () => ({ getCurrentUser: mocks.user }));
+vi.mock("@/actions/organization/authorization", () => ({
+ getOrganizationAccess: mocks.organizationAccess,
+}));
+vi.mock("@/actions/organization/space-authorization", () => ({
+ getSpaceAccess: mocks.spaceAccess,
+}));
+
+const orgId = Organisation.OrganisationId.make("org-1");
+const spaceId = Space.SpaceId.make("space-1");
+
+const spaceAccess = (
+ organizationRole: "owner" | "admin" | "member" | null,
+ spaceRole: "admin" | "member" | null,
+) => ({
+ spaceId,
+ organizationId: orgId,
+ organizationOwnerId: "owner",
+ createdById: "creator",
+ organizationRole,
+ spaceRole,
+ canManage: false,
+});
+
+beforeEach(() => {
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ mocks.user.mockResolvedValue({ id: "user-1", activeOrganizationId: orgId });
+ mocks.query.mockResolvedValue({ rows: [["video-1"], ["video-2"]] });
+});
+
+describe("getSpaceVideoIds for a space", () => {
+ it("lists the videos for a member of the space", async () => {
+ mocks.spaceAccess.mockResolvedValue(spaceAccess("member", "member"));
+
+ await expect(getSpaceVideoIds(spaceId)).resolves.toEqual({
+ success: true,
+ data: ["video-1", "video-2"],
+ });
+ });
+
+ it("lists the videos for an organization admin outside the space", async () => {
+ mocks.spaceAccess.mockResolvedValue(spaceAccess("admin", null));
+
+ await expect(getSpaceVideoIds(spaceId)).resolves.toMatchObject({
+ success: true,
+ });
+ });
+
+ // The space page only opens a space for its members and for organization
+ // admins and owners (Spaces.getSpaceOrOrg), so this action must not list
+ // the videos of a space to anyone else in the organization.
+ it("rejects an organization member who is not in the space", async () => {
+ mocks.spaceAccess.mockResolvedValue(spaceAccess("member", null));
+
+ await expect(getSpaceVideoIds(spaceId)).resolves.toMatchObject({
+ success: false,
+ });
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+
+ it("rejects a caller with no access to the space", async () => {
+ mocks.spaceAccess.mockResolvedValue(null);
+
+ await expect(getSpaceVideoIds(spaceId)).resolves.toMatchObject({
+ success: false,
+ });
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+});
+
+describe("getSpaceVideoIds for the organization-wide entry", () => {
+ it("lists the videos for any organization member", async () => {
+ mocks.organizationAccess.mockResolvedValue({ role: "member" });
+
+ await expect(getSpaceVideoIds(orgId)).resolves.toMatchObject({
+ success: true,
+ });
+ expect(mocks.spaceAccess).not.toHaveBeenCalled();
+ });
+
+ it("rejects a caller outside the organization", async () => {
+ mocks.organizationAccess.mockResolvedValue(null);
+
+ await expect(getSpaceVideoIds(orgId)).resolves.toMatchObject({
+ success: false,
+ });
+ expect(mocks.query).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/actions/folders/folder-access.ts b/apps/web/actions/folders/folder-access.ts
index ce33352fc..fe871a29a 100644
--- a/apps/web/actions/folders/folder-access.ts
+++ b/apps/web/actions/folders/folder-access.ts
@@ -1,6 +1,7 @@
import type { Organisation, Space, User } from "@cap/web-domain";
import { getOrganizationAccess } from "@/actions/organization/authorization";
import { getSpaceAccess } from "@/actions/organization/space-authorization";
+import { canManageSpace } from "@/lib/permissions/roles";
export async function canViewFolder(
userId: User.UserId,
@@ -22,6 +23,6 @@ export async function canViewFolder(
return Boolean(
access &&
access.organizationId === folder.organizationId &&
- (access.organizationRole || access.spaceRole),
+ (access.spaceRole || canManageSpace(access)),
);
}
diff --git a/apps/web/actions/spaces/get-space-videos.ts b/apps/web/actions/spaces/get-space-videos.ts
index a53dc3ac0..24e18efc2 100644
--- a/apps/web/actions/spaces/get-space-videos.ts
+++ b/apps/web/actions/spaces/get-space-videos.ts
@@ -7,6 +7,7 @@ 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";
+import { canManageSpace } from "@/lib/permissions/roles";
export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
try {
@@ -32,7 +33,7 @@ export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
}
} else {
const access = await getSpaceAccess(user.id, spaceId);
- if (!access || (!access.organizationRole && !access.spaceRole)) {
+ if (!access || (!access.spaceRole && !canManageSpace(access))) {
throw new Error("Space not found");
}
}