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
+12
View File
@@ -541,6 +541,12 @@ proc declared(env: Envelope, key: string): bool =
## `vision_radius: null` means "the scene default" (6.1), so both are real patch fields.
not env.node.isNil and env.node.hasKey(key)
proc opaqueRefused(field: string, node: JsonNode) =
## #35: one structured line naming the field and the limit it broke, never the contents.
let limit = props.checkOpaqueObject(node)
if limit != props.olOk:
app.lg.log(lvInfo, "opaque_refused", [("field", field), ("limit", props.limitName(limit))])
# Wall payload fields (DESIGN 6.1, proto 2, #54). `kind` and (in an update) `open` are never read:
# they are unknown fields and ignored (6.2). Absent and JSON null are the same field-absent for
# every wall field except `height` and `label`, where an explicit null means "clear it".
@@ -638,6 +644,8 @@ proc parseWallPatch(env: Envelope, patch: var WallPatch, allowOpen = false): boo
if present(env, "flags"):
let f = parseFlags(env.node{"flags"})
if f.isNone: return false
# #35: wall `flags` is opaque but bounded (token `flags` keeps its old, uncapped path).
if props.checkOpaqueObject(f.get) != props.olOk: return false
patch.flags = f
true
@@ -1993,6 +2001,8 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
if not sceneOk(conn, env): return
var patchOpt = parseScenePatch(env.node)
if patchOpt.isNone:
if not env.node.isNil and env.node.hasKey("extra"):
opaqueRefused("extra", env.node{"extra"})
ackErr(conn, env.reqId, CodeInvalid)
return
var patch = patchOpt.get
@@ -2056,6 +2066,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
# #55); `a` and `b` are the only required fields here.
var patch: WallPatch
if not parseWallPatch(env, patch, allowOpen = true):
if present(env, "flags"): opaqueRefused("flags", env.node{"flags"})
ackErr(conn, env.reqId, CodeInvalid)
return
if patch.a.isNone or patch.b.isNone:
@@ -2112,6 +2123,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
return
var patch: WallPatch
if not parseWallPatch(env, patch):
if present(env, "flags"): opaqueRefused("flags", env.node{"flags"})
ackErr(conn, env.reqId, CodeInvalid)
return
if not wallPatchHasField(patch):
+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)))
+3 -1
View File
@@ -32,7 +32,7 @@
## no locking: the caller holds the global lock (4.3).
import std/[json, math, options, strutils]
import db_connector/db_sqlite
import db, geometry, text
import db, geometry, props, text
type
WallPosition* = enum
@@ -344,6 +344,8 @@ proc parseScenePatch*(node: JsonNode): Option[ScenePatch] =
if node.hasKey("extra"):
let v = node["extra"]
if v.kind != JObject: return none(ScenePatch)
# #35: the same opaque caps as wall `flags`; the contents stay uninterpreted.
if checkOpaqueObject(v) != olOk: return none(ScenePatch)
p.extra = some(v)
any = true
if not any: return none(ScenePatch)