Repository navigation
fix(collab): store lone surrogates and NUL as U+FFFD (wiki-v2 issue 773) - #80
Merged
Merged
Conversation
Yjs keeps a lone UTF-16 surrogate in format (mark attribute) values and keys, embeds and JSON content, and U+0000 everywhere. Postgres jsonb refuses both (22P02/22P05) and text refuses NUL (22021), so a crafted collab client could make every store of a page fail while it was open. The store now serializes through the fork-owned storeSafeJson (authz/page-write/store-safe-json.ts): each such code point becomes U+FFFD in values and keys, copy-on-write (a clean page is the same reference; Yjs's own objects are never written to). It runs before jsonToText, the deep-equal no-op check, updatePage and the post-store effects. The live doc and the ydoc bytes keep the raw value; every later store normalizes the same way, so the row converges as `unchanged`. Seam: one line plus an import in persistence.extension.ts (UPSTREAM_ MODIFICATIONS.md section 122). Pinned by a unit spec and a real-Postgres spec that applies a raw Yjs update through real Hocuspocus 3.4.4 and goes red without the line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
|
Paired super-repo PR: https://github.com/cccvu/wiki-v2/pull/782 (review comments go there). Merge this PR first, then re-pin the super gitlink to its merge commit, then merge the super PR. 🤖 Generated with Claude Code |
…ection (wiki-v2 issue 773) Review round 1 on wiki-v2 PR 782. - storeSafeJson skipped any object whose prototype was not Object.prototype or null, and never called toJSON. A crafted collab client could still wedge every store with 22P05: an element attribute named __proto__ (Yjs builds attrs by assignment, so it re-prototypes them), an object value with an own __proto__ key (lib0 readAny does the same), or a Y.Map attribute value (serialized through toJSON). The walk now follows JSON.stringify: toJSON first, own enumerable keys whatever the prototype (copied into a plain object), undefined/function/symbol dropped or null, non-finite numbers null, -0 as 0, ArrayBuffer views left as is. - A BigInt attribute made JSON.stringify throw on every store and every settle. It now stores as its decimal string. - The ADR 0019 digest hashes storeSafeJson(fromYdoc(doc)), the projection the store writes: a clean page is the same reference, so its digest is unchanged; a BigInt no longer fails the settle; and a version survives an edit that changes nothing the store keeps. - store-safe-json.fitness.spec.ts: every fromYdoc call is the direct argument of storeSafeJson, in the two allowlisted files. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
A crafted collab client could put a lone UTF-16 surrogate or a NUL into a page. Postgres then refused every store of that page while it was open. The collab store now writes the serialized JSON through
storeSafeJson, which turns each of these into U+FFFD. The live Y.Doc and theydocbytes keep the raw value.What
apps/server/src/authz/page-write/store-safe-json.tswithstoreSafeStringandstoreSafeJson.toWellFormed). U+0000 becomes U+FFFD too.Uint8Arrayare left alone. An own__proto__key is kept viadefineProperty. If two keys normalize to the same name, the last one wins.collaboration/extensions/persistence.extension.ts(super-repoUPSTREAM_MODIFICATIONS.md§122):tiptapJson = storeSafeJson(TiptapTransformer.fromYdoc(document, 'default')); // CCC #773It runs before
jsonToText, the deep-equal no-op check, the blank-clobber guard,updatePage,syncTransclusionand mentions. Sopages.content,text_contentandpage_transclusions.contentare all covered.store-safe-json.spec.ts(13 unit tests) andpersistence-store-unicode.pg.spec.ts(4 tests on real Postgres, plus theAUTHZ_REQUIRE_PGanti-vacuity check).authz/route-guard/import-boundary.spec.ts: comment text only.Why
Fixes https://github.com/cccvu/wiki-v2/issues/773.
"\ud83d"and{"\ud83d":1}, and 22P05 for"\^@".textgives 22021 for 0x00.COLLAB_STORE_FAILED, a settle answered 503, a write returnedwrite_outcome_unknown, and edits were lost on unload.How
fromYdocin exactly two places: the store and the ADR 0019 digest. History, restore, duplicate, export,/v1and MCP all read the row, which is now clean.ydocbytea keeps the raw state. That is safe because it is binary, and NUL is already stored there legitimately by the write-idem keys. Every later store normalizes the same state to the same JSON, so it reportsunchangedand no rewrite loop starts.unchanged, so theydocbytes are not rewritten. The difference is invisible after normalization. This is the same behaviour write-idem already has, and a test pins it.Testing
Red before (seam line reverted): 4 failed and 14 passed of 18.
expect(storeFailures).toEqual([])got["22P05"], andflushPageContentreturned{flushed:false, reason:"error"}.Mutations:
toWellFormedSuites:
src/authznon-pg: 146 suites, 2561 passed, 0 failed, 334 skipped;Checks: typecheck is clean in both orders; super
scripts/check-upstream-boundary.shexits 0;node --test scripts/ci/*.test.mjspasses 264/264; eslint is clean on the new files.Perf (40k-node page, median of 20):
storeSafeJsontakes 5.9 ms clean (same reference) and 6.0 ms with one poisoned leaf.fromYdoctakes 9–12 ms on the same doc, so this adds a few ms per debounced store.Risk
storeSafeJson(fromYdoc(doc)), the same projection the store writes. A clean page comes back as the same reference, so its digest does not change. A page that holds a lone surrogate, a NUL, a BigInt or a non-plain attribute object gets a new digest. A caller holding a version issued before the deploy gets one412and must re-read; an old and a new task disagree in the same way during the rolling overlap. Nothing is lost: a 412 changes nothing.🤖 Generated with Claude Code