fix: cap wall flags and scene extra as opaque objects (#35)

One shared validator next to the property caps: 8 KB serialized, 64 keys
per object, depth 8 (top-level object = 1). At a cap is accepted; over
is invalid and applies nothing. Both call sites use it, and a refusal is
logged once with the field and limit, never the value. Token flags and
props keep their existing rules; stored over-cap rows still load as
stored.
This commit is contained in:
2026-11-20 21:46:53 +01:00
parent 41c2fe695b
commit b0e1633aa9
9 changed files with 264 additions and 8 deletions
+8 -1
View File
@@ -209,10 +209,17 @@ run c3 "an out-of-grid tile coordinate is accepted" server/tiles.nim 's/^ x >=
run c4 "a leading-zero tile coordinate is canonical" server/tiles.nim "s/ if raw\[0\] == '0' and raw.len > 1: return false/ discard/" "$TILE_ZERO"
# slice 10 (#25): scene.set needs at least one patch field (server/world.nim parseScenePatch)
run d3 "scene.set accepts a patch with no field" server/world.nim 's/^ if not any: return none(ScenePatch)$/ discard/' "a patch with none is invalid"
# #32: the rollback on a Defect, in both transaction helpers, and the one-ack decision
run e4 "a Defect skips the ordinary rollback" server/db.nim 's/^ except Exception:$/ except CatchableError:/' "rolls back a Defect and leaves"
run e5 "a Defect skips the authoring rollback" server/world.nim 's/^ except Exception:$/ except CatchableError:/' "authoringTransaction rolls back a Defect"
# #35: the three opaque-object caps (server/props.nim checkOpaqueObject)
run f1 "the opaque size cap is not enforced" server/props.nim 's/> MaxOpaqueObjectBytes: return olSize/> MaxOpaqueObjectBytes + 100000: return olSize/' "size: exactly 8 KB"
run f2 "the opaque key cap is not enforced" server/props.nim 's/if n\.len > MaxOpaqueObjectKeys: return olKeys/if false: return olKeys/' "keys: 64 per object"
run f3 "the opaque depth cap is not enforced" server/props.nim 's/if depth > MaxOpaqueObjectDepth: return olDepth/if false: return olDepth/' "depth: 8 containers"
wait
rc=0; : > "$LOG"
for id in control a b c d1 d2 d3 e e1 e2 e3 f g h i j k l m n o p q r s t u v w x y z z2 \
for id in control a b c d1 d2 d3 e e1 e2 e3 e4 e5 f1 f2 f3 f g h i j k l m n o p q r s t u v w x y z z2 \
s1 s2 s3 s4 s5 s6 s7 aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au \
av aw ax ay az b1 b2 b3 b4 c1 c2 c3 c4; do
[ -f "$T/$id/res" ] && cat "$T/$id/res" >> "$LOG"
+63
View File
@@ -209,3 +209,66 @@ test('token_radius_default: a grow with no room on either side squeezes', async
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
// #35: scene `extra` uses the same opaque caps as wall `flags` (8 KB, 64 keys per object, depth 8).
function extraObjectWithKeys(n) {
const o = {}
for (let i = 0; i < n; i++) o[`k${i}`] = i
return o
}
function extraNested(depth) {
let o = {}
const root = o
for (let i = 1; i < depth; i++) { o.a = {}; o = o.a }
return root
}
test('scene.extra: size, key-count and depth caps, boundaries included (#35)', async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
const atCap = { blob: 'x'.repeat(8192 - 11) }
const mark = gm.ws.messages.length
const ack = await gm.ws.request({ t: 'scene.set', scene: gm.sceneId, extra: atCap })
assert.equal(ack.ok, true, JSON.stringify(ack))
const hello = await after(gm.ws, mark, 'hello')
assert.equal(JSON.stringify(hello.extra).length, 8192, 'exactly 8 KB is echoed')
// One byte over, with another declared field: `invalid`, and none of the patch is applied.
const refused = await gm.ws.request({ t: 'scene.set', scene: gm.sceneId, name: 'Nope',
extra: { blob: 'x'.repeat(8192 - 10) } })
assert.equal(refused.code, 'invalid')
const mark2 = gm.ws.messages.length
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
grid_size: 60 })).ok, true)
const hello2 = await after(gm.ws, mark2, 'hello')
assert.equal(hello2.scene.name, 'Scene', 'the refused patch applied no other field')
assert.equal(hello2.scene.grid_size, 60)
assert.match(s.log(), /opaque_refused field=extra limit=size/)
// Key count: 64 accepted, 65 refused; per object, so a 64-key nested object is fine.
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraObjectWithKeys(64) })).ok, true)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraObjectWithKeys(65) })).code, 'invalid')
const nested = extraObjectWithKeys(63)
nested.inner = extraObjectWithKeys(64)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: nested })).ok, true)
nested.inner = extraObjectWithKeys(65)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: nested })).code, 'invalid')
// Depth: 8 accepted, 9 refused.
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraNested(8) })).ok, true)
assert.equal((await gm.ws.request({ t: 'scene.set', scene: gm.sceneId,
extra: extraNested(9) })).code, 'invalid')
gm.ws.close()
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
+66
View File
@@ -723,3 +723,69 @@ test('restart: the typed walls, door state and the id counter are identical afte
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
// #35: wall `flags` is opaque but bounded: 8 KB serialized, 64 keys per object, depth 8.
function objectWithKeys(n) {
const o = {}
for (let i = 0; i < n; i++) o[`k${i}`] = i
return o
}
function nestedObjects(depth) {
let o = {}
const root = o
for (let i = 1; i < depth; i++) { o.a = {}; o = o.a }
return root
}
const atByteCap = () => ({ blob: 'x'.repeat(8192 - 11) })
const overByteCap = () => ({ blob: 'x'.repeat(8192 - 10) })
test('wall.flags: size, key-count and depth caps, boundaries included (#35)', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
// Exactly 8 KB is accepted and echoed; one byte over is `invalid` and stores nothing.
const ok = await addWall(gm, { a: [10, 10], b: [100, 10], flags: atByteCap() })
assert.equal(ok.ok, true, JSON.stringify(ok))
const frame = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === ok.id)
assert.equal(JSON.stringify(frame.flags).length, 8192)
const before = gm.ws.messages.filter(m => m.t === 'wall').length
const over = await addWall(gm, { a: [10, 20], b: [100, 20], flags: overByteCap() })
assert.equal(over.code, 'invalid')
await sleep(80)
assert.equal(gm.ws.messages.filter(m => m.t === 'wall').length, before,
'a refused add emits no wall frame')
assert.match(s.log(), /opaque_refused field=flags limit=size/)
assert.equal(s.log().includes('xxxxxxxx'), false, 'the refused value is never logged')
// The refused add consumes no wall id: the next wall follows the accepted one densely.
const next = await addWall(gm, { a: [10, 30], b: [100, 30] })
assert.equal(next.id, ok.id + 1)
// 64 keys per object accepted, 65 refused; the cap is per object, not per value.
assert.equal((await addWall(gm, { a: [5, 5], b: [60, 5],
flags: objectWithKeys(64) })).ok, true)
assert.equal((await addWall(gm, { a: [5, 15], b: [60, 15],
flags: objectWithKeys(65) })).code, 'invalid')
const nestedBase = objectWithKeys(63)
nestedBase.inner = objectWithKeys(64)
assert.equal((await addWall(gm, { a: [5, 25], b: [60, 25], flags: nestedBase })).ok, true)
nestedBase.inner = objectWithKeys(65)
assert.equal((await addWall(gm, { a: [5, 35], b: [60, 35], flags: nestedBase })).code, 'invalid')
// Depth 8 accepted, 9 refused.
assert.equal((await addWall(gm, { a: [5, 45], b: [60, 45],
flags: nestedObjects(8) })).ok, true)
assert.equal((await addWall(gm, { a: [5, 55], b: [60, 55],
flags: nestedObjects(9) })).code, 'invalid')
// A refused update leaves the previous flags intact.
const keep = await addWall(gm, { a: [200, 200], b: [300, 200], flags: { note: 'keep' } })
const bad = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: keep.id,
flags: overByteCap() })
assert.equal(bad.code, 'invalid')
const good = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: keep.id,
thickness: 5 })
assert.equal(good.ok, true)
const after = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === keep.id)
assert.deepEqual(after.flags, { note: 'keep' })
gm.ws.close()
})
})