# Atomic DB, escaping, clamping - Status: SHIPPED in C++ port - Spec: FR-12, caps table - Sources: `tosv.cpp` load_db plus save_db, `archive/tosv.py` legacy, `ARCHITECTURE.md`, SPEC.md 6.1 ## What it is The grant store is a flat `key=value` plus `grant=` text file saved by write-tmp-plus-rename, with backslash and equals escaping on values, pipe-separated grant fields, and layers clamped to 1-8 on load. A crash mid-save leaves the old DB or the new DB, never a mix. ## How TOSV does it `save_db` serializes to `tosv_db.txt.tmp` then `rename`s over the live file (atomic on POSIX). `load_db` unescapes, parses grants by `|` split, and clamps layers with max-min. Legacy Python used JSON with `os.replace`; same atomicity, heavier parser. The C++ port dropped JSON to kill a whole parser class. ## What it protects against Corruption from crashes and kills (the vault dies mid-grant without losing prior grants), line-injection via form fields (escaping keeps `=` and `\` inside values), and layer-overflow weirdness (clamping). ## Attack realism High for reliability-as-security: vaults that corrupt themselves under load push operators toward dangerous manual edits. Atomic saves plus tiny parser surface make the store boring, which is the goal. Against malicious local writers, file perms carry the load. ## Pros Crash-safe by construction. No JSON parser exploits possible. Human-readable and human-repairable with a text editor. ## Cons and how the vault resolves them - Whole-DB rewrite per grant. Resolved by the 10k-grant [cap](sec-caps.md): rewrites stay milliseconds; sharding is tracked under assumption A-03 if ever needed. - Concurrent grants can interleave. Closed in M5: DBM reload-modify-save locks on grant and bootstrap paths, proven by a 20-parallel Gate 2 hammer with zero lost grants. - Flat file, no transactions. Accepted: single-writer grant flow needs none; multivalue atomicity comes free from whole-file rename. - Manual edits can still break lines. Resolved by load-time tolerance (bad lines skipped, layers clamped) plus the [audit](sec-audit.md) trail showing what changed when. ## Residual risk None structural at v1 scale; growth past the caps triggers the tracked A-03 ADR, not silent pain. M6: grant fields escape pipes plus newlines (injection proof in Gate 2), writes are checked with 500 on failure, and the DB file is 0600. ## See also - [Security index](security.md), [caps](sec-caps.md), [audit](sec-audit.md), [grant creds](sec-grant-creds.md)