Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(comments): require canView access before posting a comment

Olena Ponomarovaf8c6a87a35a0
diff --git a/apps/web/actions/videos/new-comment.ts b/apps/web/actions/videos/new-comment.ts
index 6b7814a46..cc99dbd50 100644
--- a/apps/web/actions/videos/new-comment.ts
+++ b/apps/web/actions/videos/new-comment.ts
@@ -4,10 +4,13 @@ import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
import { comments } from "@cap/database/schema";
+import { provideOptionalAuth, VideosPolicy } from "@cap/web-backend";
import type { ImageUpload } from "@cap/web-domain";
-import { Comment, type Video } from "@cap/web-domain";
+import { Comment, Policy, type Video } from "@cap/web-domain";
+import { Effect, Exit } from "effect";
import { revalidatePath } from "next/cache";
import { createNotification } from "@/lib/Notification";
+import * as EffectRuntime from "@/lib/server";
export async function newComment(data: {
content: string;
@@ -37,6 +40,20 @@ export async function newComment(data: {
if (!content || !videoId) {
throw new Error("Content and videoId are required");
}
+
+ // Authentication alone isn't authorization: without this, any logged-in
+ // user could comment on someone else's private video (CVE-worthy IDOR).
+ const accessExit = await Effect.gen(function* () {
+ const videosPolicy = yield* VideosPolicy;
+ return yield* Effect.void.pipe(
+ Policy.withPublicPolicy(videosPolicy.canView(videoId)),
+ );
+ }).pipe(provideOptionalAuth, EffectRuntime.runPromiseExit);
+
+ if (Exit.isFailure(accessExit)) {
+ throw new Error("Video not found");
+ }
+
const id = Comment.CommentId.make(nanoId());
const newComment = {