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:
@@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user