feat(auth): persist protected transport authority

Signed-off-by: Cea Stapleton Cordasco <261786559+cea-block@users.noreply.github.com>
This commit is contained in:
Cea Stapleton Cordasco
2026-08-04 15:49:23 -05:00
parent dc43209fd2
commit 7421262f71
41 changed files with 15476 additions and 1152 deletions
Generated
+1
View File
@@ -1056,6 +1056,7 @@ version = "0.1.0"
dependencies = [
"axum",
"blurhash",
"buzz-auth",
"buzz-core",
"bytes",
"chrono",
+212
View File
@@ -0,0 +1,212 @@
//! Blossom kind:24242 authentication verification (BUD-11 compliant).
/// Blossom kind:24242 verbs Buzz currently accepts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlossomVerb {
/// Authorize one blob upload.
Upload,
/// Authorize one blob download or a server-scoped download.
Get,
}
impl BlossomVerb {
fn as_str(self) -> &'static str {
match self {
Self::Upload => "upload",
Self::Get => "get",
}
}
}
/// Rejection from full Blossom operation verification.
#[derive(Debug, thiserror::Error)]
pub enum BlossomAuthError {
/// The Schnorr signature is invalid.
#[error("invalid signature")]
InvalidSignature,
/// The event is not kind 24242.
#[error("invalid auth event kind")]
InvalidAuthKind,
/// The event does not contain a human-readable description.
#[error("invalid auth event")]
InvalidAuthEvent,
/// The `t` tag does not name the required operation.
#[error("invalid auth verb")]
InvalidAuthVerb,
/// A required tag is absent.
#[error("missing required tag: {0}")]
MissingTag(&'static str),
/// The event has expired.
#[error("token expired")]
TokenExpired,
/// The event creation time is outside the accepted replay window.
#[error("timestamp out of window")]
TimestampOutOfWindow,
/// A server tag does not match the request-bound host.
#[error("server mismatch")]
ServerMismatch,
/// No `x` tag matches the exact blob hash.
#[error("hash mismatch")]
HashMismatch,
/// The event does not authorize the requested blob or server.
#[error("insufficient scope")]
InsufficientScope,
}
/// Verify common kind:24242 Blossom event validity for one exact verb.
///
/// This verifies the signature, kind, non-empty content, verb, expiration,
/// creation-time replay window, and any request-bound server tags. It does not
/// check verb-specific blob scope.
pub fn verify_blossom_auth_event_for_verb(
auth_event: &nostr::Event,
verb: BlossomVerb,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), BlossomAuthError> {
auth_event
.verify()
.map_err(|_| BlossomAuthError::InvalidSignature)?;
if auth_event.kind.as_u16() != 24242 {
return Err(BlossomAuthError::InvalidAuthKind);
}
if auth_event.content.trim().is_empty() {
return Err(BlossomAuthError::InvalidAuthEvent);
}
let mut found_t = false;
let mut found_exp = false;
let mut server_tags: Vec<&str> = Vec::new();
let mut exp_value: u64 = 0;
for tag in auth_event.tags.iter() {
match tag.kind().to_string().as_str() {
"t" => {
if let Some(value) = tag.content() {
if value != verb.as_str() {
return Err(BlossomAuthError::InvalidAuthVerb);
}
found_t = true;
}
}
"expiration" => {
if let Some(value) = tag.content() {
exp_value = value.parse().unwrap_or(0);
found_exp = true;
}
}
"server" => {
if let Some(value) = tag.content() {
server_tags.push(value);
}
}
_ => {}
}
}
if !found_t {
return Err(BlossomAuthError::MissingTag("t"));
}
if !found_exp {
return Err(BlossomAuthError::MissingTag("expiration"));
}
let now = nostr::Timestamp::now().as_secs();
if exp_value <= now {
return Err(BlossomAuthError::TokenExpired);
}
let created = auth_event.created_at.as_secs();
if created > now + 5 || now > created + max_age_secs {
return Err(BlossomAuthError::TimestampOutOfWindow);
}
if !server_tags.is_empty() {
let Some(domain) = server_domain else {
return Err(BlossomAuthError::ServerMismatch);
};
let expected = normalize_server_host(domain);
if !server_tags
.iter()
.any(|tag| normalize_server_host(tag) == expected)
{
return Err(BlossomAuthError::ServerMismatch);
}
}
Ok(())
}
/// Verify common upload auth event validity without checking the blob hash.
pub fn verify_blossom_auth_event(
auth_event: &nostr::Event,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), BlossomAuthError> {
verify_blossom_auth_event_for_verb(auth_event, BlossomVerb::Upload, server_domain, max_age_secs)
}
/// Verify a kind:24242 upload event including the exact `x` tag blob hash.
pub fn verify_blossom_upload_auth(
auth_event: &nostr::Event,
sha256: &str,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), BlossomAuthError> {
verify_blossom_auth_event_for_verb(
auth_event,
BlossomVerb::Upload,
server_domain,
max_age_secs,
)?;
let has_matching_x = auth_event
.tags
.iter()
.any(|tag| tag.kind().to_string() == "x" && tag.content() == Some(sha256));
if !has_matching_x {
return Err(BlossomAuthError::HashMismatch);
}
Ok(())
}
/// Verify a kind:24242 download event for one exact blob and server.
///
/// BUD-01 permits either an `x` tag matching `sha256` or a matching `server`
/// tag. Callers must still enforce relay membership after this verifier.
pub fn verify_blossom_get_auth(
auth_event: &nostr::Event,
sha256: &str,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), BlossomAuthError> {
verify_blossom_auth_event_for_verb(auth_event, BlossomVerb::Get, server_domain, max_age_secs)?;
let has_matching_x = auth_event
.tags
.iter()
.any(|tag| tag.kind().to_string() == "x" && tag.content() == Some(sha256));
let has_matching_server = server_domain.is_some_and(|domain| {
let expected = normalize_server_host(domain);
auth_event.tags.iter().any(|tag| {
tag.kind().to_string() == "server"
&& tag
.content()
.is_some_and(|value| normalize_server_host(value) == expected)
})
});
if !has_matching_x && !has_matching_server {
return Err(BlossomAuthError::InsufficientScope);
}
Ok(())
}
fn normalize_server_host(value: &str) -> String {
let authority = match value.split_once("://") {
Some((_scheme, rest)) => rest.split('/').next().unwrap_or(rest),
None => value.split('/').next().unwrap_or(value),
};
buzz_core::tenant::normalize_host(authority)
}
+116 -1
View File
@@ -197,6 +197,82 @@ impl VerifiedEvidenceAdapter {
.map_err(Into::into)
}
/// Fully verify and bind one exact Blossom upload operation.
pub fn verify_blossom_upload(
&self,
authorization_domain: CommunityId,
event: &Event,
sha256: &str,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<VerifiedNostrProof, EvidenceAdapterError> {
crate::blossom::verify_blossom_upload_auth(event, sha256, server_domain, max_age_secs)?;
let event_id = event.id.to_bytes();
let max_age = max_age_secs.to_be_bytes();
let binding = operation_binding(
VerifiedOperationBindingKind::BlossomUpload,
&[
&event_id,
sha256.as_bytes(),
server_domain.unwrap_or_default().as_bytes(),
&max_age,
],
);
self.blossom_proof(
authorization_domain,
AuthTransport::MediaUpload,
event,
binding,
)
}
/// Fully verify and bind one exact Blossom GET or HEAD operation.
pub fn verify_blossom_download(
&self,
authorization_domain: CommunityId,
event: &Event,
sha256: &str,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<VerifiedNostrProof, EvidenceAdapterError> {
crate::blossom::verify_blossom_get_auth(event, sha256, server_domain, max_age_secs)?;
let event_id = event.id.to_bytes();
let max_age = max_age_secs.to_be_bytes();
let binding = operation_binding(
VerifiedOperationBindingKind::BlossomDownload,
&[
&event_id,
sha256.as_bytes(),
server_domain.unwrap_or_default().as_bytes(),
&max_age,
],
);
self.blossom_proof(
authorization_domain,
AuthTransport::MediaDownload,
event,
binding,
)
}
fn blossom_proof(
&self,
authorization_domain: CommunityId,
transport: AuthTransport,
event: &Event,
binding: VerifiedOperationBinding,
) -> Result<VerifiedNostrProof, EvidenceAdapterError> {
VerifiedNostrProof::from_evidence_adapter(
authorization_domain,
transport,
event.pubkey,
AuthMethod::Blossom,
binding,
None,
)
.map_err(Into::into)
}
fn delegation(
&self,
actor: PublicKey,
@@ -392,6 +468,9 @@ pub enum EvidenceAdapterError {
/// Existing cryptographic proof failed.
#[error(transparent)]
Authentication(#[from] crate::AuthError),
/// Existing full Blossom operation verification failed.
#[error(transparent)]
Blossom(#[from] crate::blossom::BlossomAuthError),
/// Sealed context evidence was inconsistent.
#[error(transparent)]
Context(#[from] AuthContextError),
@@ -438,7 +517,7 @@ pub enum EvidenceAdapterError {
#[cfg(test)]
mod tests {
use nostr::{EventBuilder, Keys, RelayUrl};
use nostr::{EventBuilder, Keys, Kind, RelayUrl, Tag, Timestamp};
use super::*;
@@ -673,4 +752,40 @@ mod tests {
Err(EvidenceAdapterError::InvalidBindingResolution)
));
}
#[test]
fn blossom_factories_reverify_exact_hash_verb_and_server() {
let adapter = VerifiedEvidenceAdapter::new();
let expiration = (Timestamp::now().as_secs() + 300).to_string();
let upload_hash = "a".repeat(64);
let substituted_hash = "b".repeat(64);
let upload = EventBuilder::new(Kind::from(24_242), "upload")
.tags([
Tag::parse(["t", "upload"]).expect("verb"),
Tag::parse(["x", &upload_hash]).expect("hash"),
Tag::parse(["server", "relay.example"]).expect("server"),
Tag::parse(["expiration", &expiration]).expect("expiration"),
])
.sign_with_keys(&Keys::generate())
.expect("event");
assert!(adapter
.verify_blossom_upload(domain(1), &upload, &upload_hash, Some("relay.example"), 600,)
.is_ok());
assert!(adapter
.verify_blossom_upload(
domain(1),
&upload,
&substituted_hash,
Some("relay.example"),
600,
)
.is_err());
assert!(adapter
.verify_blossom_download(domain(1), &upload, &upload_hash, Some("relay.example"), 600,)
.is_err());
assert!(adapter
.verify_blossom_upload(domain(1), &upload, &upload_hash, Some("other.example"), 600,)
.is_err());
}
}
+2
View File
@@ -17,6 +17,8 @@
/// Channel access checking trait and helpers.
pub mod access;
/// Complete Blossom operation authentication verification.
pub mod blossom;
/// Versioned, transport-neutral authorization context.
pub mod context;
/// Authentication error types.
+1 -1
View File
@@ -1246,7 +1246,7 @@ impl CapabilitySnapshot {
{
return Err(ProviderContractError::CapabilityBindingChanged.into());
}
let admission = VerifiedOwnerAdmission::new(
let admission = VerifiedOwnerAdmission::from_capability_snapshot(
self.authorization_domain,
self.principal,
AdmissionExpiry::new(self.effective_until)?,
File diff suppressed because it is too large Load Diff
+189 -1
View File
@@ -5,7 +5,7 @@
use chrono::{DateTime, Utc};
use sha2::{Digest, Sha256};
use sqlx::{PgPool, Row};
use sqlx::{PgPool, Postgres, Row, Transaction};
use uuid::Uuid;
use crate::channel::ChannelRecord;
@@ -387,6 +387,164 @@ pub async fn open_dm(
Ok((channel, true))
}
/// Open or retrieve a DM inside a caller-owned authorization transaction.
/// The participant-set advisory lock serializes first creation, and every
/// channel/member change commits with the caller's operation receipt.
pub async fn open_dm_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
pubkeys: &[&[u8]],
created_by: &[u8],
) -> Result<(ChannelRecord, bool)> {
let mut all: Vec<&[u8]> = pubkeys.to_vec();
if !all.contains(&created_by) {
all.push(created_by);
}
all.sort_unstable();
all.dedup();
if !(2..=9).contains(&all.len()) || all.iter().any(|pubkey| pubkey.len() != 32) {
return Err(DbError::InvalidData(
"DM requires 2-9 valid participant pubkeys".to_string(),
));
}
let hash = compute_participant_hash(&all);
let lock_key = i64::from_be_bytes(hash[..8].try_into().expect("eight-byte digest prefix"));
sqlx::query("SELECT pg_advisory_xact_lock($1)")
.bind(lock_key)
.execute(&mut **tx)
.await?;
let existing = sqlx::query(
r#"
SELECT id, name, channel_type::text AS channel_type, visibility::text AS visibility,
description, canvas, created_by, created_at, updated_at, archived_at,
deleted_at, nip29_group_id, topic_required, max_members, topic,
topic_set_by, topic_set_at, purpose, purpose_set_by, purpose_set_at
FROM channels
WHERE community_id = $1 AND participant_hash = $2
AND channel_type = 'dm' AND deleted_at IS NULL
LIMIT 1 FOR SHARE
"#,
)
.bind(community_id.as_uuid())
.bind(hash.as_slice())
.fetch_optional(&mut **tx)
.await?;
if let Some(row) = existing {
let channel = row_to_channel_record(row)?;
sqlx::query(
"UPDATE channel_members SET hidden_at = NULL \
WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3 \
AND removed_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(channel.id)
.bind(created_by)
.execute(&mut **tx)
.await?;
return Ok((channel, false));
}
let id = Uuid::new_v4();
let name = if all.len() == 2 {
"DM".to_string()
} else {
format!("Group DM ({})", all.len())
};
sqlx::query(
"INSERT INTO channels \
(id, community_id, name, channel_type, visibility, created_by, participant_hash) \
VALUES ($1, $2, $3, 'dm', 'private', $4, $5)",
)
.bind(id)
.bind(community_id.as_uuid())
.bind(name)
.bind(created_by)
.bind(hash.as_slice())
.execute(&mut **tx)
.await?;
for pubkey in &all {
sqlx::query(
"INSERT INTO channel_members \
(community_id, channel_id, pubkey, role, invited_by) \
VALUES ($1, $2, $3, 'member', $4) \
ON CONFLICT (community_id, channel_id, pubkey) DO UPDATE SET \
removed_at = NULL, removed_by = NULL, role = EXCLUDED.role",
)
.bind(community_id.as_uuid())
.bind(id)
.bind(*pubkey)
.bind(created_by)
.execute(&mut **tx)
.await?;
}
let row = sqlx::query(
r#"
SELECT id, name, channel_type::text AS channel_type, visibility::text AS visibility,
description, canvas, created_by, created_at, updated_at, archived_at,
deleted_at, nip29_group_id, topic_required, max_members, topic,
topic_set_by, topic_set_at, purpose, purpose_set_by, purpose_set_at
FROM channels WHERE community_id = $1 AND id = $2
"#,
)
.bind(community_id.as_uuid())
.bind(id)
.fetch_one(&mut **tx)
.await?;
Ok((row_to_channel_record(row)?, true))
}
/// Read and lock a source DM's active participant set, require the actor to be
/// an active member, then open the expanded immutable participant set inside
/// the same transaction.
pub async fn expand_dm_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
source_channel_id: Uuid,
additions: &[Vec<u8>],
actor: &[u8],
) -> Result<(ChannelRecord, bool, Vec<Vec<u8>>)> {
let channel_type = sqlx::query_scalar::<_, String>(
"SELECT channel_type::text FROM channels \
WHERE community_id = $1 AND id = $2 AND deleted_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(source_channel_id)
.fetch_optional(&mut **tx)
.await?
.ok_or_else(|| DbError::NotFound(format!("DM {source_channel_id}")))?;
if channel_type != "dm" {
return Err(DbError::AccessDenied("channel is not a DM".into()));
}
let mut participants = sqlx::query_scalar::<_, Vec<u8>>(
"SELECT pubkey FROM channel_members \
WHERE community_id = $1 AND channel_id = $2 AND removed_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(source_channel_id)
.fetch_all(&mut **tx)
.await?;
if !participants.iter().any(|pubkey| pubkey == actor) {
return Err(DbError::AccessDenied("actor is not a DM member".into()));
}
for pubkey in additions {
if pubkey.len() != 32 {
return Err(DbError::InvalidData("invalid DM participant pubkey".into()));
}
if !participants.contains(pubkey) {
participants.push(pubkey.clone());
}
}
if participants.len() > 9 {
return Err(DbError::InvalidData(
"DM supports at most 9 participants".into(),
));
}
let refs = participants.iter().map(Vec::as_slice).collect::<Vec<_>>();
let (channel, created) = open_dm_tx(tx, community_id, &refs, actor).await?;
Ok((channel, created, participants))
}
// -- Hide / unhide ------------------------------------------------------------
/// Hide a DM for a specific user by setting `hidden_at = NOW()`.
@@ -422,6 +580,36 @@ pub async fn hide_dm(
Ok(())
}
/// Hide a DM inside a caller-owned authorization transaction. The joined
/// channel predicate makes the active membership and DM type part of the
/// authoritative update rather than an adjacent preflight.
pub async fn hide_dm_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
channel_id: Uuid,
pubkey: &[u8],
) -> Result<()> {
let result = sqlx::query(
"UPDATE channel_members AS cm SET hidden_at = NOW() \
FROM channels AS c \
WHERE cm.community_id = $1 AND cm.channel_id = $2 AND cm.pubkey = $3 \
AND cm.removed_at IS NULL \
AND c.community_id = cm.community_id AND c.id = cm.channel_id \
AND c.channel_type = 'dm' AND c.deleted_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(pubkey)
.execute(&mut **tx)
.await?;
if result.rows_affected() == 0 {
return Err(DbError::AccessDenied(
"actor is not an active DM member".into(),
));
}
Ok(())
}
/// Unhide a DM for a specific user by clearing `hidden_at`.
///
/// This is called automatically when a user re-opens a DM via [`open_dm`].
+685 -8
View File
@@ -329,6 +329,34 @@ pub async fn query_events(pool: &PgPool, q: &EventQuery) -> Result<Vec<StoredEve
query_events_on(&mut conn, q).await
}
/// Query events on a caller-owned transaction so authorization-relevant event
/// state can be read and retained through the protected commit boundary.
pub async fn query_events_tx(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
q: &EventQuery,
) -> Result<Vec<StoredEvent>> {
query_events_on(transaction, q).await
}
/// Share-lock a live event through the caller's commit boundary. Callers that
/// first resolve an authorization-bearing event must use this before trusting
/// its contents so replacement or deletion cannot race the protected effect.
pub async fn lock_live_event_tx(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
community_id: CommunityId,
event_id: &[u8],
) -> Result<bool> {
Ok(sqlx::query(
"SELECT 1 FROM events \
WHERE community_id = $1 AND id = $2 AND deleted_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(event_id)
.fetch_optional(&mut **transaction)
.await?
.is_some())
}
/// [`query_events`] on a specific session — the replica-routing path runs
/// follow-up (aux) queries on the exact reader connection whose heartbeat
/// observation proved coverage for the page they annotate.
@@ -980,6 +1008,30 @@ pub async fn get_event_by_id(
}
}
/// Fetch and share-lock one non-deleted event inside a caller-owned
/// transaction. The lock keeps deletion or replacement from changing the edit
/// target after ownership is validated and before the edit commits.
pub async fn get_event_by_id_tx(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
community_id: CommunityId,
id_bytes: &[u8],
) -> Result<Option<StoredEvent>> {
let row = sqlx::query(
"SELECT id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id \
FROM events WHERE community_id = $1 AND id = $2 AND deleted_at IS NULL \
ORDER BY created_at DESC LIMIT 1 FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(id_bytes)
.fetch_optional(&mut **transaction)
.await?;
match row {
Some(r) => row_to_stored_event(r),
None => Ok(None),
}
}
/// Fetches the latest global (non-channel, `channel_id IS NULL`) replaceable event
/// for a (kind, pubkey) pair.
///
@@ -1111,7 +1163,11 @@ pub struct ThreadMetadataParams<'a> {
pub broadcast: bool,
}
async fn insert_event_with_thread_metadata_tx(
/// Insert an event and optional thread metadata in a caller-owned transaction.
///
/// Protected callers use this to commit the event, thread counters, and
/// authorization receipt at one PostgreSQL boundary.
pub async fn insert_event_with_thread_metadata_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
event: &Event,
@@ -1280,6 +1336,585 @@ async fn insert_event_with_thread_metadata_tx(
))
}
/// Replace one NIP-16 addressable event inside a caller-owned transaction.
pub async fn replace_addressable_event_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
event: &Event,
channel_id: Option<Uuid>,
) -> Result<(StoredEvent, bool)> {
let kind = event_kind_i32(event);
let pubkey = event.pubkey.to_bytes();
let created_at_secs = event.created_at.as_secs() as i64;
let created_at = DateTime::from_timestamp(created_at_secs, 0)
.ok_or(DbError::InvalidTimestamp(created_at_secs))?;
let lock_key = crate::event_replacement_lock_key(
community_id,
kind,
pubkey.as_slice(),
channel_id.as_ref().map(|id| id.as_bytes().as_slice()),
);
sqlx::query("SELECT pg_advisory_xact_lock($1)")
.bind(lock_key)
.execute(&mut **tx)
.await?;
let received_at = Utc::now();
if sqlx::query_scalar::<_, i32>("SELECT 1 FROM events WHERE community_id = $1 AND id = $2")
.bind(community_id.as_uuid())
.bind(event.id.as_bytes().as_slice())
.fetch_optional(&mut **tx)
.await?
.is_some()
{
return Ok((
StoredEvent::with_received_at(event.clone(), received_at, channel_id, false),
false,
));
}
let existing: Option<(DateTime<Utc>, Vec<u8>)> = sqlx::query_as(
"SELECT created_at, id FROM events WHERE community_id = $1 AND kind = $2 \
AND pubkey = $3 AND channel_id IS NOT DISTINCT FROM $4 AND deleted_at IS NULL \
ORDER BY created_at DESC, id ASC LIMIT 1",
)
.bind(community_id.as_uuid())
.bind(kind)
.bind(pubkey.as_slice())
.bind(channel_id)
.fetch_optional(&mut **tx)
.await?;
if existing.as_ref().is_some_and(|(accepted_at, accepted_id)| {
created_at < *accepted_at
|| (created_at == *accepted_at
&& event.id.as_bytes().as_slice() >= accepted_id.as_slice())
}) {
return Ok((
StoredEvent::with_received_at(event.clone(), received_at, channel_id, false),
false,
));
}
sqlx::query(
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 AND kind = $2 \
AND pubkey = $3 AND channel_id IS NOT DISTINCT FROM $4 AND deleted_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(kind)
.bind(pubkey.as_slice())
.bind(channel_id)
.execute(&mut **tx)
.await?;
let result =
insert_event_with_thread_metadata_tx(tx, community_id, event, channel_id, None).await?;
if !result.1 && existing.is_some() {
return Err(DbError::InvalidData(
"replacement insert conflicted after retiring the prior event".into(),
));
}
Ok(result)
}
/// Replace one NIP-33 parameterized event inside a caller-owned transaction.
pub async fn replace_parameterized_event_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
event: &Event,
d_tag: &str,
channel_id: Option<Uuid>,
) -> Result<(StoredEvent, bool)> {
let kind = event_kind_i32(event);
let pubkey = event.pubkey.to_bytes();
let created_at_secs = event.created_at.as_secs() as i64;
let created_at = DateTime::from_timestamp(created_at_secs, 0)
.ok_or(DbError::InvalidTimestamp(created_at_secs))?;
let lock_key = crate::event_replacement_lock_key(
community_id,
kind,
pubkey.as_slice(),
Some(d_tag.as_bytes()),
);
sqlx::query("SELECT pg_advisory_xact_lock($1)")
.bind(lock_key)
.execute(&mut **tx)
.await?;
let received_at = Utc::now();
if sqlx::query_scalar::<_, i32>("SELECT 1 FROM events WHERE community_id = $1 AND id = $2")
.bind(community_id.as_uuid())
.bind(event.id.as_bytes().as_slice())
.fetch_optional(&mut **tx)
.await?
.is_some()
{
return Ok((
StoredEvent::with_received_at(event.clone(), received_at, channel_id, false),
false,
));
}
let d_tag_count = event
.tags
.iter()
.filter(|tag| tag.as_slice().first().is_some_and(|part| part == "d"))
.count();
let has_exact_d_tag = event.tags.iter().any(|tag| {
let parts = tag.as_slice();
parts.len() >= 2 && parts[0] == "d" && parts[1] == d_tag
});
let read_state_t_tag_count = event
.tags
.iter()
.filter(|tag| {
let parts = tag.as_slice();
parts.len() == 2 && parts[0] == "t" && parts[1] == "read-state"
})
.count();
let is_nip_rs = kind == buzz_core::kind::KIND_READ_STATE as i32
&& d_tag_count == 1
&& has_exact_d_tag
&& d_tag.strip_prefix("read-state:").is_some_and(|slot| {
slot.len() == 32
&& slot
.bytes()
.all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
})
&& read_state_t_tag_count == 1;
let is_buzz_mesh_status = kind == buzz_core::kind::KIND_BOOKMARK_SET as i32
&& d_tag.starts_with("buzz-mesh-member-status:")
&& event.tags.iter().any(|tag| {
let parts = tag.as_slice();
parts.len() == 2 && parts[0] == "k" && parts[1] == "buzz-mesh-status"
});
let hard_delete_superseded = is_nip_rs || is_buzz_mesh_status;
let existing: Option<(DateTime<Utc>, Vec<u8>)> = sqlx::query_as(
"SELECT created_at, id FROM events WHERE community_id = $1 AND kind = $2 \
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL \
ORDER BY created_at DESC, id ASC LIMIT 1",
)
.bind(community_id.as_uuid())
.bind(kind)
.bind(pubkey.as_slice())
.bind(d_tag)
.fetch_optional(&mut **tx)
.await?;
let watermark: Option<(DateTime<Utc>, Vec<u8>)> = if is_nip_rs {
sqlx::query_as(
"SELECT created_at, event_id FROM parameterized_event_watermarks \
WHERE community_id = $1 AND kind = $2 AND pubkey = $3 AND d_tag = $4",
)
.bind(community_id.as_uuid())
.bind(kind)
.bind(pubkey.as_slice())
.bind(d_tag)
.fetch_optional(&mut **tx)
.await?
} else {
None
};
let incoming_id = event.id.as_bytes().as_slice();
if existing
.iter()
.chain(watermark.iter())
.any(|(accepted_at, accepted_id)| {
created_at < *accepted_at
|| (created_at == *accepted_at && incoming_id >= accepted_id.as_slice())
})
{
return Ok((
StoredEvent::with_received_at(event.clone(), received_at, channel_id, false),
false,
));
}
if existing.is_some() {
if is_nip_rs {
sqlx::query("SELECT set_config('buzz.nip_rs_hard_delete', 'on', true)")
.execute(&mut **tx)
.await?;
}
let statement = if hard_delete_superseded {
"DELETE FROM events WHERE community_id = $1 AND kind = $2 \
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL"
} else {
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 AND kind = $2 \
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL"
};
sqlx::query(statement)
.bind(community_id.as_uuid())
.bind(kind)
.bind(pubkey.as_slice())
.bind(d_tag)
.execute(&mut **tx)
.await?;
if hard_delete_superseded {
if let Some((_, existing_id)) = &existing {
sqlx::query("DELETE FROM event_mentions WHERE community_id = $1 AND event_id = $2")
.bind(community_id.as_uuid())
.bind(existing_id)
.execute(&mut **tx)
.await?;
}
}
}
let sig = event.sig.serialize();
let tags = serde_json::to_value(&event.tags)?;
let inserted = sqlx::query(
"INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, \
received_at, channel_id, d_tag, not_before) VALUES \
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) ON CONFLICT DO NOTHING",
)
.bind(community_id.as_uuid())
.bind(incoming_id)
.bind(pubkey.as_slice())
.bind(created_at)
.bind(kind)
.bind(tags)
.bind(&event.content)
.bind(sig.as_slice())
.bind(received_at)
.bind(channel_id)
.bind(d_tag)
.bind(extract_not_before(event))
.execute(&mut **tx)
.await?
.rows_affected()
> 0;
if !inserted {
return Err(DbError::InvalidData(
"parameterized replacement insert conflicted after retiring the prior event".into(),
));
}
if is_nip_rs {
sqlx::query(
"INSERT INTO parameterized_event_watermarks \
(community_id, kind, pubkey, d_tag, created_at, event_id) \
VALUES ($1, $2, $3, $4, $5, $6) \
ON CONFLICT (community_id, kind, pubkey, d_tag) DO UPDATE SET \
created_at = EXCLUDED.created_at, event_id = EXCLUDED.event_id",
)
.bind(community_id.as_uuid())
.bind(kind)
.bind(pubkey.as_slice())
.bind(d_tag)
.bind(created_at)
.bind(incoming_id)
.execute(&mut **tx)
.await?;
}
Ok((
StoredEvent::with_received_at(event.clone(), received_at, channel_id, true),
true,
))
}
/// Apply the durable projection of a validated NIP-09 deletion inside the same
/// authorization transaction that stores the deletion event.
pub async fn apply_standard_deletion_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
event: &Event,
actor: &[u8],
relay_pubkey: &[u8],
) -> Result<()> {
let targets = event
.tags
.iter()
.filter_map(|tag| {
let parts = tag.as_slice();
(parts.len() >= 2 && parts[0] == "e")
.then(|| hex::decode(&parts[1]).ok())
.flatten()
.filter(|value| value.len() == 32)
})
.collect::<Vec<_>>();
if targets.is_empty() {
if let Some(coordinate) = event.tags.iter().find_map(|tag| {
let parts = tag.as_slice();
(parts.len() >= 2 && parts[0] == "a").then_some(parts[1].as_str())
}) {
let parts = coordinate.splitn(3, ':').collect::<Vec<_>>();
if parts.len() == 3 {
let kind = parts[0].parse::<u32>().ok();
let pubkey = hex::decode(parts[1]).ok();
if let (Some(kind), Some(pubkey)) = (kind, pubkey) {
if pubkey.len() != 32 {
return Err(DbError::InvalidData(
"invalid addressable event pubkey".into(),
));
}
if is_parameterized_replaceable(kind)
&& kind != buzz_core::kind::KIND_WORKFLOW_DEF
&& kind != buzz_core::kind::KIND_PUSH_LEASE
{
let owns_target = pubkey == actor
|| sqlx::query_scalar::<_, bool>(
"SELECT EXISTS(SELECT 1 FROM users WHERE community_id = $1 \
AND pubkey = $2 AND agent_owner_pubkey = $3)",
)
.bind(community_id.as_uuid())
.bind(&pubkey)
.bind(actor)
.fetch_one(&mut **tx)
.await?;
if !owns_target {
return Err(DbError::AccessDenied(
"actor does not own the addressable event".into(),
));
}
sqlx::query(
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 \
AND kind = $2 AND pubkey = $3 AND d_tag = $4 \
AND deleted_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(kind as i32)
.bind(pubkey)
.bind(parts[2])
.execute(&mut **tx)
.await?;
}
}
}
}
return Ok(());
}
for target in targets {
let row = sqlx::query(
"SELECT e.kind, e.pubkey, e.tags, tm.parent_event_id, tm.root_event_id FROM events e \
LEFT JOIN thread_metadata tm ON tm.community_id = e.community_id \
AND tm.event_id = e.id AND tm.event_created_at = e.created_at \
WHERE e.community_id = $1 AND e.id = $2 AND e.deleted_at IS NULL \
ORDER BY e.created_at DESC LIMIT 1 FOR UPDATE OF e",
)
.bind(community_id.as_uuid())
.bind(&target)
.fetch_optional(&mut **tx)
.await?;
let Some(row) = row else { continue };
let kind: i32 = row.try_get("kind")?;
if kind == buzz_core::kind::KIND_PUSH_LEASE as i32 {
continue;
}
let stored_pubkey: Vec<u8> = row.try_get("pubkey")?;
let tags: serde_json::Value = row.try_get("tags")?;
let effective_author = if stored_pubkey == relay_pubkey {
tags.as_array()
.and_then(|tags| {
tags.iter().find_map(|tag| {
let tag = tag.as_array()?;
(tag.first()?.as_str()? == "p")
.then(|| tag.get(1)?.as_str())
.flatten()
})
})
.and_then(|value| hex::decode(value).ok())
.filter(|value| value.len() == 32)
.unwrap_or(stored_pubkey)
} else {
stored_pubkey
};
let owns_target = effective_author == actor
|| sqlx::query_scalar::<_, bool>(
"SELECT EXISTS(SELECT 1 FROM users WHERE community_id = $1 \
AND pubkey = $2 AND agent_owner_pubkey = $3)",
)
.bind(community_id.as_uuid())
.bind(&effective_author)
.bind(actor)
.fetch_one(&mut **tx)
.await?;
if !owns_target {
return Err(DbError::AccessDenied(
"actor does not own the target event".into(),
));
}
let parent: Option<Vec<u8>> = row.try_get("parent_event_id")?;
let root: Option<Vec<u8>> = row.try_get("root_event_id")?;
sqlx::query(
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 \
AND id = $2 AND deleted_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(&target)
.execute(&mut **tx)
.await?;
if let Some(parent) = parent {
sqlx::query(
"UPDATE thread_metadata SET reply_count = GREATEST(reply_count - 1, 0) \
WHERE community_id = $1 AND event_id = $2",
)
.bind(community_id.as_uuid())
.bind(parent)
.execute(&mut **tx)
.await?;
}
if let Some(root) = root {
sqlx::query(
"UPDATE thread_metadata SET descendant_count = GREATEST(descendant_count - 1, 0) \
WHERE community_id = $1 AND event_id = $2",
)
.bind(community_id.as_uuid())
.bind(root)
.execute(&mut **tx)
.await?;
}
if kind == buzz_core::kind::KIND_REACTION as i32 {
sqlx::query(
"UPDATE reactions SET removed_at = NOW() WHERE community_id = $1 \
AND reaction_event_id = $2 AND removed_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(&target)
.execute(&mut **tx)
.await?;
}
}
Ok(())
}
/// Apply a NIP-29 channel-admin deletion inside the caller-owned sealed
/// authorization transaction. The target, its channel, and the actor's live
/// role/agent relationship are revalidated from locked rows before deletion.
pub async fn apply_nip29_delete_event_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
deletion: &Event,
actor: &[u8],
relay_pubkey: &[u8],
channel_id: Uuid,
) -> Result<bool> {
let target = deletion
.tags
.iter()
.find_map(|tag| {
let parts = tag.as_slice();
(parts.len() >= 2 && parts[0] == "e")
.then(|| hex::decode(&parts[1]).ok())
.flatten()
.filter(|value| value.len() == 32)
})
.ok_or_else(|| DbError::InvalidData("missing deletion target".into()))?;
sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))")
.bind(format!(
"buzz_channel_membership:{}:{}",
community_id.as_uuid(),
channel_id
))
.execute(&mut **tx)
.await?;
let row = sqlx::query(
"SELECT e.pubkey, e.tags, e.channel_id, tm.parent_event_id, tm.root_event_id \
FROM events e LEFT JOIN thread_metadata tm \
ON tm.community_id = e.community_id AND tm.event_id = e.id \
AND tm.event_created_at = e.created_at \
WHERE e.community_id = $1 AND e.id = $2 AND e.deleted_at IS NULL \
ORDER BY e.created_at DESC LIMIT 1 FOR UPDATE OF e",
)
.bind(community_id.as_uuid())
.bind(&target)
.fetch_optional(&mut **tx)
.await?
.ok_or_else(|| DbError::NotFound("target event not found".into()))?;
let target_channel: Option<Uuid> = row.try_get("channel_id")?;
if target_channel != Some(channel_id) {
return Err(DbError::AccessDenied(
"target event belongs to a different channel".into(),
));
}
let channel_visibility: String = sqlx::query_scalar(
"SELECT visibility::text FROM channels WHERE community_id = $1 AND id = $2 \
AND deleted_at IS NULL FOR UPDATE",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.fetch_optional(&mut **tx)
.await?
.ok_or(DbError::ChannelNotFound(channel_id))?;
let stored_pubkey: Vec<u8> = row.try_get("pubkey")?;
let tags: serde_json::Value = row.try_get("tags")?;
let effective_author = if stored_pubkey == relay_pubkey {
tags.as_array()
.and_then(|tags| {
tags.iter().find_map(|tag| {
let tag = tag.as_array()?;
(tag.first()?.as_str()? == "p")
.then(|| tag.get(1)?.as_str())
.flatten()
})
})
.and_then(|value| hex::decode(value).ok())
.filter(|value| value.len() == 32)
.unwrap_or(stored_pubkey)
} else {
stored_pubkey
};
let is_author = effective_author == actor;
let active_member = sqlx::query_scalar::<_, bool>(
"SELECT EXISTS(SELECT 1 FROM channel_members WHERE community_id = $1 \
AND channel_id = $2 AND pubkey = $3 AND removed_at IS NULL)",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(actor)
.fetch_one(&mut **tx)
.await?;
let author_path_allowed = is_author && (channel_visibility == "open" || active_member);
let elevated = sqlx::query_scalar::<_, bool>(
"SELECT EXISTS(SELECT 1 FROM channel_members WHERE community_id = $1 \
AND channel_id = $2 AND pubkey = $3 AND removed_at IS NULL \
AND role IN ('owner', 'admin'))",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(actor)
.fetch_one(&mut **tx)
.await?;
let owns_author = sqlx::query_scalar::<_, bool>(
"SELECT EXISTS(SELECT 1 FROM users WHERE community_id = $1 AND pubkey = $2 \
AND agent_owner_pubkey = $3)",
)
.bind(community_id.as_uuid())
.bind(&effective_author)
.bind(actor)
.fetch_one(&mut **tx)
.await?;
if !author_path_allowed && !elevated && !owns_author {
return Err(DbError::AccessDenied(
"actor may not delete the target event".into(),
));
}
let parent: Option<Vec<u8>> = row.try_get("parent_event_id")?;
let root: Option<Vec<u8>> = row.try_get("root_event_id")?;
let changed = sqlx::query(
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 \
AND id = $2 AND deleted_at IS NULL",
)
.bind(community_id.as_uuid())
.bind(&target)
.execute(&mut **tx)
.await?
.rows_affected()
> 0;
if changed {
if let Some(parent) = parent {
sqlx::query(
"UPDATE thread_metadata SET reply_count = GREATEST(reply_count - 1, 0) \
WHERE community_id = $1 AND event_id = $2",
)
.bind(community_id.as_uuid())
.bind(parent)
.execute(&mut **tx)
.await?;
}
if let Some(root) = root {
sqlx::query(
"UPDATE thread_metadata SET descendant_count = GREATEST(descendant_count - 1, 0) \
WHERE community_id = $1 AND event_id = $2",
)
.bind(community_id.as_uuid())
.bind(root)
.execute(&mut **tx)
.await?;
}
}
Ok(changed)
}
/// Atomically insert an event and its optional thread metadata.
///
/// `insert_event` and `insert_thread_metadata` calls could leave reply counters
@@ -1320,6 +1955,33 @@ pub async fn insert_reaction_event_with_thread_metadata(
) -> Result<ReactionEventInsertOutcome> {
let mut tx = pool.begin().await?;
let result = insert_reaction_event_with_thread_metadata_tx(
&mut tx,
community_id,
reaction_event,
channel_id,
thread_meta,
target_event_id,
actor_pubkey,
emoji,
)
.await?;
tx.commit().await?;
Ok(result)
}
/// Insert a reaction and its event inside a caller-owned authorization transaction.
#[allow(clippy::too_many_arguments)]
pub async fn insert_reaction_event_with_thread_metadata_tx(
tx: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
reaction_event: &Event,
channel_id: Option<Uuid>,
thread_meta: Option<ThreadMetadataParams<'_>>,
target_event_id: &[u8],
actor_pubkey: &[u8],
emoji: &str,
) -> Result<ReactionEventInsertOutcome> {
let target_row = sqlx::query(
"SELECT created_at FROM events \
WHERE community_id = $1 AND id = $2 AND deleted_at IS NULL \
@@ -1327,18 +1989,17 @@ pub async fn insert_reaction_event_with_thread_metadata(
)
.bind(community_id.as_uuid())
.bind(target_event_id)
.fetch_optional(&mut *tx)
.fetch_optional(&mut **tx)
.await?;
let Some(target_row) = target_row else {
tx.rollback().await?;
return Ok(ReactionEventInsertOutcome::TargetMissing);
};
let target_created_at: DateTime<Utc> = target_row.get("created_at");
// Preserve add_reaction's exact new / re-activate / active-duplicate semantics.
let reaction_inserted = crate::reaction::add_reaction_tx(
&mut tx,
tx,
community_id,
target_event_id,
target_created_at,
@@ -1349,12 +2010,11 @@ pub async fn insert_reaction_event_with_thread_metadata(
.await?;
if !reaction_inserted {
tx.rollback().await?;
return Ok(ReactionEventInsertOutcome::Duplicate);
}
let (stored_event, was_inserted) = insert_event_with_thread_metadata_tx(
&mut tx,
tx,
community_id,
reaction_event,
channel_id,
@@ -1362,8 +2022,6 @@ pub async fn insert_reaction_event_with_thread_metadata(
)
.await?;
tx.commit().await?;
Ok(ReactionEventInsertOutcome::Inserted {
stored_event: Box::new(stored_event),
was_inserted,
@@ -1404,6 +2062,16 @@ pub async fn query_due_reminders(
pool: &PgPool,
now_secs: i64,
batch_limit: i64,
) -> Result<Vec<DueReminder>> {
query_due_reminders_excluding(pool, now_secs, batch_limit, &[]).await
}
/// Query due reminders while leaving protected Enforce domains unclaimed.
pub async fn query_due_reminders_excluding(
pool: &PgPool,
now_secs: i64,
batch_limit: i64,
excluded_communities: &[Uuid],
) -> Result<Vec<DueReminder>> {
let kind_i32 = KIND_EVENT_REMINDER as i32;
let rows = sqlx::query(
@@ -1413,6 +2081,7 @@ pub async fn query_due_reminders(
FROM events AS e
JOIN communities AS c ON c.id = e.community_id
WHERE e.kind = $1
AND NOT (e.community_id = ANY($4::uuid[]))
AND e.not_before IS NOT NULL
AND e.not_before <= $2
AND e.deleted_at IS NULL
@@ -1425,6 +2094,7 @@ pub async fn query_due_reminders(
.bind(kind_i32)
.bind(now_secs)
.bind(batch_limit)
.bind(excluded_communities)
.fetch_all(pool)
.await?;
@@ -2370,6 +3040,13 @@ mod tests {
assert!(due.iter().any(|row| {
row.id == event_b.id.as_bytes() && row.community_id == community_b && row.host == host_b
}));
let excluded =
query_due_reminders_excluding(&pool, Utc::now().timestamp(), 100, &[community_a_uuid])
.await
.expect("query with protected exclusion");
assert!(!excluded.iter().any(|row| row.community_id == community_a));
assert!(excluded.iter().any(|row| row.community_id == community_b));
}
/// Two pods race to claim the same due reminder: exactly one wins. The
+244 -2
View File
@@ -16,10 +16,11 @@
//! idempotent re-announce (same owner) from a collision (different owner), and
//! backs the per-pubkey quota via `COUNT`.
use sqlx::{PgPool, Row as _};
use nostr::Event;
use sqlx::{PgPool, Postgres, Row as _, Transaction};
use crate::error::Result;
use crate::CommunityId;
use crate::{CommunityId, DbError, StoredEvent};
/// Outcome of a name-reservation attempt.
///
@@ -155,6 +156,25 @@ pub async fn count_repos_for_owner(
row.try_get("n").map_err(crate::error::DbError::from)
}
/// Return the immutable publication origin for an existing reservation.
pub async fn repo_publication_origin(
pool: &PgPool,
community: CommunityId,
repo_id: &str,
owner_pubkey: &str,
) -> Result<Option<String>> {
sqlx::query_scalar(
"SELECT publication_origin FROM git_repo_names \
WHERE community_id = $1 AND repo_id = $2 AND owner_pubkey = $3",
)
.bind(community.as_uuid())
.bind(repo_id)
.bind(owner_pubkey)
.fetch_optional(pool)
.await
.map_err(Into::into)
}
/// Release a reservation held by `owner_pubkey` (rollback path).
///
/// Used only when seeding the manifest pointer fails *after* a fresh
@@ -179,9 +199,127 @@ pub async fn release_repo_name(
Ok(result.rows_affected())
}
/// Atomically replace a protected repository announcement and reserve its
/// tenant-local name inside the caller-owned authorization transaction.
///
/// The owner-scoped advisory lock makes the quota exact across concurrent new
/// names, while the coordinate lock preserves NIP-33 timestamp/id ordering.
/// Re-announcing an existing same-owner name is idempotent and does not consume
/// quota. A different owner can never claim an already-reserved name.
pub async fn replace_protected_announcement_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
event: &Event,
repo_id: &str,
max_repos_per_owner: i64,
) -> Result<(StoredEvent, bool)> {
if max_repos_per_owner <= 0 {
return Err(DbError::InvalidData(
"repository quota must be positive".into(),
));
}
let owner = hex::encode(event.pubkey.to_bytes());
let coordinate_lock = format!(
"git-announcement:{}:{}:{}",
community.as_uuid(),
owner,
repo_id
);
let name_lock = format!("git-name:{}:{}", community.as_uuid(), repo_id);
let quota_lock = format!("git-owner-quota:{}:{}", community.as_uuid(), owner);
for lock in [&name_lock, &coordinate_lock, &quota_lock] {
sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))")
.bind(lock)
.execute(&mut **transaction)
.await?;
}
let created_at_seconds = event.created_at.as_secs() as i64;
let created_at = chrono::DateTime::from_timestamp(created_at_seconds, 0)
.ok_or(DbError::InvalidTimestamp(created_at_seconds))?;
let event_id = event.id.as_bytes().as_slice();
let existing: Option<(chrono::DateTime<chrono::Utc>, Vec<u8>)> = sqlx::query_as(
"SELECT created_at, id FROM events \
WHERE community_id = $1 AND kind = 30617 AND pubkey = $2 \
AND d_tag = $3 AND deleted_at IS NULL \
ORDER BY created_at DESC, id ASC LIMIT 1 FOR UPDATE",
)
.bind(community.as_uuid())
.bind(event.pubkey.to_bytes().as_slice())
.bind(repo_id)
.fetch_optional(&mut **transaction)
.await?;
if existing.as_ref().is_some_and(|(accepted_at, accepted_id)| {
created_at < *accepted_at
|| (created_at == *accepted_at && event_id >= accepted_id.as_slice())
}) {
return Ok((
StoredEvent::with_received_at(event.clone(), chrono::Utc::now(), None, false),
false,
));
}
let holder: Option<String> = sqlx::query_scalar(
"SELECT owner_pubkey FROM git_repo_names \
WHERE community_id = $1 AND repo_id = $2 FOR UPDATE",
)
.bind(community.as_uuid())
.bind(repo_id)
.fetch_optional(&mut **transaction)
.await?;
match holder.as_deref() {
Some(holder) if holder != owner => {
return Err(DbError::InvalidData(
"repository name is already reserved".into(),
));
}
Some(_) => {}
None => {
let count: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM git_repo_names \
WHERE community_id = $1 AND owner_pubkey = $2",
)
.bind(community.as_uuid())
.bind(&owner)
.fetch_one(&mut **transaction)
.await?;
if count >= max_repos_per_owner {
return Err(DbError::InvalidData("repository quota exceeded".into()));
}
sqlx::query(
"INSERT INTO git_repo_names \
(community_id, repo_id, owner_pubkey, publication_origin) \
VALUES ($1, $2, $3, 'protected_unpublished')",
)
.bind(community.as_uuid())
.bind(repo_id)
.bind(&owner)
.execute(&mut **transaction)
.await?;
}
}
if existing.is_some() {
sqlx::query(
"UPDATE events SET deleted_at = clock_timestamp() \
WHERE community_id = $1 AND kind = 30617 AND pubkey = $2 \
AND d_tag = $3 AND deleted_at IS NULL",
)
.bind(community.as_uuid())
.bind(event.pubkey.to_bytes().as_slice())
.bind(repo_id)
.execute(&mut **transaction)
.await?;
}
crate::event::insert_event_with_thread_metadata_tx(transaction, community, event, None, None)
.await
}
#[cfg(test)]
mod tests {
use super::*;
use nostr::{EventBuilder, Keys, Kind, Tag, Timestamp};
use uuid::Uuid;
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz";
@@ -211,6 +349,110 @@ mod tests {
format!("{:064x}", Uuid::new_v4().as_u128())
}
fn announcement(keys: &Keys, repo: &str, created_at: u64) -> Event {
EventBuilder::new(Kind::Custom(30_617), "")
.tags([Tag::parse(["d", repo]).expect("d tag")])
.custom_created_at(Timestamp::from(created_at))
.sign_with_keys(keys)
.expect("signed announcement")
}
#[tokio::test]
#[ignore = "requires Postgres"]
async fn protected_announcement_replaces_and_reserves_in_one_transaction() {
let pool = setup_pool().await;
let community = make_test_community(&pool).await;
let owner = Keys::generate();
let repo = format!("repo-{}", Uuid::new_v4().simple());
let first = announcement(&owner, &repo, 1_800_000_000);
let second = announcement(&owner, &repo, 1_800_000_001);
let mut first_tx = pool.begin().await.expect("first transaction");
let (_, inserted) =
replace_protected_announcement_tx(&mut first_tx, community, &first, &repo, 10)
.await
.expect("first announcement");
assert!(inserted);
first_tx.commit().await.expect("commit first");
let mut second_tx = pool.begin().await.expect("second transaction");
let (_, inserted) =
replace_protected_announcement_tx(&mut second_tx, community, &second, &repo, 10)
.await
.expect("replacement announcement");
assert!(inserted);
second_tx.commit().await.expect("commit replacement");
assert_eq!(
repo_name_owner(&pool, community, &repo)
.await
.expect("registered owner"),
Some(owner.public_key().to_hex())
);
let live: Vec<Vec<u8>> = sqlx::query_scalar(
"SELECT id FROM events WHERE community_id = $1 AND kind = 30617 \
AND pubkey = $2 AND d_tag = $3 AND deleted_at IS NULL",
)
.bind(community.as_uuid())
.bind(owner.public_key().to_bytes().as_slice())
.bind(&repo)
.fetch_all(&pool)
.await
.expect("live announcements");
assert_eq!(live, vec![second.id.as_bytes().to_vec()]);
}
#[tokio::test]
#[ignore = "requires Postgres"]
async fn protected_announcement_quota_is_exact_under_concurrency() {
let pool = setup_pool().await;
let community = make_test_community(&pool).await;
let owner = Keys::generate();
let first_repo = format!("repo-a-{}", Uuid::new_v4().simple());
let second_repo = format!("repo-b-{}", Uuid::new_v4().simple());
let first = announcement(&owner, &first_repo, 1_800_000_000);
let second = announcement(&owner, &second_repo, 1_800_000_000);
let first_attempt = async {
let mut transaction = pool.begin().await.expect("first transaction");
let result = replace_protected_announcement_tx(
&mut transaction,
community,
&first,
&first_repo,
1,
)
.await;
if result.is_ok() {
transaction.commit().await.expect("first commit");
}
result
};
let second_attempt = async {
let mut transaction = pool.begin().await.expect("second transaction");
let result = replace_protected_announcement_tx(
&mut transaction,
community,
&second,
&second_repo,
1,
)
.await;
if result.is_ok() {
transaction.commit().await.expect("second commit");
}
result
};
let (first_result, second_result) = tokio::join!(first_attempt, second_attempt);
assert_ne!(first_result.is_ok(), second_result.is_ok());
assert_eq!(
count_repos_for_owner(&pool, community, &owner.public_key().to_hex())
.await
.expect("quota count"),
1
);
}
/// A fresh name is `Reserved`; re-announcing it as the *same* owner is
/// `AlreadyOwned` (idempotent) and never grows the owner's count; a
/// *different* owner is `TakenByOther`.
+340 -1
View File
@@ -15,7 +15,7 @@
//! through the integration thread.
use chrono::{DateTime, Utc};
use sqlx::{PgPool, Row as _};
use sqlx::{PgPool, Postgres, Row as _, Transaction};
use uuid::Uuid;
use crate::error::Result;
@@ -208,6 +208,49 @@ pub async fn insert_report(
Ok(row.try_get("id")?)
}
/// Insert a report inside a caller-owned transaction.
///
/// Protected Enforce callers use this variant so the report row and the
/// authorization receipt share one commit boundary.
pub async fn insert_report_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
report: NewReport<'_>,
) -> Result<Uuid> {
let (target_kind, target_event_id, target_pubkey, target_blob_sha256) = match &report.target {
ReportTarget::Event(id) => ("event", Some(id.as_slice()), None, None),
ReportTarget::Pubkey(pubkey) => ("pubkey", None, Some(pubkey.as_slice()), None),
ReportTarget::Blob(sha256) => ("blob", None, None, Some(sha256.as_slice())),
};
let row = sqlx::query(
r#"
INSERT INTO moderation_reports (
community_id, report_event_id, reporter_pubkey, target_kind,
target_event_id, target_pubkey, target_blob_sha256, channel_id,
report_type, note
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (community_id, report_event_id) DO UPDATE SET
report_event_id = EXCLUDED.report_event_id
RETURNING id
"#,
)
.bind(community.as_uuid())
.bind(report.report_event_id)
.bind(report.reporter_pubkey)
.bind(target_kind)
.bind(target_event_id)
.bind(target_pubkey)
.bind(target_blob_sha256)
.bind(report.channel_id)
.bind(report.report_type)
.bind(report.note)
.fetch_one(&mut **transaction)
.await?;
Ok(row.try_get("id")?)
}
/// List reports for the moderation queue, newest first.
/// `status = None` lists all; `Some("open")` etc. filters.
pub async fn list_reports(
@@ -236,6 +279,32 @@ pub async fn list_reports(
rows.into_iter().map(row_to_report).collect()
}
/// List reports inside a caller-owned authorization transaction.
pub async fn list_reports_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
status: Option<&str>,
limit: i64,
) -> Result<Vec<ReportRecord>> {
let rows = sqlx::query(
r#"
SELECT id, report_event_id, reporter_pubkey, target_kind, target_event_id,
target_pubkey, target_blob_sha256, channel_id, report_type, note,
status, resolved_by, resolved_at, action_id, created_at
FROM moderation_reports
WHERE community_id = $1 AND ($2::text IS NULL OR status = $2)
ORDER BY created_at DESC
LIMIT $3
"#,
)
.bind(community.as_uuid())
.bind(status)
.bind(limit)
.fetch_all(&mut **transaction)
.await?;
rows.into_iter().map(row_to_report).collect()
}
/// Fetch one report by row id.
pub async fn get_report(
pool: &PgPool,
@@ -282,6 +351,31 @@ pub async fn get_report_by_event(
row.map(row_to_report).transpose()
}
/// Lock and fetch a report by signed event id inside a caller-owned
/// transaction.
pub async fn get_report_by_event_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
report_event_id: &[u8],
) -> Result<Option<ReportRecord>> {
let row = sqlx::query(
r#"
SELECT id, report_event_id, reporter_pubkey, target_kind, target_event_id,
target_pubkey, target_blob_sha256, channel_id, report_type, note,
status, resolved_by, resolved_at, action_id, created_at
FROM moderation_reports
WHERE community_id = $1 AND report_event_id = $2
FOR UPDATE
"#,
)
.bind(community.as_uuid())
.bind(report_event_id)
.fetch_optional(&mut **transaction)
.await?;
row.map(row_to_report).transpose()
}
/// Mark a report resolved/dismissed/escalated, linking the audit action.
/// Returns `false` if the report was not found or already closed.
pub async fn resolve_report(
@@ -310,6 +404,33 @@ pub async fn resolve_report(
Ok(result.rows_affected() > 0)
}
/// Resolve a report inside a caller-owned transaction.
pub async fn resolve_report_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
report_id: Uuid,
status: &str,
resolved_by: &[u8],
action_id: Option<Uuid>,
) -> Result<bool> {
let result = sqlx::query(
r#"
UPDATE moderation_reports
SET status = $3, resolved_by = $4, resolved_at = now(), action_id = $5
WHERE community_id = $1 AND id = $2 AND status = 'open'
"#,
)
.bind(community.as_uuid())
.bind(report_id)
.bind(status)
.bind(resolved_by)
.bind(action_id)
.execute(&mut **transaction)
.await?;
Ok(result.rows_affected() > 0)
}
/// Upsert a ban: sets `banned = true` with optional expiry + reason.
pub async fn ban_member(
pool: &PgPool,
@@ -343,6 +464,38 @@ pub async fn ban_member(
Ok(())
}
/// Upsert a ban inside a caller-owned transaction.
pub async fn ban_member_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
pubkey: &[u8],
actor: &[u8],
reason: Option<&str>,
expires_at: Option<DateTime<Utc>>,
) -> Result<()> {
sqlx::query(
r#"
INSERT INTO community_bans (
community_id, pubkey, banned, ban_expires_at, ban_reason, actor_pubkey
) VALUES ($1, $2, true, $3, $4, $5)
ON CONFLICT (community_id, pubkey) DO UPDATE SET
banned = true,
ban_expires_at = EXCLUDED.ban_expires_at,
ban_reason = EXCLUDED.ban_reason,
actor_pubkey = EXCLUDED.actor_pubkey,
updated_at = now()
"#,
)
.bind(community.as_uuid())
.bind(pubkey)
.bind(expires_at)
.bind(reason)
.bind(actor)
.execute(&mut **transaction)
.await?;
Ok(())
}
/// Lift a ban. Returns `false` if the member was not banned.
pub async fn unban_member(
pool: &PgPool,
@@ -367,6 +520,29 @@ pub async fn unban_member(
Ok(result.rows_affected() > 0)
}
/// Lift a ban inside a caller-owned transaction.
pub async fn unban_member_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
pubkey: &[u8],
actor: &[u8],
) -> Result<bool> {
let result = sqlx::query(
r#"
UPDATE community_bans
SET banned = false, ban_expires_at = NULL, ban_reason = NULL,
actor_pubkey = $3, updated_at = now()
WHERE community_id = $1 AND pubkey = $2 AND banned = true
"#,
)
.bind(community.as_uuid())
.bind(pubkey)
.bind(actor)
.execute(&mut **transaction)
.await?;
Ok(result.rows_affected() > 0)
}
/// Upsert a timeout: sets `muted_until` + reason.
pub async fn timeout_member(
pool: &PgPool,
@@ -399,6 +575,37 @@ pub async fn timeout_member(
Ok(())
}
/// Upsert a timeout inside a caller-owned transaction.
pub async fn timeout_member_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
pubkey: &[u8],
actor: &[u8],
muted_until: DateTime<Utc>,
reason: Option<&str>,
) -> Result<()> {
sqlx::query(
r#"
INSERT INTO community_bans (
community_id, pubkey, muted_until, mute_reason, actor_pubkey
) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (community_id, pubkey) DO UPDATE SET
muted_until = EXCLUDED.muted_until,
mute_reason = EXCLUDED.mute_reason,
actor_pubkey = EXCLUDED.actor_pubkey,
updated_at = now()
"#,
)
.bind(community.as_uuid())
.bind(pubkey)
.bind(muted_until)
.bind(reason)
.bind(actor)
.execute(&mut **transaction)
.await?;
Ok(())
}
/// Clear a timeout early. Returns `false` if the member was not timed out.
pub async fn untimeout_member(
pool: &PgPool,
@@ -423,6 +630,29 @@ pub async fn untimeout_member(
Ok(result.rows_affected() > 0)
}
/// Clear a timeout inside a caller-owned transaction.
pub async fn untimeout_member_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
pubkey: &[u8],
actor: &[u8],
) -> Result<bool> {
let result = sqlx::query(
r#"
UPDATE community_bans
SET muted_until = NULL, mute_reason = NULL,
actor_pubkey = $3, updated_at = now()
WHERE community_id = $1 AND pubkey = $2 AND muted_until > now()
"#,
)
.bind(community.as_uuid())
.bind(pubkey)
.bind(actor)
.execute(&mut **transaction)
.await?;
Ok(result.rows_affected() > 0)
}
/// Restriction snapshot consumed by the auth-seam gate (L4) and write gates.
///
/// One cheap read per check: `banned` already accounts for expiry;
@@ -466,6 +696,36 @@ pub async fn restriction_state(
}
}
/// Fetch and share-lock the current restriction state inside a caller-owned
/// authorization transaction.
pub async fn restriction_state_tx(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
community: CommunityId,
pubkey: &[u8],
) -> Result<RestrictionState> {
let row = sqlx::query(
r#"
SELECT
(banned AND (ban_expires_at IS NULL OR ban_expires_at > now())) AS banned,
CASE WHEN muted_until > now() THEN muted_until ELSE NULL END AS muted_until
FROM community_bans
WHERE community_id = $1 AND pubkey = $2
FOR SHARE
"#,
)
.bind(community.as_uuid())
.bind(pubkey)
.fetch_optional(&mut **transaction)
.await?;
match row {
Some(row) => Ok(RestrictionState {
banned: row.try_get("banned")?,
muted_until: row.try_get("muted_until")?,
}),
None => Ok(RestrictionState::default()),
}
}
/// Fetch the full ban/timeout row (moderation queue / audit views).
pub async fn get_ban(
pool: &PgPool,
@@ -514,6 +774,32 @@ pub async fn list_restricted(pool: &PgPool, community: CommunityId) -> Result<Ve
rows.into_iter().map(row_to_ban).collect()
}
/// List active restrictions inside a caller-owned authorization transaction.
pub async fn list_restricted_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
) -> Result<Vec<BanRecord>> {
let rows = sqlx::query(
r#"
SELECT pubkey,
(banned AND (ban_expires_at IS NULL OR ban_expires_at > now())) AS banned,
ban_expires_at, ban_reason, muted_until,
mute_reason, actor_pubkey, updated_at
FROM community_bans
WHERE community_id = $1
AND (
(banned AND (ban_expires_at IS NULL OR ban_expires_at > now()))
OR muted_until > now()
)
ORDER BY updated_at DESC
"#,
)
.bind(community.as_uuid())
.fetch_all(&mut **transaction)
.await?;
rows.into_iter().map(row_to_ban).collect()
}
/// Insert a moderation audit row, returning its id.
pub async fn insert_action(
pool: &PgPool,
@@ -545,6 +831,36 @@ pub async fn insert_action(
Ok(row.try_get("id")?)
}
/// Insert a moderation audit row inside a caller-owned transaction.
pub async fn insert_action_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
action: NewAction<'_>,
) -> Result<Uuid> {
let row = sqlx::query(
r#"
INSERT INTO moderation_actions (
community_id, actor_pubkey, action, target_pubkey, target_event_id,
channel_id, reason_code, public_reason, private_reason, matched_principal
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id
"#,
)
.bind(community.as_uuid())
.bind(action.actor_pubkey)
.bind(action.action)
.bind(action.target_pubkey)
.bind(action.target_event_id)
.bind(action.channel_id)
.bind(action.reason_code)
.bind(action.public_reason)
.bind(action.private_reason)
.bind(action.matched_principal)
.fetch_one(&mut **transaction)
.await?;
Ok(row.try_get("id")?)
}
/// List audit rows, newest first (`buzz moderation audit`).
pub async fn list_actions(
pool: &PgPool,
@@ -569,6 +885,29 @@ pub async fn list_actions(
rows.into_iter().map(row_to_action).collect()
}
/// List audit rows inside a caller-owned authorization transaction.
pub async fn list_actions_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
limit: i64,
) -> Result<Vec<ActionRecord>> {
let rows = sqlx::query(
r#"
SELECT id, actor_pubkey, action, target_pubkey, target_event_id, channel_id,
reason_code, public_reason, private_reason, matched_principal, created_at
FROM moderation_actions
WHERE community_id = $1
ORDER BY created_at DESC
LIMIT $2
"#,
)
.bind(community.as_uuid())
.bind(limit)
.fetch_all(&mut **transaction)
.await?;
rows.into_iter().map(row_to_action).collect()
}
fn row_to_report(row: sqlx::postgres::PgRow) -> Result<ReportRecord> {
let target_kind: String = row.try_get("target_kind")?;
let target = match target_kind.as_str() {
+33 -1
View File
@@ -5,7 +5,7 @@
use chrono::{DateTime, Utc};
use serde::Serialize;
use sqlx::{PgPool, Row as _};
use sqlx::{PgPool, Postgres, Row as _, Transaction};
use uuid::Uuid;
use crate::{error::Result, CommunityId};
@@ -85,6 +85,38 @@ pub async fn insert(
Ok(row.try_get("id")?)
}
/// Insert product feedback inside a caller-owned authorization transaction.
/// The durable feedback row and the authorization receipt therefore commit or
/// roll back together.
pub async fn insert_tx(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
feedback: NewProductFeedback<'_>,
) -> Result<Uuid> {
let row = sqlx::query(
r#"
INSERT INTO product_feedback (
community_id, event_id, submitter_pubkey, category, body, tags,
event_created_at
) VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (event_id) DO UPDATE SET
event_id = EXCLUDED.event_id
RETURNING id
"#,
)
.bind(community.as_uuid())
.bind(feedback.event_id)
.bind(feedback.submitter_pubkey)
.bind(feedback.category)
.bind(feedback.body)
.bind(feedback.tags)
.bind(feedback.event_created_at)
.fetch_one(&mut **transaction)
.await?;
Ok(row.try_get("id")?)
}
/// List feedback across all communities, newest received first.
pub async fn list(pool: &PgPool, limit: i64) -> Result<Vec<ProductFeedbackRecord>> {
let rows = sqlx::query(
+873
View File
@@ -0,0 +1,873 @@
//! PostgreSQL-authoritative visibility for protected object-store content.
use buzz_core::CommunityId;
use serde_json::Value;
use sqlx::{Postgres, Row, Transaction};
use uuid::Uuid;
use crate::protected_visibility::{
require_protected_object_authority, ProtectedObjectAuthorityState, ProtectedObjectSurface,
};
use crate::{Db, DbError, Result};
/// Exact database policy state evaluated by the Git pre-receive hook.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct GitPolicyCommitFence {
/// Exact active kind-30617 event evaluated by the hook.
pub announcement_id: String,
/// Channel bound by that announcement, when present.
pub channel_id: Option<Uuid>,
/// Exact database relationship used to derive the evaluated role.
pub grant: GitPolicyGrant,
}
/// Database relationship that granted the evaluated push role.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum GitPolicyGrant {
/// The authenticated pusher is the repository announcement author.
RepoOwner,
/// The authenticated pusher owns the managed-agent repository key.
ManagedAgentOwner,
/// The authenticated pusher held this exact active channel role.
ChannelMember {
/// Role text as stored in PostgreSQL and evaluated by the hook.
role: String,
},
}
/// Current Git publication selected by PostgreSQL.
#[derive(Clone, PartialEq, Eq)]
pub struct GitPublication {
/// Repository owner key encoded by the existing Git namespace.
pub owner_pubkey: String,
/// Verified immutable manifest digest.
pub manifest_sha256: String,
/// Monotonic compare-and-set version.
pub publication_version: u64,
}
impl std::fmt::Debug for GitPublication {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("GitPublication")
.field("owner_pubkey", &"[redacted]")
.field("manifest_sha256", &"[redacted]")
.field("publication_version", &self.publication_version)
.finish()
}
}
/// Expected parent for one PostgreSQL Git publication CAS.
#[derive(Clone, PartialEq, Eq)]
pub struct ExpectedGitPublication {
/// Monotonic parent version.
pub publication_version: u64,
/// Exact parent manifest digest.
pub manifest_sha256: String,
}
/// Outcome of a PostgreSQL Git publication CAS.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GitPublicationOutcome {
/// The new manifest became authoritative at this version.
Published(GitPublication),
/// The authoritative parent did not match the supplied expectation.
Conflict,
}
/// Inputs for one PostgreSQL-authoritative Git publication.
pub struct GitPublicationRequest<'a> {
/// Community whose repository namespace is being changed.
pub community_id: CommunityId,
/// Stable repository identifier.
pub repo_id: &'a str,
/// Repository owner key encoded by the existing namespace.
pub owner_pubkey: &'a str,
/// Required parent publication, or no parent for initial publication.
pub expected: Option<&'a ExpectedGitPublication>,
/// Digest of the immutable manifest being published.
pub manifest_sha256: &'a str,
/// Authenticated pusher key evaluated by the policy fence.
pub pusher_pubkey: &'a [u8],
/// Exact policy state evaluated before the transaction began.
pub policy: &'a GitPolicyCommitFence,
}
/// Canonical media publication metadata selected by PostgreSQL.
#[derive(Clone, PartialEq, Eq)]
pub struct MediaPublication {
/// Content digest.
pub sha256: String,
/// Immutable object-store key.
pub object_key: String,
/// Canonical path extension.
pub extension: String,
/// Canonical MIME type.
pub mime_type: String,
/// Immutable object size.
pub object_size: u64,
/// Bounded provider-neutral metadata.
pub metadata: Value,
/// Optional immutable thumbnail key.
pub thumbnail_key: Option<String>,
/// Monotonic publication version.
pub publication_version: u64,
}
impl std::fmt::Debug for MediaPublication {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("MediaPublication")
.field("sha256", &"[redacted]")
.field("object_key", &"[redacted]")
.field("extension", &self.extension)
.field("mime_type", &self.mime_type)
.field("object_size", &self.object_size)
.field("metadata", &"[redacted]")
.field("thumbnail_key", &"[redacted]")
.field("publication_version", &self.publication_version)
.finish()
}
}
fn positive_version(value: i64) -> Result<u64> {
u64::try_from(value)
.ok()
.filter(|value| *value > 0)
.ok_or_else(|| DbError::InvalidData("publication version is invalid".into()))
}
fn nonnegative_size(value: i64) -> Result<u64> {
u64::try_from(value)
.map_err(|_| DbError::InvalidData("publication object size is invalid".into()))
}
fn validate_digest(value: &str) -> Result<()> {
if value.len() != 64
|| !value
.chars()
.all(|character| matches!(character, '0'..='9' | 'a'..='f'))
{
return Err(DbError::InvalidData("publication digest is invalid".into()));
}
Ok(())
}
impl Db {
/// Read the active PostgreSQL-authoritative Git publication.
pub async fn git_publication(
&self,
community_id: CommunityId,
repo_id: &str,
owner_pubkey: &str,
) -> Result<Option<GitPublication>> {
let row = sqlx::query(
"SELECT owner_pubkey, manifest_sha256, publication_version \
FROM git_repo_publications \
WHERE community_id = $1 AND repo_id = $2 AND owner_pubkey = $3 \
AND state = 'active'",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.bind(owner_pubkey)
.fetch_optional(&self.pool)
.await?;
row.map(|row| {
Ok(GitPublication {
owner_pubkey: row.try_get("owner_pubkey")?,
manifest_sha256: row.try_get("manifest_sha256")?,
publication_version: positive_version(row.try_get("publication_version")?)?,
})
})
.transpose()
}
/// Read the active PostgreSQL-authoritative media publication.
pub async fn media_publication(
&self,
community_id: CommunityId,
sha256: &str,
) -> Result<Option<MediaPublication>> {
validate_digest(sha256)?;
let row = sqlx::query(
"SELECT sha256, object_key, extension, mime_type, object_size, metadata, \
thumbnail_key, publication_version \
FROM media_publications \
WHERE community_id = $1 AND sha256 = $2 AND state = 'active'",
)
.bind(community_id.as_uuid())
.bind(sha256)
.fetch_optional(&self.pool)
.await?;
row.map(media_publication_from_row).transpose()
}
}
/// Compare and publish one Git manifest inside the caller-owned transaction.
pub async fn compare_and_publish_git(
transaction: &mut Transaction<'_, Postgres>,
request: GitPublicationRequest<'_>,
) -> Result<GitPublicationOutcome> {
let GitPublicationRequest {
community_id,
repo_id,
owner_pubkey,
expected,
manifest_sha256,
pusher_pubkey,
policy,
} = request;
validate_digest(manifest_sha256)?;
require_protected_object_authority(
transaction,
community_id,
ProtectedObjectSurface::Git,
ProtectedObjectAuthorityState::PostgreSql,
)
.await?;
if repo_id.is_empty() || owner_pubkey.is_empty() {
return Err(DbError::InvalidData(
"Git publication identity is invalid".into(),
));
}
validate_git_policy_fence(
transaction,
community_id,
repo_id,
owner_pubkey,
pusher_pubkey,
policy,
)
.await?;
let registered_owner: Option<String> = sqlx::query_scalar(
"SELECT owner_pubkey FROM git_repo_names \
WHERE community_id = $1 AND repo_id = $2 FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.fetch_optional(&mut **transaction)
.await?;
if registered_owner.as_deref() != Some(owner_pubkey) {
return Err(DbError::InvalidData(
"Git publication does not match the registered owner".into(),
));
}
let current = sqlx::query(
"SELECT owner_pubkey, manifest_sha256, publication_version, state \
FROM git_repo_publications \
WHERE community_id = $1 AND repo_id = $2 FOR UPDATE",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.fetch_optional(&mut **transaction)
.await?;
match (current, expected) {
(None, None) => {
sqlx::query(
"INSERT INTO git_repo_publications \
(community_id, repo_id, owner_pubkey, manifest_sha256, publication_version) \
VALUES ($1, $2, $3, $4, 1)",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.bind(owner_pubkey)
.bind(manifest_sha256)
.execute(&mut **transaction)
.await?;
Ok(GitPublicationOutcome::Published(GitPublication {
owner_pubkey: owner_pubkey.to_owned(),
manifest_sha256: manifest_sha256.to_owned(),
publication_version: 1,
}))
}
(Some(row), Some(expected)) => {
let current_owner: String = row.try_get("owner_pubkey")?;
let current_digest: String = row.try_get("manifest_sha256")?;
let current_version = positive_version(row.try_get("publication_version")?)?;
let state: String = row.try_get("state")?;
if state != "active"
|| current_owner != owner_pubkey
|| current_version != expected.publication_version
|| current_digest != expected.manifest_sha256
{
return Ok(GitPublicationOutcome::Conflict);
}
let next = current_version
.checked_add(1)
.ok_or_else(|| DbError::InvalidData("Git publication version exhausted".into()))?;
let next_i64 = i64::try_from(next)
.map_err(|_| DbError::InvalidData("Git publication version exhausted".into()))?;
sqlx::query(
"UPDATE git_repo_publications \
SET manifest_sha256 = $3, publication_version = $4, \
updated_at = clock_timestamp() \
WHERE community_id = $1 AND repo_id = $2",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.bind(manifest_sha256)
.bind(next_i64)
.execute(&mut **transaction)
.await?;
Ok(GitPublicationOutcome::Published(GitPublication {
owner_pubkey: owner_pubkey.to_owned(),
manifest_sha256: manifest_sha256.to_owned(),
publication_version: next,
}))
}
_ => Ok(GitPublicationOutcome::Conflict),
}
}
async fn validate_git_policy_fence(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
repo_id: &str,
owner_pubkey: &str,
pusher_pubkey: &[u8],
policy: &GitPolicyCommitFence,
) -> Result<()> {
let owner = hex::decode(owner_pubkey)
.map_err(|_| DbError::InvalidData("Git policy owner is invalid".into()))?;
let announcement = hex::decode(&policy.announcement_id)
.map_err(|_| DbError::InvalidData("Git policy announcement is invalid".into()))?;
if owner.len() != 32 || pusher_pubkey.len() != 32 || announcement.len() != 32 {
return Err(DbError::InvalidData(
"Git policy identity is invalid".into(),
));
}
let current: Option<i32> = sqlx::query_scalar(
"SELECT 1 FROM events \
WHERE community_id = $1 AND id = $2 AND kind = 30617 \
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(&announcement)
.bind(&owner)
.bind(repo_id)
.fetch_optional(&mut **transaction)
.await?;
if current.is_none() {
return Err(DbError::InvalidData(
"Git policy announcement changed before publication".into(),
));
}
if let Some(channel_id) = policy.channel_id {
let channel_active: Option<i32> = sqlx::query_scalar(
"SELECT 1 FROM channels \
WHERE community_id = $1 AND id = $2 \
AND archived_at IS NULL AND deleted_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.fetch_optional(&mut **transaction)
.await?;
if channel_active.is_none() {
return Err(DbError::InvalidData(
"Git policy channel changed before publication".into(),
));
}
}
let granted = match &policy.grant {
GitPolicyGrant::RepoOwner => pusher_pubkey == owner.as_slice(),
GitPolicyGrant::ManagedAgentOwner => {
let row: Option<i32> = sqlx::query_scalar(
"SELECT 1 FROM users \
WHERE community_id = $1 AND pubkey = $2 \
AND agent_owner_pubkey = $3 AND deactivated_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(&owner)
.bind(pusher_pubkey)
.fetch_optional(&mut **transaction)
.await?;
row.is_some()
}
GitPolicyGrant::ChannelMember { role } => {
let Some(channel_id) = policy.channel_id else {
return Err(DbError::InvalidData("Git policy channel is missing".into()));
};
let current_role: Option<String> = sqlx::query_scalar(
"SELECT role::text FROM channel_members \
WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3 \
AND removed_at IS NULL FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(pusher_pubkey)
.fetch_optional(&mut **transaction)
.await?;
current_role.as_deref() == Some(role.as_str())
}
};
if !granted {
return Err(DbError::InvalidData(
"Git policy grant changed before publication".into(),
));
}
Ok(())
}
/// Publish immutable media metadata inside the caller-owned transaction.
///
/// Republication of identical bytes/metadata is idempotent. A conflicting row
/// for the same content digest fails closed rather than silently changing what
/// an existing URL means.
pub async fn publish_media(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
publication: &MediaPublication,
) -> Result<MediaPublication> {
validate_digest(&publication.sha256)?;
require_protected_object_authority(
transaction,
community_id,
ProtectedObjectSurface::Media,
ProtectedObjectAuthorityState::PostgreSql,
)
.await?;
let size = i64::try_from(publication.object_size)
.map_err(|_| DbError::InvalidData("media publication size is invalid".into()))?;
sqlx::query(
"INSERT INTO media_publications \
(community_id, sha256, object_key, extension, mime_type, object_size, \
metadata, thumbnail_key, publication_version, state) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, 'active') \
ON CONFLICT (community_id, sha256) DO NOTHING",
)
.bind(community_id.as_uuid())
.bind(&publication.sha256)
.bind(&publication.object_key)
.bind(&publication.extension)
.bind(&publication.mime_type)
.bind(size)
.bind(&publication.metadata)
.bind(&publication.thumbnail_key)
.execute(&mut **transaction)
.await?;
let row = sqlx::query(
"SELECT sha256, object_key, extension, mime_type, object_size, metadata, \
thumbnail_key, publication_version \
FROM media_publications \
WHERE community_id = $1 AND sha256 = $2 AND state = 'active' FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(&publication.sha256)
.fetch_optional(&mut **transaction)
.await?
.ok_or_else(|| DbError::InvalidData("media publication is unavailable".into()))?;
let stored = media_publication_from_row(row)?;
if stored.object_key != publication.object_key
|| stored.extension != publication.extension
|| stored.mime_type != publication.mime_type
|| stored.object_size != publication.object_size
|| stored.metadata != publication.metadata
|| stored.thumbnail_key != publication.thumbnail_key
{
return Err(DbError::InvalidData(
"media publication conflicts with existing content metadata".into(),
));
}
Ok(stored)
}
/// Idempotently import one legacy Git publication while visibility remains fenced.
pub async fn import_git_publication(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
repo_id: &str,
owner_pubkey: &str,
manifest_sha256: &str,
) -> Result<()> {
validate_digest(manifest_sha256)?;
require_protected_object_authority(
transaction,
community_id,
ProtectedObjectSurface::Git,
ProtectedObjectAuthorityState::Importing,
)
.await?;
sqlx::query(
"INSERT INTO git_repo_publications \
(community_id, repo_id, owner_pubkey, manifest_sha256, publication_version, state) \
VALUES ($1, $2, $3, $4, 1, 'active') \
ON CONFLICT (community_id, repo_id) DO NOTHING",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.bind(owner_pubkey)
.bind(manifest_sha256)
.execute(&mut **transaction)
.await?;
let stored = sqlx::query(
"SELECT owner_pubkey, manifest_sha256, publication_version, state \
FROM git_repo_publications WHERE community_id = $1 AND repo_id = $2 FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(repo_id)
.fetch_one(&mut **transaction)
.await?;
let stored_owner: String = stored.try_get("owner_pubkey")?;
let stored_digest: String = stored.try_get("manifest_sha256")?;
let stored_version = positive_version(stored.try_get("publication_version")?)?;
let stored_state: String = stored.try_get("state")?;
if stored_owner != owner_pubkey
|| stored_digest != manifest_sha256
|| stored_version != 1
|| stored_state != "active"
{
return Err(DbError::InvalidData(
"Git import conflicts with an existing publication".into(),
));
}
Ok(())
}
/// Idempotently import one legacy media publication while visibility remains fenced.
pub async fn import_media_publication(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
publication: &MediaPublication,
) -> Result<()> {
require_protected_object_authority(
transaction,
community_id,
ProtectedObjectSurface::Media,
ProtectedObjectAuthorityState::Importing,
)
.await?;
// `publish_media` requires the final PostgreSQL state, so reproduce its
// exact idempotent row contract under the import-state lock.
validate_digest(&publication.sha256)?;
let size = i64::try_from(publication.object_size)
.map_err(|_| DbError::InvalidData("media publication size is invalid".into()))?;
sqlx::query(
"INSERT INTO media_publications \
(community_id, sha256, object_key, extension, mime_type, object_size, \
metadata, thumbnail_key, publication_version, state) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, 'active') \
ON CONFLICT (community_id, sha256) DO NOTHING",
)
.bind(community_id.as_uuid())
.bind(&publication.sha256)
.bind(&publication.object_key)
.bind(&publication.extension)
.bind(&publication.mime_type)
.bind(size)
.bind(&publication.metadata)
.bind(&publication.thumbnail_key)
.execute(&mut **transaction)
.await?;
let row = sqlx::query(
"SELECT sha256, object_key, extension, mime_type, object_size, metadata, \
thumbnail_key, publication_version \
FROM media_publications \
WHERE community_id = $1 AND sha256 = $2 AND state = 'active' FOR SHARE",
)
.bind(community_id.as_uuid())
.bind(&publication.sha256)
.fetch_one(&mut **transaction)
.await?;
let stored = media_publication_from_row(row)?;
if stored.object_key != publication.object_key
|| stored.extension != publication.extension
|| stored.mime_type != publication.mime_type
|| stored.object_size != publication.object_size
|| stored.metadata != publication.metadata
|| stored.thumbnail_key != publication.thumbnail_key
|| stored.publication_version != 1
{
return Err(DbError::InvalidData(
"media import conflicts with an existing publication".into(),
));
}
Ok(())
}
/// List exact Git repository reservations for one migration domain.
pub async fn list_git_repo_reservations(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
) -> Result<Vec<(String, String, String)>> {
require_protected_object_authority(
transaction,
community_id,
ProtectedObjectSurface::Git,
ProtectedObjectAuthorityState::Importing,
)
.await?;
let rows = sqlx::query(
"SELECT repo_id, owner_pubkey, publication_origin FROM git_repo_names \
WHERE community_id = $1 ORDER BY repo_id FOR SHARE",
)
.bind(community_id.as_uuid())
.fetch_all(&mut **transaction)
.await?;
rows.into_iter()
.map(|row| {
Ok((
row.try_get("repo_id")?,
row.try_get("owner_pubkey")?,
row.try_get("publication_origin")?,
))
})
.collect()
}
/// List the exact imported Git inventory for parity validation.
pub async fn list_git_publications(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
) -> Result<Vec<(String, String, String)>> {
let rows = sqlx::query(
"SELECT repo_id, owner_pubkey, manifest_sha256 FROM git_repo_publications \
WHERE community_id = $1 AND state = 'active' ORDER BY repo_id FOR SHARE",
)
.bind(community_id.as_uuid())
.fetch_all(&mut **transaction)
.await?;
rows.into_iter()
.map(|row| {
Ok((
row.try_get("repo_id")?,
row.try_get("owner_pubkey")?,
row.try_get("manifest_sha256")?,
))
})
.collect()
}
/// List the exact imported media inventory for parity validation.
pub async fn list_media_publications(
transaction: &mut Transaction<'_, Postgres>,
community_id: CommunityId,
) -> Result<Vec<MediaPublication>> {
let rows = sqlx::query(
"SELECT sha256, object_key, extension, mime_type, object_size, metadata, \
thumbnail_key, publication_version FROM media_publications \
WHERE community_id = $1 AND state = 'active' ORDER BY sha256 FOR SHARE",
)
.bind(community_id.as_uuid())
.fetch_all(&mut **transaction)
.await?;
rows.into_iter().map(media_publication_from_row).collect()
}
fn media_publication_from_row(row: sqlx::postgres::PgRow) -> Result<MediaPublication> {
Ok(MediaPublication {
sha256: row.try_get("sha256")?,
object_key: row.try_get("object_key")?,
extension: row.try_get("extension")?,
mime_type: row.try_get("mime_type")?,
object_size: nonnegative_size(row.try_get("object_size")?)?,
metadata: row.try_get("metadata")?,
thumbnail_key: row.try_get("thumbnail_key")?,
publication_version: positive_version(row.try_get("publication_version")?)?,
})
}
#[cfg(test)]
mod tests {
use super::*;
use sqlx::PgPool;
use uuid::Uuid;
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz";
async fn setup() -> (Db, CommunityId) {
let database_url = std::env::var("BUZZ_TEST_DATABASE_URL")
.or_else(|_| std::env::var("DATABASE_URL"))
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
let pool = PgPool::connect(&database_url).await.expect("test database");
sqlx::migrate!("../../migrations")
.run(&pool)
.await
.expect("migrated test database");
let id = Uuid::new_v4();
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
.bind(id)
.bind(format!("publication-{}.example", id.simple()))
.execute(&pool)
.await
.expect("community");
for surface in ["git", "media"] {
sqlx::query(
"INSERT INTO protected_object_authority \
(community_id, surface, state, generation, imported_objects, \
inventory_sha256, started_at, completed_at) \
VALUES ($1, $2, 'postgresql', 2, 0, $3, \
clock_timestamp(), clock_timestamp())",
)
.bind(id)
.bind(surface)
.bind("0".repeat(64))
.execute(&pool)
.await
.expect("protected object authority");
}
(Db::from_pool(pool), CommunityId::from_uuid(id))
}
fn digest(byte: u8) -> String {
format!("{byte:02x}").repeat(32)
}
#[tokio::test]
#[ignore = "requires migrated Postgres"]
async fn git_publication_is_owner_bound_and_compare_and_set() {
let (db, community) = setup().await;
let repo = format!("repo-{}", Uuid::new_v4().simple());
let owner = digest(1);
let owner_bytes = hex::decode(&owner).expect("owner bytes");
let announcement_id = digest(8);
sqlx::query(
"INSERT INTO events \
(community_id, id, pubkey, created_at, kind, tags, content, sig, d_tag) \
VALUES ($1, $2, $3, clock_timestamp(), 30617, $4, '', $5, $6)",
)
.bind(community.as_uuid())
.bind(hex::decode(&announcement_id).expect("announcement bytes"))
.bind(&owner_bytes)
.bind(serde_json::json!([["d", repo]]))
.bind(vec![0_u8; 64])
.bind(&repo)
.execute(&db.pool)
.await
.expect("announcement");
sqlx::query(
"INSERT INTO git_repo_names (community_id, repo_id, owner_pubkey) \
VALUES ($1, $2, $3)",
)
.bind(community.as_uuid())
.bind(&repo)
.bind(&owner)
.execute(&db.pool)
.await
.expect("repo reservation");
let policy = GitPolicyCommitFence {
announcement_id,
channel_id: None,
grant: GitPolicyGrant::RepoOwner,
};
let mut transaction = db.begin_transaction().await.expect("transaction");
let first = compare_and_publish_git(
&mut transaction,
GitPublicationRequest {
community_id: community,
repo_id: &repo,
owner_pubkey: &owner,
expected: None,
manifest_sha256: &digest(2),
pusher_pubkey: &owner_bytes,
policy: &policy,
},
)
.await
.expect("first publication");
transaction.commit().await.expect("commit");
let GitPublicationOutcome::Published(first) = first else {
panic!("first publication must win")
};
assert_eq!(first.publication_version, 1);
assert!(db
.git_publication(community, &repo, &digest(9))
.await
.expect("wrong-owner read")
.is_none());
let mut stale = db.begin_transaction().await.expect("stale transaction");
assert_eq!(
compare_and_publish_git(
&mut stale,
GitPublicationRequest {
community_id: community,
repo_id: &repo,
owner_pubkey: &owner,
expected: None,
manifest_sha256: &digest(3),
pusher_pubkey: &owner_bytes,
policy: &policy,
},
)
.await
.expect("stale compare"),
GitPublicationOutcome::Conflict
);
stale.rollback().await.expect("rollback stale compare");
let mut next = db.begin_transaction().await.expect("next transaction");
let expected = ExpectedGitPublication {
publication_version: first.publication_version,
manifest_sha256: first.manifest_sha256,
};
let second = compare_and_publish_git(
&mut next,
GitPublicationRequest {
community_id: community,
repo_id: &repo,
owner_pubkey: &owner,
expected: Some(&expected),
manifest_sha256: &digest(3),
pusher_pubkey: &owner_bytes,
policy: &policy,
},
)
.await
.expect("next publication");
next.commit().await.expect("commit next");
assert!(matches!(
second,
GitPublicationOutcome::Published(GitPublication {
publication_version: 2,
..
})
));
}
#[tokio::test]
#[ignore = "requires migrated Postgres"]
async fn media_visibility_is_transaction_owned_and_rollback_leaves_no_row() {
let (db, community) = setup().await;
let publication = MediaPublication {
sha256: digest(4),
object_key: format!("{}.jpg", digest(4)),
extension: "jpg".into(),
mime_type: "image/jpeg".into(),
object_size: 17,
metadata: serde_json::json!({"synthetic": true}),
thumbnail_key: None,
publication_version: 1,
};
let mut rolled_back = db.begin_transaction().await.expect("transaction");
publish_media(&mut rolled_back, community, &publication)
.await
.expect("staged publication");
rolled_back.rollback().await.expect("rollback");
assert!(db
.media_publication(community, &publication.sha256)
.await
.expect("publication read")
.is_none());
let mut committed = db.begin_transaction().await.expect("transaction");
publish_media(&mut committed, community, &publication)
.await
.expect("publication");
committed.commit().await.expect("commit");
assert_eq!(
db.media_publication(community, &publication.sha256)
.await
.expect("publication read")
.expect("published row")
.object_key,
publication.object_key
);
}
}
+396
View File
@@ -0,0 +1,396 @@
//! Monotonic migration authority for protected Git and media visibility.
use buzz_core::CommunityId;
use sqlx::{Postgres, Row, Transaction};
use crate::{Db, DbError, Result};
/// Object-store visibility family being migrated.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProtectedObjectSurface {
/// Git repository pointers and manifests.
Git,
/// Media sidecars and immutable blobs.
Media,
}
impl ProtectedObjectSurface {
fn as_str(self) -> &'static str {
match self {
Self::Git => "git",
Self::Media => "media",
}
}
}
/// Durable authority state for one community and surface.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProtectedObjectAuthorityState {
/// Legacy pointer or sidecar remains authoritative.
Legacy,
/// Legacy writes are fenced while an idempotent import is validated.
Importing,
/// PostgreSQL is the sole visibility authority.
PostgreSql,
}
/// Current durable migration state.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProtectedObjectAuthority {
/// Monotonic migration generation.
pub generation: u64,
/// Current authority state.
pub state: ProtectedObjectAuthorityState,
/// Exact imported-object count recorded at cutover.
pub imported_objects: Option<u64>,
/// Exact imported inventory digest recorded at cutover.
pub inventory_sha256: Option<String>,
}
fn decode_state(state: &str) -> Result<ProtectedObjectAuthorityState> {
match state {
"legacy" => Ok(ProtectedObjectAuthorityState::Legacy),
"importing" => Ok(ProtectedObjectAuthorityState::Importing),
"postgresql" => Ok(ProtectedObjectAuthorityState::PostgreSql),
_ => Err(DbError::InvalidData(
"protected object authority state is invalid".into(),
)),
}
}
fn decode_generation(value: i64) -> Result<u64> {
u64::try_from(value)
.ok()
.filter(|value| *value > 0)
.ok_or_else(|| {
DbError::InvalidData("protected object authority generation is invalid".into())
})
}
async fn ensure_row(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
surface: ProtectedObjectSurface,
) -> Result<()> {
sqlx::query(
"INSERT INTO protected_object_authority \
(community_id, surface, state, generation) VALUES ($1, $2, 'legacy', 1) \
ON CONFLICT (community_id, surface) DO NOTHING",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.execute(&mut **transaction)
.await?;
Ok(())
}
impl Db {
/// Read durable authority, treating an untouched domain as legacy.
pub async fn protected_object_authority(
&self,
community: CommunityId,
surface: ProtectedObjectSurface,
) -> Result<ProtectedObjectAuthority> {
let row = sqlx::query(
"SELECT state, generation, imported_objects, inventory_sha256 \
FROM protected_object_authority \
WHERE community_id = $1 AND surface = $2",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.fetch_optional(&self.pool)
.await?;
match row {
Some(row) => {
let imported_objects: i64 = row.try_get("imported_objects")?;
Ok(ProtectedObjectAuthority {
state: decode_state(row.try_get("state")?)?,
generation: decode_generation(row.try_get("generation")?)?,
imported_objects: (imported_objects > 0)
.then(|| u64::try_from(imported_objects).ok())
.flatten()
.or_else(|| (imported_objects == 0).then_some(0)),
inventory_sha256: row.try_get("inventory_sha256")?,
})
}
None => Ok(ProtectedObjectAuthority {
state: ProtectedObjectAuthorityState::Legacy,
generation: 1,
imported_objects: None,
inventory_sha256: None,
}),
}
}
/// Begin or resume an import after draining transaction-held legacy writers.
pub async fn begin_protected_object_import(
&self,
community: CommunityId,
surface: ProtectedObjectSurface,
) -> Result<ProtectedObjectAuthority> {
let mut transaction = self.begin_transaction().await?;
ensure_row(&mut transaction, community, surface).await?;
let row = sqlx::query(
"SELECT state, generation FROM protected_object_authority \
WHERE community_id = $1 AND surface = $2 FOR UPDATE",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.fetch_one(&mut *transaction)
.await?;
let state = decode_state(row.try_get("state")?)?;
let mut generation = decode_generation(row.try_get("generation")?)?;
if state == ProtectedObjectAuthorityState::Legacy {
generation = generation.checked_add(1).ok_or_else(|| {
DbError::InvalidData("protected object authority generation exhausted".into())
})?;
let generation_i64 = i64::try_from(generation).map_err(|_| {
DbError::InvalidData("protected object authority generation exhausted".into())
})?;
sqlx::query(
"UPDATE protected_object_authority SET state = 'importing', \
generation = $3, started_at = clock_timestamp(), completed_at = NULL, \
imported_objects = 0, inventory_sha256 = NULL, \
updated_at = clock_timestamp() \
WHERE community_id = $1 AND surface = $2",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.bind(generation_i64)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(ProtectedObjectAuthority {
generation,
state: if state == ProtectedObjectAuthorityState::Legacy {
ProtectedObjectAuthorityState::Importing
} else {
state
},
imported_objects: None,
inventory_sha256: None,
})
}
/// Finalize one exact import generation after a complete parity pass.
pub async fn finalize_protected_object_import(
&self,
community: CommunityId,
surface: ProtectedObjectSurface,
generation: u64,
imported_objects: u64,
inventory_sha256: &str,
) -> Result<()> {
validate_inventory_digest(inventory_sha256)?;
let generation = i64::try_from(generation).map_err(|_| {
DbError::InvalidData("protected object authority generation is invalid".into())
})?;
let imported_objects = i64::try_from(imported_objects)
.map_err(|_| DbError::InvalidData("protected object import count is invalid".into()))?;
let result = sqlx::query(
"UPDATE protected_object_authority SET state = 'postgresql', \
imported_objects = $4, inventory_sha256 = $5, \
completed_at = clock_timestamp(), updated_at = clock_timestamp() \
WHERE community_id = $1 AND surface = $2 AND state = 'importing' \
AND generation = $3",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.bind(generation)
.bind(imported_objects)
.bind(inventory_sha256)
.execute(&self.pool)
.await?;
if result.rows_affected() != 1 {
let current = self.protected_object_authority(community, surface).await?;
if current.state != ProtectedObjectAuthorityState::PostgreSql
|| current.generation != u64::try_from(generation).unwrap_or_default()
|| current.imported_objects
!= Some(u64::try_from(imported_objects).unwrap_or_default())
|| current.inventory_sha256.as_deref() != Some(inventory_sha256)
{
return Err(DbError::InvalidData(
"protected object import generation changed".into(),
));
}
}
Ok(())
}
/// Acquire a transaction-held fence spanning a legacy pointer/sidecar write.
pub async fn begin_legacy_visibility_write(
&self,
community: CommunityId,
surface: ProtectedObjectSurface,
) -> Result<LegacyVisibilityWrite> {
let mut transaction = self.begin_transaction().await?;
ensure_row(&mut transaction, community, surface).await?;
let state: String = sqlx::query_scalar(
"SELECT state FROM protected_object_authority \
WHERE community_id = $1 AND surface = $2 FOR SHARE",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.fetch_one(&mut *transaction)
.await?;
if decode_state(&state)? != ProtectedObjectAuthorityState::Legacy {
return Err(DbError::InvalidData(
"legacy object visibility is no longer writable".into(),
));
}
Ok(LegacyVisibilityWrite { transaction })
}
}
/// Transaction lock held across one actual legacy visibility write.
pub struct LegacyVisibilityWrite {
transaction: Transaction<'static, Postgres>,
}
impl LegacyVisibilityWrite {
/// Commit after the pointer or sidecar has become visible.
pub async fn commit(self) -> Result<()> {
self.transaction.commit().await.map_err(Into::into)
}
}
/// Require an exact authority state inside a caller-owned transaction.
pub async fn require_protected_object_authority(
transaction: &mut Transaction<'_, Postgres>,
community: CommunityId,
surface: ProtectedObjectSurface,
expected: ProtectedObjectAuthorityState,
) -> Result<()> {
ensure_row(transaction, community, surface).await?;
let state: String = sqlx::query_scalar(
"SELECT state FROM protected_object_authority \
WHERE community_id = $1 AND surface = $2 FOR SHARE",
)
.bind(community.as_uuid())
.bind(surface.as_str())
.fetch_one(&mut **transaction)
.await?;
if decode_state(&state)? != expected {
return Err(DbError::InvalidData(
"protected object visibility authority is unavailable".into(),
));
}
Ok(())
}
fn validate_inventory_digest(value: &str) -> Result<()> {
if value.len() != 64
|| !value
.chars()
.all(|character| matches!(character, '0'..='9' | 'a'..='f'))
{
return Err(DbError::InvalidData(
"protected object inventory digest is invalid".into(),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use uuid::Uuid;
#[test]
fn authority_state_decoder_is_closed() {
assert_eq!(
decode_state("postgresql").expect("known state"),
ProtectedObjectAuthorityState::PostgreSql
);
assert!(decode_state("fallback").is_err());
}
#[test]
fn inventory_digest_is_strict_lowercase_sha256() {
assert!(validate_inventory_digest(&"a".repeat(64)).is_ok());
assert!(validate_inventory_digest(&"A".repeat(64)).is_err());
assert!(validate_inventory_digest("short").is_err());
}
#[tokio::test]
#[ignore = "requires migrated Postgres"]
async fn cutover_waits_for_legacy_commit_and_never_reverses() {
let database_url = std::env::var("BUZZ_TEST_DATABASE_URL")
.or_else(|_| std::env::var("DATABASE_URL"))
.unwrap_or_else(|_| "postgres://buzz:buzz_dev@localhost:5432/buzz".to_owned());
let pool = PgPool::connect(&database_url).await.expect("test database");
sqlx::migrate!("../../migrations")
.run(&pool)
.await
.expect("migrated test database");
let id = Uuid::new_v4();
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
.bind(id)
.bind(format!("visibility-{}.example", id.simple()))
.execute(&pool)
.await
.expect("community");
let db = Db::from_pool(pool);
let community = CommunityId::from_uuid(id);
let guard = db
.begin_legacy_visibility_write(community, ProtectedObjectSurface::Git)
.await
.expect("legacy writer");
let contender_pool = PgPoolOptions::new()
.max_connections(1)
.after_connect(|connection, _| {
Box::pin(async move {
sqlx::query("SET lock_timeout = '100ms'")
.execute(connection)
.await?;
Ok(())
})
})
.connect(&database_url)
.await
.expect("contender database");
let contender = Db::from_pool(contender_pool);
let blocked = contender
.begin_protected_object_import(community, ProtectedObjectSurface::Git)
.await
.expect_err("cutover must wait for the legacy writer");
assert!(
matches!(
blocked,
DbError::Sqlx(sqlx::Error::Database(ref error))
if error.code().as_deref() == Some("55P03")
),
"the real cutover row lock must block: {blocked:?}"
);
guard.commit().await.expect("legacy commit");
let importing = db
.begin_protected_object_import(community, ProtectedObjectSurface::Git)
.await
.expect("begin import");
assert_eq!(importing.state, ProtectedObjectAuthorityState::Importing);
assert!(db
.begin_legacy_visibility_write(community, ProtectedObjectSurface::Git)
.await
.is_err());
db.finalize_protected_object_import(
community,
ProtectedObjectSurface::Git,
importing.generation,
0,
&"0".repeat(64),
)
.await
.expect("finalize");
let finished = db
.begin_protected_object_import(community, ProtectedObjectSurface::Git)
.await
.expect("monotonic retry");
assert_eq!(finished.state, ProtectedObjectAuthorityState::PostgreSql);
assert_eq!(finished.generation, importing.generation);
}
}
+1
View File
@@ -8,6 +8,7 @@ repository.workspace = true
description = "Media storage, validation, and thumbnail generation for Buzz"
[dependencies]
buzz-auth = { workspace = true }
buzz-core = { workspace = true }
nostr = { workspace = true }
serde = { workspace = true }
+31 -201
View File
@@ -1,241 +1,71 @@
//! Blossom kind:24242 auth verification (BUD-11 compliant).
use crate::error::MediaError;
pub use buzz_auth::blossom::BlossomVerb;
/// Blossom kind:24242 verbs Buzz currently accepts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlossomVerb {
Upload,
Get,
}
fn map_auth_error(error: buzz_auth::blossom::BlossomAuthError) -> MediaError {
use buzz_auth::blossom::BlossomAuthError;
impl BlossomVerb {
fn as_str(self) -> &'static str {
match self {
Self::Upload => "upload",
Self::Get => "get",
}
match error {
BlossomAuthError::InvalidSignature => MediaError::InvalidSignature,
BlossomAuthError::InvalidAuthKind => MediaError::InvalidAuthKind,
BlossomAuthError::InvalidAuthEvent => MediaError::InvalidAuthEvent,
BlossomAuthError::InvalidAuthVerb => MediaError::InvalidAuthVerb,
BlossomAuthError::MissingTag(tag) => MediaError::MissingTag(tag),
BlossomAuthError::TokenExpired => MediaError::TokenExpired,
BlossomAuthError::TimestampOutOfWindow => MediaError::TimestampOutOfWindow,
BlossomAuthError::ServerMismatch => MediaError::ServerMismatch,
BlossomAuthError::HashMismatch => MediaError::HashMismatch,
BlossomAuthError::InsufficientScope => MediaError::InsufficientScope,
}
}
/// Verify common kind:24242 Blossom auth event validity:
/// 1. Schnorr signature
/// 2. kind == 24242
/// 3. `t` tag matches `verb`
/// 4. `expiration` tag in the future
/// 5. `created_at` in the past (with 5s clock-skew tolerance)
/// 6. If `server` tags present, our domain must appear in at least one
///
/// Does NOT check verb-specific scope tags (`x` for upload, `x` OR `server`
/// for get). Call this BEFORE trusting the event's pubkey for scope resolution.
/// Verify common kind:24242 Blossom auth event validity for one exact verb.
pub fn verify_blossom_auth_event_for_verb(
auth_event: &nostr::Event,
verb: BlossomVerb,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), MediaError> {
// 1. Verify Schnorr signature
auth_event
.verify()
.map_err(|_| MediaError::InvalidSignature)?;
// 2. Kind must be 24242
if auth_event.kind.as_u16() != 24242 {
return Err(MediaError::InvalidAuthKind);
}
// 2b. Content must be non-empty (BUD-11: "human readable string")
if auth_event.content.trim().is_empty() {
return Err(MediaError::InvalidAuthEvent);
}
let mut found_t = false;
let mut found_exp = false;
let mut server_tags: Vec<&str> = Vec::new();
let mut exp_value: u64 = 0;
for tag in auth_event.tags.iter() {
let kind = tag.kind().to_string();
match kind.as_str() {
"t" => {
if let Some(v) = tag.content() {
if v != verb.as_str() {
return Err(MediaError::InvalidAuthVerb);
}
found_t = true;
}
}
"expiration" => {
if let Some(v) = tag.content() {
exp_value = v.parse().unwrap_or(0);
found_exp = true;
}
}
"server" => {
if let Some(v) = tag.content() {
server_tags.push(v);
}
}
_ => {}
}
}
// 3. t tag required
if !found_t {
return Err(MediaError::MissingTag("t"));
}
// 4. Expiration must exist and be in the future
if !found_exp {
return Err(MediaError::MissingTag("expiration"));
}
let now = nostr::Timestamp::now().as_secs();
if exp_value <= now {
return Err(MediaError::TokenExpired);
}
// 5. created_at must be recent: not in the future (5s tolerance) and not
// older than 10 minutes. This bounds the replay window — even if the
// expiration tag allows a longer lifetime, the token must have been
// freshly minted.
let created = auth_event.created_at.as_secs();
if created > now + 5 {
return Err(MediaError::TimestampOutOfWindow);
}
if now > created + max_age_secs {
return Err(MediaError::TimestampOutOfWindow);
}
// 6. Server tag enforcement (BUD-11 §5): if server tags present, our host must appear.
//
// `server_domain` is the host this request was bound to — the per-request
// tenant host (`TenantContext::host()`), NOT a single process-global domain.
// A relay process serves many tenant hosts; validating against one global
// host would 401 every non-primary tenant's server-tagged client (the stock
// CLI always tags its configured relay host). Comparison is done under the
// shared [`normalize_host`] rule so a tag and the bound host agree by
// construction across case, trailing dot, default ports, and an optional
// URL scheme/path — exactly as every other host seam resolves tenants.
//
// Fail closed: if the bound host is unknown, reject tokens that carry server
// tags rather than silently accepting them.
if !server_tags.is_empty() {
match server_domain {
Some(domain) => {
let want = normalize_server_host(domain);
let matches = server_tags
.iter()
.any(|tag| normalize_server_host(tag) == want);
if !matches {
return Err(MediaError::ServerMismatch);
}
}
None => {
// Server tags present but we don't know our own host — reject.
return Err(MediaError::ServerMismatch);
}
}
}
Ok(())
buzz_auth::blossom::verify_blossom_auth_event_for_verb(
auth_event,
verb,
server_domain,
max_age_secs,
)
.map_err(map_auth_error)
}
/// Verify common upload auth event validity.
///
/// Kept as the upload-shaped public wrapper for existing callers; new verb-aware
/// code should prefer [`verify_blossom_auth_event_for_verb`].
/// Verify common upload auth event validity without checking the blob hash.
pub fn verify_blossom_auth_event(
auth_event: &nostr::Event,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), MediaError> {
verify_blossom_auth_event_for_verb(auth_event, BlossomVerb::Upload, server_domain, max_age_secs)
buzz_auth::blossom::verify_blossom_auth_event(auth_event, server_domain, max_age_secs)
.map_err(map_auth_error)
}
/// Normalize a Blossom `server` tag value (or a bound tenant host) into the
/// canonical host form used as the community lookup key.
///
/// A `server` tag may be a bare authority (`relay.example:3100`, what the stock
/// CLI emits) or a full URL (`https://relay.example/`). We strip an optional
/// scheme and path down to the authority, then apply the one shared
/// [`buzz_core::tenant::normalize_host`] rule so the comparison agrees with how
/// the WS/HTTP/git doors resolve tenants.
fn normalize_server_host(value: &str) -> String {
let authority = match value.split_once("://") {
Some((_scheme, rest)) => rest.split('/').next().unwrap_or(rest),
None => value.split('/').next().unwrap_or(value),
};
buzz_core::tenant::normalize_host(authority)
}
/// Verify a kind:24242 Blossom upload auth event, including the x tag hash check.
///
/// Calls [`verify_blossom_auth_event`] first, then verifies that at least one
/// `x` tag matches `sha256` (BUD-11 §6: "at least one x tag matches").
/// Verify a kind:24242 upload event including the exact `x` tag blob hash.
pub fn verify_blossom_upload_auth(
auth_event: &nostr::Event,
sha256: &str,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), MediaError> {
verify_blossom_auth_event_for_verb(
auth_event,
BlossomVerb::Upload,
server_domain,
max_age_secs,
)?;
// At least one x tag must match the body sha256 (BUD-11 §6)
let has_matching_x = auth_event
.tags
.iter()
.any(|tag| tag.kind().to_string() == "x" && (tag.content() == Some(sha256)));
if !has_matching_x {
return Err(MediaError::HashMismatch);
}
Ok(())
buzz_auth::blossom::verify_blossom_upload_auth(auth_event, sha256, server_domain, max_age_secs)
.map_err(map_auth_error)
}
/// Verify a kind:24242 Blossom get auth event for one requested blob.
///
/// BUD-01 permits either blob-scoped authorization (`x` tag matches `sha256`)
/// or server-scoped authorization (`server` tag matches this relay host). The
/// latter intentionally grants reads for all blobs on the host until expiration;
/// callers must still apply relay membership after this verifier returns.
/// Verify a kind:24242 download event for one exact blob and server.
pub fn verify_blossom_get_auth(
auth_event: &nostr::Event,
sha256: &str,
server_domain: Option<&str>,
max_age_secs: u64,
) -> Result<(), MediaError> {
verify_blossom_auth_event_for_verb(auth_event, BlossomVerb::Get, server_domain, max_age_secs)?;
let has_matching_x = auth_event
.tags
.iter()
.any(|tag| tag.kind().to_string() == "x" && (tag.content() == Some(sha256)));
let has_matching_server = match server_domain {
Some(domain) => {
let want = normalize_server_host(domain);
auth_event.tags.iter().any(|tag| {
tag.kind().to_string() == "server"
&& tag
.content()
.map(|value| normalize_server_host(value) == want)
.unwrap_or(false)
})
}
None => false,
};
if !has_matching_x && !has_matching_server {
return Err(MediaError::InsufficientScope);
}
Ok(())
buzz_auth::blossom::verify_blossom_get_auth(auth_event, sha256, server_domain, max_age_secs)
.map_err(map_auth_error)
}
#[cfg(test)]
+4 -1
View File
@@ -21,7 +21,10 @@ pub use config::{MediaConfig, S3AddressingStyle};
pub use error::MediaError;
pub use storage::{BlobHeadMeta, BlobMeta, ByteStream, MediaStorage};
pub use types::BlobDescriptor;
pub use upload::{process_file_upload, process_upload, process_video_upload};
pub use upload::{
process_file_upload, process_upload, process_video_upload, PreparedUpload, UploadCommitGuard,
UploadPublicationMode,
};
pub use upload_record::{
parse_port, parse_public_ip, upload_record_key, UploadAttribution, UploadNetworkInfo,
UploadRecord, UPLOAD_RECORD_VERSION,
+64 -7
View File
@@ -7,6 +7,15 @@ use buzz_core::tenant::{CommunityId, TenantContext};
use crate::config::{MediaConfig, S3AddressingStyle};
use crate::error::MediaError;
/// Result of an immutable, create-only object write.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CreateOnlyOutcome {
/// This caller created the object.
Created,
/// An object already existed at the key.
AlreadyExists,
}
use bytes::Bytes;
use s3::creds::Credentials;
use s3::{Bucket, Region};
@@ -80,6 +89,37 @@ impl MediaStorage {
Ok(())
}
/// Create an immutable object without overwriting an existing value.
pub async fn put_create_only(
&self,
key: &str,
bytes: &[u8],
content_type: &str,
) -> Result<CreateOnlyOutcome, MediaError> {
let mut headers = axum::http::HeaderMap::new();
headers.insert(
axum::http::header::IF_NONE_MATCH,
axum::http::HeaderValue::from_static("*"),
);
match self
.bucket
.put_object_with_content_type_and_headers(key, bytes, content_type, Some(headers))
.await
{
Ok(response) if (200..300).contains(&response.status_code()) => {
Ok(CreateOnlyOutcome::Created)
}
Err(s3::error::S3Error::HttpFailWithBody(412, _)) => {
Ok(CreateOnlyOutcome::AlreadyExists)
}
Ok(response) => Err(MediaError::StorageError(format!(
"create-only object write returned status {}",
response.status_code()
))),
Err(error) => Err(MediaError::StorageError(error.to_string())),
}
}
/// Stream a file from disk into S3 without loading it into RAM.
///
/// Uses rust-s3's `put_object_stream_with_content_type` which reads from
@@ -113,6 +153,15 @@ impl MediaStorage {
}
}
/// Retrieve an object's bytes, returning `None` only for an absent key.
pub async fn get_optional(&self, key: &str) -> Result<Option<Vec<u8>>, MediaError> {
match self.bucket.get_object(key).await {
Ok(response) => Ok(Some(response.to_vec())),
Err(s3::error::S3Error::HttpFailWithBody(404, _)) => Ok(None),
Err(error) => Err(MediaError::StorageError(error.to_string())),
}
}
/// Retrieve a byte range from an object via S3-native `Range` GET.
///
/// `start` and `end` are inclusive byte offsets. Only the requested slice
@@ -246,16 +295,24 @@ impl MediaStorage {
&self,
continuation_token: Option<String>,
max_keys: usize,
) -> Result<crate::bucket_index::Page, MediaError> {
self.list_page_with_prefix(String::new(), continuation_token, max_keys)
.await
}
/// One bounded page under an exact object-key prefix.
///
/// Migration callers use this to inventory one server-resolved community
/// without observing or loading another community's metadata sidecars.
pub async fn list_page_with_prefix(
&self,
prefix: String,
continuation_token: Option<String>,
max_keys: usize,
) -> Result<crate::bucket_index::Page, MediaError> {
let (result, _status) = self
.bucket
.list_page(
String::new(),
None,
continuation_token,
None,
Some(max_keys),
)
.list_page(prefix, None, continuation_token, None, Some(max_keys))
.await?;
Ok(crate::bucket_index::Page {
objects: result
+214 -97
View File
@@ -12,11 +12,55 @@ use crate::storage::{BlobMeta, MediaStorage};
use crate::thumbnail::generate_image_metadata_sync;
use crate::types::BlobDescriptor;
use crate::upload_record::{record_upload_event, UploadAttribution, UploadEventFacts};
/// Read-only authorization checkpoint invoked immediately before durable media effects.
pub trait UploadCommitGuard: Send + Sync {
/// Deny if the upload no longer has current authority.
fn revalidate(&self) -> Result<(), MediaError>;
}
use crate::validation::{
looks_like_mp4_iso_bmff, mime_to_ext, validate_content, validate_file_content,
validate_video_file,
};
/// Visibility contract selected by the relay before any upload side effect.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UploadPublicationMode {
/// Preserve the existing sidecar and upload-record publication contract.
Legacy,
/// Stage only immutable objects; PostgreSQL decides protected visibility.
ProtectedStaging,
}
/// Validated immutable objects and metadata awaiting an authoritative publish.
#[derive(Debug, Clone)]
pub struct PreparedUpload {
pub descriptor: BlobDescriptor,
pub metadata: BlobMeta,
pub object_key: String,
pub thumbnail_key: Option<String>,
}
/// Inputs for one buffered upload request.
pub struct BufferedUploadRequest<'a> {
/// Object storage used for immutable staging.
pub storage: &'a MediaStorage,
/// Media validation and size limits.
pub config: &'a MediaConfig,
/// Server-resolved tenant context.
pub ctx: &'a TenantContext,
/// Authenticated Blossom upload event.
pub auth_event: &'a nostr::Event,
/// Bounded upload bytes.
pub body: Bytes,
/// Optional upload-event attribution for the legacy path.
pub attribution: Option<UploadAttribution>,
/// Authority checkpoint used immediately before durable effects.
pub commit_guard: &'a dyn UploadCommitGuard,
/// Visibility contract selected by the relay.
pub publication_mode: UploadPublicationMode,
}
/// Shared buffered-upload pipeline for the image and generic-file paths.
///
/// Both paths are identical except for two steps, which are injected:
@@ -49,17 +93,19 @@ struct BufferedUploadInput<'a> {
auth_event: &'a nostr::Event,
body: Bytes,
attribution: Option<UploadAttribution>,
commit_guard: &'a dyn UploadCommitGuard,
publication_mode: UploadPublicationMode,
}
async fn process_buffered_upload<V, M, Fut>(
input: BufferedUploadInput<'_>,
validate: V,
prepare_metadata: M,
) -> Result<BlobDescriptor, MediaError>
) -> Result<PreparedUpload, MediaError>
where
V: FnOnce(&Bytes, &MediaConfig) -> Result<(String, String), MediaError> + Send + 'static,
M: FnOnce(MetadataInput) -> Fut,
Fut: std::future::Future<Output = Result<BlobMeta, MediaError>>,
Fut: std::future::Future<Output = Result<(BlobMeta, Option<String>), MediaError>>,
{
let BufferedUploadInput {
storage,
@@ -68,6 +114,8 @@ where
auth_event,
body,
attribution,
commit_guard,
publication_mode,
} = input;
// CPU-bound: validate content, compute hash, verify auth.
@@ -95,13 +143,14 @@ where
// sidecar exists but the blob is missing, fall through to re-upload.
let sidecar_exists = storage.head(&meta_key).await?;
let blob_exists = storage.head(&key).await?;
if sidecar_exists && blob_exists {
if publication_mode == UploadPublicationMode::Legacy && sidecar_exists && blob_exists {
let meta = storage.get_sidecar(ctx, &sha256).await?;
// A re-upload of known bytes is still a distinct upload *event*: no
// blob PUT happens, so without this record the uploader would be
// invisible to the moderation pipeline (and takedown re-uploads
// would go unscanned).
if let Some(attribution) = &attribution {
commit_guard.revalidate()?;
record_upload_event(
storage,
ctx,
@@ -117,15 +166,20 @@ where
)
.await?;
}
return Ok(build_descriptor(
config,
&sha256,
&ext,
&mime,
body.len() as u64,
Some(&meta),
meta.uploaded_at,
));
return Ok(PreparedUpload {
descriptor: build_descriptor(
config,
&sha256,
&ext,
&mime,
body.len() as u64,
Some(&meta),
meta.uploaded_at,
),
metadata: meta,
object_key: key,
thumbnail_key: sidecar_exists.then(|| format!("{sha256}.thumb.jpg")),
});
}
// Compute uploaded_at once — single source of truth for sidecar and response.
@@ -138,9 +192,12 @@ where
// content-addressed and bounded by the upload size limit, so the storage
// cost is negligible. A V2 background GC job can sweep blobs with no
// matching sidecar after a grace period.
storage.put(&key, &body, &mime).await?;
commit_guard.revalidate()?;
if !blob_exists {
storage.put(&key, &body, &mime).await?;
}
let meta = match prepare_metadata(MetadataInput {
let (meta, thumbnail_key) = match prepare_metadata(MetadataInput {
sha256: sha256.clone(),
ext: ext.clone(),
mime: mime.clone(),
@@ -159,33 +216,42 @@ where
// The moderation record precedes the sidecar publish gate. If this write
// fails, the blob and any thumbnail remain orphaned but the media cannot be
// served. Conversely, record existence still implies those objects exist.
if let Some(attribution) = &attribution {
record_upload_event(
storage,
ctx,
&auth_event.pubkey,
attribution,
UploadEventFacts {
sha256: &sha256,
ext: &ext,
mime: &mime,
size: body.len() as u64,
uploaded_at,
},
)
.await?;
if publication_mode == UploadPublicationMode::Legacy {
if let Some(attribution) = &attribution {
commit_guard.revalidate()?;
record_upload_event(
storage,
ctx,
&auth_event.pubkey,
attribution,
UploadEventFacts {
sha256: &sha256,
ext: &ext,
mime: &mime,
size: body.len() as u64,
uploaded_at,
},
)
.await?;
}
commit_guard.revalidate()?;
storage.put_sidecar(ctx, &sha256, &meta).await?;
}
storage.put_sidecar(ctx, &sha256, &meta).await?;
Ok(build_descriptor(
config,
&sha256,
&ext,
&mime,
body.len() as u64,
Some(&meta),
uploaded_at,
))
Ok(PreparedUpload {
descriptor: build_descriptor(
config,
&sha256,
&ext,
&mime,
body.len() as u64,
Some(&meta),
uploaded_at,
),
metadata: meta,
object_key: key,
thumbnail_key,
})
}
/// Inputs handed to a buffered-upload metadata builder, after the shared
@@ -205,13 +271,18 @@ struct MetadataInput {
/// This is the image path — body is already fully buffered in RAM. Do NOT use
/// this for video uploads; use [`process_video_upload`] instead.
pub async fn process_upload(
storage: &MediaStorage,
config: &MediaConfig,
ctx: &TenantContext,
auth_event: &nostr::Event,
body: Bytes,
attribution: Option<UploadAttribution>,
) -> Result<BlobDescriptor, MediaError> {
request: BufferedUploadRequest<'_>,
) -> Result<PreparedUpload, MediaError> {
let BufferedUploadRequest {
storage,
config,
ctx,
auth_event,
body,
attribution,
commit_guard,
publication_mode,
} = request;
process_buffered_upload(
BufferedUploadInput {
storage,
@@ -220,13 +291,17 @@ pub async fn process_upload(
auth_event,
body,
attribution,
commit_guard,
publication_mode,
},
|bytes, cfg| {
let mime = validate_content(bytes, cfg)?;
let ext = mime_to_ext(&mime).to_string();
Ok((mime, ext))
},
|input| async move { prepare_image_metadata(storage, config, input).await },
|input| async move {
prepare_image_metadata(storage, config, input, commit_guard, publication_mode).await
},
)
.await
}
@@ -243,13 +318,18 @@ pub async fn process_upload(
/// The resulting blob is served with `Content-Disposition: attachment`, so the
/// client always downloads it rather than rendering it inline.
pub async fn process_file_upload(
storage: &MediaStorage,
config: &MediaConfig,
ctx: &TenantContext,
auth_event: &nostr::Event,
body: Bytes,
attribution: Option<UploadAttribution>,
) -> Result<BlobDescriptor, MediaError> {
request: BufferedUploadRequest<'_>,
) -> Result<PreparedUpload, MediaError> {
let BufferedUploadRequest {
storage,
config,
ctx,
auth_event,
body,
attribution,
commit_guard,
publication_mode,
} = request;
process_buffered_upload(
BufferedUploadInput {
storage,
@@ -258,6 +338,8 @@ pub async fn process_file_upload(
auth_event,
body,
attribution,
commit_guard,
publication_mode,
},
|bytes, cfg| validate_file_content(bytes, cfg),
|input| async move {
@@ -272,7 +354,7 @@ pub async fn process_file_upload(
uploaded_at: input.uploaded_at,
duration_secs: None,
};
Ok(meta)
Ok((meta, None))
},
)
.await
@@ -289,6 +371,9 @@ pub async fn process_file_upload(
/// 5. Writes a sidecar with `duration_secs` (no thumbnail — desktop handles that).
///
/// Returns a [`BlobDescriptor`] with the `duration` field populated.
// The guard remains an explicit trust-boundary argument so callers cannot
// accidentally choose an unguarded upload variant before the durable write.
#[allow(clippy::too_many_arguments)]
pub async fn process_video_upload(
storage: &MediaStorage,
config: &MediaConfig,
@@ -297,7 +382,9 @@ pub async fn process_video_upload(
body_stream: impl futures_core::Stream<Item = Result<Bytes, axum::Error>> + Send + 'static,
content_length: Option<u64>,
attribution: Option<UploadAttribution>,
) -> Result<BlobDescriptor, MediaError> {
commit_guard: &dyn UploadCommitGuard,
publication_mode: UploadPublicationMode,
) -> Result<PreparedUpload, MediaError> {
// --- 1. Stream body to temp file, compute SHA-256 incrementally ---
let tmp = tempfile::NamedTempFile::new().map_err(|e| MediaError::Io(e.to_string()))?;
let tmp_path = tmp.path().to_path_buf();
@@ -429,11 +516,12 @@ pub async fn process_video_upload(
// --- 5. Idempotency check ---
let sidecar_exists = storage.head(&meta_key).await?;
let blob_exists = storage.head(&key).await?;
if sidecar_exists && blob_exists {
if publication_mode == UploadPublicationMode::Legacy && sidecar_exists && blob_exists {
let meta = storage.get_sidecar(ctx, &sha256_hex).await?;
// Re-upload of known bytes: still a distinct upload event — see the
// buffered path's short-circuit for the rationale.
if let Some(attribution) = &attribution {
commit_guard.revalidate()?;
record_upload_event(
storage,
ctx,
@@ -449,21 +537,29 @@ pub async fn process_video_upload(
)
.await?;
}
return Ok(build_descriptor(
config,
&sha256_hex,
ext,
&mime,
file_size,
Some(&meta),
meta.uploaded_at,
));
return Ok(PreparedUpload {
descriptor: build_descriptor(
config,
&sha256_hex,
ext,
&mime,
file_size,
Some(&meta),
meta.uploaded_at,
),
metadata: meta,
object_key: key,
thumbnail_key: None,
});
}
let uploaded_at = chrono::Utc::now().timestamp();
// --- 6. Stream blob from temp file to S3 ---
storage.put_file(&key, &tmp_path, &mime).await?;
commit_guard.revalidate()?;
if !blob_exists {
storage.put_file(&key, &tmp_path, &mime).await?;
}
drop(tmp); // Free temp file disk space immediately after S3 upload.
// --- 7. Build metadata (no thumbnail for video — desktop handles that) ---
@@ -479,33 +575,42 @@ pub async fn process_video_upload(
};
// Record before publishing the sidecar serve gate. See the buffered path.
if let Some(attribution) = &attribution {
record_upload_event(
storage,
ctx,
&auth_event.pubkey,
attribution,
UploadEventFacts {
sha256: &sha256_hex,
ext,
mime: &mime,
size: file_size,
uploaded_at,
},
)
.await?;
if publication_mode == UploadPublicationMode::Legacy {
if let Some(attribution) = &attribution {
commit_guard.revalidate()?;
record_upload_event(
storage,
ctx,
&auth_event.pubkey,
attribution,
UploadEventFacts {
sha256: &sha256_hex,
ext,
mime: &mime,
size: file_size,
uploaded_at,
},
)
.await?;
}
commit_guard.revalidate()?;
storage.put_sidecar(ctx, &sha256_hex, &meta).await?;
}
storage.put_sidecar(ctx, &sha256_hex, &meta).await?;
Ok(build_descriptor(
config,
&sha256_hex,
ext,
&mime,
file_size,
Some(&meta),
uploaded_at,
))
Ok(PreparedUpload {
descriptor: build_descriptor(
config,
&sha256_hex,
ext,
&mime,
file_size,
Some(&meta),
uploaded_at,
),
metadata: meta,
object_key: key,
thumbnail_key: None,
})
}
/// Generate thumbnail and metadata without publishing the sidecar serve gate.
@@ -514,7 +619,9 @@ async fn prepare_image_metadata(
storage: &MediaStorage,
config: &MediaConfig,
input: MetadataInput,
) -> Result<BlobMeta, MediaError> {
commit_guard: &dyn UploadCommitGuard,
publication_mode: UploadPublicationMode,
) -> Result<(BlobMeta, Option<String>), MediaError> {
let body_ref = input.body.clone();
let mime_ref = input.mime.clone();
let ext_ref = input.ext.clone();
@@ -528,12 +635,22 @@ async fn prepare_image_metadata(
meta.uploaded_at = input.uploaded_at;
if let Some(ref tb) = thumb_bytes {
let thumb_key = format!("{}.thumb.jpg", input.sha256);
let thumbnail_key = if let Some(ref tb) = thumb_bytes {
let thumb_key = match publication_mode {
UploadPublicationMode::Legacy => format!("{}.thumb.jpg", input.sha256),
UploadPublicationMode::ProtectedStaging => {
let digest = hex::encode(Sha256::digest(tb));
format!("_objects/thumbnails/{digest}.jpg")
}
};
commit_guard.revalidate()?;
storage.put(&thumb_key, tb, "image/jpeg").await?;
}
Some(thumb_key)
} else {
None
};
Ok(meta)
Ok((meta, thumbnail_key))
}
fn build_descriptor(
@@ -158,6 +158,7 @@ pub struct PublishLimits {
struct PublishOptions {
limits: PublishLimits,
compaction_threshold: usize,
publish_pointer: bool,
}
struct CompactedPack {
@@ -258,6 +259,15 @@ impl ParentState {
parent,
}
}
/// Build parent state from a PostgreSQL-selected publication.
pub fn from_published(digest: String, parent: Manifest) -> Self {
Self {
if_match: None,
parent_digest: Some(digest),
parent,
}
}
}
/// Read `refs/*` + symbolic-HEAD from the workspace.
@@ -1013,6 +1023,36 @@ pub async fn cas_publish(
PublishOptions {
limits,
compaction_threshold: PACK_COMPACTION_THRESHOLD,
publish_pointer: true,
},
)
.await
}
/// Stage immutable packs and a validated manifest without publishing a pointer.
///
/// The caller must make the returned manifest digest visible through a
/// PostgreSQL publication CAS in the same protected operation transaction.
pub async fn prepare_publish(
store: &GitStore,
ctx: &TenantContext,
repo_path: &Path,
owner: &str,
repo: &str,
parent_state: &ParentState,
limits: PublishLimits,
) -> Result<CasSuccess, CasError> {
cas_publish_inner(
store,
ctx,
repo_path,
owner,
repo,
parent_state,
PublishOptions {
limits,
compaction_threshold: PACK_COMPACTION_THRESHOLD,
publish_pointer: false,
},
)
.await
@@ -1208,6 +1248,22 @@ async fn cas_publish_inner(
};
let manifest_digest = digest_from_manifest_key(&manifest_key)?;
if !options.publish_pointer {
if let Some(observation) = &compaction_observation {
record_compaction(
"staged",
observation.started_at,
observation.packs_before,
Some(observation.packs_after),
Some(observation.compacted_bytes),
);
}
return Ok(CasSuccess {
manifest: m_after,
manifest_key,
});
}
// Step 7: CAS the pointer.
let precond = match &parent_state.if_match {
Some(e) => Precond::IfMatch(e.clone()),
@@ -1763,6 +1819,7 @@ mod tests {
let test_options = PublishOptions {
limits,
compaction_threshold: 2,
publish_pointer: true,
};
let success = cas_publish_inner(
&store,
+9
View File
@@ -25,6 +25,7 @@ use tracing::{error, info};
/// - `BUZZ_REPO_ID` — repo identifier (d-tag)
/// - `BUZZ_COMMUNITY_ID` — server-resolved community UUID for the git HTTP request
/// - `BUZZ_PUSHER_PUBKEY` — authenticated pusher's hex pubkey
/// - `BUZZ_POLICY_FENCE_PATH` — relay-owned path for the exact allowed-policy receipt
///
/// Git sets automatically (quarantine):
/// - `GIT_OBJECT_DIRECTORY` — quarantine object store
@@ -47,6 +48,7 @@ ZERO="0000000000000000000000000000000000000000"
: "${BUZZ_PUSHER_PUBKEY:?error: BUZZ_PUSHER_PUBKEY not set}"
: "${BUZZ_HOOK_URL:?error: BUZZ_HOOK_URL not set}"
: "${BUZZ_HOOK_SECRET:?error: BUZZ_HOOK_SECRET not set}"
: "${BUZZ_POLICY_FENCE_PATH:?error: BUZZ_POLICY_FENCE_PATH not set}"
WORK_DIR=$(mktemp -d) || { echo "error: cannot create temp dir" >&2; exit 1; }
REFS_FILE="$WORK_DIR/refs"
@@ -141,6 +143,13 @@ if [ "$HTTP_CODE" != "200" ]; then
exit 1
fi
# Preserve the exact hook decision for transaction-owned commit-time locking.
# Failure to hand the receipt back rejects the push before any publication.
cp -- "$RESP_FILE" "$BUZZ_POLICY_FENCE_PATH" || {
echo "error: push authorization receipt could not be retained" >&2
exit 1
}
exit 0
"#;
+68 -5
View File
@@ -149,6 +149,19 @@ pub async fn hydrate_for_read(
result
}
/// Hydrate from a PostgreSQL-selected manifest digest.
///
/// The object-store pointer is deliberately bypassed; callers must obtain the
/// digest from the active publication row in the server-resolved domain.
pub async fn hydrate_for_published_read(
store: &GitStore,
manifest_digest: &str,
options: HydrationOptions<'_>,
) -> Result<HydratedRepo, HydrateError> {
let manifest = load_manifest_by_digest(store, manifest_digest).await?;
materialize_manifest(store, &manifest, options).await
}
async fn hydrate_for_read_inner(
store: &GitStore,
ctx: &TenantContext,
@@ -178,6 +191,26 @@ pub async fn load_manifest_for_read(
.map(|(_etag, _digest, manifest)| manifest))
}
/// Load and verify a PostgreSQL-selected immutable manifest.
pub async fn load_manifest_by_digest(
store: &GitStore,
digest: &str,
) -> Result<Manifest, HydrateError> {
if digest.len() != 64
|| !digest
.chars()
.all(|character| character.is_ascii_hexdigit())
{
return Err(HydrateError::InvalidPointer);
}
let manifest_key = format!("manifests/{digest}");
let manifest_bytes =
get_verified_limited(store, &manifest_key, digest, MAX_MANIFEST_BYTES).await?;
let manifest = Manifest::from_bytes(&manifest_bytes)?;
manifest.validate()?;
Ok(manifest)
}
async fn init_bare_repo(path: &Path) -> Result<(), HydrateError> {
run_git(path, &["init", "--bare", "--quiet"]).await?;
run_git(path, &["symbolic-ref", "HEAD", "refs/heads/main"]).await
@@ -239,6 +272,40 @@ pub async fn hydrate_for_write(
}
}
/// Hydrate a write workspace from PostgreSQL-authoritative publication state.
pub async fn hydrate_for_published_write(
store: &GitStore,
manifest_digest: Option<&str>,
options: HydrationOptions<'_>,
) -> Result<(HydratedRepo, ParentState), HydrateError> {
match manifest_digest {
Some(digest) => {
let manifest = load_manifest_by_digest(store, digest).await?;
let repo = materialize_manifest(store, &manifest, options).await?;
Ok((
repo,
ParentState::from_published(digest.to_owned(), manifest),
))
}
None => {
let tempdir = TempDir::new_in(options.scratch_dir).map_err(|error| {
HydrateError::Hydrate(format!("tempdir in {:?}: {error}", options.scratch_dir))
})?;
let path = tempdir.path().to_path_buf();
init_bare_repo(&path).await?;
Ok((
HydratedRepo {
_tempdir: tempdir,
path,
hydrated_bytes: 0,
hydrated_packs: 0,
},
ParentState::fresh(),
))
}
}
}
/// Resolve the pointer to its `(ETag, digest, verified Manifest)` triple.
///
/// `Ok(None)` if the pointer is absent (caller decides 404 vs first-push
@@ -261,11 +328,7 @@ async fn load_pointer(
if digest.len() != 64 || !digest.chars().all(|c| c.is_ascii_hexdigit()) {
return Err(HydrateError::InvalidPointer);
}
let manifest_key = format!("manifests/{digest}");
let manifest_bytes =
get_verified_limited(store, &manifest_key, &digest, MAX_MANIFEST_BYTES).await?;
let manifest = Manifest::from_bytes(&manifest_bytes)?;
manifest.validate()?;
let manifest = load_manifest_by_digest(store, &digest).await?;
Ok(Some((etag, digest, manifest)))
}
+461
View File
@@ -0,0 +1,461 @@
//! Validated one-way migration from legacy Git pointers to PostgreSQL authority.
use std::collections::BTreeMap;
use buzz_core::tenant::TenantContext;
use buzz_db::protected_visibility::{ProtectedObjectAuthorityState, ProtectedObjectSurface};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::api::git::hydrate::{hydrate_for_published_read, load_manifest_by_digest};
use crate::api::git::manifest::pointer_key;
use crate::state::AppState;
const SENTINEL_FORMAT_VERSION: u32 = 1;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct CutoverSentinel {
format_version: u32,
community_id: uuid::Uuid,
surface: String,
generation: u64,
imported_objects: u64,
inventory_sha256: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PreparationDisposition {
Begin,
Resume,
Verify,
}
fn preparation_disposition(
authority: &buzz_db::protected_visibility::ProtectedObjectAuthority,
sentinel: Option<&CutoverSentinel>,
) -> anyhow::Result<PreparationDisposition> {
match authority.state {
ProtectedObjectAuthorityState::Legacy => {
if sentinel.is_some() {
anyhow::bail!("Git cutover sentinel exists but PostgreSQL authority regressed");
}
Ok(PreparationDisposition::Begin)
}
ProtectedObjectAuthorityState::Importing => {
if sentinel.is_some_and(|sentinel| sentinel.generation != authority.generation) {
anyhow::bail!("Git resumed import generation conflicts with its sentinel");
}
Ok(PreparationDisposition::Resume)
}
ProtectedObjectAuthorityState::PostgreSql => {
let sentinel = sentinel.ok_or_else(|| {
anyhow::anyhow!("Git PostgreSQL authority is missing its sentinel")
})?;
validate_authority_snapshot(authority, sentinel)?;
Ok(PreparationDisposition::Verify)
}
}
}
fn sentinel_key(community_id: buzz_core::CommunityId) -> String {
format!("_authority/{community_id}/git-v1.json")
}
async fn read_sentinel(
state: &AppState,
community_id: buzz_core::CommunityId,
) -> anyhow::Result<Option<CutoverSentinel>> {
let key = sentinel_key(community_id);
let Some((_etag, bytes)) = state.git_store.get_pointer(&key).await? else {
return Ok(None);
};
let sentinel: CutoverSentinel = serde_json::from_slice(&bytes)?;
if sentinel.format_version != SENTINEL_FORMAT_VERSION
|| sentinel.community_id != *community_id.as_uuid()
|| sentinel.surface != "git"
{
anyhow::bail!("Git cutover sentinel does not match its domain and surface");
}
validate_digest(&sentinel.inventory_sha256)?;
Ok(Some(sentinel))
}
async fn create_sentinel(state: &AppState, sentinel: &CutoverSentinel) -> anyhow::Result<()> {
let key = sentinel_key(buzz_core::CommunityId::from_uuid(sentinel.community_id));
let body = serde_json::to_vec(sentinel)?;
match state
.git_store
.put_pointer(
&key,
&body,
crate::api::git::store::Precond::IfNoneMatchStar,
)
.await?
{
crate::api::git::store::CasOutcome::Won(_) => Ok(()),
crate::api::git::store::CasOutcome::LostRace => {
let existing = read_sentinel(
state,
buzz_core::CommunityId::from_uuid(sentinel.community_id),
)
.await?;
if existing.as_ref() == Some(sentinel) {
Ok(())
} else {
anyhow::bail!("Git cutover sentinel conflicts with the prepared inventory")
}
}
}
}
/// Prepare one domain's exact, resumable Git visibility import before serving.
pub async fn prepare_postgres_authority(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<()> {
if state.restore_protection().is_some() {
anyhow::bail!(
"Git cutover must complete before the protected restore anchor is provisioned"
);
}
let authority = state
.db
.protected_object_authority(tenant.community(), ProtectedObjectSurface::Git)
.await?;
let existing_sentinel = read_sentinel(state, tenant.community()).await?;
let disposition = preparation_disposition(&authority, existing_sentinel.as_ref())?;
let authority = if disposition == PreparationDisposition::Begin {
state
.db
.begin_protected_object_import(tenant.community(), ProtectedObjectSurface::Git)
.await?
} else {
authority
};
if disposition == PreparationDisposition::Verify {
let sentinel = existing_sentinel.ok_or_else(|| {
anyhow::anyhow!("Git verified authority is missing its cutover sentinel")
})?;
return validate_authority_snapshot(&authority, &sentinel);
}
let reservations = {
let mut transaction = state.db.begin_transaction().await?;
let reservations = buzz_db::protected_publication::list_git_repo_reservations(
&mut transaction,
tenant.community(),
)
.await?;
transaction.commit().await?;
reservations
};
let reservation_map = reservations
.iter()
.map(|(repo, owner, origin)| (repo.clone(), (owner.clone(), origin.clone())))
.collect::<BTreeMap<String, (String, String)>>();
let mut legacy = BTreeMap::new();
for (repo_id, owner, origin) in reservations {
let pointer = pointer_key(tenant.community(), &owner, &repo_id);
let Some((_etag, body)) = state.git_store.get_pointer(&pointer).await? else {
if origin == "protected_unpublished" {
// A transaction-owned Enforce announcement intentionally has
// no legacy pointer and may take its first push after cutover.
continue;
}
anyhow::bail!("Git migration found a legacy reservation without a pointer");
};
if origin != "legacy" {
anyhow::bail!("Git migration found a protected-unpublished reservation with a pointer");
}
let digest = std::str::from_utf8(&body)
.map_err(|_| anyhow::anyhow!("Git migration pointer is not UTF-8"))?
.trim()
.to_owned();
validate_digest(&digest)?;
let _manifest = load_manifest_by_digest(&state.git_store, &digest).await?;
// Fully materialize every referenced pack and ref. A digest-valid
// manifest with a missing or corrupt child must never pass cutover.
let hydrated = hydrate_for_published_read(
&state.git_store,
&digest,
crate::api::git::hydrate::HydrationOptions {
pack_cache: &state.git_pack_cache,
scratch_dir: &state.config.git_repo_path,
max_pack_bytes: state.config.git_max_pack_bytes,
max_repo_bytes: state.config.git_max_repo_bytes,
},
)
.await?;
drop(hydrated);
let mut transaction = state.db.begin_transaction().await?;
buzz_db::protected_publication::import_git_publication(
&mut transaction,
tenant.community(),
&repo_id,
&owner,
&digest,
)
.await?;
transaction.commit().await?;
legacy.insert(repo_id, (owner, digest));
}
// Re-read every pointer after import. The transaction-held legacy writer
// fence guarantees this set cannot change after `importing` began; this
// second pass detects incompatible writers and object corruption loudly.
for (repo_id, (owner, expected)) in &legacy {
let pointer = pointer_key(tenant.community(), owner, repo_id);
let Some((_etag, body)) = state.git_store.get_pointer(&pointer).await? else {
anyhow::bail!("Git migration pointer disappeared during verification");
};
let actual = std::str::from_utf8(&body)
.map_err(|_| anyhow::anyhow!("Git migration pointer is not UTF-8"))?
.trim();
if actual != expected {
anyhow::bail!("Git migration pointer changed during verification");
}
}
let verified_reservations = {
let mut transaction = state.db.begin_transaction().await?;
let rows = buzz_db::protected_publication::list_git_repo_reservations(
&mut transaction,
tenant.community(),
)
.await?;
transaction.commit().await?;
rows.into_iter()
.map(|(repo, owner, origin)| (repo, (owner, origin)))
.collect::<BTreeMap<_, _>>()
};
if verified_reservations != reservation_map {
anyhow::bail!("Git migration reservation inventory changed during verification");
}
let postgres = {
let mut transaction = state.db.begin_transaction().await?;
let rows = buzz_db::protected_publication::list_git_publications(
&mut transaction,
tenant.community(),
)
.await?;
transaction.commit().await?;
rows.into_iter()
.map(|(repo, owner, digest)| (repo, (owner, digest)))
.collect::<BTreeMap<_, _>>()
};
if postgres != legacy {
anyhow::bail!("Git migration inventory parity failed");
}
let inventory = git_inventory_digest(&postgres);
let sentinel = CutoverSentinel {
format_version: SENTINEL_FORMAT_VERSION,
community_id: *tenant.community().as_uuid(),
surface: "git".into(),
generation: authority.generation,
imported_objects: postgres.len() as u64,
inventory_sha256: inventory.clone(),
};
if let Some(existing) = existing_sentinel {
if existing != sentinel {
anyhow::bail!("Git cutover sentinel does not match the resumed import");
}
} else {
create_sentinel(state, &sentinel).await?;
}
state
.db
.finalize_protected_object_import(
tenant.community(),
ProtectedObjectSurface::Git,
authority.generation,
postgres.len() as u64,
&inventory,
)
.await?;
Ok(())
}
/// Require a completed, reconciled authority without advancing migration state.
pub async fn require_reconciled_authority(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<()> {
let authority = state
.db
.protected_object_authority(tenant.community(), ProtectedObjectSurface::Git)
.await?;
if authority.state != ProtectedObjectAuthorityState::PostgreSql {
anyhow::bail!("Git PostgreSQL authority has not completed preparation");
}
let sentinel = read_sentinel(state, tenant.community())
.await?
.ok_or_else(|| anyhow::anyhow!("Git PostgreSQL authority sentinel is missing"))?;
validate_authority_snapshot(&authority, &sentinel)
}
/// Refuse a legacy lane after the immutable cutover sentinel exists. This is
/// checked in every mode so a restored pre-cutover database cannot revive
/// stale object-store visibility.
pub async fn require_legacy_sentinel_absent(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<()> {
if read_sentinel(state, tenant.community()).await?.is_some() {
anyhow::bail!("Git legacy authority is permanently unavailable after cutover");
}
Ok(())
}
fn validate_authority_snapshot(
authority: &buzz_db::protected_visibility::ProtectedObjectAuthority,
sentinel: &CutoverSentinel,
) -> anyhow::Result<()> {
if authority.state != ProtectedObjectAuthorityState::PostgreSql
|| authority.generation != sentinel.generation
|| authority.imported_objects != Some(sentinel.imported_objects)
|| authority.inventory_sha256.as_deref() != Some(&sentinel.inventory_sha256)
{
anyhow::bail!("Git PostgreSQL authority and cutover sentinel disagree");
}
Ok(())
}
fn validate_digest(value: &str) -> anyhow::Result<()> {
if value.len() != 64
|| !value
.chars()
.all(|character| matches!(character, '0'..='9' | 'a'..='f'))
{
anyhow::bail!("Git migration pointer digest is invalid");
}
Ok(())
}
fn git_inventory_digest(inventory: &BTreeMap<String, (String, String)>) -> String {
let mut digest = Sha256::new();
digest.update(b"buzz-protected-git-inventory-v1\0");
for (repo, (owner, manifest)) in inventory {
digest.update(repo.as_bytes());
digest.update([0]);
digest.update(owner.as_bytes());
digest.update([0]);
digest.update(manifest.as_bytes());
digest.update([0]);
}
hex::encode(digest.finalize())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inventory_digest_is_order_independent_and_value_sensitive() {
let mut first = BTreeMap::new();
first.insert("b".into(), ("owner-b".into(), "b".repeat(64)));
first.insert("a".into(), ("owner-a".into(), "a".repeat(64)));
let mut second = BTreeMap::new();
second.insert("a".into(), ("owner-a".into(), "a".repeat(64)));
second.insert("b".into(), ("owner-b".into(), "b".repeat(64)));
assert_eq!(git_inventory_digest(&first), git_inventory_digest(&second));
second.get_mut("b").expect("row").1 = "c".repeat(64);
assert_ne!(git_inventory_digest(&first), git_inventory_digest(&second));
}
#[test]
fn authority_snapshot_rejects_restore_regression() {
let sentinel = CutoverSentinel {
format_version: SENTINEL_FORMAT_VERSION,
community_id: uuid::Uuid::nil(),
surface: "git".into(),
generation: 2,
imported_objects: 1,
inventory_sha256: "a".repeat(64),
};
let authority = buzz_db::protected_visibility::ProtectedObjectAuthority {
generation: 1,
state: ProtectedObjectAuthorityState::Legacy,
imported_objects: None,
inventory_sha256: None,
};
assert!(validate_authority_snapshot(&authority, &sentinel).is_err());
}
#[test]
fn migration_state_matrix_is_resumable_and_fail_closed() {
let sentinel = CutoverSentinel {
format_version: SENTINEL_FORMAT_VERSION,
community_id: uuid::Uuid::nil(),
surface: "git".into(),
generation: 2,
imported_objects: 1,
inventory_sha256: "a".repeat(64),
};
let authority = |state, generation, imported_objects, inventory_sha256| {
buzz_db::protected_visibility::ProtectedObjectAuthority {
generation,
state,
imported_objects,
inventory_sha256,
}
};
assert_eq!(
preparation_disposition(
&authority(ProtectedObjectAuthorityState::Legacy, 1, None, None),
None,
)
.unwrap(),
PreparationDisposition::Begin
);
assert_eq!(
preparation_disposition(
&authority(ProtectedObjectAuthorityState::Importing, 2, None, None),
None,
)
.unwrap(),
PreparationDisposition::Resume
);
assert_eq!(
preparation_disposition(
&authority(ProtectedObjectAuthorityState::Importing, 2, None, None),
Some(&sentinel),
)
.unwrap(),
PreparationDisposition::Resume
);
assert_eq!(
preparation_disposition(
&authority(
ProtectedObjectAuthorityState::PostgreSql,
2,
Some(1),
Some("a".repeat(64)),
),
Some(&sentinel),
)
.unwrap(),
PreparationDisposition::Verify
);
assert!(preparation_disposition(
&authority(ProtectedObjectAuthorityState::Legacy, 1, None, None),
Some(&sentinel),
)
.is_err());
assert!(preparation_disposition(
&authority(ProtectedObjectAuthorityState::Importing, 3, None, None),
Some(&sentinel),
)
.is_err());
assert!(preparation_disposition(
&authority(
ProtectedObjectAuthorityState::PostgreSql,
2,
Some(1),
Some("a".repeat(64)),
),
None,
)
.is_err());
}
}
+1
View File
@@ -28,6 +28,7 @@ pub mod hook;
pub mod hydrate;
pub mod manifest;
pub mod manifest_event;
pub mod migration;
pub mod pack_cache;
pub mod policy;
pub mod store;
+24 -6
View File
@@ -46,6 +46,7 @@ use buzz_core::git_perms::{
evaluate_push, parse_protection_tags, Denial, RefUpdate, UpdateKind,
GIT_NO_CHANNEL_BINDING_BODY,
};
use buzz_db::protected_publication::{GitPolicyCommitFence, GitPolicyGrant};
use buzz_db::EventQuery;
use crate::state::AppState;
@@ -88,17 +89,20 @@ pub struct HookRefUpdate {
}
/// Response to the hook — either allow or deny.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct HookCallbackResponse {
/// Whether the push is allowed.
pub allowed: bool,
/// Denial reasons (empty if allowed).
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub denials: Vec<DenialResponse>,
/// Exact database rows that must still match at the publication commit.
#[serde(skip_serializing_if = "Option::is_none")]
pub policy_fence: Option<GitPolicyCommitFence>,
}
/// A single denial reason in the hook response.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct DenialResponse {
/// The ref that was denied.
pub ref_name: String,
@@ -364,8 +368,10 @@ pub async fn hook_policy_check(
}
}
};
let role = if is_repo_owner || is_managed_agent_owner {
MemberRole::Owner
let (role, grant) = if is_repo_owner {
(MemberRole::Owner, GitPolicyGrant::RepoOwner)
} else if is_managed_agent_owner {
(MemberRole::Owner, GitPolicyGrant::ManagedAgentOwner)
} else {
match channel_id {
None => {
@@ -382,7 +388,7 @@ pub async fn hook_policy_check(
.await
{
Ok(Some(role_str)) => match role_str.parse::<MemberRole>() {
Ok(role) => role,
Ok(role) => (role, GitPolicyGrant::ChannelMember { role: role_str }),
Err(_) => {
error!(role = %role_str, "hook callback: unknown role");
return (StatusCode::FORBIDDEN, "internal error").into_response();
@@ -424,12 +430,18 @@ pub async fn hook_policy_check(
Ok(()) => Json(HookCallbackResponse {
allowed: true,
denials: vec![],
policy_fence: Some(GitPolicyCommitFence {
announcement_id: repo_event.event.id.to_hex(),
channel_id,
grant,
}),
})
.into_response(),
Err(denials) => {
let response = HookCallbackResponse {
allowed: false,
denials: denials.into_iter().map(DenialResponse::from).collect(),
policy_fence: None,
};
(StatusCode::FORBIDDEN, Json(response)).into_response()
}
@@ -983,5 +995,11 @@ printf '%s' "$HMAC_INPUT" | openssl dgst -sha256 -hmac "{secret}" -hex 2>/dev/nu
StatusCode::OK,
"owner push to a never-bound repo must remain allowed (got body: {body})"
);
let allowed: HookCallbackResponse = serde_json::from_str(&body).expect("policy response");
assert!(allowed.allowed);
assert!(matches!(
allowed.policy_fence.expect("commit fence").grant,
GitPolicyGrant::RepoOwner
));
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,880 @@
//! Validated one-way migration from media sidecars to PostgreSQL authority.
use std::collections::BTreeMap;
use buzz_core::tenant::TenantContext;
use buzz_db::protected_publication::MediaPublication;
use buzz_db::protected_visibility::{ProtectedObjectAuthorityState, ProtectedObjectSurface};
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::state::AppState;
const SENTINEL_FORMAT_VERSION: u32 = 1;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct CutoverSentinel {
format_version: u32,
community_id: uuid::Uuid,
surface: String,
generation: u64,
imported_objects: u64,
inventory_sha256: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PreparationDisposition {
Begin,
Resume,
Verify,
}
fn preparation_disposition(
authority: &buzz_db::protected_visibility::ProtectedObjectAuthority,
sentinel: Option<&CutoverSentinel>,
) -> anyhow::Result<PreparationDisposition> {
match authority.state {
ProtectedObjectAuthorityState::Legacy => {
if sentinel.is_some() {
anyhow::bail!("media cutover sentinel exists but PostgreSQL authority regressed");
}
Ok(PreparationDisposition::Begin)
}
ProtectedObjectAuthorityState::Importing => {
if sentinel.is_some_and(|sentinel| sentinel.generation != authority.generation) {
anyhow::bail!("media resumed import generation conflicts with its sentinel");
}
Ok(PreparationDisposition::Resume)
}
ProtectedObjectAuthorityState::PostgreSql => {
let sentinel = sentinel.ok_or_else(|| {
anyhow::anyhow!("media PostgreSQL authority is missing its sentinel")
})?;
validate_authority_snapshot(authority, sentinel)?;
Ok(PreparationDisposition::Verify)
}
}
}
fn sentinel_key(community_id: buzz_core::CommunityId) -> String {
format!("_authority/{community_id}/media-v1.json")
}
async fn read_sentinel(
state: &AppState,
community_id: buzz_core::CommunityId,
) -> anyhow::Result<Option<CutoverSentinel>> {
let Some(bytes) = state
.media_storage
.get_optional(&sentinel_key(community_id))
.await?
else {
return Ok(None);
};
let sentinel: CutoverSentinel = serde_json::from_slice(&bytes)?;
if sentinel.format_version != SENTINEL_FORMAT_VERSION
|| sentinel.community_id != *community_id.as_uuid()
|| sentinel.surface != "media"
{
anyhow::bail!("media cutover sentinel does not match its domain and surface");
}
validate_digest(&sentinel.inventory_sha256)?;
Ok(Some(sentinel))
}
async fn create_sentinel(state: &AppState, sentinel: &CutoverSentinel) -> anyhow::Result<()> {
let community_id = buzz_core::CommunityId::from_uuid(sentinel.community_id);
let body = serde_json::to_vec(sentinel)?;
match state
.media_storage
.put_create_only(&sentinel_key(community_id), &body, "application/json")
.await?
{
buzz_media::storage::CreateOnlyOutcome::Created => Ok(()),
buzz_media::storage::CreateOnlyOutcome::AlreadyExists => {
if read_sentinel(state, community_id).await?.as_ref() == Some(sentinel) {
Ok(())
} else {
anyhow::bail!("media cutover sentinel conflicts with the prepared inventory")
}
}
}
}
/// Prepare one domain's exact, resumable media visibility import before serving.
pub async fn prepare_postgres_authority(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<()> {
if state.restore_protection().is_some() {
anyhow::bail!(
"media cutover must complete before the protected restore anchor is provisioned"
);
}
let authority = state
.db
.protected_object_authority(tenant.community(), ProtectedObjectSurface::Media)
.await?;
let existing_sentinel = read_sentinel(state, tenant.community()).await?;
let disposition = preparation_disposition(&authority, existing_sentinel.as_ref())?;
let authority = if disposition == PreparationDisposition::Begin {
state
.db
.begin_protected_object_import(tenant.community(), ProtectedObjectSurface::Media)
.await?
} else {
authority
};
if disposition == PreparationDisposition::Verify {
let sentinel = existing_sentinel.ok_or_else(|| {
anyhow::anyhow!("media verified authority is missing its cutover sentinel")
})?;
return validate_authority_snapshot(&authority, &sentinel);
}
let legacy = read_legacy_inventory(state, tenant).await?;
for publication in legacy.values() {
let mut transaction = state.db.begin_transaction().await?;
buzz_db::protected_publication::import_media_publication(
&mut transaction,
tenant.community(),
publication,
)
.await?;
transaction.commit().await?;
}
let verified = read_legacy_inventory(state, tenant).await?;
if verified != legacy {
anyhow::bail!("media migration inventory changed during verification");
}
let postgres = {
let mut transaction = state.db.begin_transaction().await?;
let rows = buzz_db::protected_publication::list_media_publications(
&mut transaction,
tenant.community(),
)
.await?;
transaction.commit().await?;
rows.into_iter()
.map(|publication| (publication.sha256.clone(), publication))
.collect::<BTreeMap<_, _>>()
};
if postgres != legacy {
anyhow::bail!("media migration inventory parity failed");
}
let inventory = media_inventory_digest(&postgres);
let sentinel = CutoverSentinel {
format_version: SENTINEL_FORMAT_VERSION,
community_id: *tenant.community().as_uuid(),
surface: "media".into(),
generation: authority.generation,
imported_objects: postgres.len() as u64,
inventory_sha256: inventory.clone(),
};
if let Some(existing) = existing_sentinel {
if existing != sentinel {
anyhow::bail!("media cutover sentinel does not match the resumed import");
}
} else {
create_sentinel(state, &sentinel).await?;
}
state
.db
.finalize_protected_object_import(
tenant.community(),
ProtectedObjectSurface::Media,
authority.generation,
postgres.len() as u64,
&inventory,
)
.await?;
Ok(())
}
/// Require a completed, reconciled authority without advancing migration state.
pub async fn require_reconciled_authority(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<()> {
let authority = state
.db
.protected_object_authority(tenant.community(), ProtectedObjectSurface::Media)
.await?;
if authority.state != ProtectedObjectAuthorityState::PostgreSql {
anyhow::bail!("media PostgreSQL authority has not completed preparation");
}
let sentinel = read_sentinel(state, tenant.community())
.await?
.ok_or_else(|| anyhow::anyhow!("media PostgreSQL authority sentinel is missing"))?;
validate_authority_snapshot(&authority, &sentinel)
}
/// Refuse a legacy lane after the immutable cutover sentinel exists. This is
/// checked in every mode so a restored pre-cutover database cannot revive
/// stale object-store visibility.
pub async fn require_legacy_sentinel_absent(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<()> {
if read_sentinel(state, tenant.community()).await?.is_some() {
anyhow::bail!("media legacy authority is permanently unavailable after cutover");
}
Ok(())
}
fn validate_authority_snapshot(
authority: &buzz_db::protected_visibility::ProtectedObjectAuthority,
sentinel: &CutoverSentinel,
) -> anyhow::Result<()> {
if authority.state != ProtectedObjectAuthorityState::PostgreSql
|| authority.generation != sentinel.generation
|| authority.imported_objects != Some(sentinel.imported_objects)
|| authority.inventory_sha256.as_deref() != Some(&sentinel.inventory_sha256)
{
anyhow::bail!("media PostgreSQL authority and cutover sentinel disagree");
}
Ok(())
}
async fn read_legacy_inventory(
state: &AppState,
tenant: &TenantContext,
) -> anyhow::Result<BTreeMap<String, MediaPublication>> {
let prefix = format!("_meta/{}/", tenant.community());
let mut continuation = None;
let mut inventory = BTreeMap::new();
loop {
let page = state
.media_storage
.list_page_with_prefix(prefix.clone(), continuation, 1000)
.await?;
for (key, _size) in page.objects {
let sha256 = key
.strip_prefix(&prefix)
.and_then(|value| value.strip_suffix(".json"))
.ok_or_else(|| anyhow::anyhow!("media migration found an invalid sidecar key"))?;
validate_digest(sha256)?;
let metadata = state.media_storage.get_sidecar(tenant, sha256).await?;
validate_extension(&metadata.ext)?;
let object_key = format!("{sha256}.{}", metadata.ext);
let head = state
.media_storage
.head_with_metadata(&object_key)
.await?
.ok_or_else(|| anyhow::anyhow!("media migration blob is missing"))?;
if head.size != metadata.size {
anyhow::bail!("media migration blob size does not match sidecar");
}
let mut body = state.media_storage.get_stream(&object_key).await?;
let mut digest = Sha256::new();
while let Some(chunk) = body.next().await {
digest.update(chunk?);
}
if hex::encode(digest.finalize()) != sha256 {
anyhow::bail!("media migration blob digest does not match its key");
}
let thumbnail_key = format!("{sha256}.thumb.jpg");
let thumbnail_key = state
.media_storage
.head(&thumbnail_key)
.await?
.then_some(thumbnail_key);
let publication = MediaPublication {
sha256: sha256.to_owned(),
object_key,
extension: metadata.ext.clone(),
mime_type: metadata.mime_type.clone(),
object_size: metadata.size,
metadata: serde_json::to_value(metadata)?,
thumbnail_key,
publication_version: 1,
};
if inventory.insert(sha256.to_owned(), publication).is_some() {
anyhow::bail!("media migration sidecar inventory contains a duplicate");
}
}
if !page.is_truncated {
break;
}
continuation = page.next_continuation_token;
if continuation.is_none() {
anyhow::bail!("media migration listing was truncated without a continuation token");
}
}
Ok(inventory)
}
fn validate_digest(value: &str) -> anyhow::Result<()> {
if value.len() != 64
|| !value
.chars()
.all(|character| matches!(character, '0'..='9' | 'a'..='f'))
{
anyhow::bail!("media migration sidecar digest is invalid");
}
Ok(())
}
fn validate_extension(value: &str) -> anyhow::Result<()> {
if value.is_empty()
|| value.len() > 16
|| !value
.chars()
.all(|character| character.is_ascii_lowercase() || character.is_ascii_digit())
{
anyhow::bail!("media migration sidecar extension is invalid");
}
Ok(())
}
fn media_inventory_digest(inventory: &BTreeMap<String, MediaPublication>) -> String {
let mut digest = Sha256::new();
digest.update(b"buzz-protected-media-inventory-v1\0");
for publication in inventory.values() {
digest.update(publication.sha256.as_bytes());
digest.update([0]);
digest.update(publication.object_key.as_bytes());
digest.update([0]);
digest.update(publication.mime_type.as_bytes());
digest.update([0]);
digest.update(publication.object_size.to_be_bytes());
digest.update([0]);
digest.update(serde_json::to_vec(&publication.metadata).unwrap_or_default());
digest.update([0]);
if let Some(thumbnail) = &publication.thumbnail_key {
digest.update(thumbnail.as_bytes());
}
digest.update([0]);
}
hex::encode(digest.finalize())
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
use std::sync::Arc;
use crate::api::git::manifest::{pointer_key, Manifest, MANIFEST_VERSION};
use crate::api::git::store::Precond;
use buzz_core::tenant::TenantContext;
use buzz_media::BlobMeta;
async fn migration_test_state() -> (Arc<AppState>, TenantContext, sqlx::PgPool) {
let mut config = crate::config::Config::from_env().expect("test configuration");
if let Ok(database_url) = std::env::var("BUZZ_TEST_DATABASE_URL") {
config.database_url = database_url;
}
config.redis_url = "redis://127.0.0.1:6379".into();
let pool = sqlx::PgPool::connect(&config.database_url)
.await
.expect("test database");
sqlx::migrate!("../../migrations")
.run(&pool)
.await
.expect("migrated test database");
let community_uuid = uuid::Uuid::new_v4();
let host = format!("migration-{}.example", community_uuid.simple());
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
.bind(community_uuid)
.bind(&host)
.execute(&pool)
.await
.expect("test community");
let tenant =
TenantContext::resolved(buzz_core::CommunityId::from_uuid(community_uuid), host);
let db = buzz_db::Db::from_pool(pool.clone());
let redis_pool = deadpool_redis::Config::from_url(&config.redis_url)
.create_pool(Some(deadpool_redis::Runtime::Tokio1))
.expect("redis pool");
let pubsub = Arc::new(
buzz_pubsub::PubSubManager::new(&config.redis_url, redis_pool.clone())
.await
.expect("pubsub manager"),
);
let audit = buzz_audit::AuditService::new(pool.clone());
let auth = buzz_auth::AuthService::new(config.auth.clone());
let search = buzz_search::SearchService::new(pool.clone());
let workflow_engine = Arc::new(buzz_workflow::WorkflowEngine::new(
db.clone(),
buzz_workflow::WorkflowConfig::default(),
));
let media_storage = buzz_media::MediaStorage::new(&config.media).expect("media storage");
let (state, _audit_shutdown) = AppState::new(
config,
db,
redis_pool,
audit,
pubsub,
auth,
search,
workflow_engine,
nostr::Keys::generate(),
media_storage,
);
(Arc::new(state), tenant, pool)
}
#[test]
fn strict_legacy_key_components_reject_ambiguous_values() {
assert!(validate_digest(&"a".repeat(64)).is_ok());
assert!(validate_digest(&"A".repeat(64)).is_err());
assert!(validate_extension("webp").is_ok());
assert!(validate_extension("../jpg").is_err());
assert!(validate_extension("JPG").is_err());
}
#[test]
fn authority_snapshot_rejects_restore_regression() {
let sentinel = CutoverSentinel {
format_version: SENTINEL_FORMAT_VERSION,
community_id: uuid::Uuid::nil(),
surface: "media".into(),
generation: 2,
imported_objects: 1,
inventory_sha256: "a".repeat(64),
};
let authority = buzz_db::protected_visibility::ProtectedObjectAuthority {
generation: 1,
state: ProtectedObjectAuthorityState::Legacy,
imported_objects: None,
inventory_sha256: None,
};
assert!(validate_authority_snapshot(&authority, &sentinel).is_err());
}
#[test]
fn migration_state_matrix_is_resumable_and_fail_closed() {
let sentinel = CutoverSentinel {
format_version: SENTINEL_FORMAT_VERSION,
community_id: uuid::Uuid::nil(),
surface: "media".into(),
generation: 2,
imported_objects: 1,
inventory_sha256: "a".repeat(64),
};
let authority = |state, generation, imported_objects, inventory_sha256| {
buzz_db::protected_visibility::ProtectedObjectAuthority {
generation,
state,
imported_objects,
inventory_sha256,
}
};
assert_eq!(
preparation_disposition(
&authority(ProtectedObjectAuthorityState::Legacy, 1, None, None),
None,
)
.unwrap(),
PreparationDisposition::Begin
);
assert_eq!(
preparation_disposition(
&authority(ProtectedObjectAuthorityState::Importing, 2, None, None),
None,
)
.unwrap(),
PreparationDisposition::Resume
);
assert_eq!(
preparation_disposition(
&authority(ProtectedObjectAuthorityState::Importing, 2, None, None),
Some(&sentinel),
)
.unwrap(),
PreparationDisposition::Resume
);
assert_eq!(
preparation_disposition(
&authority(
ProtectedObjectAuthorityState::PostgreSql,
2,
Some(1),
Some("a".repeat(64)),
),
Some(&sentinel),
)
.unwrap(),
PreparationDisposition::Verify
);
assert!(preparation_disposition(
&authority(ProtectedObjectAuthorityState::Legacy, 1, None, None),
Some(&sentinel),
)
.is_err());
assert!(preparation_disposition(
&authority(ProtectedObjectAuthorityState::Importing, 3, None, None),
Some(&sentinel),
)
.is_err());
assert!(preparation_disposition(
&authority(
ProtectedObjectAuthorityState::PostgreSql,
2,
Some(1),
Some("a".repeat(64)),
),
None,
)
.is_err());
}
#[tokio::test]
#[ignore = "requires migrated Postgres, Redis, MinIO, and git"]
async fn populated_git_and_media_cutover_is_validated_resumable_and_one_way() {
let (state, tenant, pool) = migration_test_state().await;
let media_bytes = format!("migration-media-{}", uuid::Uuid::new_v4()).into_bytes();
let media_digest = hex::encode(Sha256::digest(&media_bytes));
let media_key = format!("{media_digest}.bin");
state
.media_storage
.put(&media_key, &media_bytes, "application/octet-stream")
.await
.expect("legacy media blob");
let mut media_meta = BlobMeta {
ext: "bin".into(),
mime_type: "application/octet-stream".into(),
size: media_bytes.len() as u64 + 1,
..BlobMeta::default()
};
state
.media_storage
.put_sidecar(&tenant, &media_digest, &media_meta)
.await
.expect("corrupt legacy sidecar fixture");
// Import starts durably before inventory validation. A corrupt legacy
// row must leave that checkpoint resumable and never create authority.
assert!(
prepare_postgres_authority(&state, &tenant)
.await
.expect_err("corrupt sidecar must fail")
.to_string()
.contains("size does not match"),
"corruption must be diagnosed before cutover"
);
assert_eq!(
state
.db
.protected_object_authority(
tenant.community(),
buzz_db::protected_visibility::ProtectedObjectSurface::Media,
)
.await
.expect("failed import state")
.state,
ProtectedObjectAuthorityState::Importing
);
assert!(
read_sentinel(&state, tenant.community())
.await
.expect("failed import sentinel probe")
.is_none(),
"corrupt inventory must not create a cutover sentinel"
);
media_meta.size = media_bytes.len() as u64;
state
.media_storage
.put_sidecar(&tenant, &media_digest, &media_meta)
.await
.expect("repair sidecar fixture");
prepare_postgres_authority(&state, &tenant)
.await
.expect("resume validated media import");
let owner = hex::encode(Sha256::digest(uuid::Uuid::new_v4().as_bytes()));
let repo_id = format!("migration-{}", uuid::Uuid::new_v4().simple());
state
.db
.reserve_repo_name(tenant.community(), &repo_id, &owner)
.await
.expect("legacy repo reservation");
let manifest = Manifest {
version: MANIFEST_VERSION,
head: "refs/heads/main".into(),
refs: BTreeMap::new(),
packs: Vec::new(),
parent: None,
};
manifest.validate().expect("valid empty manifest");
let manifest_key = state
.git_store
.put_manifest(&manifest.canonical_bytes().expect("manifest bytes"))
.await
.expect("legacy manifest");
let manifest_digest = manifest_key
.strip_prefix("manifests/")
.expect("manifest digest")
.to_owned();
state
.git_store
.put_pointer(
&pointer_key(tenant.community(), &owner, &repo_id),
manifest_digest.as_bytes(),
Precond::IfNoneMatchStar,
)
.await
.expect("legacy pointer");
// A transaction-owned announcement with no legacy pointer remains a
// valid first-push reservation after cutover.
let unpublished_repo = format!("unpublished-{}", uuid::Uuid::new_v4().simple());
let announcement_id = hex::encode(Sha256::digest(uuid::Uuid::new_v4().as_bytes()));
let owner_bytes = hex::decode(&owner).expect("owner bytes");
sqlx::query(
"INSERT INTO events \
(community_id, id, pubkey, created_at, kind, tags, content, sig, d_tag) \
VALUES ($1, $2, $3, clock_timestamp(), 30617, $4, '', $5, $6)",
)
.bind(tenant.community().as_uuid())
.bind(hex::decode(&announcement_id).expect("announcement bytes"))
.bind(&owner_bytes)
.bind(serde_json::json!([["d", unpublished_repo.clone()]]))
.bind(vec![0_u8; 64])
.bind(&unpublished_repo)
.execute(&pool)
.await
.expect("protected repository announcement");
sqlx::query(
"INSERT INTO git_repo_names \
(community_id, repo_id, owner_pubkey, publication_origin) \
VALUES ($1, $2, $3, 'protected_unpublished')",
)
.bind(tenant.community().as_uuid())
.bind(&unpublished_repo)
.bind(&owner)
.execute(&pool)
.await
.expect("protected first-push reservation");
crate::api::git::migration::prepare_postgres_authority(&state, &tenant)
.await
.expect("validated Git import");
let media_publication = state
.db
.media_publication(tenant.community(), &media_digest)
.await
.expect("media publication query")
.expect("imported media publication");
assert_eq!(media_publication.object_key, media_key);
assert_eq!(media_publication.object_size, media_bytes.len() as u64);
let git_publication = state
.db
.git_publication(tenant.community(), &repo_id, &owner)
.await
.expect("Git publication query")
.expect("imported Git publication");
assert_eq!(git_publication.manifest_sha256, manifest_digest);
assert!(state
.db
.git_publication(tenant.community(), &unpublished_repo, &owner)
.await
.expect("first-push publication query")
.is_none());
// The first protected push commits only the PostgreSQL publication.
// It must remain readable through the immutable manifest after a
// migration verification restart, without reviving a legacy pointer.
let policy = buzz_db::protected_publication::GitPolicyCommitFence {
announcement_id,
channel_id: None,
grant: buzz_db::protected_publication::GitPolicyGrant::RepoOwner,
};
let mut git_transaction = state.db.begin_transaction().await.expect("Git transaction");
let first_push = buzz_db::protected_publication::compare_and_publish_git(
&mut git_transaction,
buzz_db::protected_publication::GitPublicationRequest {
community_id: tenant.community(),
repo_id: &unpublished_repo,
owner_pubkey: &owner,
expected: None,
manifest_sha256: &manifest_digest,
pusher_pubkey: &owner_bytes,
policy: &policy,
},
)
.await
.expect("first protected push");
git_transaction.commit().await.expect("commit first push");
assert!(matches!(
first_push,
buzz_db::protected_publication::GitPublicationOutcome::Published(
buzz_db::protected_publication::GitPublication {
publication_version: 1,
..
}
)
));
let first_push_publication = state
.db
.git_publication(tenant.community(), &unpublished_repo, &owner)
.await
.expect("first-push read")
.expect("first push is PostgreSQL-authoritative");
assert_eq!(first_push_publication.manifest_sha256, manifest_digest);
assert_eq!(
crate::api::git::hydrate::load_manifest_by_digest(
&state.git_store,
&first_push_publication.manifest_sha256,
)
.await
.expect("published manifest read"),
manifest
);
crate::api::git::hydrate::hydrate_for_published_read(
&state.git_store,
&first_push_publication.manifest_sha256,
crate::api::git::hydrate::HydrationOptions {
pack_cache: &state.git_pack_cache,
scratch_dir: &state.config.git_repo_path,
max_pack_bytes: state.config.git_max_pack_bytes,
max_repo_bytes: state.config.git_max_repo_bytes,
},
)
.await
.expect("first protected publication hydrates for read");
assert!(state
.git_store
.get_pointer(&pointer_key(tenant.community(), &owner, &unpublished_repo,))
.await
.expect("legacy first-push pointer probe")
.is_none());
// A protected media publication likewise uses PostgreSQL for
// visibility while the immutable object remains in object storage.
let protected_media_bytes =
format!("protected-media-{}", uuid::Uuid::new_v4()).into_bytes();
let protected_media_digest = hex::encode(Sha256::digest(&protected_media_bytes));
let protected_media_key = format!("{protected_media_digest}.bin");
state
.media_storage
.put(
&protected_media_key,
&protected_media_bytes,
"application/octet-stream",
)
.await
.expect("protected media blob");
let sidecar_key =
buzz_media::MediaStorage::ctx_sidecar_key(&tenant, &protected_media_digest);
assert!(!state
.media_storage
.head(&sidecar_key)
.await
.expect("protected sidecar probe"));
let protected_media = buzz_db::protected_publication::MediaPublication {
sha256: protected_media_digest.clone(),
object_key: protected_media_key.clone(),
extension: "bin".into(),
mime_type: "application/octet-stream".into(),
object_size: protected_media_bytes.len() as u64,
metadata: serde_json::json!({"synthetic": true}),
thumbnail_key: None,
publication_version: 1,
};
let mut media_transaction = state
.db
.begin_transaction()
.await
.expect("media transaction");
buzz_db::protected_publication::publish_media(
&mut media_transaction,
tenant.community(),
&protected_media,
)
.await
.expect("protected media publication");
media_transaction
.commit()
.await
.expect("commit media publication");
assert_eq!(
state
.db
.media_publication(tenant.community(), &protected_media_digest)
.await
.expect("protected media read")
.expect("protected media is PostgreSQL-authoritative")
.object_key,
protected_media_key
);
assert_eq!(
state
.media_storage
.get(&protected_media_key)
.await
.expect("protected object read"),
protected_media_bytes
);
let (resolved_mime, resolved_key) = crate::api::media::resolve_visible_media(
&state,
&tenant,
&format!("{protected_media_digest}.bin"),
&None,
)
.await
.expect("non-Enforce mode retains PostgreSQL-authoritative visibility");
assert_eq!(resolved_mime, "application/octet-stream");
assert_eq!(resolved_key, protected_media_key);
assert!(!state
.media_storage
.head(&sidecar_key)
.await
.expect("protected sidecar recheck"));
// Completed imports are exact idempotent verification passes.
prepare_postgres_authority(&state, &tenant)
.await
.expect("media verification retry");
crate::api::git::migration::prepare_postgres_authority(&state, &tenant)
.await
.expect("Git verification retry");
require_reconciled_authority(&state, &tenant)
.await
.expect("media reconciled");
crate::api::git::migration::require_reconciled_authority(&state, &tenant)
.await
.expect("Git reconciled");
assert!(require_legacy_sentinel_absent(&state, &tenant)
.await
.is_err());
assert!(
crate::api::git::migration::require_legacy_sentinel_absent(&state, &tenant)
.await
.is_err()
);
// Simulate restoring only the database to a pre-cutover state. The
// immutable object-store sentinel must force domain denial rather than
// reviving the legacy lane or creating split-brain visibility.
sqlx::query(
"UPDATE protected_object_authority \
SET state = 'legacy', generation = 1, imported_objects = 0, \
inventory_sha256 = NULL, started_at = NULL, completed_at = NULL \
WHERE community_id = $1 AND surface IN ('git', 'media')",
)
.bind(tenant.community().as_uuid())
.execute(&pool)
.await
.expect("simulate database restore");
assert!(require_reconciled_authority(&state, &tenant).await.is_err());
assert!(
crate::api::git::migration::require_reconciled_authority(&state, &tenant)
.await
.is_err()
);
assert!(require_legacy_sentinel_absent(&state, &tenant)
.await
.is_err());
assert!(
crate::api::git::migration::require_legacy_sentinel_absent(&state, &tenant)
.await
.is_err()
);
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+666 -59
View File
@@ -46,16 +46,185 @@
//! is guaranteed by the directory's companion INCR counter (session-directory
//! lane); this module trusts that and only enforces "reject < known".
use std::collections::HashMap;
use std::sync::Arc;
use bytes::Bytes;
use tokio::sync::mpsc;
use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::CancellationToken;
use tracing::{debug, warn};
use uuid::Uuid;
use buzz_relay_mesh::{FencedHeader, MeshDatagram, RelayPeerTransport, RuntimeId};
use super::room::AudioRoomManager;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct OwnerIngressKey {
session_id: Uuid,
generation: u64,
sender: RuntimeId,
peer_index: u8,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct OwnerFanoutKey {
session_id: Uuid,
generation: u64,
owner: RuntimeId,
}
struct OwnerIngressAttachment {
admission_id: Uuid,
expires_at: u64,
}
enum MediaAttachmentKind {
OwnerIngress(OwnerIngressKey),
OwnerFanout(OwnerFanoutKey),
}
/// Live, compensated media attachments established by the reliable huddle
/// control path. A datagram alone can never create an attachment or advance a
/// generation floor.
#[derive(Default)]
pub struct MediaAttachmentRegistry {
owner_ingress: dashmap::DashMap<OwnerIngressKey, OwnerIngressAttachment>,
owner_fanout: dashmap::DashMap<OwnerFanoutKey, HashMap<Uuid, u64>>,
}
/// Drop guard for one live media attachment.
pub(crate) struct MediaAttachmentGuard {
registry: Arc<MediaAttachmentRegistry>,
kind: MediaAttachmentKind,
admission_id: Uuid,
}
impl Drop for MediaAttachmentGuard {
fn drop(&mut self) {
match self.kind {
MediaAttachmentKind::OwnerIngress(key) => {
if self
.registry
.owner_ingress
.get(&key)
.is_some_and(|entry| entry.admission_id == self.admission_id)
{
self.registry.owner_ingress.remove(&key);
}
}
MediaAttachmentKind::OwnerFanout(key) => {
if let Some(mut admissions) = self.registry.owner_fanout.get_mut(&key) {
admissions.remove(&self.admission_id);
let empty = admissions.is_empty();
drop(admissions);
if empty {
self.registry
.owner_fanout
.remove_if(&key, |_, value| value.is_empty());
}
}
}
}
}
}
impl MediaAttachmentRegistry {
/// Register one protected non-owner participant as an authorized media
/// author on the owner pod. The owner-assigned index and authenticated
/// runtime are both part of the key.
pub(crate) fn register_owner_ingress(
self: &Arc<Self>,
fenced: FencedHeader,
sender: RuntimeId,
peer_index: u8,
admission_id: Uuid,
expires_at: u64,
) -> Option<MediaAttachmentGuard> {
use dashmap::mapref::entry::Entry;
let key = OwnerIngressKey {
session_id: fenced.session_id,
generation: fenced.generation,
sender,
peer_index,
};
match self.owner_ingress.entry(key) {
Entry::Vacant(entry) => {
entry.insert(OwnerIngressAttachment {
admission_id,
expires_at,
});
}
Entry::Occupied(entry) if entry.get().admission_id == admission_id => {}
Entry::Occupied(_) => return None,
}
Some(MediaAttachmentGuard {
registry: Arc::clone(self),
kind: MediaAttachmentKind::OwnerIngress(key),
admission_id,
})
}
/// Register the owner as the only accepted fan-out source for one local
/// protected participant on a non-owner pod.
pub(crate) fn register_owner_fanout(
self: &Arc<Self>,
fenced: FencedHeader,
admission_id: Uuid,
expires_at: u64,
) -> MediaAttachmentGuard {
let key = OwnerFanoutKey {
session_id: fenced.session_id,
generation: fenced.generation,
owner: fenced.owner_runtime_id,
};
self.owner_fanout
.entry(key)
.or_default()
.insert(admission_id, expires_at);
MediaAttachmentGuard {
registry: Arc::clone(self),
kind: MediaAttachmentKind::OwnerFanout(key),
admission_id,
}
}
fn authorizes_owner_ingress(&self, key: OwnerIngressKey) -> Option<Uuid> {
let (admission_id, expires_at) = self
.owner_ingress
.get(&key)
.map(|entry| (entry.admission_id, entry.expires_at))
.unwrap_or((Uuid::nil(), 0));
let current = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.map(|duration| duration.as_secs());
if current.is_none_or(|current| current >= expires_at) {
self.owner_ingress
.remove_if(&key, |_, entry| entry.admission_id == admission_id);
return None;
}
self.owner_ingress
.get(&key)
.filter(|entry| entry.admission_id == admission_id)
.map(|entry| entry.admission_id)
}
fn authorized_owner_fanout_admissions(
&self,
key: OwnerFanoutKey,
) -> std::collections::HashSet<Uuid> {
let current = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()
.map(|duration| duration.as_secs());
let Some(mut admissions) = self.owner_fanout.get_mut(&key) else {
return std::collections::HashSet::new();
};
admissions.retain(|_, expires_at| current.is_some_and(|current| current < *expires_at));
admissions.keys().copied().collect()
}
}
/// The slice of the session directory that huddle audio needs.
///
@@ -160,13 +329,19 @@ pub struct MeshAudioRouter {
rooms: Arc<AudioRoomManager>,
fence: Arc<GenerationFloor>,
local_runtime_id: RuntimeId,
attachments: Arc<MediaAttachmentRegistry>,
}
impl MeshAudioRouter {
/// Construct a router over this pod's rooms, tagged with the local runtime
/// identity (used to distinguish owner vs non-owner delivery paths).
pub fn new(rooms: Arc<AudioRoomManager>, local_runtime_id: RuntimeId) -> Self {
Self::with_fence(rooms, local_runtime_id, Arc::new(GenerationFloor::new()))
Self::with_fence(
rooms,
local_runtime_id,
Arc::new(GenerationFloor::new()),
Arc::new(MediaAttachmentRegistry::default()),
)
}
/// Construct a router that enforces an externally owned generation floor.
@@ -178,11 +353,13 @@ impl MeshAudioRouter {
rooms: Arc<AudioRoomManager>,
local_runtime_id: RuntimeId,
fence: Arc<GenerationFloor>,
attachments: Arc<MediaAttachmentRegistry>,
) -> Self {
Self {
rooms,
fence,
local_runtime_id,
attachments,
}
}
@@ -210,8 +387,55 @@ impl MeshAudioRouter {
/// re-fan across the mesh: if we are the owner, cross-pod fan-out happens
/// through the remote peers' mesh sinks during `broadcast_frame`, so an
/// owner-side inbound datagram only needs local delivery here.
pub fn on_media_datagram(&self, dgram: &MeshDatagram) -> FenceVerdict {
pub fn on_media_datagram(&self, from: RuntimeId, dgram: &MeshDatagram) -> Option<FenceVerdict> {
let session_id = dgram.fenced.session_id;
let Some((&author_index, rest)) = dgram.payload.split_first() else {
warn!(%session_id, "empty media datagram payload — dropping");
return None;
};
let owner_ingress = dgram.fenced.owner_runtime_id == self.local_runtime_id;
let mut owner_ingress_admission = None;
let authorized_fanout = if owner_ingress {
let Some(admission_id) = self.attachments.authorizes_owner_ingress(OwnerIngressKey {
session_id,
generation: dgram.fenced.generation,
sender: from,
peer_index: author_index,
}) else {
debug!(%session_id, %from, "dropping media without a live control attachment");
return None;
};
owner_ingress_admission = Some(admission_id);
None
} else {
if from != dgram.fenced.owner_runtime_id {
debug!(%session_id, %from, "dropping media from a non-owner runtime");
return None;
}
let admissions = self
.attachments
.authorized_owner_fanout_admissions(OwnerFanoutKey {
session_id,
generation: dgram.fenced.generation,
owner: from,
});
if admissions.is_empty() {
debug!(%session_id, %from, "dropping media without a live control attachment");
return None;
}
Some(admissions)
};
let room = self.rooms.get_unambiguous_by_channel(session_id);
if let Some(admission_id) = owner_ingress_admission {
let room = room.as_ref()?;
if !room.is_published_admission(admission_id, author_index) {
debug!(%session_id, %from, "dropping media from an unpublished admission");
return None;
}
}
let verdict = self.fence.check(session_id, dgram.fenced.generation);
if let FenceVerdict::RejectStale { known } = verdict {
debug!(
@@ -220,21 +444,16 @@ impl MeshAudioRouter {
known_generation = known,
"dropping stale-generation media datagram (fence)"
);
return verdict;
return Some(verdict);
}
let Some(room) = self.rooms.get_unambiguous_by_channel(session_id) else {
// No local room for this session: nothing to deliver to. Not an
// error — membership can race a datagram in flight. An ambiguous
// same-UUID room collision is also dropped because the current
// media envelope has no community label.
return verdict;
let Some(room) = room else {
// Fan-out may race local room construction. The authenticated
// owner generation still advances monotonically, but no output is
// disclosed. Owner-ingress took the stricter path above.
return Some(verdict);
};
let Some((&author_index, rest)) = dgram.payload.split_first() else {
warn!(%session_id, "empty media datagram payload — dropping");
return verdict;
};
// Reconstruct the exact on-wire frame the local fan-out uses:
// [peer_index][v2 header][Opus]. `rest` is [v2 header][Opus]; the
// prefix is the author's index. We hand peers the already-prefixed
@@ -244,8 +463,13 @@ impl MeshAudioRouter {
prefixed.extend_from_slice(rest);
let prefixed = prefixed.freeze();
room.deliver_prefixed(author_index, prefixed);
verdict
match authorized_fanout {
Some(admissions) => {
room.deliver_prefixed_to_admissions(author_index, prefixed, &admissions)
}
None => room.deliver_prefixed(author_index, prefixed),
}
Some(verdict)
}
}
@@ -256,40 +480,167 @@ impl MeshAudioRouter {
/// feeds this task, which wraps each frame as a [`MeshDatagram`] and sends it to
/// the pod that hosts that participant. Drops on a disconnected/oversize peer —
/// realtime audio never blocks fan-out on one slow remote link.
pub fn spawn_remote_peer_sink(
pub(crate) struct RemotePeerSinkGuard {
cancel: CancellationToken,
active: Arc<std::sync::Mutex<bool>>,
}
impl RemotePeerSinkGuard {
/// Stop the sink without draining frames already queued for this peer.
///
/// The mutex makes `close` the local authoritative boundary: once it
/// returns, no later transport send can begin. A send already holding the
/// mutex completes before `close` returns.
pub(crate) fn close(&self) {
self.cancel.cancel();
if let Ok(mut active) = self.active.lock() {
*active = false;
}
}
}
impl Drop for RemotePeerSinkGuard {
fn drop(&mut self) {
self.close();
}
}
/// One-shot start capability for a prepared remote sink. Protected callers
/// install the guard in their exact expiry effect set before consuming this.
pub(crate) struct RemotePeerSinkStart {
start: Option<oneshot::Sender<()>>,
cancel: CancellationToken,
wake_at: Option<tokio::time::Instant>,
}
impl RemotePeerSinkStart {
pub(crate) fn start(mut self) -> bool {
!self.cancel.is_cancelled()
&& self
.wake_at
.is_none_or(|wake_at| tokio::time::Instant::now() < wake_at)
&& self
.start
.take()
.is_some_and(|start| start.send(()).is_ok())
}
}
pub(crate) fn prepare_remote_peer_sink(
transport: Arc<dyn RelayPeerTransport>,
to: RuntimeId,
fenced: FencedHeader,
mut frames: mpsc::Receiver<Bytes>,
) {
wake_at: Option<tokio::time::Instant>,
) -> (RemotePeerSinkGuard, RemotePeerSinkStart) {
let cancel = CancellationToken::new();
let active = Arc::new(std::sync::Mutex::new(true));
let task_cancel = cancel.clone();
let task_active = Arc::clone(&active);
let (start_tx, start_rx) = oneshot::channel();
tokio::spawn(async move {
tokio::select! {
biased;
_ = task_cancel.cancelled() => return,
started = start_rx => if started.is_err() { return },
}
let mut seq: u64 = 0;
while let Some(frame) = frames.recv().await {
loop {
let frame = tokio::select! {
biased;
_ = task_cancel.cancelled() => break,
frame = frames.recv() => {
let Some(frame) = frame else { break };
frame
}
};
if wake_at.is_some_and(|wake_at| tokio::time::Instant::now() >= wake_at) {
task_cancel.cancel();
break;
}
let dgram = MeshDatagram {
fenced,
seq,
payload: frame.to_vec(),
};
seq = seq.wrapping_add(1);
let Ok(active) = task_active.lock() else {
break;
};
if !*active || task_cancel.is_cancelled() {
break;
}
if let Err(e) = transport.send_datagram(to, dgram) {
// Disconnected peer or oversize frame: drop and keep going.
// The MTU case is the ship-gate's job to prevent; here we just
// never let one bad link stall the room.
debug!(%to, "remote peer datagram send failed: {e}");
}
}
debug!(%to, "remote peer sink closed");
});
(
RemotePeerSinkGuard {
cancel: cancel.clone(),
active,
},
RemotePeerSinkStart {
start: Some(start_tx),
cancel,
wake_at,
},
)
}
pub(crate) fn spawn_remote_peer_sink(
transport: Arc<dyn RelayPeerTransport>,
to: RuntimeId,
fenced: FencedHeader,
frames: mpsc::Receiver<Bytes>,
) -> RemotePeerSinkGuard {
let (guard, start) = prepare_remote_peer_sink(transport, to, fenced, frames, None);
let _ = start.start();
guard
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct RecordingTransport {
sent: std::sync::Mutex<Vec<MeshDatagram>>,
}
impl RelayPeerTransport for RecordingTransport {
fn send_datagram(
&self,
_to: RuntimeId,
dgram: MeshDatagram,
) -> Result<(), buzz_relay_mesh::MeshError> {
self.sent.lock().expect("recording lock").push(dgram);
Ok(())
}
fn open_session_stream(
&self,
_to: RuntimeId,
_hello: buzz_relay_mesh::StreamHello,
) -> futures_util::future::BoxFuture<
'_,
Result<buzz_relay_mesh::MeshStream, buzz_relay_mesh::MeshError>,
> {
Box::pin(async { Err(buzz_relay_mesh::MeshError::Transport("unused".into())) })
}
fn set_inbound(&self, _handler: Box<dyn buzz_relay_mesh::InboundHandler>) {}
}
fn rt(b: u8) -> RuntimeId {
RuntimeId([b; 32])
}
fn community() -> buzz_core::CommunityId {
buzz_core::CommunityId::from_uuid(Uuid::from_u128(1))
}
fn fenced(session: Uuid, generation: u64) -> FencedHeader {
FencedHeader {
session_id: session,
@@ -298,6 +649,73 @@ mod tests {
}
}
#[tokio::test(flavor = "current_thread")]
async fn revoked_remote_sink_discards_buffered_fanout_before_transport_send() {
let recording = Arc::new(RecordingTransport::default());
let transport: Arc<dyn RelayPeerTransport> = recording.clone();
let (tx, rx) = mpsc::channel(8);
let guard = spawn_remote_peer_sink(transport, rt(2), fenced(Uuid::new_v4(), 1), rx);
tx.try_send(Bytes::from_static(b"already-authorized"))
.expect("initial frame queues");
while recording.sent.lock().expect("recording lock").is_empty() {
tokio::task::yield_now().await;
}
for _ in 0..8 {
tx.try_send(Bytes::from_static(b"must-be-discarded"))
.expect("revocation backlog queues");
}
guard.close();
tokio::task::yield_now().await;
assert_eq!(
recording.sent.lock().expect("recording lock").len(),
1,
"closing a revoked sink must discard its buffered fan-out"
);
}
#[tokio::test(flavor = "current_thread")]
async fn prepared_remote_sink_cannot_emit_before_exact_effect_installation() {
let recording = Arc::new(RecordingTransport::default());
let transport: Arc<dyn RelayPeerTransport> = recording.clone();
let (tx, rx) = mpsc::channel(1);
let (guard, start) =
prepare_remote_peer_sink(transport, rt(2), fenced(Uuid::new_v4(), 1), rx, None);
tx.try_send(Bytes::from_static(b"not-yet-authorized"))
.expect("frame queues");
tokio::task::yield_now().await;
assert!(recording.sent.lock().expect("recording lock").is_empty());
guard.close();
assert!(!start.start(), "closed prepared sink cannot be started");
tokio::task::yield_now().await;
assert!(recording.sent.lock().expect("recording lock").is_empty());
}
#[tokio::test(start_paused = true)]
async fn prepared_remote_sink_cannot_start_at_exact_authority_deadline() {
let recording = Arc::new(RecordingTransport::default());
let transport: Arc<dyn RelayPeerTransport> = recording.clone();
let (tx, rx) = mpsc::channel(1);
let wake_at = tokio::time::Instant::now() + std::time::Duration::from_millis(250);
let (_guard, start) = prepare_remote_peer_sink(
transport,
rt(2),
fenced(Uuid::new_v4(), 1),
rx,
Some(wake_at),
);
tx.try_send(Bytes::from_static(b"must-not-cross-deadline"))
.expect("frame queues while hidden");
tokio::time::advance(std::time::Duration::from_millis(250)).await;
assert!(!start.start(), "deadline equality fails closed");
tokio::task::yield_now().await;
assert!(recording.sent.lock().expect("recording lock").is_empty());
}
#[test]
fn fence_accepts_first_and_equal_and_higher() {
let f = GenerationFloor::new();
@@ -340,54 +758,243 @@ mod tests {
assert_eq!(f.check(s, 3), FenceVerdict::Accept { advanced: false });
}
#[test]
fn router_drops_stale_datagram_without_delivering() {
fn test_router(
rooms: Arc<AudioRoomManager>,
local_runtime_id: RuntimeId,
) -> (MeshAudioRouter, Arc<MediaAttachmentRegistry>) {
let attachments = Arc::new(MediaAttachmentRegistry::default());
(
MeshAudioRouter::with_fence(
rooms,
local_runtime_id,
Arc::new(GenerationFloor::new()),
Arc::clone(&attachments),
),
attachments,
)
}
#[tokio::test]
async fn router_drops_stale_datagram_without_delivering() {
let rooms = Arc::new(AudioRoomManager::new());
let router = MeshAudioRouter::new(Arc::clone(&rooms), rt(1));
let (router, attachments) = test_router(Arc::clone(&rooms), rt(1));
let s = Uuid::new_v4();
let fence = fenced(s, 5);
let _attached = attachments.register_owner_fanout(fence, Uuid::new_v4(), u64::MAX);
// Establish a floor at generation 5.
assert!(matches!(
router.on_media_datagram(&MeshDatagram {
fenced: fenced(s, 5),
seq: 0,
payload: vec![0, 1, 2],
}),
FenceVerdict::Accept { .. }
router.on_media_datagram(
rt(0xAA),
&MeshDatagram {
fenced: fence,
seq: 0,
payload: vec![0, 1, 2],
},
),
Some(FenceVerdict::Accept { .. })
));
// A stale frame is rejected.
let stale = fenced(s, 4);
let _stale_attached = attachments.register_owner_fanout(stale, Uuid::new_v4(), u64::MAX);
assert_eq!(
router.on_media_datagram(&MeshDatagram {
fenced: fenced(s, 4),
seq: 1,
payload: vec![0, 1, 2],
}),
FenceVerdict::RejectStale { known: 5 }
router.on_media_datagram(
rt(0xAA),
&MeshDatagram {
fenced: stale,
seq: 1,
payload: vec![0, 1, 2],
},
),
Some(FenceVerdict::RejectStale { known: 5 })
);
}
#[tokio::test]
async fn router_tolerates_missing_room_and_empty_payload() {
let rooms = Arc::new(AudioRoomManager::new());
let (router, attachments) = test_router(Arc::clone(&rooms), rt(1));
let s = Uuid::new_v4();
let fence = fenced(s, 1);
let _attached = attachments.register_owner_fanout(fence, Uuid::new_v4(), u64::MAX);
// No local room for this session: accepted by fence, no panic.
assert!(matches!(
router.on_media_datagram(
rt(0xAA),
&MeshDatagram {
fenced: fence,
seq: 0,
payload: vec![7, 8],
},
),
Some(FenceVerdict::Accept { .. })
));
// Empty payload is dropped before it can alter the fence.
let s2 = Uuid::new_v4();
assert_eq!(
router.on_media_datagram(
rt(0xAA),
&MeshDatagram {
fenced: fenced(s2, 1),
seq: 0,
payload: vec![],
},
),
None
);
}
#[tokio::test]
async fn realtime_media_requires_registered_control_attachment() {
let rooms = Arc::new(AudioRoomManager::new());
let (router, _) = test_router(rooms, rt(1));
let session = Uuid::new_v4();
assert_eq!(
router.on_media_datagram(
rt(0xAA),
&MeshDatagram {
fenced: fenced(session, 99),
seq: 0,
payload: vec![3, 1, 2],
},
),
None
);
assert_eq!(
router.fence().check(session, 1),
FenceVerdict::Accept { advanced: false }
);
}
#[tokio::test]
async fn realtime_media_rejects_non_owner_sender_on_ingress() {
let rooms = Arc::new(AudioRoomManager::new());
let (router, attachments) = test_router(rooms, rt(1));
let fence = fenced(Uuid::new_v4(), 2);
let _attached = attachments.register_owner_fanout(fence, Uuid::new_v4(), u64::MAX);
assert_eq!(
router.on_media_datagram(
rt(0xBB),
&MeshDatagram {
fenced: fence,
seq: 0,
payload: vec![4, 1],
},
),
None
);
}
#[tokio::test]
async fn late_media_after_abort_is_dropped() {
let rooms = Arc::new(AudioRoomManager::new());
let (router, attachments) = test_router(rooms, rt(1));
let fence = fenced(Uuid::new_v4(), 3);
let attached = attachments.register_owner_fanout(fence, Uuid::new_v4(), u64::MAX);
drop(attached);
assert_eq!(
router.on_media_datagram(
rt(0xAA),
&MeshDatagram {
fenced: fence,
seq: 1,
payload: vec![5, 1],
},
),
None
);
}
#[tokio::test]
async fn expired_local_remote_peer_cannot_receive_through_live_sibling() {
let rooms = Arc::new(AudioRoomManager::new());
let local_runtime = rt(1);
let (router, attachments) = test_router(Arc::clone(&rooms), local_runtime);
let session = Uuid::new_v4();
let room = rooms.get_or_create(community(), session);
let expired_admission = Uuid::new_v4();
let live_admission = Uuid::new_v4();
let expired = room
.reserve_peer(expired_admission, "expired".into(), 2)
.expect("reserve expired peer")
.activate()
.expect("activate expired peer");
let live = room
.reserve_peer(live_admission, "live".into(), 2)
.expect("reserve live peer")
.activate()
.expect("activate live peer");
let fence = fenced(session, 4);
let expired_attachment =
attachments.register_owner_fanout(fence, expired_admission, u64::MAX);
let _live_attachment = attachments.register_owner_fanout(fence, live_admission, u64::MAX);
drop(expired_attachment);
assert!(matches!(
router.on_media_datagram(
fence.owner_runtime_id,
&MeshDatagram {
fenced: fence,
seq: 0,
payload: vec![99, 1, 2, 3],
},
),
Some(FenceVerdict::Accept { .. })
));
let mut expired_rx = expired.2;
let mut live_rx = live.2;
assert!(expired_rx.try_recv().is_err());
assert_eq!(
live_rx.try_recv().expect("live sibling receives").as_ref(),
&[99, 1, 2, 3]
);
}
#[test]
fn router_tolerates_missing_room_and_empty_payload() {
fn expired_owner_ingress_cannot_deliver_or_advance_the_fence() {
let rooms = Arc::new(AudioRoomManager::new());
let router = MeshAudioRouter::new(Arc::clone(&rooms), rt(1));
let s = Uuid::new_v4();
// No local room for this session: accepted by fence, no panic.
assert!(matches!(
router.on_media_datagram(&MeshDatagram {
fenced: fenced(s, 1),
seq: 0,
payload: vec![7, 8],
}),
FenceVerdict::Accept { .. }
));
// Empty payload after a valid fence: dropped, no panic.
let s2 = Uuid::new_v4();
assert!(matches!(
router.on_media_datagram(&MeshDatagram {
fenced: fenced(s2, 1),
seq: 0,
payload: vec![],
}),
FenceVerdict::Accept { .. }
));
let (router, attachments) = test_router(rooms, rt(0xAA));
let fence = fenced(Uuid::new_v4(), 7);
let sender = rt(1);
let _attached = attachments
.register_owner_ingress(fence, sender, 9, Uuid::new_v4(), 0)
.expect("unique attachment");
assert_eq!(
router.on_media_datagram(
sender,
&MeshDatagram {
fenced: fence,
seq: 0,
payload: vec![9, 1, 2],
},
),
None
);
assert_eq!(
router.fence().check(fence.session_id, 1),
FenceVerdict::Accept { advanced: false }
);
}
#[test]
fn expired_owner_fanout_cannot_deliver_or_advance_the_fence() {
let rooms = Arc::new(AudioRoomManager::new());
let (router, attachments) = test_router(rooms, rt(1));
let fence = fenced(Uuid::new_v4(), 7);
let _attached = attachments.register_owner_fanout(fence, Uuid::new_v4(), 0);
assert_eq!(
router.on_media_datagram(
fence.owner_runtime_id,
&MeshDatagram {
fenced: fence,
seq: 0,
payload: vec![9, 1, 2],
},
),
None
);
assert_eq!(
router.fence().check(fence.session_id, 1),
FenceVerdict::Accept { advanced: false }
);
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,50 @@
-- PostgreSQL-authoritative visibility for protected object-store content.
-- Immutable objects may be staged before these rows commit; without a current
-- publication row they are not visible in Enforce.
CREATE TABLE git_repo_publications (
community_id UUID NOT NULL,
repo_id TEXT NOT NULL,
owner_pubkey TEXT NOT NULL,
manifest_sha256 TEXT NOT NULL CHECK (
manifest_sha256 ~ '^[0-9a-f]{64}$'
),
publication_version BIGINT NOT NULL CHECK (publication_version > 0),
state TEXT NOT NULL DEFAULT 'active' CHECK (
state IN ('active', 'unpublished')
),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (community_id, repo_id),
FOREIGN KEY (community_id, repo_id)
REFERENCES git_repo_names (community_id, repo_id)
);
CREATE TABLE media_publications (
community_id UUID NOT NULL REFERENCES communities(id),
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
object_key TEXT NOT NULL CHECK (
length(object_key) > 0 AND length(object_key) <= 512
),
extension TEXT NOT NULL CHECK (extension ~ '^[a-z0-9]{1,8}$'),
mime_type TEXT NOT NULL CHECK (
length(mime_type) > 0 AND length(mime_type) <= 255
),
object_size BIGINT NOT NULL CHECK (object_size >= 0),
metadata JSONB NOT NULL CHECK (
octet_length(metadata::text) <= 16384
),
thumbnail_key TEXT CHECK (
thumbnail_key IS NULL OR (length(thumbnail_key) > 0 AND length(thumbnail_key) <= 512)
),
publication_version BIGINT NOT NULL DEFAULT 1 CHECK (publication_version > 0),
state TEXT NOT NULL DEFAULT 'active' CHECK (
state IN ('active', 'unpublished')
),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (community_id, sha256)
);
CREATE INDEX idx_media_publications_state
ON media_publications (community_id, state);
@@ -0,0 +1,22 @@
-- Durable authority boundary for protected audio sessions.
--
-- The row records a bounded existing-member admission. It does not create
-- membership and cannot outlive the finalized access lease.
CREATE TABLE audio_session_admissions (
community_id UUID NOT NULL REFERENCES communities(id) ON DELETE CASCADE,
admission_id UUID NOT NULL,
channel_id UUID NOT NULL,
pubkey BYTEA NOT NULL CHECK (octet_length(pubkey) = 32),
lease_expires_at TIMESTAMPTZ NOT NULL,
admitted_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (community_id, admission_id),
CONSTRAINT audio_session_admissions_channel_fk
FOREIGN KEY (community_id, channel_id)
REFERENCES channels(community_id, id) ON DELETE CASCADE,
CONSTRAINT audio_session_admissions_positive_lease
CHECK (lease_expires_at > admitted_at)
);
CREATE INDEX idx_audio_session_admissions_active
ON audio_session_admissions (community_id, channel_id, lease_expires_at);
@@ -0,0 +1,28 @@
-- Monotonic per-community authority for protected Git and media visibility.
--
-- Missing rows are treated as legacy only by migration-aware binaries. Once a
-- row enters `importing`, writes to the legacy visibility object are fenced and
-- the state can advance only to `postgresql`.
CREATE TABLE protected_object_authority (
community_id UUID NOT NULL REFERENCES communities(id) ON DELETE CASCADE,
surface TEXT NOT NULL CHECK (surface IN ('git', 'media')),
state TEXT NOT NULL CHECK (state IN ('legacy', 'importing', 'postgresql')),
generation BIGINT NOT NULL CHECK (generation > 0),
imported_objects BIGINT NOT NULL DEFAULT 0 CHECK (imported_objects >= 0),
inventory_sha256 TEXT,
started_at TIMESTAMPTZ,
completed_at TIMESTAMPTZ,
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (community_id, surface),
CHECK (inventory_sha256 IS NULL OR inventory_sha256 ~ '^[0-9a-f]{64}$'),
CHECK (
(state = 'legacy' AND started_at IS NULL AND completed_at IS NULL)
OR (state = 'importing' AND started_at IS NOT NULL AND completed_at IS NULL)
OR (state = 'postgresql' AND started_at IS NOT NULL AND completed_at IS NOT NULL
AND inventory_sha256 IS NOT NULL)
)
);
CREATE INDEX idx_protected_object_authority_state
ON protected_object_authority (state, community_id, surface);
@@ -0,0 +1,7 @@
-- Distinguish a legacy reservation whose pointer must exist from an Enforce
-- announcement that intentionally starts unpublished and takes its first push
-- only after PostgreSQL cutover. Existing reservations are conservatively
-- classified as legacy; migration must fail on a missing legacy pointer.
ALTER TABLE git_repo_names
ADD COLUMN publication_origin TEXT NOT NULL DEFAULT 'legacy'
CHECK (publication_origin IN ('legacy', 'protected_unpublished'));
@@ -0,0 +1,39 @@
-- Durable lifecycle for PostgreSQL-authorized audio attachment attempts.
--
-- These rows authorize bounded attempts; they never assert live presence or
-- create membership. Existing one-shot receipts are closed during upgrade.
ALTER TABLE audio_session_admissions
ADD COLUMN state TEXT,
ADD COLUMN state_version BIGINT,
ADD COLUMN updated_at TIMESTAMPTZ,
ADD COLUMN activated_at TIMESTAMPTZ,
ADD COLUMN aborted_at TIMESTAMPTZ,
ADD COLUMN finished_at TIMESTAMPTZ,
ADD COLUMN failure_code TEXT;
UPDATE audio_session_admissions
SET state = 'aborted',
state_version = 1,
updated_at = admitted_at,
aborted_at = admitted_at,
failure_code = 'upgrade_closed';
ALTER TABLE audio_session_admissions
ALTER COLUMN state SET DEFAULT 'reserved',
ALTER COLUMN state SET NOT NULL,
ALTER COLUMN state_version SET DEFAULT 1,
ALTER COLUMN state_version SET NOT NULL,
ALTER COLUMN updated_at SET DEFAULT clock_timestamp(),
ALTER COLUMN updated_at SET NOT NULL,
ADD CONSTRAINT audio_session_admissions_state
CHECK (state IN ('reserved', 'active', 'aborted', 'finished')),
ADD CONSTRAINT audio_session_admissions_state_version
CHECK (state_version > 0),
ADD CONSTRAINT audio_session_admissions_failure_code
CHECK (failure_code IS NULL OR
(length(failure_code) BETWEEN 1 AND 64 AND
failure_code ~ '^[a-z0-9_]+$'));
CREATE INDEX idx_audio_session_admissions_reconcile
ON audio_session_admissions (state, updated_at, lease_expires_at);
@@ -0,0 +1,31 @@
-- O4 has no authority model for community archive, restore, or physical
-- teardown. Keep those transitions unavailable for activated Enforce domains
-- so a stale database restore cannot resurrect a protected community. Off and
-- observational communities retain their existing lifecycle behavior.
CREATE FUNCTION deny_unwitnessed_protected_community_lifecycle() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF EXISTS (
SELECT 1 FROM authorization_invalidation_domains
WHERE community_id = OLD.id
) THEN
IF TG_OP = 'DELETE' THEN
RAISE EXCEPTION 'protected community lifecycle requires a separately witnessed authority model'
USING ERRCODE = 'check_violation';
END IF;
IF NEW.archived_at IS DISTINCT FROM OLD.archived_at THEN
RAISE EXCEPTION 'protected community lifecycle requires a separately witnessed authority model'
USING ERRCODE = 'check_violation';
END IF;
END IF;
IF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
RETURN NEW;
END
$$;
CREATE TRIGGER protected_community_lifecycle_guard
BEFORE UPDATE OF archived_at OR DELETE ON communities
FOR EACH ROW EXECUTE FUNCTION deny_unwitnessed_protected_community_lifecycle();
@@ -0,0 +1,16 @@
-- Protected-domain activation is a one-way V1 cutover. Removing the marker
-- would disable database lifecycle fencing and let an unaware release treat
-- the domain as legacy. O4 has no witnessed downgrade operation, so direct
-- marker removal remains unavailable.
CREATE FUNCTION deny_protected_domain_marker_delete() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'protected domain activation cannot be removed without a separately witnessed authority model'
USING ERRCODE = 'check_violation';
END
$$;
CREATE TRIGGER protected_domain_marker_delete_guard
BEFORE DELETE ON authorization_invalidation_domains
FOR EACH ROW EXECUTE FUNCTION deny_protected_domain_marker_delete();
@@ -0,0 +1,11 @@
-- Durable disconnect/cancellation intent for live audio attempts. A request is
-- witnessed before ephemeral cleanup begins, allowing another relay to finish
-- compensation immediately when the initiating process loses the race.
ALTER TABLE audio_session_admissions
ADD COLUMN cleanup_requested_at TIMESTAMPTZ;
CREATE INDEX idx_audio_session_admissions_cleanup_requested
ON audio_session_admissions (community_id, cleanup_requested_at)
WHERE cleanup_requested_at IS NOT NULL
AND state IN ('reserved', 'active');
@@ -0,0 +1,18 @@
-- Kind 30617 is live Git authorization policy. Its replacement or deletion
-- must advance the independently witnessed PostgreSQL authority vector so a
-- stale restore cannot revive an earlier, more permissive policy.
CREATE TRIGGER git_policy_insert_authority_epoch
AFTER INSERT ON events
FOR EACH ROW WHEN (NEW.kind = 30617)
EXECUTE FUNCTION advance_authorization_authority_epoch();
CREATE TRIGGER git_policy_update_authority_epoch
AFTER UPDATE ON events
FOR EACH ROW WHEN (OLD.kind = 30617 OR NEW.kind = 30617)
EXECUTE FUNCTION advance_authorization_authority_epoch();
CREATE TRIGGER git_policy_delete_authority_epoch
AFTER DELETE ON events
FOR EACH ROW WHEN (OLD.kind = 30617)
EXECUTE FUNCTION advance_authorization_authority_epoch();
@@ -0,0 +1,57 @@
-- Durable proof that an authorized audio attempt became peer-visible.
-- `active` remains authorization for an attempt, never proof of presence.
ALTER TABLE audio_session_admissions
ADD COLUMN visibility_observed_at TIMESTAMPTZ;
ALTER TABLE audio_session_admissions
DROP CONSTRAINT audio_session_admissions_state,
DROP CONSTRAINT audio_session_admissions_claim;
ALTER TABLE audio_session_admissions
ADD CONSTRAINT audio_session_admissions_state
CHECK (state IN ('reserved', 'active', 'visible', 'aborted', 'finished')),
ADD CONSTRAINT audio_session_admissions_claim CHECK (
(state = 'reserved'
AND claimant_id IS NOT NULL
AND attachment_generation = 0
AND claim_expires_at IS NOT NULL)
OR
(state IN ('active', 'visible', 'finished')
AND claimant_id IS NOT NULL
AND attachment_generation > 0
AND claim_expires_at IS NOT NULL)
OR
(state = 'aborted')
),
ADD CONSTRAINT audio_session_admissions_visibility CHECK (
state <> 'visible' OR visibility_observed_at IS NOT NULL
);
DROP INDEX idx_audio_session_admissions_cleanup_requested;
CREATE INDEX idx_audio_session_admissions_cleanup_requested
ON audio_session_admissions (community_id, cleanup_requested_at)
WHERE cleanup_requested_at IS NOT NULL
AND state IN ('reserved', 'active', 'visible');
-- Mixed-version relays must not retain the former active -> finished path.
-- An older binary consequently fails closed after this migration instead of
-- treating an authorization receipt as proof that visibility occurred.
CREATE FUNCTION audio_admission_visibility_transition_guard()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.state = OLD.state THEN
RETURN NEW;
END IF;
IF (OLD.state = 'reserved' AND NEW.state IN ('active', 'aborted'))
OR (OLD.state = 'active' AND NEW.state IN ('visible', 'aborted'))
OR (OLD.state = 'visible' AND NEW.state IN ('finished', 'aborted')) THEN
RETURN NEW;
END IF;
RAISE EXCEPTION 'invalid audio admission state transition';
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER audio_admission_visibility_transition_guard
BEFORE UPDATE OF state ON audio_session_admissions
FOR EACH ROW EXECUTE FUNCTION audio_admission_visibility_transition_guard();