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
83 changes: 83 additions & 0 deletions pkgs/id/web/src/world-records.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,87 @@ describe("world page records fetch (#1458)", () => {
expect(renderedRecords()).toEqual({ only: 1 });
expect(socket.sent("records")).toHaveLength(7);
});

it("rejects malformed snapshots without partial state changes and accepts the next valid snapshot", () => {
const status = q<HTMLOutputElement>("[data-world-status]");
const participantList = q<HTMLElement>("[data-world-participants]");
const log = q<HTMLOListElement>("[data-world-log]");
const recordsRequests = socket.sent("records").length;
const malformedSnapshots: Array<Record<string, unknown>> = [
{ type: "snapshot" },
{ type: "snapshot", snapshot: null },
{ type: "snapshot", snapshot: { world_id: "bad", participants: [] } },
{ type: "snapshot", snapshot: { world_id: "bad", current_sequence: 90, participants: "bad" } },
{ type: "snapshot", snapshot: { world_id: "bad", current_sequence: Number.NaN, participants: [] } },
{ type: "snapshot", snapshot: { world_id: 3, current_sequence: 90, participants: [] } },
{
type: "snapshot",
snapshot: { world_id: "bad", current_sequence: 90, participants: [{ id: 1, display_name: 2 }] },
},
];

for (const frame of malformedSnapshots) {
socket.deliver(frame);
expect(status.textContent).toBe("Received an invalid world frame");
expect(status.dataset.state).toBe("error");
expect(participantList.textContent).toBe("ada");
expect(socket.sent("records")).toHaveLength(recordsRequests);
}
expect(log.textContent).toContain("error: invalid world frame");

socket.deliver({
type: "snapshot",
snapshot: { world_id: "recovered", current_sequence: 8, participants: [{ id: 2, display_name: "grace" }] },
});
expect(status.textContent).toBe("Connected");
expect(status.dataset.state).toBe("good");
expect(participantList.textContent).toBe("grace");
expect(log.textContent).toContain("Connected to recovered at event 8");
expect(socket.sent("records")).toHaveLength(recordsRequests + 1);
});

it("rejects malformed events before cursor or log changes and accepts the next valid event", async () => {
const status = q<HTMLOutputElement>("[data-world-status]");
const log = q<HTMLOListElement>("[data-world-log]");
const recordsRequests = socket.sent("records").length;

socket.deliver({
type: "event",
event: { sequence: 9, participant_id: 2, kind: { kind: "chat", data: "recovered" } },
});
expect(log.textContent).toContain("grace: recovered");

const malformedEvents: Array<Record<string, unknown>> = [
{ type: "event", event: { sequence: 100, participant_id: 2 } },
{ type: "event", event: null },
{ type: "event", event: { sequence: 101, participant_id: 2, kind: null } },
{ type: "event", event: { sequence: 102, participant_id: 2, kind: { kind: "other", data: "bad" } } },
{ type: "event", event: { sequence: 103, participant_id: 2, kind: { kind: "chat", data: 1 } } },
{ type: "event", event: { sequence: 104, participant_id: 2, kind: { kind: "input", data: "bad" } } },
{
type: "event",
event: { sequence: 105, participant_id: 2, kind: { kind: "input", data: [Number.NaN] } },
},
{ type: "event", event: { sequence: 106, participant_id: "2", kind: { kind: "input", data: [27] } } },
{ type: "event", event: { participant_id: 2, kind: { kind: "chat", data: "bad sequence" } } },
{ type: "event", event: { sequence: Number.NaN, participant_id: 2, kind: { kind: "chat", data: "bad sequence" } } },
];

for (const frame of malformedEvents) {
socket.deliver(frame);
expect(status.textContent).toBe("Received an invalid world frame");
expect(status.dataset.state).toBe("error");
}
expect(socket.sent("records")).toHaveLength(recordsRequests);
expect(log.textContent).not.toContain("input #100");
expect(log.textContent).not.toContain("bad sequence");

const closedSocket = socket;
closedSocket.close();
await new Promise((resolve) => setTimeout(resolve, 600));
const reconnectedSocket = FakeSocket.all.at(-1);
expect(reconnectedSocket).toBeDefined();
reconnectedSocket!.open();
expect(reconnectedSocket!.sent("join")[0]).toHaveProperty("after", 9);
});
});
61 changes: 53 additions & 8 deletions pkgs/id/web/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,40 +133,85 @@ function initWorld(): void {
status.dataset.state = state;
};

const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const isFiniteNumber = (value: unknown): value is number =>
typeof value === "number" && Number.isFinite(value);
const isWorldSnapshot = (value: unknown): value is NonNullable<WorldFrame["snapshot"]> =>
isRecord(value) &&
typeof value.world_id === "string" &&
isFiniteNumber(value.current_sequence) &&
Array.isArray(value.participants) &&
value.participants.every(
(participant) =>
isRecord(participant) && isFiniteNumber(participant.id) && typeof participant.display_name === "string",
);
const isWorldEvent = (value: unknown): value is NonNullable<WorldFrame["event"]> => {
if (
!isRecord(value) ||
!isFiniteNumber(value.sequence) ||
!isFiniteNumber(value.participant_id) ||
!isRecord(value.kind)
) {
return false;
}
if (value.kind.kind === "chat") return typeof value.kind.data === "string";
if (value.kind.kind === "input") {
return Array.isArray(value.kind.data) && value.kind.data.every(isFiniteNumber);
}
return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept program-install events delivered during reconnect catch-up

When reconnecting across a module installation, the server sends retained program_installed events through the catch-up loop in pkgs/id/src/world_session/mod.rs:2005-2008, even though the live-stream path handles installations separately. This validator rejects that valid protocol variant, so the browser reports an invalid-frame error instead of advancing its cursor and refreshing records as it previously did. If installation is the latest event, subsequent reconnects request and reject the same event again. Validate this variant rather than treating it as malformed.

Useful? React with 👍 / 👎.

};
const rejectFrame = () => {
setStatus("Received an invalid world frame", "error");
appendLine(log, "error: invalid world frame");
};

const showCapability = (value: string) => {
capability.value = value;
sessionStorage.setItem(storageKey, value);
inviteOutput.hidden = false;
};

const receive = (raw: string) => {
let frame: WorldFrame;
let parsed: unknown;
try {
frame = JSON.parse(raw) as WorldFrame;
parsed = JSON.parse(raw) as unknown;
} catch {
setStatus("Received an invalid world frame", "error");
rejectFrame();
return;
}
if (!isRecord(parsed) || typeof parsed.type !== "string") {
rejectFrame();
return;
}
const frame = parsed as WorldFrame;
switch (frame.type) {
case "snapshot": {
if (!frame.snapshot) return;
cursor = frame.snapshot.current_sequence;
const snapshot = frame.snapshot;
if (!isWorldSnapshot(snapshot)) {
rejectFrame();
return;
}
cursor = snapshot.current_sequence;
participants.replaceChildren();
for (const participant of frame.snapshot.participants) {
for (const participant of snapshot.participants) {
const tag = document.createElement("span");
tag.className = "badge badge-outline mr-2";
tag.dataset.participant = String(participant.id);
tag.textContent = participant.display_name;
participants.append(tag);
}
appendLine(log, `Connected to ${frame.snapshot.world_id} at event ${cursor}`);
appendLine(log, `Connected to ${snapshot.world_id} at event ${cursor}`);
setStatus("Connected", "good");
requestRecords();
break;
}
case "event": {
const event = frame.event;
if (!event) return;
if (!isWorldEvent(event)) {
rejectFrame();
return;
}
cursor = Math.max(cursor, event.sequence);
if (event.kind.kind === "chat") {
const who = participants.querySelector<HTMLElement>(`[data-participant="${event.participant_id}"]`);
Expand Down
Loading