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
+49 -4
View File
@@ -54,6 +54,55 @@ const
MaxPropValueBytes* = 8192
## 6.1: a value's serialized JSON is at most 8 KB.
# Opaque authored objects (`wall.flags`, `scene.extra`) [FIXED (owner-delegated), 2026-11-20,
# #35]. The same 8 KB measure as a prop value, read from one place; the key and depth caps bound a
# pasted blob before it is stored and echoed to every GM connect.
MaxOpaqueObjectBytes* = 8192
MaxOpaqueObjectKeys* = 64
MaxOpaqueObjectDepth* = 8
## A top-level object is depth 1; depth counts nested objects and arrays along any path.
type
OpaqueObjectLimit* = enum
olOk
olSize ## serialized JSON is over MaxOpaqueObjectBytes
olKeys ## one object has over MaxOpaqueObjectKeys keys
olDepth ## a container is deeper than MaxOpaqueObjectDepth
proc limitName*(limit: OpaqueObjectLimit): string =
case limit
of olOk: "ok"
of olSize: "size"
of olKeys: "keys"
of olDepth: "depth"
proc valueBytes*(value: JsonNode): int =
## The cap is on the serialized size (6.1), which is the number that actually crosses the wire.
if value.isNil: 0 else: ($value).len
proc checkOpaqueObject*(node: JsonNode): OpaqueObjectLimit =
## The one cap check for both call sites (wall `flags`, scene `extra`): size, key count and depth
## in one walk. The contents stay uninterpreted; only these bounds are enforced.
if node.isNil: return olOk
if valueBytes(node) > MaxOpaqueObjectBytes: return olSize
proc walk(n: JsonNode, depth: int): OpaqueObjectLimit =
case n.kind
of JObject:
if depth > MaxOpaqueObjectDepth: return olDepth
if n.len > MaxOpaqueObjectKeys: return olKeys
for _, value in n:
let r = walk(value, depth + 1)
if r != olOk: return r
of JArray:
if depth > MaxOpaqueObjectDepth: return olDepth
for value in n:
let r = walk(value, depth + 1)
if r != olOk: return r
else: discard
olOk
walk(node, 1)
proc newProps*(): Props =
for b in PropBucket: result[b] = newJObject()
@@ -71,10 +120,6 @@ proc bucketName*(b: PropBucket): string = $b
proc validPropKey*(key: string): bool = validText(key, MaxPropKeyLen)
proc valueBytes*(value: JsonNode): int =
## The cap is on the serialized size (6.1), which is the number that actually crosses the wire.
if value.isNil: 0 else: ($value).len
proc defaultRegistry*(): Registry =
## v1's registry: nothing registered, every key accepted (DESIGN 8: props are not interpreted).
Registry(fallback: PropSchema(kind: pkAny, bucket: none(PropBucket)))