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
55 changes: 55 additions & 0 deletions pkgs/id/web/src/collab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const unpackr = new Unpackr({ useRecords: false, structuredClone: true });

const MSG_AUTH = 8;
const MSG_AUTH_OK = 9;
const MSG_NEW_VERSION = 7;
const MSG_INIT = 0;

/** Minimal WebSocket stand-in: records sent frames, lets the test drive
Expand Down Expand Up @@ -155,6 +156,60 @@ describe("initCollab protocol handling (#1463, #2824)", () => {
conn.disconnect();
});

// Oracle (#7854): the frame-field validation contract — a
// refreshed token is adopted only when it is a non-empty string,
// and NEW_VERSION fires only for string hash/name. Malformed
// frames are dropped, leaving the current token untouched.
it.each([
["numeric", 12345],
["object", { token: "nested" }],
])("AUTH_OK with a %s token is dropped and the reconnect re-sends the old token", (_kind, badToken) => {
const onTokenRefresh = vi.fn();
const conn = track(
initCollab("ws://example.test/collab", makeContainer(), "doc-1", "notes.md", "OLD_TOKEN_A", onTokenRefresh),
);

const first = FakeWebSocket.instances[0];
first.open();
first.deliver([MSG_AUTH_OK, "client-1", "alice", badToken]);
expect(onTokenRefresh).not.toHaveBeenCalled();

first.drop();
vi.advanceTimersByTime(1_000);
const second = FakeWebSocket.instances[1];
second.open();
expect(second.firstFrame()).toEqual([MSG_AUTH, "OLD_TOKEN_A"]);

conn.disconnect();
});

it("NEW_VERSION with a non-string hash is dropped; the string control fires", () => {
const onNewVersion = vi.fn();
const conn = track(
initCollab(
"ws://example.test/collab",
makeContainer(),
"doc-1",
"notes.md",
null,
undefined,
undefined,
undefined,
onNewVersion,
),
);

const socket = FakeWebSocket.instances[0];
socket.open();
socket.deliver([MSG_NEW_VERSION, { x: 1 }, "notes.md"]);
expect(onNewVersion).not.toHaveBeenCalled();

socket.deliver([MSG_NEW_VERSION, "abc123hash", "notes.md"]);
expect(onNewVersion).toHaveBeenCalledWith("abc123hash", "notes.md");

conn.disconnect();
});

it("reconnect without any refresh re-sends the init token", () => {
const conn = track(
initCollab("ws://example.test/collab", makeContainer(), "doc-1", "notes.md", "ONLY_TOKEN", undefined),
Expand Down
25 changes: 23 additions & 2 deletions pkgs/id/web/src/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,17 @@ export function initCollab(

case MSG.NEW_VERSION: {
// [7, hash, name] — document was saved, new content hash available
const hash = msg[1] as string;
const name = msg[2] as string;
const hash = msg[1];
const name = msg[2];
// #7854: validate at the boundary, like INIT (#2824) and
// Cursor (#7873). A non-string hash flowed unchecked into
// onNewVersion, whose callback writes it into dataset.hash —
// an object became the literal "[object Object]" the next
// save posts as the document hash.
if (typeof hash !== "string" || typeof name !== "string") {
console.error("[collab] Dropping malformed NewVersion message: hash and name must be strings");
break;
}
console.log("[collab] NewVersion: hash=%s, name=%s", hash, name);
if (onNewVersion) {
onNewVersion(hash, name);
Expand All @@ -585,6 +594,18 @@ export function initCollab(
const authName = msg[2] as string | null;
const refreshedToken = msg[3] as string | null | undefined;
console.log("[collab] Auth OK: client_id=%s, name=%s", authClientId, authName);
// #7854: validate at the boundary, like INIT (#2824). A
// refreshed token is adopted only when it is a non-empty
// string: any other truthy value (a number, an object) was
// adopted verbatim, persisted by the caller's saveIdentity
// via String(value) — "[object Object]" over the valid
// stored token — and re-sent on every reconnect. A malformed
// refresh is logged and dropped, leaving the current token
// and the stored identity untouched.
if (refreshedToken !== undefined && refreshedToken !== null && (typeof refreshedToken !== "string" || refreshedToken === "")) {
console.error("[collab] Dropping malformed AuthOk message: refreshed token must be a non-empty string");
break;
}
// Save the refreshed token so long-lived WS sessions stay fresh.
// Write it back to currentToken first: reconnects authenticate with
// currentToken, so the refreshed value must live here, not only in
Expand Down
Loading