Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: preserve edit ownership across MySQL JSON key ordering

Richie McIlroy0c388ec8af15
diff --git a/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts b/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts
index e3a417ef4..ee20daa6d 100644
--- a/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-output-replacement.test.ts
@@ -96,6 +96,7 @@ let video: {
};
let events: string[];
let updates: Record<string, unknown>[];
+let reorderSourceKeys: boolean;
const operation = {
token: "11111111-1111-4111-8111-111111111111",
startedAt: "2026-09-08T12:00:00.000Z",
@@ -139,6 +140,11 @@ function createClient() {
if (table.table === "videos") {
updates.push(values);
Object.assign(video, values);
+ if (values.source && reorderSourceKeys) {
+ video.source = Object.fromEntries(
+ Object.entries(video.source).reverse(),
+ ) as typeof video.source;
+ }
}
return [{ affectedRows: 1 }];
},
@@ -171,6 +177,7 @@ function createClient() {
}
beforeEach(() => {
+ reorderSourceKeys = false;
video = {
id: "video",
ownerId: "user",
@@ -270,49 +277,53 @@ describe("edited recording publication", () => {
);
});
- it("publishes a completed edit from its own output and retires old upload proof atomically", async () => {
- video.metadata.editProcessing = {
- ...operation,
- ownerId: video.ownerId,
- bucket: video.bucket,
- storageIntegrationId: video.storageIntegrationId,
- sourceKey,
- source: JSON.stringify(video.source),
- dispatch: "accepted",
- };
- await saveEditResultAndComplete(
- "video",
- "user/video/edit-original.mp4",
- editSpec,
- editSpec,
- metadata,
- operation,
- );
- expect(video.source).toEqual({
- type: "desktopMP4",
- outputKey: `user/video/.recording/outputs/edit-${operation.token}/result.mp4`,
- thumbnailKey: `user/video/.recording/outputs/edit-${operation.token}/thumbnail.jpg`,
- previewKey: `user/video/.recording/outputs/edit-${operation.token}/preview.gif`,
- });
- expect(video.metadata).not.toHaveProperty("desktopRecordingUpload");
- expect(video.metadata.customCreatedAt).toBe("2020-01-01T00:00:00Z");
- expect(events.indexOf("lock-job")).toBeLessThan(
- events.indexOf("lock-video"),
- );
- expect(events.indexOf("lock-video")).toBeLessThan(
- events.indexOf("update-videos"),
- );
- const before = events.length;
- await saveEditResultAndComplete(
- "video",
- sourceKey,
- editSpec,
- editSpec,
- metadata,
- operation,
- );
- expect(events).toHaveLength(before);
- });
+ it.each([false, true])(
+ "publishes a completed edit atomically with reordered JSON keys: %s",
+ async (reorder) => {
+ reorderSourceKeys = reorder;
+ video.metadata.editProcessing = {
+ ...operation,
+ ownerId: video.ownerId,
+ bucket: video.bucket,
+ storageIntegrationId: video.storageIntegrationId,
+ sourceKey,
+ source: JSON.stringify(video.source),
+ dispatch: "accepted",
+ };
+ await saveEditResultAndComplete(
+ "video",
+ "user/video/edit-original.mp4",
+ editSpec,
+ editSpec,
+ metadata,
+ operation,
+ );
+ expect(video.source).toEqual({
+ type: "desktopMP4",
+ outputKey: `user/video/.recording/outputs/edit-${operation.token}/result.mp4`,
+ thumbnailKey: `user/video/.recording/outputs/edit-${operation.token}/thumbnail.jpg`,
+ previewKey: `user/video/.recording/outputs/edit-${operation.token}/preview.gif`,
+ });
+ expect(video.metadata).not.toHaveProperty("desktopRecordingUpload");
+ expect(video.metadata.customCreatedAt).toBe("2020-01-01T00:00:00Z");
+ expect(events.indexOf("lock-job")).toBeLessThan(
+ events.indexOf("lock-video"),
+ );
+ expect(events.indexOf("lock-video")).toBeLessThan(
+ events.indexOf("update-videos"),
+ );
+ const before = events.length;
+ await saveEditResultAndComplete(
+ "video",
+ sourceKey,
+ editSpec,
+ editSpec,
+ metadata,
+ operation,
+ );
+ expect(events).toHaveLength(before);
+ },
+ );
it("checks a reprocessed canonical object before clearing its previous immutable publication", async () => {
await saveMetadataAndComplete("video", metadata);
diff --git a/apps/web/__tests__/unit/video-edit-operation.test.ts b/apps/web/__tests__/unit/video-edit-operation.test.ts
index 9900d434c..ede6e3186 100644
--- a/apps/web/__tests__/unit/video-edit-operation.test.ts
+++ b/apps/web/__tests__/unit/video-edit-operation.test.ts
@@ -96,6 +96,41 @@ beforeEach(() => {
});
describe("edit operation ownership", () => {
+ it("matches reordered source fields but rejects changed or malformed sources", () => {
+ const output = getEditOutputKeys("owner", "video", operation);
+ const published = { ...source, ...output };
+ const video = {
+ ...storage,
+ source: {
+ ...source,
+ previewKey: output.previewKey,
+ thumbnailKey: output.thumbnailKey,
+ outputKey: output.outputKey,
+ },
+ metadata: {
+ editProcessing: { ...state, source: JSON.stringify(published) },
+ },
+ };
+ const upload = {
+ rawFileKey: sourceKey,
+ startedAt: new Date(operation.startedAt),
+ };
+ expect(matchesEditOperation(video, upload, sourceKey, operation)).toBe(
+ true,
+ );
+ expect(
+ matchesEditOperation(
+ { ...video, source: { ...video.source, outputKey: "different.mp4" } },
+ upload,
+ sourceKey,
+ operation,
+ ),
+ ).toBe(false);
+ video.metadata.editProcessing.source = "invalid JSON";
+ expect(matchesEditOperation(video, upload, sourceKey, operation)).toBe(
+ false,
+ );
+ });
it("matches both the original source and the precise operation", () => {
const video = { ...storage, source, metadata: { editProcessing: state } };
const upload = {
diff --git a/apps/web/lib/video-edit-operation.ts b/apps/web/lib/video-edit-operation.ts
index 7c2d853d6..b5392fd98 100644
--- a/apps/web/lib/video-edit-operation.ts
+++ b/apps/web/lib/video-edit-operation.ts
@@ -1,3 +1,4 @@
+import { isDeepStrictEqual } from "node:util";
import { db } from "@cap/database";
import {
videoProcessingJobs,
@@ -69,16 +70,22 @@ export function matchesEditOperation(
operation: EditOperation,
) {
const state = getEditProcessingState(video.metadata);
+ if (!state) return false;
+ let expectedSource: unknown;
+ try {
+ expectedSource = JSON.parse(state.source);
+ } catch {
+ return false;
+ }
return Boolean(
- state &&
- upload &&
+ upload &&
state.token === operation.token &&
state.startedAt === operation.startedAt &&
state.ownerId === video.ownerId &&
state.bucket === video.bucket &&
state.storageIntegrationId === video.storageIntegrationId &&
state.sourceKey === sourceKey &&
- state.source === JSON.stringify(video.source) &&
+ isDeepStrictEqual(expectedSource, video.source) &&
upload.rawFileKey === sourceKey &&
upload.startedAt.toISOString() === operation.startedAt,
);