Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): encrypt stored edit transcript objects

Richie McIlroy66a4cc212983
diff --git a/apps/web/__tests__/unit/edit-transcript-storage.test.ts b/apps/web/__tests__/unit/edit-transcript-storage.test.ts
new file mode 100644
index 000000000..60cb23b29
--- /dev/null
+++ b/apps/web/__tests__/unit/edit-transcript-storage.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, it, vi } from "vitest";
+
+vi.mock("@cap/env", () => ({
+ serverEnv: () => ({
+ NEXTAUTH_SECRET: "test-secret-with-enough-entropy",
+ }),
+}));
+
+vi.mock("server-only", () => ({}));
+
+describe("edit transcript storage", () => {
+ it("encrypts and authenticates transcript objects for one video", async () => {
+ const { decryptEditTranscriptObject, encryptEditTranscriptObject } =
+ await import("@/lib/edit-transcript-storage");
+ const plaintext = JSON.stringify({
+ words: [{ text: "private deleted phrase" }],
+ });
+
+ const encrypted = encryptEditTranscriptObject(
+ plaintext,
+ "owner-1",
+ "video-1",
+ );
+
+ expect(encrypted).not.toContain("private deleted phrase");
+ expect(decryptEditTranscriptObject(encrypted, "owner-1", "video-1")).toBe(
+ plaintext,
+ );
+ expect(
+ decryptEditTranscriptObject(encrypted, "owner-1", "video-2"),
+ ).toBeNull();
+ });
+
+ it("rejects modified and plaintext objects", async () => {
+ const { decryptEditTranscriptObject, encryptEditTranscriptObject } =
+ await import("@/lib/edit-transcript-storage");
+ const encrypted = encryptEditTranscriptObject(
+ '{"words":[]}',
+ "owner-1",
+ "video-1",
+ );
+ const parts = encrypted.split(".");
+ const ciphertext = parts[3] ?? "";
+ parts[3] = `${ciphertext.startsWith("A") ? "B" : "A"}${ciphertext.slice(1)}`;
+ const tampered = parts.join(".");
+
+ expect(
+ decryptEditTranscriptObject(tampered, "owner-1", "video-1"),
+ ).toBeNull();
+ expect(
+ decryptEditTranscriptObject(
+ '{"words":[{"text":"plaintext"}]}',
+ "owner-1",
+ "video-1",
+ ),
+ ).toBeNull();
+ });
+});
diff --git a/apps/web/lib/edit-transcript-storage.ts b/apps/web/lib/edit-transcript-storage.ts
new file mode 100644
index 000000000..cc62b839b
--- /dev/null
+++ b/apps/web/lib/edit-transcript-storage.ts
@@ -0,0 +1,80 @@
+import "server-only";
+
+import {
+ createCipheriv,
+ createDecipheriv,
+ createHmac,
+ randomBytes,
+} from "node:crypto";
+import { serverEnv } from "@cap/env";
+
+const FORMAT_VERSION = "cap-edit-transcript-v1";
+const IV_BYTES = 12;
+const AUTH_TAG_BYTES = 16;
+
+function getEncryptionKey() {
+ return createHmac("sha256", serverEnv().NEXTAUTH_SECRET)
+ .update(FORMAT_VERSION)
+ .digest();
+}
+
+function getAdditionalData(ownerId: string, videoId: string) {
+ return Buffer.from(`${FORMAT_VERSION}:${ownerId}:${videoId}`);
+}
+
+export function encryptEditTranscriptObject(
+ value: string,
+ ownerId: string,
+ videoId: string,
+) {
+ const iv = randomBytes(IV_BYTES);
+ const cipher = createCipheriv("aes-256-gcm", getEncryptionKey(), iv);
+ cipher.setAAD(getAdditionalData(ownerId, videoId));
+ const ciphertext = Buffer.concat([
+ cipher.update(value, "utf8"),
+ cipher.final(),
+ ]);
+
+ return [
+ FORMAT_VERSION,
+ iv.toString("base64url"),
+ cipher.getAuthTag().toString("base64url"),
+ ciphertext.toString("base64url"),
+ ].join(".");
+}
+
+export function decryptEditTranscriptObject(
+ value: string,
+ ownerId: string,
+ videoId: string,
+) {
+ const [version, encodedIv, encodedTag, encodedCiphertext, extra] =
+ value.split(".");
+ if (
+ version !== FORMAT_VERSION ||
+ !encodedIv ||
+ !encodedTag ||
+ !encodedCiphertext ||
+ extra !== undefined
+ ) {
+ return null;
+ }
+
+ try {
+ const iv = Buffer.from(encodedIv, "base64url");
+ const authTag = Buffer.from(encodedTag, "base64url");
+ if (iv.length !== IV_BYTES || authTag.length !== AUTH_TAG_BYTES) {
+ return null;
+ }
+
+ const decipher = createDecipheriv("aes-256-gcm", getEncryptionKey(), iv);
+ decipher.setAAD(getAdditionalData(ownerId, videoId));
+ decipher.setAuthTag(authTag);
+ return Buffer.concat([
+ decipher.update(Buffer.from(encodedCiphertext, "base64url")),
+ decipher.final(),
+ ]).toString("utf8");
+ } catch {
+ return null;
+ }
+}