Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(migrate): validate and clamp v2 cropRegion before applying it

Etienne Lescota5212bdc7451
diff --git a/src/lib/ai-edition/document/migrate.test.ts b/src/lib/ai-edition/document/migrate.test.ts
index af6d3ef16..98c0585fb 100644
--- a/src/lib/ai-edition/document/migrate.test.ts
+++ b/src/lib/ai-edition/document/migrate.test.ts
@@ -190,6 +190,49 @@ describe("migrateProjectDataToAxcutDocument", () => {
expect(doc.timeline.clips[0]?.cropRegion).toBeUndefined();
});
+ it("leaves the migrated clip without a cropRegion when editor.cropRegion is missing", () => {
+ const v2 = makeV2Project();
+ delete (v2.editor as Partial<typeof v2.editor>).cropRegion;
+ const doc = migrateProjectDataToAxcutDocument(v2);
+ expect(doc.timeline.clips[0]?.cropRegion).toBeUndefined();
+ });
+
+ it.each([
+ ["NaN", { x: Number.NaN, y: 0, width: 0.5, height: 0.5 }],
+ ["Infinity", { x: 0, y: 0, width: Number.POSITIVE_INFINITY, height: 0.5 }],
+ ["-Infinity", { x: 0, y: Number.NEGATIVE_INFINITY, width: 0.5, height: 0.5 }],
+ ])("ignores a cropRegion with a %s component", (_label, cropRegion) => {
+ const v2 = makeV2Project();
+ v2.editor.cropRegion = cropRegion;
+ const doc = migrateProjectDataToAxcutDocument(v2);
+ expect(doc.timeline.clips[0]?.cropRegion).toBeUndefined();
+ });
+
+ it("clamps an out-of-range cropRegion into the unit square", () => {
+ const v2 = makeV2Project();
+ v2.editor.cropRegion = { x: -0.2, y: 0.5, width: 0.6, height: 0.8 };
+ const doc = migrateProjectDataToAxcutDocument(v2);
+ expect(doc.timeline.clips[0]?.cropRegion).toEqual({ x: 0, y: 0.5, width: 0.6, height: 0.5 });
+ });
+
+ it("drops a cropRegion that clamps back to the identity", () => {
+ const v2 = makeV2Project();
+ v2.editor.cropRegion = { x: -1, y: -1, width: 3, height: 3 };
+ const doc = migrateProjectDataToAxcutDocument(v2);
+ expect(doc.timeline.clips[0]?.cropRegion).toBeUndefined();
+ });
+
+ it.each([
+ ["zero width", { x: 0.2, y: 0.2, width: 0, height: 0.5 }],
+ ["negative height", { x: 0.2, y: 0.2, width: 0.5, height: -0.1 }],
+ ["x at the right edge", { x: 1.5, y: 0.2, width: 0.5, height: 0.5 }],
+ ])("ignores a cropRegion with %s", (_label, cropRegion) => {
+ const v2 = makeV2Project();
+ v2.editor.cropRegion = cropRegion;
+ const doc = migrateProjectDataToAxcutDocument(v2);
+ expect(doc.timeline.clips[0]?.cropRegion).toBeUndefined();
+ });
+
it("carries cursor tuning keys from a v2 editor into getEditorSettings", () => {
const v2 = makeV2Project();
v2.editor.cursorSize = 0.3;
diff --git a/src/lib/ai-edition/document/migrate.ts b/src/lib/ai-edition/document/migrate.ts
index e76599daa..6a32d52ab 100644
--- a/src/lib/ai-edition/document/migrate.ts
+++ b/src/lib/ai-edition/document/migrate.ts
@@ -75,14 +75,22 @@ function toLegacyMedia(input: ProjectMedia | undefined): ProjectMedia | null {
}
/**
- * A v2 `editor.cropRegion` becomes the migrated clip's crop. Returns null for a
- * missing, malformed, or identity region so the clip stays lean (see clipSchema).
+ * A v2 `editor.cropRegion` becomes the migrated clip's crop. The region is
+ * normalised to the unit square: x/y clamp to [0, 1], width/height clamp so the
+ * region stays inside it. Returns null for a missing, non-finite, empty, or
+ * identity region so the clip stays lean (see clipSchema).
*/
function toClipCropRegion(region: CropRegion | undefined): CropRegion | null {
if (!region) return null;
- const { x, y, width, height } = region;
- const finite = [x, y, width, height].every((v) => typeof v === "number" && Number.isFinite(v));
+ const finite = [region.x, region.y, region.width, region.height].every(
+ (v) => typeof v === "number" && Number.isFinite(v),
+ );
if (!finite) return null;
+ const x = Math.min(1, Math.max(0, region.x));
+ const y = Math.min(1, Math.max(0, region.y));
+ const width = Math.min(1 - x, Math.max(0, region.width));
+ const height = Math.min(1 - y, Math.max(0, region.height));
+ if (width <= 0 || height <= 0) return null;
if (x === 0 && y === 0 && width === 1 && height === 1) return null;
return { x, y, width, height };
}