fix: FixedSegments robustness

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs committed 2026-09-28 17:55:00 +00:00
1 parent a1d6df8978
commit 06e8ff9746
3 files changed
+270 -4

No files matched your search

+115
View File
@@ -1,8 +1,10 @@
import { ARROW_TYPE, DEFAULT_ZOOM } from "@excalidraw/common";
import { CaptureUpdateAction } from "@excalidraw/element";
import { pointFrom } from "@excalidraw/math";
import { Excalidraw } from "@excalidraw/excalidraw";
import { actionSelectAll } from "@excalidraw/excalidraw/actions";
import { actionDuplicateSelection } from "@excalidraw/excalidraw/actions/actionDuplicateSelection";
import { restoreElements } from "@excalidraw/excalidraw/data/restore";
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
import { Pointer, UI } from "@excalidraw/excalidraw/tests/helpers/ui";
import {
@@ -428,3 +430,116 @@ describe("elbow arrow ui", () => {
]);
});
});
describe("elbow arrow with malformed fixedSegments", () => {
beforeEach(async () => {
localStorage.clear();
await render(<Excalidraw handleKeyboardGlobally={true} />);
});
it.each([
["string", "str"],
["array-like object", { length: 1, 0: { index: 2 } }],
["array of junk", [null, 1, "x", [], {}]],
["missing points", [{ index: 2 }]],
["malformed points", [{ start: [125], end: null, index: 2 }]],
[
"first/last/out of range indices",
[
{ start: [0, 0], end: [125, 0], index: 1 },
{ start: [125, 200], end: [250, 200], index: 3 },
{ start: [125, 0], end: [125, 200], index: 9 },
],
],
[
"unsorted duplicates",
[
{ start: [125, 0], end: [125, 200], index: 2, extra: "x" },
{ start: [125, 0], end: [125, 200], index: 2 },
"junk",
],
],
])(
"can render and interact with a restored elbow arrow with %s fixedSegments",
(_, fixedSegments) => {
const arrow = {
...API.createElement({
type: "arrow",
elbowed: true,
x: 0,
y: 0,
points: [
pointFrom<LocalPoint>(0, 0),
pointFrom<LocalPoint>(125, 0),
pointFrom<LocalPoint>(125, 200),
pointFrom<LocalPoint>(250, 200),
],
}),
fixedSegments,
} as unknown as ExcalidrawElbowArrowElement;
API.updateScene({
elements: restoreElements([arrow], null),
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
});
const restored = h.elements[0] as ExcalidrawElbowArrowElement;
expect(
restored.fixedSegments === null ||
(Array.isArray(restored.fixedSegments) &&
restored.fixedSegments.every(
(segment) =>
segment.index === 2 &&
Object.keys(segment).sort().join() === "end,index,start",
)),
).toBe(true);
// select the arrow
mouse.reset();
mouse.clickAt(60, 0);
expect(h.state.selectedLinearElement?.elementId).toBe(restored.id);
// hover and drag the middle segment
mouse.reset();
mouse.moveTo(125, 100);
mouse.down();
mouse.moveTo(140, 100);
mouse.up();
expect(
(h.elements[0] as ExcalidrawElbowArrowElement).fixedSegments,
).toEqual([
{
index: 2,
start: pointFrom<LocalPoint>(140, 0),
end: pointFrom<LocalPoint>(140, 200),
},
]);
// drag the end point with a fixed segment
mouse.reset();
mouse.moveTo(250, 200);
mouse.down();
mouse.moveTo(300, 250);
mouse.up();
expect(
(h.elements[0] as ExcalidrawElbowArrowElement).fixedSegments,
).toHaveLength(1);
// release the fixed segment
mouse.reset();
mouse.moveTo(140, 125);
mouse.doubleClick();
const updated = h.elements[0] as ExcalidrawElbowArrowElement;
expect(updated.isDeleted).toBe(false);
expect(updated.fixedSegments).toBe(null);
expect(
updated.points.every(
(p) => Number.isFinite(p[0]) && Number.isFinite(p[1]),
),
).toBe(true);
},
);
});
+51 -4
View File
@@ -98,6 +98,7 @@ import type {
ExcalidrawSelectionElement,
ExcalidrawTextElement,
FixedPointBinding,
FixedSegment,
FontFamilyValues,
GroupId,
NonDeleted,
@@ -348,6 +349,52 @@ const normalizeBoundElements = (
.map(({ type, id }) => ({ type, id }));
};
// Malformed fixedSegments (e.g. a string or an array of junk from a malicious
// peer) crash rendering (`.map`) and elbow arrow routing. Segment `index` N
// is the segment between points[N - 1] and points[N]; the first (1) and last
// (points.length - 1) segments can never be fixed. The elbow arrow code also
// assumes fixed segments are axis-aligned, sorted by index and unique (e.g.
// segment release looks up neighbors by array position), so enforce that.
// Entries are copied so no unknown props leak through.
const normalizeFixedSegments = (
fixedSegments: unknown,
points: readonly LocalPoint[],
): FixedSegment[] | null => {
if (!Array.isArray(fixedSegments) || points.length < 4) {
return null;
}
const byIndex = new Map<number, FixedSegment>();
for (const segment of fixedSegments) {
if (
!segment ||
typeof segment !== "object" ||
!isValidPoint(segment.start) ||
!isValidPoint(segment.end) ||
!Number.isInteger(segment.index) ||
segment.index < 2 ||
segment.index > points.length - 2 ||
// must be either horizontal or vertical
(segment.start[0] !== segment.end[0] &&
segment.start[1] !== segment.end[1]) ||
byIndex.has(segment.index)
) {
continue;
}
byIndex.set(segment.index, {
start: pointFrom<LocalPoint>(segment.start[0], segment.start[1]),
end: pointFrom<LocalPoint>(segment.end[0], segment.end[1]),
index: segment.index,
});
}
if (byIndex.size === 0) {
return null;
}
return [...byIndex.values()].sort((a, b) => a.index - b.index);
};
const getStrokeWidthKey = (strokeWidth: unknown): StrokeWidthKey | null => {
return isFiniteNumber(strokeWidth)
? STROKE_WIDTH_KEYS.find((key) => STROKE_WIDTH[key] === strokeWidth) ?? null
@@ -787,10 +834,10 @@ export const restoreElement = (
? restoreElementWithProperties(element as ExcalidrawElbowArrowElement, {
...base,
elbowed: true,
fixedSegments:
element.fixedSegments?.length && base.points.length >= 4
? element.fixedSegments
: null,
fixedSegments: normalizeFixedSegments(
element.fixedSegments,
base.points,
),
startIsSpecial: element.startIsSpecial,
endIsSpecial: element.endIsSpecial,
})
@@ -22,6 +22,7 @@ import type { LocalPoint } from "@excalidraw/math";
import type {
ExcalidrawStickyNoteElement,
ExcalidrawArrowElement,
ExcalidrawElbowArrowElement,
ExcalidrawElement,
ExcalidrawFreeDrawElement,
ExcalidrawLinearElement,
@@ -242,6 +243,109 @@ describe("restoreElements", () => {
expect(restored.opacity).toBe(expected);
});
describe("fixedSegments", () => {
// 5 points -> segments 1..4, of which only 2 and 3 may be fixed
const points = [
pointFrom<LocalPoint>(0, 0),
pointFrom<LocalPoint>(50, 0),
pointFrom<LocalPoint>(50, 100),
pointFrom<LocalPoint>(150, 100),
pointFrom<LocalPoint>(150, 200),
];
const seg2 = { start: [50, 0], end: [50, 100], index: 2 };
const seg3 = { start: [50, 100], end: [150, 100], index: 3 };
const restoreElbowArrow = (
fixedSegments: unknown,
arrowPoints: LocalPoint[] = points,
) => {
const element = {
...API.createElement({
type: "arrow",
elbowed: true,
points: arrowPoints,
}),
fixedSegments,
} as unknown as ExcalidrawElement;
const [restored] = restore.restoreElements([element], null);
return restored as ExcalidrawElbowArrowElement;
};
it.each([
"str",
"",
123,
true,
{},
{ length: 1 },
{ length: 1, 0: seg2 },
null,
undefined,
[],
])("normalizes fixedSegments=%j to null", (fixedSegments) => {
expect(restoreElbowArrow(fixedSegments).fixedSegments).toBe(null);
});
it.each([
[[null, undefined, 1, "seg", [], {}, [50, 0]]],
[[{ start: [50, 0], end: [50, 100] }]],
[[{ start: [50, 0], end: [50, 100], index: "2" }]],
[[{ start: [50, 0], end: [50, 100], index: 2.5 }]],
[[{ start: [50, 0], end: [50, 100], index: NaN }]],
[[{ start: "50,0", end: [50, 100], index: 2 }]],
[[{ start: [50, 0], end: null, index: 2 }]],
[[{ start: [50], end: [50, 100], index: 2 }]],
[[{ start: [50, 0, 1], end: [50, 100], index: 2 }]],
[[{ start: [50, "0"], end: [50, 100], index: 2 }]],
[[{ start: [50, Infinity], end: [50, 100], index: 2 }]],
// neither horizontal nor vertical
[[{ start: [0, 0], end: [50, 100], index: 2 }]],
// first and last segments cannot be fixed
[[{ start: [0, 0], end: [50, 0], index: 1 }]],
[[{ start: [150, 100], end: [150, 200], index: 4 }]],
// out of range
[[{ start: [50, 0], end: [50, 100], index: 0 }]],
[[{ start: [50, 0], end: [50, 100], index: -2 }]],
[[{ start: [50, 0], end: [50, 100], index: 5 }]],
[[{ start: [50, 0], end: [50, 100], index: 1e9 }]],
])("normalizes invalid fixedSegments=%j to null", (fixedSegments) => {
expect(restoreElbowArrow(fixedSegments).fixedSegments).toBe(null);
});
it("keeps only valid fixedSegments, sorted and deduplicated", () => {
const restored = restoreElbowArrow([
"junk",
null,
{ ...seg3, extra: "x" },
{ start: [0, 0], end: [50, 0], index: 1 },
seg2,
{ ...seg2, start: [50, 10] },
{ start: [150, 100], end: [150, 200], index: 4 },
{ start: [50, 0], end: [50, 100], index: 7 },
]);
expect(restored.fixedSegments).toEqual([seg2, seg3]);
expect(restored.fixedSegments?.[1]).not.toHaveProperty("extra");
});
it("keeps valid fixedSegments", () => {
expect(restoreElbowArrow([seg2, seg3]).fixedSegments).toEqual([
seg2,
seg3,
]);
});
it("drops fixedSegments of elbow arrows with less than 4 points", () => {
expect(
restoreElbowArrow(
[seg2],
[pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(50, 100)],
).fixedSegments,
).toBe(null);
});
});
it("keeps valid boundElements on repair", () => {
const container = API.createElement({ type: "rectangle" });
const arrow = API.createElement({ type: "arrow" });