| 1 | // fixtures/poison.mjs — documents no op can produce: the malformed |
| 2 | // shapes a peer could put into the CRDT by hand (box.x = "a", |
| 3 | // background.kind = 5, fonts.body = [], an asset that is a string), |
| 4 | // built directly with yjs. view.test.mjs runs the view over them and |
| 5 | // core/src/view.rs's tests build the same shapes with yrs; run as a |
| 6 | // script this writes each one as a Yjs v1 update into |
| 7 | // internal/slidestest/testdata/poison/, which |
| 8 | // TestPoisonUpdateDoesNotBrickTheDeck posts at the real server. |
| 9 | // |
| 10 | // Every document has a fixed guid and client id, so a re-run writes the |
| 11 | // same bytes and the committed files only move when this file does. |
| 12 | // Each update adds a slide of its own (or touches meta/assets), so it |
| 13 | // applies cleanly to any deck. |
| 14 | import * as Y from 'yjs' |
| 15 | import { mkdirSync, writeFileSync } from 'node:fs' |
| 16 | import { join, dirname } from 'node:path' |
| 17 | import { fileURLToPath } from 'node:url' |
| 18 | |
| 19 | const here = dirname(fileURLToPath(import.meta.url)) |
| 20 | export const poisonDir = join(here, '..', 'internal', 'slidestest', 'testdata', 'poison') |
| 21 | |
| 22 | function slideWith (doc, id, fill) { |
| 23 | const slide = new Y.Map() |
| 24 | doc.getArray('slides').push([slide]) |
| 25 | slide.set('id', id) |
| 26 | slide.set('layout', 'canvas') |
| 27 | slide.set('notes', new Y.XmlFragment()) |
| 28 | const blocks = new Y.Array() |
| 29 | slide.set('blocks', blocks) |
| 30 | fill(slide, blocks) |
| 31 | } |
| 32 | |
| 33 | function blockWith (blocks, id, fill) { |
| 34 | const block = new Y.Map() |
| 35 | blocks.push([block]) |
| 36 | block.set('id', id) |
| 37 | block.set('type', 'text') |
| 38 | block.set('props', new Y.Map()) |
| 39 | block.set('phone', new Y.Map()) |
| 40 | block.set('z', 0) |
| 41 | block.set('text', new Y.XmlFragment()) |
| 42 | fill(block) |
| 43 | } |
| 44 | |
| 45 | /** A scene written straight into the scenes map, as no op would. */ |
| 46 | function poisonScene (doc, id, data) { |
| 47 | const scene = new Y.Map() |
| 48 | doc.getMap('scenes').set(id, scene) |
| 49 | scene.set('name', 'Poison') |
| 50 | scene.set('code', '0123456789abcdef'.repeat(4)) |
| 51 | scene.set('states', [{ id: 'a' }]) |
| 52 | scene.set('data', data) |
| 53 | } |
| 54 | |
| 55 | /** The poison shapes, name → a fill function over a fresh Y.Doc. */ |
| 56 | export const shapes = { |
| 57 | // A canvas block whose box is not four numbers. |
| 58 | box: doc => slideWith(doc, 'poisonboxsld', (slide, blocks) => { |
| 59 | blockWith(blocks, 'poisonboxblk', block => block.set('box', { x: 'a', y: 0, w: 1, h: 1 })) |
| 60 | }), |
| 61 | // A slide whose background kind is a number. |
| 62 | background: doc => slideWith(doc, 'poisonbgsld1', slide => { |
| 63 | const bg = new Y.Map() |
| 64 | slide.set('background', bg) |
| 65 | bg.set('kind', 5) |
| 66 | bg.set('value', '') |
| 67 | }), |
| 68 | // meta.fonts with a body that is an array. |
| 69 | fonts: doc => { doc.getMap('meta').set('fonts', { heading: 'Serif', body: [] }) }, |
| 70 | // An assets entry that is a string, one with no ref, one with a |
| 71 | // mistyped name. |
| 72 | assets: doc => { |
| 73 | const assets = doc.getMap('assets') |
| 74 | assets.set('poisonstr', 'not a map') |
| 75 | assets.set('poisonnoref', { name: 'x.png' }) |
| 76 | assets.set('poisonbadnm', { ref: 'poisonref', name: 3, width: 'w', height: 9.9, alt: null }) |
| 77 | }, |
| 78 | // A phone order the view passes (it is a number) and the server's |
| 79 | // typed reader cannot hold: what the parse gate in appendUpdate |
| 80 | // exists for, over and above the view's own tolerance. |
| 81 | magnitude: doc => slideWith(doc, 'poisonmagsld', (slide, blocks) => { |
| 82 | blockWith(blocks, 'poisonmagblk', block => { |
| 83 | block.set('region', 'body') |
| 84 | block.get('phone').set('order', 1e300) |
| 85 | }) |
| 86 | }), |
| 87 | // A run under a formatting attribute the schema does not define |
| 88 | // (codex 3). The view reads the words fine, so the typed parse |
| 89 | // passes; y-prosemirror 1.3.7 deletes the XmlText when it binds. The |
| 90 | // gate refuses it by checking the shared text, not the projection |
| 91 | // (core/src/check.rs). Last in this list, so the client ids of the |
| 92 | // shapes before it — and their committed bytes — do not move. |
| 93 | unknownmark: doc => slideWith(doc, 'poisonmrksld', (slide, blocks) => { |
| 94 | blockWith(blocks, 'poisonmrkblk', block => { |
| 95 | block.set('region', 'body') |
| 96 | const p = new Y.XmlElement('paragraph') |
| 97 | const t = new Y.XmlText() |
| 98 | p.insert(0, [t]) |
| 99 | block.get('text').insert(0, [p]) |
| 100 | t.applyDelta([{ insert: 'keep ' }, { insert: 'these words', attributes: { unknown_mark: true } }]) |
| 101 | }) |
| 102 | }), |
| 103 | // A scene whose data is one byte over the 960 KiB its ops allow |
| 104 | // (canonical JSON: the string and its two quotes), under a well-formed |
| 105 | // id. The view and the text check take it; the server refuses it |
| 106 | // after applying it (internal/slides/scene_writes.go). Last, for the |
| 107 | // same reason as unknownmark. |
| 108 | scenedata: doc => poisonScene(doc, 'poisonscenea', 'x'.repeat(960 * 1024 - 1)), |
| 109 | // The same oversized data under the empty id, which no op can write. |
| 110 | scenenoid: doc => poisonScene(doc, '', 'x'.repeat(960 * 1024 - 1)), |
| 111 | // A small scene under the empty id: the id alone is refused. |
| 112 | scenebadid: doc => poisonScene(doc, '', { n: 1 }), |
| 113 | // Data holding a "__proto__" key, which a browser's decoder loses. |
| 114 | sceneproto: doc => poisonScene(doc, 'poisonprotoa', JSON.parse('{"a": [{"__proto__": 1}]}')) |
| 115 | } |
| 116 | |
| 117 | /** A fresh document holding one poison shape. */ |
| 118 | export function poisonDoc (name) { |
| 119 | const fill = shapes[name] |
| 120 | if (!fill) throw new Error(`no poison shape ${name}`) |
| 121 | const doc = new Y.Doc({ guid: `poison-${name}` }) |
| 122 | doc.clientID = 900 + Object.keys(shapes).indexOf(name) |
| 123 | doc.transact(() => fill(doc)) |
| 124 | return doc |
| 125 | } |
| 126 | |
| 127 | /** Every shape as a Yjs v1 update, name → bytes. */ |
| 128 | export function poisonUpdates () { |
| 129 | const out = {} |
| 130 | for (const name of Object.keys(shapes)) out[name] = Y.encodeStateAsUpdate(poisonDoc(name)) |
| 131 | return out |
| 132 | } |
| 133 | |
| 134 | export function writePoison () { |
| 135 | mkdirSync(poisonDir, { recursive: true }) |
| 136 | for (const [name, bytes] of Object.entries(poisonUpdates())) { |
| 137 | writeFileSync(join(poisonDir, `${name}.yjs`), Buffer.from(bytes)) |
| 138 | console.log(`${name}: ${bytes.length} bytes`) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) writePoison() |
| 143 | |