Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions pkgs/id/web/src/collab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ function openCollab(onEditorReady = vi.fn(), onStatus?: StatusCallback) {
return { conn, socket, onEditorReady };
}

const validInit = () => [MSG_INIT, 0, { type: "doc", content: [{ type: "paragraph" }] }, "raw"];
// Raw mode schema-valid INIT: the raw schema only allows code_block nodes.
// (A paragraph doc in raw mode does not parse and is now rejected, #7884.)
const validInit = () => [MSG_INIT, 0, { type: "doc", content: [{ type: "code_block" }] }, "raw"];

describe("initCollab protocol handling (#1463, #2824)", () => {
beforeEach(() => {
Expand Down Expand Up @@ -228,7 +230,7 @@ describe("initCollab protocol handling (#1463, #2824)", () => {
}

socket.open();
socket.deliver([0, 0, { type: "doc", content: [{ type: "paragraph" }] }, "raw"]);
socket.deliver([0, 0, { type: "doc", content: [{ type: "code_block" }] }, "raw"]);
socket.drop();
vi.advanceTimersToNextTimer();
socket = FakeWebSocket.instances.at(-1)!;
Expand Down Expand Up @@ -305,6 +307,52 @@ describe("initCollab protocol handling (#1463, #2824)", () => {
expect(onEditorReady).toHaveBeenCalledOnce();
});

it("rejects INIT docs whose content entries do not parse against the mode schema (#7884)", () => {
const onEditorReady = vi.fn();
const { conn, socket } = openCollab(onEditorReady);
vi.spyOn(console, "error").mockImplementation(() => {});

// One bad entry must reject the whole INIT at the frame boundary:
// before this check, Node.fromJSON threw inside initEditor and the
// session silently continued with an empty editor.
for (const doc of [
// unknown node type alongside a valid paragraph
{
type: "doc",
content: [
{ type: "paragraph", content: [{ type: "text", text: "good paragraph" }] },
{ type: "future_node_from_newer_server" },
],
},
// unknown mark type
{
type: "doc",
content: [
{ type: "paragraph", content: [{ type: "text", text: "hi", marks: [{ type: "future_mark" }] }] },
],
},
// non-string text
{ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: 12345 }] }] },
// numeric content entry
{ type: "doc", content: [42] },
]) {
socket.deliver([MSG_INIT, 0, doc, "rich"]);
expect(conn.editor).toBeNull();
expect(onEditorReady).not.toHaveBeenCalled();
}

// Control: the same shape with only valid entries initializes normally.
socket.deliver([
MSG_INIT,
0,
{ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "hello rich" }] }] },
"rich",
]);
expect(conn.editor).not.toBeNull();
expect(onEditorReady).toHaveBeenCalledOnce();
expect(conn.editor?.view.state.doc.textContent).toBe("hello rich");
});

it("rejects malformed INIT mode fields before editor initialization (#7859)", () => {
const onEditorReady = vi.fn();
const { conn, socket } = openCollab(onEditorReady);
Expand Down
17 changes: 15 additions & 2 deletions pkgs/id/web/src/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@

import { Packr, Unpackr } from "msgpackr";
import { getVersion, receiveTransaction } from "prosemirror-collab";
import { Node } from "prosemirror-model";
import { Step } from "prosemirror-transform";
import { markCursorFresh, onInitReceived, removeCursor, setConnectionState, updateCursor } from "./cursors";
import { type ContentMode, type EditorInstance, getSendableSteps, initEditor } from "./editor";
import { type ContentMode, type EditorInstance, getSchema, getSendableSteps, initEditor } from "./editor";
import { closeImageBrowser } from "./image-browser";
import { MAX_STEPS_MESSAGE_BYTES, MAX_STEPS_PER_MESSAGE, stepWireSize } from "./collab-step-budget";

Expand Down Expand Up @@ -328,7 +329,7 @@ export function initCollab(
console.error("[collab] Dropping malformed Init message: invalid document");
return;
}
// #7859: the mode field is validated like version/doc above —
// #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.
Expand All @@ -338,6 +339,18 @@ export function initCollab(
return;
}
const mode: ContentMode = rawMode ?? "raw";
// Validate the doc content entries against the mode schema (#7884):
// the top-level checks above cannot see a bad entry (unknown node or
// mark type, non-string text, non-object entry), and initEditor would
// silently replace the whole document with an empty one when
// Node.fromJSON throws on it. A doc the schema cannot parse is
// rejected here, at the frame boundary, instead.
try {
Node.fromJSON(getSchema(mode), docRecord);
} catch (err) {
console.error("[collab] Dropping malformed Init message: document does not parse against the mode schema:", err);
return;
}
documentMode = mode;
console.log("[collab] Received Init, version:", version, "mode:", mode);

Expand Down
Loading