diff --git a/doc/2026-11-04_audit_id3232_receipt_validation.md b/doc/2026-11-04_audit_id3232_receipt_validation.md new file mode 100644 index 0000000000..5cb66e092a --- /dev/null +++ b/doc/2026-11-04_audit_id3232_receipt_validation.md @@ -0,0 +1,82 @@ +# Receipt-validation audit for `pkgs/id` artifacts (#3232) + +- Audit date: 2026-11-04 +- Code audited: `main` at `9ac5314f6a1904542094582614397527a1ea60b0` (merged into `fix/id-3232` before this audit) +- Design: [#3231](https://github.com/developing-today/code/issues/3231), [`doc/2026-11-01_design_id3231.md`](2026-11-01_design_id3231.md) +- Audit task: [#3232](https://github.com/developing-today/code/issues/3232) + +## Gate and contract status + +The Phase 1–2 gate was closed before this audit began: + +| Phase | Issue | Closing evidence | +| --- | --- | --- | +| 1 — list integrity | #3196 | Closed 2026-11-02; PR #4318, with final builder/error-classification work in #5576. | +| 1 — list integrity | #3146 | Closed 2026-11-02; PR #4368, with replay behavior completed in #5576. | +| 2 — text fields | #3192 | Closed 2026-11-02; PR #4331. | +| 2 — home declarations | #3187 | Closed 2026-11-04; PR #4432. | + +PR [#5576](https://github.com/developing-today/code/pull/5576) merged at `876c2f8539e70068e13b84d1aed79771a1c1ad6b` on 2026-11-04. Its `artifact.rs` module documentation already states the contract at lines 11–21: known kinds are validated against producer rules, policy refusals are `Forbidden`, signed bytes are not rewritten, unknown kinds remain envelope-only, and replay carries scoped grandfathering. No source change was needed for that acceptance item. Issue #3232 closed after #5576, but the field audit remained outstanding; this file is the closing evidence for that audit. The open replay follow-ups below are not hidden by that closure. + +## Disposition table + +`(a)` means a fresh receipt check is present; `(b)` means the omission is deliberate, with its reason; `(c)` means the audit found a gap and routed it to a named follow-up. Receipt is `ArtifactLog::insert` → `verify` → `parsed`; journal replay uses `store(..., false)` → `verify` → `parsed_for_replay` (`artifact.rs:425–529, 563–620, 733–775`). + +### Common envelope + +| Field | Disposition | Rule and evidence | +| --- | --- | --- | +| `author` | (a) | `check_authenticity` requires a canonical account key and a valid Ed25519 key before verifying the signature (`artifact.rs:390–416`; `directory.rs:2110–2114`). `sign` derives it from the signing key. The author is also checked against kind-specific authority rules below. | +| `seq` | (a) | Construction requires `seq >= 1`; `ArtifactLog::sign_after` chooses the next position. `store` refuses zero, gaps, forks, and an unexpected position (`artifact.rs:335, 691–703, 577–605`). | +| `prev` | (a), with (b) for separate syntax | `store` requires the empty string at sequence 1 or exact equality with the held previous artifact's verified hash (`artifact.rs:606–614`). No independent hash-shape rule is needed: the only accepted nonempty value is the prior verified hash. | +| `kind` | (a) for known kinds; (b) for unknown kinds | Known kinds are exact protocol literals matched by `parsed_mode`. Unknown strings have no separate length or NFC rule and return `Ok(None)`; their bodies are not applied. This preserves the module's forward-compatibility rule. Fresh insertion still applies the whole-frame limit (`artifact.rs:418–425, 437–523, 563–575`). | +| `body` | (a) for known-kind schema; (b) for unknown-kind payload and extra keys | A known kind must deserialize to its typed body and pass the checks below. Unknown-kind JSON is opaque and unapplied. Serde ignores extra object keys in the known structs; there is no `deny_unknown_fields`. Keep that behavior for cross-version payload compatibility absent evidence that historical signed objects must be rejected; the bytes remain signed and are never rewritten (`artifact.rs:526–529`). | +| `after` | (a) for fresh construction/receipt; (c) for pre-upgrade replay | `check_after`, called by `sign_after` and `verify`, enforces at most 32 lowercase 64-hex hashes and no duplicates (`artifact.rs:327–388`). The new duplicate rule is not bypassed by replay. Before #3196, the public builder and parser could persist duplicate entries, so reopening an artifact produced under that behavior can now fail. Routed to [#5625](https://github.com/developing-today/code/issues/5625); no replay behavior changed in this audit. | +| `hash` | (a) | Recomputed from the canonical signed body and compared exactly with `self.hash`; this also requires the producer's lowercase hex spelling (`artifact.rs:390–406`). | +| `signature` | (a), with (b) for hex case | The decoder requires 64 bytes of ASCII hex and verifies the Ed25519 signature strictly against `author` (`artifact.rs:407–416`; `directory.rs:2215–2227`). The producer writes lowercase hex. The decoder accepts uppercase hex as an equivalent encoding of the same signature bytes; no separate case rule is imposed because signature validity is over the decoded bytes. | + +### Known-kind bodies + +| Kind / field | Disposition | Rule and evidence | +| --- | --- | --- | +| `friend_remove.a`, `.b` | (a) | Both must be canonical account keys, must differ, and `author` must equal one of them (`artifact.rs:439–478`). | +| `friend_remove.requests` | (a) for fresh receipt; (b) for duplicate replay; (c) for upstream ID admission | Missing `requests` defaults to an empty list. Fresh receipt allows at most three unique, 32-character lowercase-hex IDs; it refuses rather than deduplicates (`artifact.rs:443–468`). `Directory::friendship_requests` sorts/deduplicates its known request IDs and caps the producer result at three (`directory.rs:1553–1568, 2003–2015`). Replay permits duplicate IDs because applying the removal is idempotent, but still checks the count and ID shape. Separate gap: `Envelope::verify` checks signature only, while `Directory::receive` persists the signed `Envelope.id`; a noncanonical incoming ID can later make a legitimate removal fail this artifact rule. Routed to [#5624](https://github.com/developing-today/code/issues/5624). | +| `home_declared.account` | (a) | Must be a canonical account key and equal the signing author (`artifact.rs:250–255`). | +| `home_declared.servers` | (a) | At most eight entries (`artifact.rs:257–260`). Fresh declarations may name each validated endpoint identity once; duplicates compare `EndpointAddr.id`, so a bare ID and a ticket for the same node collide (`artifact.rs:262–273`; tests at `artifact.rs:2162–2186`). The duplicate rule is skipped for local replay to preserve old accepted logs. `Directory::declare_home` runs the shared check before signing (`directory.rs:1578–1588`). | +| `home_declared.servers[].endpoint` | (a) | Every endpoint is parsed by `check_target`; malformed targets are forbidden on fresh receipt and replay (`artifact.rs:265–273`; `envelope_outbox.rs:457`). The validated identity also governs list uniqueness. | +| `home_declared.servers[].world_id` | (a) for fresh receipt; (b) for local replay; (c) for legacy peer pulls | Fresh values are 1–64 UTF-8 bytes, not whitespace-only, and contain no control (`Cc`) or format (`Cf`) characters (`artifact.rs:274–300`). Local replay skips the new bounds/categories, preserving older declarations; the old non-empty check remains. Evidence shows `ArtifactLog::declared` consumers use the endpoint for outbound delivery and only test whether the server list is nonempty (`directory_view.rs:379–405, 1444–1463`); they do not resolve `world_id` to a served world. `WorldCore::new` requires only nonblank IDs (`world/core.rs:280–296`), while `WorldHub::validate_world_name` applies the narrower `a-z0-9-_` grammar to local hub names (`world_hub.rs:36–59`). No current declared-world consumer justifies imposing that full charset on `world_id`; retain the recorded #3231 rule. Remote pull is different: `Directory::receive_artifact` still uses strict `parsed()`, so a pre-upgrade declaration can halt a peer pull. Existing follow-up [#4459](https://github.com/developing-today/code/issues/4459) records the accepted artifact-format cutoff and awaits implementation (task 9660). | +| `document.media_type` | (a) for fresh receipt; (c) for historical replay | Must be 1–128 bytes and NFC (`artifact.rs:485–492`), matching the sibling `DocumentRef::check` (`documents.rs:99–119`). `Directory::publish_document` is refused by the shared insertion parser on current builds, but the pre-#3192 producer/parser path could persist non-NFC values. `parsed_for_replay` still applies NFC unconditionally, so affected old logs may fail to reopen. Routed with tag text and duplicate `after` to [#5625](https://github.com/developing-today/code/issues/5625). | +| `document.content` | (a) for serialized size; (b) for internal text | Serialized JSON is limited to 64 KiB (`artifact.rs:493–500`). No recursive NFC check is applied to string values or object keys. Treat content as opaque structured payload: `Directory::publish_document` accepts a general JSON value, the sibling `DocumentRef` rule covers media type rather than content, and the chain event rule (`chain.rs:474–494`) is specific to its CBOR event value. Do not extend the chain rule to artifact payload text without producer/consumer evidence. | +| `tag.subject` | (a) | Must be a lowercase 64-hex artifact hash (`artifact.rs:503–507`). Receipt does not require that the referenced artifact is already present: the tag names a content address and may arrive before its subject. | +| `tag.name` | (a) for fresh receipt; (c) for historical replay | Must be 1–64 bytes and NFC (`artifact.rs:508–512`), matching `tag_claims::check_text`'s corresponding name rule (`tag_claims.rs:151–162`). Pre-#3192 artifact producers could persist non-NFC names; replay still applies NFC unconditionally. Routed to [#5625](https://github.com/developing-today/code/issues/5625). | +| `tag.value` | (a) for fresh receipt; (c) for historical replay | Optional (absent defaults to `None`); when present, it is at most 256 bytes and NFC (`artifact.rs:514–519`). The sibling tag validator allows an empty value and applies the same NFC/byte-limit style (`tag_claims.rs:126–129, 151–162`). Pre-#3192 non-NFC values may be in old logs; routed to [#5625](https://github.com/developing-today/code/issues/5625). | + +## Explicit audit decisions + +1. **Unknown `kind` string — forward compatibility wins.** Do not add the chain event's 1–`MAX_KIND_BYTES`/NFC rule to unknown artifact kinds. The artifact contract deliberately stores, but never applies, kinds this version does not know; the fresh frame limit still bounds total encoded size. Known kinds are exact constants. +2. **`document.content` strings — opaque payload.** Enforce serialized JSON size only. The document sibling checks media type, while the recursive NFC rule belongs to the chain event value model; no artifact consumer establishes an all-strings NFC invariant. +3. **`prev` shape — chain relation is sufficient.** Sequence 1 requires empty `prev`; later entries must equal the verified previous hash in the author's chain. An independent syntax check would duplicate the stronger position check. +4. **Builder symmetry — known-kind generic signing needs follow-up.** `SignedArtifact::sign`/`sign_after` are generic and support future kinds (for example, the board artifact producer); `sign_after` enforces `after` but does not call `parsed()` for recognized kinds. The directory's document/tag paths eventually refuse invalid values at `ArtifactLog::insert`, but the public generic signer can return a known-kind artifact that receipt rejects. Since `parsed()` returns `None` for unknown kinds, validating recognized kinds would preserve forward compatibility. This is a rule-1 gap, routed to [#5623](https://github.com/developing-today/code/issues/5623), not fixed in the audit. `Directory::declare_home` already calls its shared check before signing. +5. **`world_id` charset — do not impose hub-name grammar.** `declared()` consumers route to the declared endpoint and do not resolve the world ID; local `WorldCore` also accepts any nonblank ID. Keep the design's 1–64 byte/nonblank/no-`Cc`/no-`Cf` rule, not `validate_world_name`'s ASCII grammar. + +## Follow-ups and replay risk + +The audit found and routed the following (c) items; no implementation fixes were included here: + +- [#5623](https://github.com/developing-today/code/issues/5623) — validate known-kind bodies in generic signing helpers while preserving unknown-kind signing. +- [#5624](https://github.com/developing-today/code/issues/5624) — validate incoming friend-request IDs and keep friendships with already-persisted noncanonical IDs removable. +- [#5625](https://github.com/developing-today/code/issues/5625) — preserve local replay of pre-upgrade duplicate-`after` and non-NFC document/tag artifacts while keeping fresh receipts strict. +- [#4459](https://github.com/developing-today/code/issues/4459) — existing peer-pull compatibility follow-up for pre-upgrade `home_declared` artifacts; the artifact-format cutoff ruling is recorded, but the receive-path implementation remains open. + +The local replay distinction is deliberate only where explicitly implemented: `home_declared` skips #3187's new world-ID/duplicate-endpoint rules, and `friend_remove` skips only its newly added duplicate-list refusal. The audit did not broaden those exceptions or change any receipt behavior. + +## Verification record + +Inspected the merged implementations and existing tests in `artifact.rs`, `directory.rs`, `documents.rs`, `tag_claims.rs`, `chain.rs`, `world_hub.rs`, `world/core.rs`, and `directory_view.rs`. Existing tests cover unknown-kind storage, fresh request-list checks, home-ID constraints, duplicate endpoint identity, and local replay of long `world_id` and duplicate `friend_remove.requests` cases. + +Targeted tests passed with the package's normal feature set: + +- `cargo test -p id --lib artifact::tests` — 37 passed. +- `cargo test -p id --lib directory::tests::` — 53 passed. + +The initial `--all-features` attempt could not compile because this worktree lacks `pkgs/id/web/dist`, causing the `RustEmbed` asset type to lack generated `get`/`iter` methods; it was superseded by the successful normal-feature runs above. This audit added no implementation or validation changes; the only audit deliverable is this evidence file.