diff --git a/pkgs/id/web/src/collab.test.ts b/pkgs/id/web/src/collab.test.ts index 8e3d14202d..9f129626f3 100644 --- a/pkgs/id/web/src/collab.test.ts +++ b/pkgs/id/web/src/collab.test.ts @@ -5,6 +5,7 @@ import { MAX_STEPS_PER_MESSAGE, buildCollabWsUrl, initCollab, + isContentMode, stepBatch, stepWireSize, } from "./collab"; @@ -302,6 +303,61 @@ describe("initCollab protocol handling (#1463, #2824)", () => { expect(conn.editor).not.toBeNull(); expect(onEditorReady).toHaveBeenCalledOnce(); }); + + it("rejects malformed INIT mode fields before editor initialization (#7859)", () => { + const onEditorReady = vi.fn(); + const { conn, socket } = openCollab(onEditorReady); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + const richDoc = { type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "hello" }] }] }; + + // Non-string, unknown-string, and falsy modes are all present-but- + // invalid: the frame drops, the editor never initializes, and in + // particular a rich doc is never coerced into the raw schema. + for (const frame of [ + [MSG_INIT, 0, richDoc, 123], + [MSG_INIT, 0, richDoc, { type: "doc" }], + [MSG_INIT, 0, richDoc, "bogus"], + [MSG_INIT, 0, richDoc, 0], + [MSG_INIT, 0, richDoc, false], + [MSG_INIT, 0, richDoc, null], + ]) { + socket.deliver(frame); + expect(conn.editor).toBeNull(); + expect(onEditorReady).not.toHaveBeenCalled(); + } + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("invalid mode")); + + socket.deliver(validInit()); + expect(conn.editor).not.toBeNull(); + expect(onEditorReady).toHaveBeenCalledOnce(); + }); + + it("keeps the raw default for an absent INIT mode and adopts valid modes verbatim (#7859)", () => { + const absent = openCollab(); + absent.socket.deliver([MSG_INIT, 0, { type: "doc", content: [{ type: "code_block" }] }]); + expect(absent.conn.editor).not.toBeNull(); + expect(absent.conn.mode).toBe("raw"); + + const markdown = openCollab(); + markdown.socket.deliver([ + MSG_INIT, + 0, + { type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "hello" }] }] }, + "markdown", + ]); + expect(markdown.conn.editor).not.toBeNull(); + expect(markdown.conn.mode).toBe("markdown"); + expect(markdown.conn.editor!.view.state.doc.textContent).toContain("hello"); + }); + + it("isContentMode accepts exactly the ContentMode union (#7859)", () => { + for (const mode of ["rich", "markdown", "plain", "raw", "media", "binary"]) { + expect(isContentMode(mode)).toBe(true); + } + for (const mode of ["bogus", "", "RAW", 123, 0, false, null, undefined, { type: "doc" }, ["raw"]]) { + expect(isContentMode(mode)).toBe(false); + } + }); }); describe("stepBatch (client side of the server step cap, #1965)", () => { diff --git a/pkgs/id/web/src/collab.ts b/pkgs/id/web/src/collab.ts index 785eedab31..a63355c85c 100644 --- a/pkgs/id/web/src/collab.ts +++ b/pkgs/id/web/src/collab.ts @@ -59,6 +59,17 @@ const MSG = { AUTH_OK: 9, } as const; +/** The ContentMode values an INIT frame may name (#7859). The mode + * field is shape-trusted wire data until validated here: adopting a + * non-string or unknown mode verbatim corrupts the session, and a + * falsy mode coerced to "raw" opens a rich doc in the raw schema as + * an empty editor. */ +export const CONTENT_MODES: readonly ContentMode[] = ["rich", "markdown", "plain", "raw", "media", "binary"]; + +export function isContentMode(value: unknown): value is ContentMode { + return typeof value === "string" && (CONTENT_MODES as readonly string[]).includes(value); +} + // MessagePack encoder/decoder configured for array format const packr = new Packr({ useRecords: false, structuredClone: true }); const unpackr = new Unpackr({ useRecords: false, structuredClone: true }); @@ -313,7 +324,16 @@ export function initCollab( console.error("[collab] Dropping malformed Init message: invalid document"); return; } - const mode = ((msg[3] as string) || "raw") as ContentMode; + // #7859: the mode field is validated like version/doc above — + // an absent mode keeps the "raw" default, but a present mode + // that is not a known ContentMode drops the frame instead of + // being adopted verbatim or coerced. + const rawMode = msg[3]; + if (rawMode !== undefined && !isContentMode(rawMode)) { + console.error("[collab] Dropping malformed Init message: invalid mode"); + return; + } + const mode: ContentMode = rawMode ?? "raw"; documentMode = mode; console.log("[collab] Received Init, version:", version, "mode:", mode);