diff --git a/shared/editor/lib/ChangesetHelper.test.ts b/shared/editor/lib/ChangesetHelper.test.ts index 7df65fdcdc..76806e58bc 100644 --- a/shared/editor/lib/ChangesetHelper.test.ts +++ b/shared/editor/lib/ChangesetHelper.test.ts @@ -305,6 +305,14 @@ describe("ChangesetHelper.getChangeset", () => { expect(ChangesetHelper.getChangeset(after, before)).toBeNull(); }); + it("returns null when every paragraph of a large document changes", () => { + const many = Array.from({ length: 3000 }, (_, i) => `line ${i}`); + const before = paras(...many); + const after = paras(...many.map((text) => `${text} rewritten`)); + + expect(ChangesetHelper.getChangeset(after, before)).toBeNull(); + }); + it("still computes a changeset for a small change to a large document", () => { const before = paras(...texts); const after = paras(...texts.slice(0, -1), "a different final paragraph"); diff --git a/shared/utils/rfc6902/diff.test.ts b/shared/utils/rfc6902/diff.test.ts new file mode 100644 index 0000000000..6b9392408e --- /dev/null +++ b/shared/utils/rfc6902/diff.test.ts @@ -0,0 +1,38 @@ +import { applyPatch, createPatch } from "./index"; +import { clone } from "./util"; + +describe("diffArrays", () => { + it("returns the minimal patch for a small array change", () => { + const input = ["a", "b", "c", "d"]; + const output = ["a", "x", "c", "d", "e"]; + const patch = createPatch(input, output); + + expect(patch).toEqual([ + { op: "replace", path: "/1", value: "x" }, + { op: "add", path: "/-", value: "e" }, + ]); + + const result = clone(input); + applyPatch(result, patch); + expect(result).toEqual(output); + }); + + it("throws quickly instead of building a huge memo", () => { + const input = Array.from({ length: 3000 }, (_, i) => `before ${i}`); + const output = Array.from({ length: 3000 }, (_, i) => `after ${i}`); + const started = Date.now(); + + expect(() => createPatch(input, output)).toThrow(/max complexity/); + expect(Date.now() - started).toBeLessThan(1000); + }); + + it("still diffs large arrays when the changed range is small", () => { + const input = Array.from({ length: 3000 }, (_, i) => `item ${i}`); + const output = [...input]; + output[1500] = "changed"; + + expect(createPatch(input, output)).toEqual([ + { op: "replace", path: "/1500", value: "changed" }, + ]); + }); +}); diff --git a/shared/utils/rfc6902/diff.ts b/shared/utils/rfc6902/diff.ts index 4993bd7552..7ead10d32b 100644 --- a/shared/utils/rfc6902/diff.ts +++ b/shared/utils/rfc6902/diff.ts @@ -179,6 +179,9 @@ function isArrayRemove( return array_operation.op === "remove"; } +/** Maximum number of memo cells diffArrays may allocate. */ +const MAX_ARRAY_DIFF_CELLS = 1_000_000; + interface DynamicAlternative { /** Previous i coordinate (-1 means no previous) */ prevI: number; @@ -235,6 +238,7 @@ function buildOperations( * resulting in an array of 'remove' operations. * * @returns a list of add/remove/replace operations. + * @throws if the changed ranges are too large to diff in bounded memory. */ export function diffArrays( input: T[], @@ -275,6 +279,13 @@ export function diffArrays( const subInput = input_end - start; const subOutput = output_end - start; + // The memo below holds one object per cell, so cap it to bound memory. + if ((subInput + 1) * (subOutput + 1) > MAX_ARRAY_DIFF_CELLS) { + throw new Error( + `Array diff exceeds max complexity: ${subInput} x ${subOutput} changed items` + ); + } + // oxlint-disable-next-line unicorn/no-new-array const memo: Array> = new Array(subInput + 1); for (let i = 0; i <= subInput; i++) {