mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-22 22:02:45 +03:00
fix: more conservative validation
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
3 files changed
+25
-9
No files matched your search
@@ -604,7 +604,8 @@ class Collab extends PureComponent<CollabProps, CollabState> {
|
||||
// valid INIT can still initialize the room
|
||||
if (
|
||||
!decryptedData.payload ||
|
||||
typeof decryptedData.payload !== "object"
|
||||
typeof decryptedData.payload !== "object" ||
|
||||
!Array.isArray(decryptedData.payload.elements)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -116,15 +116,14 @@ const decryptElements = async (
|
||||
);
|
||||
|
||||
// the payload authenticated with the room key, but its contents may still
|
||||
// be malformed. Treat it as an empty scene so the room can self-heal on
|
||||
// the next save instead of failing forever.
|
||||
try {
|
||||
const elements = JSON.parse(decodedData);
|
||||
return Array.isArray(elements) ? elements : [];
|
||||
} catch (error: any) {
|
||||
console.error("Failed to parse stored scene", error);
|
||||
return [];
|
||||
// be unreadable (corrupted, or a format we don't understand). Throw rather
|
||||
// than treating it as an empty scene, as the next save would otherwise
|
||||
// reconcile against nothing and irrecoverably replace the stored scene.
|
||||
const elements = JSON.parse(decodedData);
|
||||
if (!Array.isArray(elements)) {
|
||||
throw new Error("Stored scene is not an array of elements");
|
||||
}
|
||||
return elements;
|
||||
};
|
||||
|
||||
class FirebaseSceneVersionCache {
|
||||
|
||||
@@ -291,6 +291,10 @@ describe("collaboration", () => {
|
||||
await emitBroadcast(42);
|
||||
await emitBroadcast({ type: "SCENE_INIT" });
|
||||
await emitBroadcast({ type: "SCENE_INIT", payload: null });
|
||||
await emitBroadcast({ type: "SCENE_INIT", payload: {} });
|
||||
await emitBroadcast({ type: "SCENE_INIT", payload: { elements: {} } });
|
||||
await emitBroadcast({ type: "SCENE_INIT", payload: { elements: "x" } });
|
||||
await emitBroadcast({ type: "SCENE_INIT", payload: { elements: null } });
|
||||
await emitBroadcast({ type: "SCENE_UPDATE" });
|
||||
|
||||
expect(window.collab.portal.socketInitialized).toBe(false);
|
||||
@@ -306,6 +310,18 @@ describe("collaboration", () => {
|
||||
expect(scene?.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
||||
});
|
||||
|
||||
it("should initialize an empty room from SCENE_INIT", async () => {
|
||||
const { scenePromise } = await joinRoom();
|
||||
|
||||
await emitBroadcast({ type: "SCENE_INIT", payload: { elements: [] } });
|
||||
|
||||
await expect(scenePromise).resolves.toEqual({
|
||||
elements: [],
|
||||
scrollToContent: true,
|
||||
});
|
||||
expect(window.collab.portal.socketInitialized).toBe(true);
|
||||
});
|
||||
|
||||
it("should fall back to firebase when processing SCENE_INIT throws", async () => {
|
||||
const { scenePromise } = await joinRoom();
|
||||
|
||||
|
||||
Reference in new issue
Block a user