Skip to content

Commit 284cc69

Browse files
tlongwell-blockDawn (sprout agent)
andcommitted
feat(audit): widen NewAuditEntry.community_id to CommunityId
Make the provenance fence visible in the type signature, not a per-call-site convention. `NewAuditEntry.community_id` becomes `CommunityId` (the server-resolved newtype) instead of a raw `Uuid`, so a wiring call site can no longer pass an arbitrary UUID off the event/channel being acted on — the only doors to a `CommunityId` are host resolution or a server-scoped DB row, never client input. The DB-row type `AuditEntry` stays `Uuid`: sqlx reads/writes it directly and `compute_hash` does `.as_bytes()` on it, so the stored hash bytes are byte-for-byte identical and the already-integrated chain stays valid — no migration, no re-hash. The `as_uuid()` dereference moves inside `AuditService::log` at the DB boundary, where the column is written; the advisory-lock key is unchanged (CommunityId's Display delegates to Uuid). Drop the now-orphaned `Serialize`/`Deserialize` derive (and the `#[serde(default)]` on `detail`) from `NewAuditEntry`: it has no serde consumer — it travels only through the in-process audit sink (mpsc), never a wire/DB boundary. Keeping it non-deserializable reinforces the fence: no client blob can mint a NewAuditEntry. Full package green (13/13, incl. the 6 PG isolation tests and the community_id_is_part_of_identity fence); clippy -D warnings + fmt clean. Adversarially verified the fence is non-vacuous: dropping community_id from compute_hash turns community_id_is_part_of_identity RED, restored. Co-authored-by: Dawn (sprout agent) <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co> Signed-off-by: tlongwell-block <[email protected]>
1 parent ba11d66 commit 284cc69

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎crates/buzz-audit/src/entry.rs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use buzz_core::CommunityId;
12
use chrono::{DateTime, Utc};
23
use serde::{Deserialize, Serialize};
34
use uuid::Uuid;
@@ -41,10 +42,19 @@ pub struct AuditEntry {
4142
/// `community_id` is the **server-resolved** tenant (from the request's
4243
/// `TenantContext`), never a client-supplied value — the same provenance rule
4344
/// the whole multi-tenant model rests on.
44-
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45+
///
46+
/// Not `Serialize`/`Deserialize`: this is an in-process input struct (consumed
47+
/// by `AuditService::log`, threaded through the in-memory audit sink), never
48+
/// crossing a wire or DB boundary as a whole. Keeping it non-deserializable
49+
/// reinforces the fence — there is no path by which a client-supplied blob
50+
/// becomes a `NewAuditEntry` (and thus a `CommunityId`).
51+
#[derive(Debug, Clone, PartialEq, Eq)]
4552
pub struct NewAuditEntry {
46-
/// Server-resolved community this entry belongs to.
47-
pub community_id: Uuid,
53+
/// Server-resolved community this entry belongs to. Typed as [`CommunityId`]
54+
/// (not a raw `Uuid`) so the provenance rule is visible in the signature:
55+
/// the only ways to obtain one are host resolution or a server-scoped DB
56+
/// row — never a value parsed from client input.
57+
pub community_id: CommunityId,
4858
/// Action that was performed.
4959
pub action: AuditAction,
5060
/// Raw bytes of the actor's Nostr pubkey, if the action has one.
@@ -58,6 +68,5 @@ pub struct NewAuditEntry {
5868
/// or other secrets here. `AuthSuccess`/`AuthFailure` entries carry only
5969
/// outcome metadata — the token has no slot in this type, and `detail` must
6070
/// not become one.
61-
#[serde(default)]
6271
pub detail: serde_json::Value,
6372
}

‎crates/buzz-audit/src/service.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ impl AuditService {
7777
) -> Result<AuditEntry, AuditError> {
7878
let mut tx = conn.begin().await?;
7979

80+
// The stored row keys on the raw UUID; the typed `CommunityId` on the
81+
// input is the provenance fence, dereferenced here at the DB boundary.
82+
let community_id = *entry.community_id.as_uuid();
83+
8084
// Head of THIS community's chain — scoped by community_id.
8185
let head = sqlx::query(
8286
"SELECT seq, hash FROM audit_log
8387
WHERE community_id = $1
8488
ORDER BY seq DESC LIMIT 1",
8589
)
86-
.bind(entry.community_id)
90+
.bind(community_id)
8791
.fetch_optional(&mut *tx)
8892
.await?;
8993

@@ -99,7 +103,7 @@ impl AuditService {
99103
let created_at: DateTime<Utc> = Utc::now();
100104

101105
let mut audit_entry = AuditEntry {
102-
community_id: entry.community_id,
106+
community_id,
103107
seq,
104108
hash: Vec::new(),
105109
prev_hash,
@@ -280,7 +284,7 @@ mod tests {
280284

281285
fn new_entry(community_id: Uuid, action: AuditAction) -> NewAuditEntry {
282286
NewAuditEntry {
283-
community_id,
287+
community_id: CommunityId::from_uuid(community_id),
284288
action,
285289
actor_pubkey: Some(vec![0xab; 32]),
286290
object_id: Some(format!("obj_{}", Uuid::new_v4())),

0 commit comments

Comments
 (0)