Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): gate thumbnail and view-count endpoints and tighten CORS
Richie McIlroy2a624e0f2433
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.tsindex 178741e6b..d3629ed33 100644--- a/apps/web/app/api/analytics/route.ts+++ b/apps/web/app/api/analytics/route.ts@@ -1,5 +1,12 @@+import { db } from "@cap/database";+import { videos } from "@cap/database/schema";+import { provideOptionalAuth, VideosPolicy } from "@cap/web-backend";+import { Policy, Video } from "@cap/web-domain";+import { eq } from "drizzle-orm";+import { Effect, Exit } from "effect";import type { NextRequest } from "next/server";import { getVideoAnalytics } from "@/actions/videos/get-analytics";+import * as EffectRuntime from "@/lib/server";const parseRangeParam = (value: string | null) => {if (!value) return undefined;@@ -26,6 +33,22 @@ export async function GET(request: NextRequest) {return Response.json({ error: "Video ID is required" }, { status: 400 });}+ const id = Video.VideoId.make(videoId);++ // Gate on canView so view counts of private / password-protected videos are+ // not disclosed to unauthorized callers. Public videos, owners, org/space+ // members and password-protected videos with a valid cookie still pass.+ const exit = await Effect.gen(function* () {+ const videosPolicy = yield* VideosPolicy;+ return yield* Effect.promise(() =>+ db().select({ id: videos.id }).from(videos).where(eq(videos.id, id)),+ ).pipe(Policy.withPublicPolicy(videosPolicy.canView(id)));+ }).pipe(provideOptionalAuth, EffectRuntime.runPromiseExit);++ if (Exit.isFailure(exit) || !exit.value[0]) {+ return Response.json({ error: "Video not found" }, { status: 404 });+ }+try {console.log("videoId", videoId);const result = await getVideoAnalytics(videoId, { rangeDays });diff --git a/apps/web/app/api/thumbnail/route.ts b/apps/web/app/api/thumbnail/route.tsindex a42e5c8fe..470612b30 100644--- a/apps/web/app/api/thumbnail/route.ts+++ b/apps/web/app/api/thumbnail/route.ts@@ -1,9 +1,11 @@import { db } from "@cap/database";import { videos } from "@cap/database/schema";-import { Storage } from "@cap/web-backend";-import { Video } from "@cap/web-domain";+import { provideOptionalAuth, Storage, VideosPolicy } from "@cap/web-backend";+import { Policy, Video } from "@cap/web-domain";import { eq } from "drizzle-orm";+import { Effect, Exit } from "effect";import type { NextRequest } from "next/server";+import * as EffectRuntime from "@/lib/server";import { runPromise } from "@/lib/server";import { decodeStorageVideo } from "@/lib/video-storage";import { getHeaders } from "@/utils/helpers";@@ -25,10 +27,28 @@ export async function GET(request: NextRequest) {},);- const [query] = await db()- .select()- .from(videos)- .where(eq(videos.id, Video.VideoId.make(videoId)));+ const id = Video.VideoId.make(videoId);++ // Gate on canView so private / password-protected videos' thumbnails are not+ // exposed to unauthorized callers (owner, org/space members, public videos+ // and password-protected videos with a valid cookie still pass).+ const exit = await Effect.gen(function* () {+ const videosPolicy = yield* VideosPolicy;+ return yield* Effect.promise(() =>+ db().select().from(videos).where(eq(videos.id, id)),+ ).pipe(Policy.withPublicPolicy(videosPolicy.canView(id)));+ }).pipe(provideOptionalAuth, EffectRuntime.runPromiseExit);++ if (Exit.isFailure(exit))+ return new Response(+ JSON.stringify({ error: true, message: "Video not found" }),+ {+ status: 404,+ headers: getHeaders(origin),+ },+ );++ const [query] = exit.value;if (!query)return new Response(diff --git a/apps/web/utils/helpers.ts b/apps/web/utils/helpers.tsindex 6433d1105..b92978b06 100644--- a/apps/web/utils/helpers.ts+++ b/apps/web/utils/helpers.ts@@ -1,4 +1,4 @@-import { buildEnv } from "@cap/env";+import { buildEnv, serverEnv } from "@cap/env";import { type ClassValue, clsx } from "clsx";import type { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";import type { NextRequest } from "next/server";@@ -15,11 +15,44 @@ export const allowedOrigins = ["cap.link",];-export function getHeaders(origin: string | null) {- // Allow "*" for custom domains or allowedOrigins for main domains+// Origins that are trusted to make credentialed (cookie-bearing) cross-origin+// reads. Only these may receive `Access-Control-Allow-Credentials: true` with a+// reflected origin. Any other origin gets a wildcard, credential-less response+// so a malicious site cannot read authenticated responses cross-origin.+function isTrustedCredentialedOrigin(origin: string) {+ let host: string;+ try {+ host = new URL(origin).hostname;+ } catch {+ return false;+ }++ if (host === "localhost" || host === "cap.so" || host === "cap.link")+ return true;+ if (host.endsWith(".cap.so")) return true;++ try {+ if (host === new URL(serverEnv().WEB_URL).hostname) return true;+ } catch {}++ return false;+}++export function getHeaders(origin: string | null): Record<string, string> {+ // Only reflect the specific origin + allow credentials for trusted origins.+ // Everyone else gets a wildcard with NO credentials so authenticated+ // responses can't be read cross-origin (credential-read CSRF).+ if (origin && isTrustedCredentialedOrigin(origin)) {+ return {+ "Access-Control-Allow-Origin": origin,+ "Access-Control-Allow-Credentials": "true",+ "Access-Control-Allow-Methods": "GET, OPTIONS",+ "Access-Control-Allow-Headers": "Content-Type, Authorization",+ };+ }+return {- "Access-Control-Allow-Origin": origin || "*",- "Access-Control-Allow-Credentials": "true",+ "Access-Control-Allow-Origin": "*","Access-Control-Allow-Methods": "GET, OPTIONS","Access-Control-Allow-Headers": "Content-Type, Authorization",};