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.
207 lines
8.8 KiB
Nim
207 lines
8.8 KiB
Nim
## Property buckets, their caps and the property-schema registry (DESIGN 6.1 prop payload rules, 8,
|
|
## 9; M3 slice 7, issue #22).
|
|
##
|
|
## A token (and, from slice 8, a character) carries three JSON objects by visibility bucket:
|
|
## `public` (anyone who sees it), `owner` (the token's controller + the GM) and `gm` (the GM only).
|
|
## The egress (views.nim) decides which buckets a recipient gets; nothing here knows about sockets.
|
|
##
|
|
## Caps [FIXED (owner-delegated, 2026-11-15, #22)]: at most 64 keys per bucket, a key is a string of
|
|
## 1..64 code points with no control characters, and a value's serialized JSON is at most 8 KB.
|
|
## 6.1 delegated these numbers (#11) and slice 7 fixes them.
|
|
##
|
|
## The registry (DESIGN 8/9): `key -> PropSchema{kind, bucket}` plus a fallback rule. v1 ships it
|
|
## **empty**, and its fallback is permissive (`pkAny`, any bucket), because DESIGN 8 says the server
|
|
## never interprets props in v1: any key may be written to any bucket, under the caps above. A key
|
|
## the rules module (FFF, post-v1) registers is enforced from then on: a value of the wrong JSON
|
|
## kind, or a write to a bucket the schema does not name, is `invalid`. That is the "the registry
|
|
## validates, the server does not interpret" rule of DESIGN 9 in one place: `checkWrite` is the only
|
|
## gate, so flipping the fallback to strict is a one-line change when FFF lands.
|
|
##
|
|
## Ordering: props are `JsonNode` objects, so key order is insertion order and a reload preserves
|
|
## it. JSON object equality ignores order (std/json), so a re-serialisation can never produce a
|
|
## spurious `token` frame (views.Known compares built frames).
|
|
import std/[json, options, tables]
|
|
import text
|
|
|
|
type
|
|
PropBucket* = enum
|
|
pbPublic = "public"
|
|
pbOwner = "owner"
|
|
pbGm = "gm"
|
|
|
|
PropKind* = enum
|
|
pkAny, pkString, pkNumber, pkBool, pkObject, pkArray
|
|
## The value kinds a schema can demand; `pkAny` accepts any JSON value (the v1 fallback).
|
|
|
|
PropSchema* = object
|
|
kind*: PropKind
|
|
bucket*: Option[PropBucket] ## none = the key may live in any bucket
|
|
|
|
Registry* = object
|
|
## `schemas` is what the rules module registered; `fallback` answers every other key.
|
|
schemas*: Table[string, PropSchema]
|
|
fallback*: PropSchema
|
|
|
|
Props* = array[PropBucket, JsonNode]
|
|
## One JSON object per bucket, never nil. A record's props always have all three buckets even
|
|
## when they are empty, so egress can filter by bucket alone.
|
|
|
|
const
|
|
MaxPropKeys* = 64
|
|
## 6.1, numbers delegated (#11), fixed by slice 7.
|
|
MaxPropKeyLen* = 64
|
|
## 1..64 code points, same rule as a character name.
|
|
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()
|
|
|
|
proc normalize*(p: Props): Props =
|
|
## Stored-props invariants: every bucket is an object, never nil.
|
|
result = p
|
|
for b in PropBucket:
|
|
if result[b].isNil or result[b].kind != JObject: result[b] = newJObject()
|
|
|
|
proc parseBucket*(raw: string): Option[PropBucket] =
|
|
for b in PropBucket:
|
|
if $b == raw: return some(b)
|
|
|
|
proc bucketName*(b: PropBucket): string = $b
|
|
|
|
proc validPropKey*(key: string): bool = validText(key, MaxPropKeyLen)
|
|
|
|
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)))
|
|
|
|
proc schemaFor*(reg: Registry, key: string): PropSchema =
|
|
reg.schemas.getOrDefault(key, reg.fallback)
|
|
|
|
proc kindMatches*(kind: PropKind, value: JsonNode): bool =
|
|
if value.isNil: return false
|
|
case kind
|
|
of pkAny: true
|
|
of pkString: value.kind == JString
|
|
of pkNumber: value.kind in {JInt, JFloat}
|
|
of pkBool: value.kind == JBool
|
|
of pkObject: value.kind == JObject
|
|
of pkArray: value.kind == JArray
|
|
|
|
proc checkWrite*(reg: Registry, bucket: PropBucket, key: string, value: JsonNode): bool =
|
|
## The registry gate for one write. Structural checks live in `parseProps`/`setProp`; this is the
|
|
## per-key schema (kind, bucket). Unregistered keys use the fallback rule.
|
|
let schema = reg.schemaFor(key)
|
|
if schema.bucket.isSome and schema.bucket.get != bucket: return false
|
|
kindMatches(schema.kind, value)
|
|
|
|
proc setProp*(p: var Props, bucket: PropBucket, key: string, value: JsonNode): bool =
|
|
## Sets one value. false = the bucket is at its key cap and the key is new (nothing changed).
|
|
if p[bucket].isNil or p[bucket].kind != JObject: p[bucket] = newJObject()
|
|
if not p[bucket].hasKey(key) and p[bucket].len >= MaxPropKeys: return false
|
|
p[bucket][key] = value
|
|
true
|
|
|
|
proc unsetProp*(p: var Props, bucket: PropBucket, key: string): bool =
|
|
## Removes one value; false = it was not there (unsetting an absent key is still `ok` on the wire).
|
|
if p[bucket].isNil or p[bucket].kind != JObject: return false
|
|
result = p[bucket].hasKey(key)
|
|
if result: p[bucket].delete(key)
|
|
|
|
proc contains*(p: Props, bucket: PropBucket, key: string): bool =
|
|
p[bucket].kind == JObject and p[bucket].hasKey(key)
|
|
|
|
proc propsJson*(p: Props): JsonNode =
|
|
## All three buckets: the shape a GM frame carries (6.1).
|
|
result = newJObject()
|
|
for b in PropBucket: result[$b] = p[b]
|
|
|
|
proc clone*(p: Props): Props =
|
|
## A copy whose buckets are fresh objects: a value node is shared (never mutated), but adding or
|
|
## removing a key cannot reach the original bucket. `main.nim` copies a record before it edits it,
|
|
## and a Nim array of refs does not give that for free.
|
|
for b in PropBucket:
|
|
var bucket = newJObject()
|
|
if not p[b].isNil and p[b].kind == JObject:
|
|
for key, value in p[b]: bucket[key] = value
|
|
result[b] = bucket
|
|
|
|
proc valid*(p: Props): bool =
|
|
## The stored-record invariant: every bucket an object, every key and value within the caps.
|
|
result = true # Nim's default for `bool` is false: set it before the early returns below
|
|
for b in PropBucket:
|
|
if p[b].isNil or p[b].kind != JObject: return false
|
|
if p[b].len > MaxPropKeys: return false
|
|
for key, value in p[b]:
|
|
if not validPropKey(key): return false
|
|
if value.kind == JNull: return false # 6.1: a value is JSON, null is not a value
|
|
if valueBytes(value) > MaxPropValueBytes: return false
|
|
|
|
proc parseProps*(reg: Registry, node: JsonNode): Option[Props] =
|
|
## `props` on `token.create` (6.1): absent/null = all empty; else an object whose keys are the
|
|
## three bucket names and whose values are objects that pass the caps and the registry.
|
|
## `some(...)` = valid, `none` = refused `invalid`.
|
|
if node.isNil or node.kind == JNull: return some(newProps())
|
|
if node.kind != JObject: return none(Props)
|
|
var parsed = newProps()
|
|
for name, bucketNode in node:
|
|
let bucket = parseBucket(name)
|
|
if bucket.isNone: return none(Props)
|
|
if bucketNode.kind != JObject: return none(Props)
|
|
for key, value in bucketNode:
|
|
if not validPropKey(key): return none(Props)
|
|
if value.kind == JNull: return none(Props)
|
|
if valueBytes(value) > MaxPropValueBytes: return none(Props)
|
|
if not reg.checkWrite(bucket.get, key, value): return none(Props)
|
|
if not parsed.setProp(bucket.get, key, value): return none(Props)
|
|
some(parsed)
|