Skip to content

id(web/collab): INIT frame mode shape-trusted — a non-string or unknown mode is adopted verbatim, and a falsy mode silently opens a rich doc in the raw schema as an empty editor #7859

Closed

Description

@dezren39

id(web/collab): INIT frame mode shape-trusted — a non-string or unknown mode is adopted verbatim, and a falsy mode silently opens a rich doc in the raw schema as an empty editor

Summary

handleMessage in pkgs/id/web/src/collab.ts validates the INIT frame's
version and doc (#2824: version must be a safe non-negative integer, doc must
be a doc object) — but the frame's fourth field, the content mode, is still
read with an unchecked cast (collab.ts:316 on origin/main df780d3c3b):

const mode = ((msg[3] as string) || "raw") as ContentMode;
documentMode = mode;
...
editorInstance = initEditor(container, docRecord, version, mode, sendCursor, filename);

Any value is adopted as the editor's ContentMode ("rich" | "markdown" | "plain" | "raw" | "media" | "binary", editor.ts:41) and drives schema
selection (getSchema: only "raw" gets the raw schema, editor.ts:476-478),
toolbar/editable decisions, and the mode stored on the editor instance. Two
failure classes result:

  • Truthy wrong shapes are adopted verbatim. A numeric, object, or
    unknown-string mode becomes the session's mode: initEditor is called with
    it, instance.mode returns it, and every mode comparison downstream
    silently takes the "not raw / no toolbar" branch for a document that may
    be raw text.
  • Falsy wrong shapes coerce to "raw". A mode of 0 / false (deploy
    skew, a future server contract change) hits the || "raw" fallback, so a
    rich/markdown document is parsed with the raw schema (doc = code_block+
    only). Node.fromJSON throws (RangeError: Unknown node type: paragraph), initEditor catches it and falls back to an empty doc —
    the editor opens showing nothing for a non-empty file, one
    save/autosave away from overwriting the real content with the empty state.

This is the INIT sibling of #7854 (AUTH_OK / NEW_VERSION frame fields):
that filing covers the token and hash fields; the INIT mode field was not
covered by #2824 (version/doc only) or by #7854.

Verified behavior (this run)

Probe: ~/git/fleet/probe-initmode (laptop) — a detached worktree at
origin/main df780d3c3b (collab.ts md5 d4ecdd5034a0219d3a88424940a73b05,
identical to git show origin/main:pkgs/id/web/src/collab.ts) plus
src/probe-initmode.test.ts, which drives the real initCollab
through the same FakeWebSocket harness as collab.test.ts, frames packed
with the repo's MessagePack config. All 6 probe tests pass; the editor
instance is captured via onEditorReady:

INIT frame Observed
[0, 0, rawDoc(code_block "hello raw"), "raw"] (control) instance.mode === "raw", doc text "hello raw" ✅
[0, 0, richDoc(paragraph "hello rich"), "markdown"] (control) instance.mode === "markdown", doc text "hello rich" ✅
[0, 0, rawDoc, 123] instance.mode === 123 (number, adopted verbatim); schema selection takes the non-raw branch
[0, 0, richDoc, {type:"doc"}] instance.mode === {"type":"doc"} (object, adopted verbatim)
[0, 0, rawDoc, "bogus"] instance.mode === "bogus"; hasToolbar false, non-raw schema for a raw doc
[0, 0, richDoc, 0] mode coerced to "raw"; Node.fromJSON throws RangeError: Unknown node type: paragraph; editor initialized with an empty doc (doc(code_block), text "") — the sent content silently vanishes

Why it matters

Acceptance criteria

  • The INIT mode is validated at the frame boundary in collab.ts: it must be one of the ContentMode union values ("rich", "markdown", "plain", "raw", "media", "binary"). A missing mode may keep the "raw" default only when the field is absent/undefined.
  • An INIT frame with a present-but-invalid mode (non-string, or a string outside the union) is dropped with a diagnosable console.error, like the invalid version/doc cases — the editor is never initialized with an adopted-verbatim or coerced mode.
  • A rich/markdown document can never be silently opened as an empty raw-schema editor because of a wrong-typed mode field.
  • Regression tests cover mode 123, mode {...}, mode "bogus", and mode 0/false with a rich doc, plus the "raw" and "markdown" controls (a test in collab.test.ts style, driving initCollab through the fake-socket harness).
  • pkgs/id web vitest suite + typecheck green.

Scope notes

Activity

  1. developing-today commented on Nov 8, 2026

    @developing-today
    Contributor
  2. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/small-fixQueue packet kind: bounded fix with reproduction and targeted tests
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    app/idPackage/app: id (pkgs/ or apps/ in this repo)
    effort/sSmall: expected under 2 hours of work
    on Nov 8, 2026
  3. dezren39 commented on Nov 8, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · filed

  4. dezren39 commented on Nov 10, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · parked — awaiting triage

  5. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    work/assignedQueue worker lifecycle state
    and removed on Nov 10, 2026
  6. 6 remaining items

  7. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    and removed
    queue/claimedQueue worker lifecycle state
    on Nov 11, 2026
  8. dezren39 commented on Nov 11, 2026

    @dezren39
    MemberAuthor

    agent-note:v1
    state: pr_opened (parked CI wait)
    task: 14128
    lane: tmp-code-f
    Fix implemented in this claim: PR #8534 adds the isContentMode guard at the INIT frame boundary in pkgs/id/web/src/collab.ts (absent mode keeps the raw default; present-but-invalid drops the frame with a diagnosable console.error) plus 3 regression tests in collab.test.ts. collab suite 27/27; oracle verified (malformed-mode test fails pre-fix); full web suite 727 pass with 1 pre-existing search-panel failure that reproduces on clean main; tsc clean. Parked as broker CI wait on PR #8534. Resume: checks green + Codex disposition -> merge -> verify #7859 closes -> complete fixed_merged.

  9. dezren39 commented on Nov 11, 2026

    @dezren39
    MemberAuthor

    agent-note:v1
    state: parked
    worker: repo-worker-code
    lane: any-b
    task: 14128
    note: Resumed and re-verified PR #8534 (head 7f3f271): Codex review completed with no findings, no inline review comments. Blocked only on CI: 11 basic checks QUEUED in the fleet-wide backlog, 0 failed. Parked as CI wait. Resume: checks green -> merge -> verify issue closes -> complete fixed_merged.

  10. added a commit that references this issue on Nov 11, 2026
  11. dezren39 commented on Nov 11, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done — PR merged

  12. removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    work/assignedQueue worker lifecycle state
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    app/idPackage/app: id (pkgs/ or apps/ in this repo)effort/sSmall: expected under 2 hours of workkind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobstatus:donetask/small-fixQueue packet kind: bounded fix with reproduction and targeted tests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions