Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

10.9 KB
Raw Blame
import { RecordingObjectReadError } from "@cap/web-backend/src/Storage/recording-object-identity";
import { Storage as StorageDomain } from "@cap/web-domain";
import { Effect } from "effect";
import { NextRequest } from "next/server";
import { beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
	head: vi.fn(),
	read: vi.fn(),
	download: vi.fn(),
	token: null as { videoId: string; key: string } | null,
	video: {
		id: "video",
		ownerId: "owner",
		source: { type: "desktopMP4" } as {
			type: string;
			outputKey?: string;
			audioLevelSourceKey?: string;
			audioLevelOutputKey?: string;
			thumbnailKey?: string;
			previewKey?: string;
		},
	},
}));

vi.mock("@cap/web-backend", async () => {
	const { Effect, Option } = await import("effect");
	return {
		provideOptionalAuth: (effect: unknown) => effect,
		Storage: {
			getAccessForVideo: () =>
				Effect.succeed([
					{
						headObject: mocks.head,
						getObjectResponse: mocks.read,
						getInternalDownload: mocks.download,
					},
				]),
		},
		Videos: Effect.succeed({
			getByIdForViewing: () => Effect.succeed(Option.some([mocks.video])),
		}),
		VideosRepo: Effect.succeed({
			getById: () => Effect.succeed(Option.some([mocks.video])),
		}),
		verifyStorageObjectToken: () => mocks.token,
	};
});
vi.mock("@cap/env", () => ({
	serverEnv: () => ({ MEDIA_SERVER_WEBHOOK_SECRET: "test-media-secret" }),
}));
vi.mock("@/lib/server", async () => {
	const { Effect } = await import("effect");
	return { runPromise: Effect.runPromise };
});
vi.mock("@/utils/helpers", () => ({ CACHE_CONTROL_HEADERS: {} }));

import { GET, HEAD } from "@/app/api/storage/object/route";

function request(
	headers: Record<string, string> = {},
	key = "owner/video/result.mp4",
) {
	return GET(
		new NextRequest(
			`https://cap.test/api/storage/object?videoId=video&key=${encodeURIComponent(key)}&token=test`,
			{ headers },
		),
	);
}

describe("recording verification object reads", () => {
	beforeEach(() => {
		mocks.token = { videoId: "video", key: "owner/video/result.mp4" };
		mocks.video.source = { type: "desktopMP4" };
		mocks.head.mockReturnValue(
			Effect.succeed({ ETag: '"drive-file:42"', ContentLength: 100 }),
		);
		mocks.read.mockImplementation(() =>
			Effect.succeed(
				new Response("data", {
					status: 206,
					headers: { "Content-Range": "bytes 0-3/100" },
				}),
			),
		);
	});

	it("does not expose Drive credentials to ordinary clients", async () => {
		expect((await request({ "x-cap-internal-download": "1" })).status).toBe(
			401,
		);
		expect(
			(
				await request({
					"x-cap-internal-download": "1",
					"x-media-server-secret": "invalid",
				})
			).status,
		).toBe(401);
		expect(mocks.download).not.toHaveBeenCalled();
	});

	it.each(["if-match", "x-cap-recording-object-identity"])(
		"authorizes an internal descriptor using %s without downloading media",
		async (identityHeader) => {
			mocks.download.mockReturnValue(
				Effect.succeed({
					version: 1,
					url: "https://www.googleapis.com/drive/v3/files/file/revisions/revision?alt=media",
				}),
			);
			const response = await request({
				"x-cap-internal-download": "1",
				"x-media-server-secret": "test-media-secret",
				[identityHeader]: '"identity"',
			});
			expect(response.status).toBe(200);
			expect(response.headers.get("Cache-Control")).toBe("private, no-store");
			expect(mocks.download).toHaveBeenCalledWith(
				"owner/video/result.mp4",
				expect.objectContaining({ objectIdentity: '"identity"' }),
			);
			expect(mocks.read).not.toHaveBeenCalled();
		},
	);

	it("does not add metadata requests to ordinary playback", async () => {
		const response = await request();
		expect(response.status).toBe(206);
		expect(mocks.head).not.toHaveBeenCalled();
	});

	it("cancels ordinary upstream downloads when the client disconnects", async () => {
		const controller = new AbortController();
		const req = new NextRequest(
			"https://cap.test/api/storage/object?videoId=video&key=owner/video/result.mp4&token=test",
			{ signal: controller.signal },
		);
		await GET(req);
		expect(mocks.read).toHaveBeenCalledWith("owner/video/result.mp4", null, {
			signal: req.signal,
		});
		controller.abort();
		expect(mocks.read.mock.calls.at(-1)?.[2].signal.aborted).toBe(true);
	});

	it("answers ordinary HEAD requests without downloading the video", async () => {
		const response = await HEAD(
			new NextRequest(
				"https://cap.test/api/storage/object?videoId=video&key=owner/video/result.mp4&token=test",
				{ method: "HEAD" },
			),
		);
		expect(response.status).toBe(200);
		expect(response.headers.get("Content-Length")).toBe("100");
		expect(await response.text()).toBe("");
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it("serves content-bound HEAD without downloading media", async () => {
		const identity = `"cap-drive-content-v1:${"a".repeat(64)}"`;
		mocks.head.mockReturnValue(
			Effect.succeed({
				ETag: '"drive-file:6"',
				RecordingContentETag: identity,
				ContentLength: 100,
			}),
		);
		const response = await HEAD(
			new NextRequest(
				"https://cap.test/api/storage/object?videoId=video&key=owner/video/result.mp4&token=test",
				{
					method: "HEAD",
					headers: {
						"X-Cap-Recording-Verification": "1",
						"If-Match": identity,
					},
				},
			),
		);
		expect(response.status).toBe(200);
		expect(response.headers.get("ETag")).toBe(identity);
		expect(response.headers.get("Content-Length")).toBe("100");
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it("passes content identity and cancellation through a verification range read", async () => {
		const identity = `"cap-drive-content-v1:${"a".repeat(64)}"`;
		mocks.head.mockReturnValue(
			Effect.succeed({
				ETag: '"drive-file:6"',
				RecordingContentETag: identity,
				ContentLength: 100,
			}),
		);
		const response = await request({
			"X-Cap-Recording-Verification": "1",
			"If-Match": identity,
			Range: "bytes=0-3",
		});
		expect(response.headers.get("ETag")).toBe(identity);
		expect(mocks.read).toHaveBeenCalledWith(
			"owner/video/result.mp4",
			"bytes=0-3",
			{ objectIdentity: identity, signal: expect.any(AbortSignal) },
		);
	});

	it("keeps an old expected version strict when content identity is also available", async () => {
		mocks.head.mockReturnValue(
			Effect.succeed({
				ETag: '"drive-file:6"',
				RecordingContentETag: `"cap-drive-content-v1:${"a".repeat(64)}"`,
				ContentLength: 100,
			}),
		);
		const response = await request({
			"X-Cap-Recording-Verification": "1",
			"If-Match": '"drive-file:1"',
		});
		expect(response.status).toBe(412);
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it("does not fall back to a version tag when new content proof is unavailable", async () => {
		mocks.head.mockReturnValue(
			Effect.succeed({
				ETag: '"drive-file:6"',
				RecordingContentETag: null,
				ContentLength: 100,
			}),
		);
		expect(
			(await request({ "X-Cap-Recording-Verification": "1" })).status,
		).toBe(503);
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it.each([412, 503] as const)(
		"returns a pinned read failure %s without serving bytes",
		async (status) => {
			mocks.read.mockReturnValue(
				Effect.fail(
					new StorageDomain.StorageError({
						cause: new RecordingObjectReadError(
							status,
							"Pinned revision unavailable",
							{ cause: new Error("Google Drive request failed: 404") },
						),
					}),
				),
			);
			expect(
				(await request({ "X-Cap-Recording-Verification": "1" })).status,
			).toBe(status);
		},
	);

	it("returns the provider version with a verification read", async () => {
		const response = await request({
			"X-Cap-Recording-Verification": "1",
			Range: "bytes=0-3",
		});
		expect(response.headers.get("ETag")).toBe('"drive-file:42"');
		expect(await response.text()).toBe("data");
		expect(mocks.read).toHaveBeenCalledWith(
			"owner/video/result.mp4",
			"bytes=0-3",
			{ objectIdentity: '"drive-file:42"', signal: expect.any(AbortSignal) },
		);
	});

	it("refuses a replaced object before streaming its bytes", async () => {
		const response = await request({
			"X-Cap-Recording-Verification": "1",
			"If-Match": '"drive-file:41"',
		});
		expect(response.status).toBe(412);
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it("refuses verification when no stable provider version exists", async () => {
		mocks.head.mockReturnValue(Effect.succeed({ ContentLength: 100 }));
		const response = await request({ "X-Cap-Recording-Verification": "1" });
		expect(response.status).toBe(503);
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it.each([
		"owner/video/.recording/sources/generation/snapshot/inventory.json",
		"owner/video/.recording/sources/generation/snapshot/video/0.mp4",
		"owner/video/.recording/outputs/generation/unpublished.mp4",
	])(
		"does not expose retained or unpublished data to ordinary viewers: %s",
		async (key) => {
			mocks.token = null;
			const response = await request({}, key);
			expect(response.status).toBe(404);
			expect(mocks.read).not.toHaveBeenCalled();
		},
	);

	it("allows a server-signed read of the exact retained source object", async () => {
		const key =
			"owner/video/.recording/sources/generation/snapshot/video/0.mp4";
		mocks.token = { videoId: "video", key };
		const response = await request(
			{ "X-Cap-Recording-Verification": "1", "If-Match": '"drive-file:42"' },
			key,
		);
		expect(response.status).toBe(206);
		expect(mocks.read).toHaveBeenCalledWith(key, null, {
			objectIdentity: '"drive-file:42"',
			signal: expect.any(AbortSignal),
		});
	});

	it("does not authorize another retained object using a valid token", async () => {
		mocks.token = {
			videoId: "video",
			key: "owner/video/.recording/sources/generation/snapshot/video/0.mp4",
		};
		const response = await request(
			{},
			"owner/video/.recording/sources/generation/snapshot/audio/0.mp4",
		);
		expect(response.status).toBe(404);
		expect(mocks.read).not.toHaveBeenCalled();
	});

	it.each(["desktopMP4", "webMP4"])(
		"allows only a %s audio derivative bound to the current original",
		async (type) => {
			const source =
				type === "webMP4"
					? "owner/video/result.mp4"
					: "owner/video/.recording/outputs/generation/original.mp4";
			const key =
				"owner/video/.recording/outputs/audio-quality-v3/published.mp4";
			mocks.token = null;
			mocks.video.source = {
				type,
				outputKey: source,
				audioLevelSourceKey: source,
				audioLevelOutputKey: key,
			};
			expect((await request({}, key)).status).toBe(206);
			mocks.video.source.audioLevelSourceKey = "stale";
			expect((await request({}, key)).status).toBe(404);
		},
	);

	it.each(["outputKey", "thumbnailKey", "previewKey"] as const)(
		"allows viewers to read only the published %s",
		async (field) => {
			const key = "owner/video/.recording/outputs/generation/published.mp4";
			mocks.token = null;
			mocks.video.source = { type: "desktopMP4", [field]: key };
			const response = await request({}, key);
			expect(response.status).toBe(206);
			expect(mocks.read).toHaveBeenCalledWith(key, null, {
				signal: expect.any(AbortSignal),
			});
		},
	);
});