diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-23 15:41:32 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-23 15:41:32 +0000 |
| commit | c54ce061d6d278cce8362d5af085808ca60c239b (patch) | |
| tree | ec967d6195d9f7ec4f061449596611afe3a0950f /src/nostr | |
| parent | e0ad39a489b3398f8208713bf728db0cb11475b0 (diff) | |
| parent | 113928aa84894ea8f65c247d9987527e792b32a9 (diff) | |
feat: announcement purgatory
Extends purgatory to hold repository announcements until git data arrives,
preventing empty repositories from being served to clients.
When an announcement is received, a bare repo is created immediately and the
announcement is held in purgatory. It is only promoted and served once a git
push confirms real content exists. If no push arrives before expiry, the bare
repo is deleted and the announcement is silently discarded.
Key behaviours:
- Soft expiry: announcements are hidden from clients but kept alive while git
pushes are in progress, reviving on successful push
- Expiry is extended when a matching state event or git push is observed
- NIP-09 deletion events remove announcements from purgatory
- Purgatory state (announcements, state events, PR events, expired set) is
persisted to disk on graceful shutdown and restored on startup, with elapsed
downtime subtracted from expiry deadlines
- Purgatory announcements drive StateOnly sync in the sync system so state
events are fetched from listed relays before promotion
- SyncLevel added to RepoSyncIndex to distinguish purgatory repos (StateOnly)
from promoted repos (Full L2+L3 sync)
Diffstat (limited to 'src/nostr')
| -rw-r--r-- | src/nostr/builder.rs | 33 | ||||
| -rw-r--r-- | src/nostr/policy/announcement.rs | 273 | ||||
| -rw-r--r-- | src/nostr/policy/deletion.rs | 498 | ||||
| -rw-r--r-- | src/nostr/policy/mod.rs | 2 | ||||
| -rw-r--r-- | src/nostr/policy/pr_event.rs | 8 | ||||
| -rw-r--r-- | src/nostr/policy/related.rs | 5 | ||||
| -rw-r--r-- | src/nostr/policy/state.rs | 75 |
7 files changed, 880 insertions, 14 deletions
diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index 713c129..7a05348 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs | |||
| @@ -14,10 +14,11 @@ use nostr_relay_builder::prelude::*; | |||
| 14 | use crate::config::{Config, DatabaseBackend}; | 14 | use crate::config::{Config, DatabaseBackend}; |
| 15 | use crate::nostr::events::RepositoryAnnouncement; | 15 | use crate::nostr::events::RepositoryAnnouncement; |
| 16 | use crate::nostr::policy::{ | 16 | use crate::nostr::policy::{ |
| 17 | AnnouncementPolicy, AnnouncementResult, PolicyContext, PrEventPolicy, ReferenceResult, | 17 | AnnouncementPolicy, AnnouncementResult, DeletionPolicy, PolicyContext, PrEventPolicy, |
| 18 | RelatedEventPolicy, StatePolicy, StateResult, | 18 | ReferenceResult, RelatedEventPolicy, StatePolicy, StateResult, |
| 19 | }; | 19 | }; |
| 20 | 20 | ||
| 21 | |||
| 21 | /// Type alias for the shared database used by the relay | 22 | /// Type alias for the shared database used by the relay |
| 22 | pub type SharedDatabase = Arc<dyn NostrDatabase>; | 23 | pub type SharedDatabase = Arc<dyn NostrDatabase>; |
| 23 | 24 | ||
| @@ -28,6 +29,7 @@ pub type SharedDatabase = Arc<dyn NostrDatabase>; | |||
| 28 | /// - `StatePolicy` - State event validation + ref alignment | 29 | /// - `StatePolicy` - State event validation + ref alignment |
| 29 | /// - `PrEventPolicy` - PR/PR Update validation | 30 | /// - `PrEventPolicy` - PR/PR Update validation |
| 30 | /// - `RelatedEventPolicy` - Forward/backward reference checking | 31 | /// - `RelatedEventPolicy` - Forward/backward reference checking |
| 32 | /// - `DeletionPolicy` - NIP-09 event deletion request handling | ||
| 31 | /// | 33 | /// |
| 32 | /// Uses stateful database queries to check event relationships. | 34 | /// Uses stateful database queries to check event relationships. |
| 33 | #[derive(Clone)] | 35 | #[derive(Clone)] |
| @@ -37,6 +39,7 @@ pub struct Nip34WritePolicy { | |||
| 37 | state_policy: StatePolicy, | 39 | state_policy: StatePolicy, |
| 38 | pr_event_policy: PrEventPolicy, | 40 | pr_event_policy: PrEventPolicy, |
| 39 | related_event_policy: RelatedEventPolicy, | 41 | related_event_policy: RelatedEventPolicy, |
| 42 | deletion_policy: DeletionPolicy, | ||
| 40 | } | 43 | } |
| 41 | 44 | ||
| 42 | impl std::fmt::Debug for Nip34WritePolicy { | 45 | impl std::fmt::Debug for Nip34WritePolicy { |
| @@ -68,6 +71,7 @@ impl Nip34WritePolicy { | |||
| 68 | state_policy: StatePolicy::new(ctx.clone()), | 71 | state_policy: StatePolicy::new(ctx.clone()), |
| 69 | pr_event_policy: PrEventPolicy::new(ctx.clone()), | 72 | pr_event_policy: PrEventPolicy::new(ctx.clone()), |
| 70 | related_event_policy: RelatedEventPolicy::new(ctx.clone()), | 73 | related_event_policy: RelatedEventPolicy::new(ctx.clone()), |
| 74 | deletion_policy: DeletionPolicy::new(ctx.clone()), | ||
| 71 | ctx, | 75 | ctx, |
| 72 | } | 76 | } |
| 73 | } | 77 | } |
| @@ -205,6 +209,30 @@ impl Nip34WritePolicy { | |||
| 205 | } | 209 | } |
| 206 | } | 210 | } |
| 207 | } | 211 | } |
| 212 | AnnouncementResult::AcceptPurgatory => { | ||
| 213 | // New announcement - add to purgatory | ||
| 214 | match self.announcement_policy.add_to_purgatory(event) { | ||
| 215 | Ok(()) => { | ||
| 216 | tracing::info!( | ||
| 217 | "Accepted announcement to purgatory: {} (waiting for git data)", | ||
| 218 | event_id_str | ||
| 219 | ); | ||
| 220 | |||
| 221 | WritePolicyResult::Reject { | ||
| 222 | status: true, // Client sees OK | ||
| 223 | message: "purgatory: won't be served until git data arrives".into(), | ||
| 224 | } | ||
| 225 | } | ||
| 226 | Err(e) => { | ||
| 227 | tracing::warn!( | ||
| 228 | "Failed to add announcement to purgatory {}: {}", | ||
| 229 | event_id_str, | ||
| 230 | e | ||
| 231 | ); | ||
| 232 | WritePolicyResult::reject(e) | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 208 | AnnouncementResult::AcceptMaintainer => { | 236 | AnnouncementResult::AcceptMaintainer => { |
| 209 | // Parse announcement to get details for logging | 237 | // Parse announcement to get details for logging |
| 210 | match RepositoryAnnouncement::from_event(event.clone()) { | 238 | match RepositoryAnnouncement::from_event(event.clone()) { |
| @@ -621,6 +649,7 @@ impl WritePolicy for Nip34WritePolicy { | |||
| 621 | ); | 649 | ); |
| 622 | WritePolicyResult::Accept | 650 | WritePolicyResult::Accept |
| 623 | } | 651 | } |
| 652 | Kind::EventDeletion => self.deletion_policy.handle(event).await, | ||
| 624 | _ => self.handle_related_event(event, "Event").await, | 653 | _ => self.handle_related_event(event, "Event").await, |
| 625 | } | 654 | } |
| 626 | }) | 655 | }) |
diff --git a/src/nostr/policy/announcement.rs b/src/nostr/policy/announcement.rs index 15a6e58..b366f0b 100644 --- a/src/nostr/policy/announcement.rs +++ b/src/nostr/policy/announcement.rs | |||
| @@ -3,6 +3,8 @@ | |||
| 3 | /// Handles validation of NIP-34 repository announcements (kind 30617) | 3 | /// Handles validation of NIP-34 repository announcements (kind 30617) |
| 4 | /// according to GRASP-01 specification. | 4 | /// according to GRASP-01 specification. |
| 5 | use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag}; | 5 | use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag}; |
| 6 | use std::collections::HashSet; | ||
| 7 | use std::time::Duration; | ||
| 6 | 8 | ||
| 7 | use super::PolicyContext; | 9 | use super::PolicyContext; |
| 8 | use crate::config::Config; | 10 | use crate::config::Config; |
| @@ -11,12 +13,14 @@ use crate::nostr::events::{validate_announcement, RepositoryAnnouncement}; | |||
| 11 | /// Result of announcement policy evaluation | 13 | /// Result of announcement policy evaluation |
| 12 | #[derive(Debug, Clone, PartialEq)] | 14 | #[derive(Debug, Clone, PartialEq)] |
| 13 | pub enum AnnouncementResult { | 15 | pub enum AnnouncementResult { |
| 14 | /// Accept: Event lists our service (GRASP-01 compliant) | 16 | /// Accept: Event lists our service (GRASP-01 compliant) - replacement announcement |
| 15 | Accept, | 17 | Accept, |
| 16 | /// Accept as maintainer: Event accepted via maintainer exception (multi-maintainer) | 18 | /// Accept as maintainer: Event accepted via maintainer exception (multi-maintainer) |
| 17 | AcceptMaintainer, | 19 | AcceptMaintainer, |
| 18 | /// Accept as archive: Event accepted via GRASP-05 archive whitelist (read-only) | 20 | /// Accept as archive: Event accepted via GRASP-05 archive whitelist (read-only) |
| 19 | AcceptArchive, | 21 | AcceptArchive, |
| 22 | /// Accept to purgatory: New announcement, waiting for git data | ||
| 23 | AcceptPurgatory, | ||
| 20 | /// Reject: Event fails validation with reason | 24 | /// Reject: Event fails validation with reason |
| 21 | Reject(String), | 25 | Reject(String), |
| 22 | } | 26 | } |
| @@ -35,10 +39,13 @@ impl AnnouncementPolicy { | |||
| 35 | 39 | ||
| 36 | /// Validate a repository announcement event | 40 | /// Validate a repository announcement event |
| 37 | /// | 41 | /// |
| 38 | /// Returns `Accept` if the announcement lists the service properly, | 42 | /// Returns: |
| 39 | /// `AcceptMaintainer` if accepted via maintainer exception, | 43 | /// - `Accept` if this is a replacement announcement (active announcement exists in DB or |
| 40 | /// `AcceptArchive` if accepted via GRASP-05 archive config, | 44 | /// purgatory) |
| 41 | /// or `Reject` with reason. | 45 | /// - `AcceptPurgatory` if this is a new announcement (no active announcement exists) |
| 46 | /// - `AcceptMaintainer` if accepted via maintainer exception | ||
| 47 | /// - `AcceptArchive` if accepted via GRASP-05 archive config | ||
| 48 | /// - `Reject` with reason if validation fails | ||
| 42 | pub async fn validate(&self, event: &Event) -> AnnouncementResult { | 49 | pub async fn validate(&self, event: &Event) -> AnnouncementResult { |
| 43 | // First, try validation (GRASP-01 + GRASP-05) | 50 | // First, try validation (GRASP-01 + GRASP-05) |
| 44 | let validation_result = validate_announcement(event, &self.config); | 51 | let validation_result = validate_announcement(event, &self.config); |
| @@ -49,6 +56,23 @@ impl AnnouncementPolicy { | |||
| 49 | // GRASP-01 Exception: Accept announcements from recursive maintainers | 56 | // GRASP-01 Exception: Accept announcements from recursive maintainers |
| 50 | match RepositoryAnnouncement::from_event(event.clone()) { | 57 | match RepositoryAnnouncement::from_event(event.clone()) { |
| 51 | Ok(announcement) => { | 58 | Ok(announcement) => { |
| 59 | // If this pubkey+identifier has a purgatory entry AND the incoming | ||
| 60 | // event is strictly newer, the owner is sending a replacement that | ||
| 61 | // removes our service. Clear the purgatory entry and its bare repo. | ||
| 62 | // | ||
| 63 | // If the incoming event is older than the purgatory entry (e.g. a | ||
| 64 | // relay replay of a superseded announcement), ignore it — the newer | ||
| 65 | // purgatory entry takes precedence and must not be evicted. | ||
| 66 | let should_evict = self | ||
| 67 | .ctx | ||
| 68 | .purgatory | ||
| 69 | .find_announcement(&event.pubkey, &announcement.identifier) | ||
| 70 | .is_some_and(|entry| event.created_at > entry.event.created_at); | ||
| 71 | |||
| 72 | if should_evict { | ||
| 73 | self.remove_purgatory_announcement(&event.pubkey, &announcement.identifier); | ||
| 74 | } | ||
| 75 | |||
| 52 | match self | 76 | match self |
| 53 | .is_maintainer_in_any_announcement( | 77 | .is_maintainer_in_any_announcement( |
| 54 | &announcement.identifier, | 78 | &announcement.identifier, |
| @@ -67,11 +91,221 @@ impl AnnouncementPolicy { | |||
| 67 | Err(_) => AnnouncementResult::Reject(reason), | 91 | Err(_) => AnnouncementResult::Reject(reason), |
| 68 | } | 92 | } |
| 69 | } | 93 | } |
| 70 | // Accept, AcceptArchive, or AcceptMaintainer - return as-is | 94 | AnnouncementResult::Accept | AnnouncementResult::AcceptArchive => { |
| 95 | // Parse announcement to check for existing active announcement | ||
| 96 | match RepositoryAnnouncement::from_event(event.clone()) { | ||
| 97 | Ok(announcement) => { | ||
| 98 | let in_db = match self | ||
| 99 | .has_db_announcement(&event.pubkey, &announcement.identifier) | ||
| 100 | .await | ||
| 101 | { | ||
| 102 | Ok(v) => v, | ||
| 103 | Err(e) => { | ||
| 104 | tracing::warn!( | ||
| 105 | error = %e, | ||
| 106 | "Failed to check for existing DB announcement - rejecting" | ||
| 107 | ); | ||
| 108 | return AnnouncementResult::Reject(format!( | ||
| 109 | "Database error checking existing announcement: {}", | ||
| 110 | e | ||
| 111 | )); | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | if in_db { | ||
| 116 | // Replacement announcement with DB entry - accept immediately | ||
| 117 | tracing::debug!( | ||
| 118 | identifier = %announcement.identifier, | ||
| 119 | "Replacement announcement (DB) - accepting immediately" | ||
| 120 | ); | ||
| 121 | return validation_result; | ||
| 122 | } | ||
| 123 | |||
| 124 | let in_purgatory = self | ||
| 125 | .ctx | ||
| 126 | .purgatory | ||
| 127 | .has_purgatory_announcement(&event.pubkey, &announcement.identifier); | ||
| 128 | |||
| 129 | if in_purgatory { | ||
| 130 | // Replacement announcement with purgatory entry - replace it and | ||
| 131 | // extend expiry so the new announcement gets a fresh 30-minute window. | ||
| 132 | tracing::debug!( | ||
| 133 | identifier = %announcement.identifier, | ||
| 134 | "Replacement announcement (purgatory) - replacing purgatory entry" | ||
| 135 | ); | ||
| 136 | self.replace_purgatory_announcement(event, &announcement); | ||
| 137 | // Return Accept (not AcceptPurgatory) - this is a replacement, not new | ||
| 138 | return validation_result; | ||
| 139 | } | ||
| 140 | |||
| 141 | // No existing announcement - route to purgatory | ||
| 142 | tracing::debug!( | ||
| 143 | identifier = %announcement.identifier, | ||
| 144 | "New announcement - routing to purgatory" | ||
| 145 | ); | ||
| 146 | AnnouncementResult::AcceptPurgatory | ||
| 147 | } | ||
| 148 | Err(e) => AnnouncementResult::Reject(format!( | ||
| 149 | "Failed to parse announcement: {}", | ||
| 150 | e | ||
| 151 | )), | ||
| 152 | } | ||
| 153 | } | ||
| 154 | // AcceptPurgatory shouldn't come from validate_announcement, but handle it | ||
| 71 | result => result, | 155 | result => result, |
| 72 | } | 156 | } |
| 73 | } | 157 | } |
| 74 | 158 | ||
| 159 | /// Replace a purgatory announcement entry with a newer event. | ||
| 160 | /// | ||
| 161 | /// Called when a replacement announcement arrives for a (pubkey, identifier) pair | ||
| 162 | /// that is currently in purgatory. Updates the purgatory entry and extends the | ||
| 163 | /// expiry so the new announcement has a fresh waiting window. | ||
| 164 | fn replace_purgatory_announcement( | ||
| 165 | &self, | ||
| 166 | event: &Event, | ||
| 167 | announcement: &RepositoryAnnouncement, | ||
| 168 | ) { | ||
| 169 | let repo_path = self.ctx.git_data_path.join(announcement.repo_path()); | ||
| 170 | let relays: HashSet<String> = announcement.relays.iter().cloned().collect(); | ||
| 171 | |||
| 172 | // add_announcement uses the (owner, identifier) key so it overwrites the old entry | ||
| 173 | self.ctx.purgatory.add_announcement( | ||
| 174 | event.clone(), | ||
| 175 | announcement.identifier.clone(), | ||
| 176 | event.pubkey, | ||
| 177 | repo_path, | ||
| 178 | relays, | ||
| 179 | ); | ||
| 180 | |||
| 181 | // Extend the announcement's expiry (reset to full 30 min window) | ||
| 182 | self.ctx.purgatory.extend_announcement_expiry( | ||
| 183 | &event.pubkey, | ||
| 184 | &announcement.identifier, | ||
| 185 | Duration::from_secs(1800), | ||
| 186 | ); | ||
| 187 | |||
| 188 | // Also extend any state events waiting for this identifier | ||
| 189 | let state_entries = self.ctx.purgatory.find_state(&announcement.identifier); | ||
| 190 | if !state_entries.is_empty() { | ||
| 191 | let state_ids: Vec<_> = state_entries.iter().map(|e| e.event.id).collect(); | ||
| 192 | self.ctx.purgatory.extend_expiry( | ||
| 193 | &announcement.identifier, | ||
| 194 | &state_ids, | ||
| 195 | Duration::from_secs(1800), | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | /// Remove a purgatory announcement and clean up associated resources. | ||
| 201 | /// | ||
| 202 | /// Called when a replacement announcement is rejected (owner removed our service). | ||
| 203 | /// Deletes the bare repository from disk and removes any state events waiting for | ||
| 204 | /// this identifier. | ||
| 205 | fn remove_purgatory_announcement(&self, pubkey: &PublicKey, identifier: &str) { | ||
| 206 | // Get the repo path before removing from purgatory | ||
| 207 | if let Some(entry) = self.ctx.purgatory.find_announcement(pubkey, identifier) { | ||
| 208 | // Delete the bare repository from disk | ||
| 209 | if entry.repo_path.exists() { | ||
| 210 | if let Err(e) = std::fs::remove_dir_all(&entry.repo_path) { | ||
| 211 | tracing::warn!( | ||
| 212 | path = %entry.repo_path.display(), | ||
| 213 | error = %e, | ||
| 214 | "Failed to delete bare repository during purgatory cleanup" | ||
| 215 | ); | ||
| 216 | } else { | ||
| 217 | tracing::info!( | ||
| 218 | path = %entry.repo_path.display(), | ||
| 219 | "Deleted bare repository for rejected purgatory announcement" | ||
| 220 | ); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | // Remove the announcement from purgatory | ||
| 226 | self.ctx.purgatory.remove_announcement(pubkey, identifier); | ||
| 227 | |||
| 228 | // Only remove state events if no other owner still has an announcement in purgatory | ||
| 229 | // for this identifier. State events are keyed by identifier alone, so blindly removing | ||
| 230 | // them would also discard state events legitimately belonging to a different owner's | ||
| 231 | // repository that happens to share the same identifier string. | ||
| 232 | let other_owners_remain = !self | ||
| 233 | .ctx | ||
| 234 | .purgatory | ||
| 235 | .get_announcements_by_identifier(identifier) | ||
| 236 | .is_empty(); | ||
| 237 | |||
| 238 | if !other_owners_remain { | ||
| 239 | self.ctx.purgatory.remove_state(identifier); | ||
| 240 | } | ||
| 241 | |||
| 242 | tracing::info!( | ||
| 243 | identifier = %identifier, | ||
| 244 | other_owners_remain = %other_owners_remain, | ||
| 245 | "Cleared purgatory entry: owner removed our service from announcement" | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | |||
| 249 | /// Check if there's an announcement in the database for this (pubkey, identifier). | ||
| 250 | /// | ||
| 251 | /// Only checks the database (promoted announcements). For purgatory checks use | ||
| 252 | /// `purgatory.has_purgatory_announcement()` directly. | ||
| 253 | async fn has_db_announcement( | ||
| 254 | &self, | ||
| 255 | pubkey: &PublicKey, | ||
| 256 | identifier: &str, | ||
| 257 | ) -> Result<bool, String> { | ||
| 258 | let filter = Filter::new() | ||
| 259 | .kind(Kind::GitRepoAnnouncement) | ||
| 260 | .author(*pubkey) | ||
| 261 | .custom_tag( | ||
| 262 | SingleLetterTag::lowercase(Alphabet::D), | ||
| 263 | identifier.to_string(), | ||
| 264 | ); | ||
| 265 | |||
| 266 | let events: Vec<Event> = match self.ctx.database.query(filter).await { | ||
| 267 | Ok(events) => events.into_iter().collect(), | ||
| 268 | Err(e) => return Err(format!("Database query failed: {}", e)), | ||
| 269 | }; | ||
| 270 | |||
| 271 | Ok(!events.is_empty()) | ||
| 272 | } | ||
| 273 | |||
| 274 | /// Add an announcement to purgatory | ||
| 275 | /// | ||
| 276 | /// Creates the bare repository and stores the announcement in purgatory | ||
| 277 | /// until git data arrives. | ||
| 278 | pub fn add_to_purgatory(&self, event: &Event) -> Result<(), String> { | ||
| 279 | let announcement = RepositoryAnnouncement::from_event(event.clone()) | ||
| 280 | .map_err(|e| format!("Failed to parse announcement: {}", e))?; | ||
| 281 | |||
| 282 | // Create bare repository | ||
| 283 | self.ensure_bare_repository(&announcement)?; | ||
| 284 | |||
| 285 | // Build repo path | ||
| 286 | let repo_path = self.ctx.git_data_path.join(announcement.repo_path()); | ||
| 287 | |||
| 288 | // Extract relays from announcement | ||
| 289 | let relays: HashSet<String> = announcement.relays.iter().cloned().collect(); | ||
| 290 | |||
| 291 | // Add to purgatory | ||
| 292 | self.ctx.purgatory.add_announcement( | ||
| 293 | event.clone(), | ||
| 294 | announcement.identifier.clone(), | ||
| 295 | event.pubkey, | ||
| 296 | repo_path, | ||
| 297 | relays, | ||
| 298 | ); | ||
| 299 | |||
| 300 | tracing::info!( | ||
| 301 | identifier = %announcement.identifier, | ||
| 302 | event_id = %event.id, | ||
| 303 | "Added announcement to purgatory" | ||
| 304 | ); | ||
| 305 | |||
| 306 | Ok(()) | ||
| 307 | } | ||
| 308 | |||
| 75 | /// Create a bare git repository if it doesn't exist | 309 | /// Create a bare git repository if it doesn't exist |
| 76 | /// Path format: <git_data_path>/<npub>/<identifier>.git | 310 | /// Path format: <git_data_path>/<npub>/<identifier>.git |
| 77 | pub fn ensure_bare_repository( | 311 | pub fn ensure_bare_repository( |
| @@ -117,6 +351,11 @@ impl AnnouncementPolicy { | |||
| 117 | /// | 351 | /// |
| 118 | /// This enables accepting announcements from maintainers even when they don't list | 352 | /// This enables accepting announcements from maintainers even when they don't list |
| 119 | /// this GRASP server, for maintainer chain discovery and GRASP-02 sync. | 353 | /// this GRASP server, for maintainer chain discovery and GRASP-02 sync. |
| 354 | /// | ||
| 355 | /// Checks both the database (promoted announcements) and purgatory (announcements | ||
| 356 | /// waiting for git data). This is necessary because a maintainer's announcement | ||
| 357 | /// (which lists the recursive maintainer) may still be in purgatory when the | ||
| 358 | /// recursive maintainer's announcement arrives. | ||
| 120 | async fn is_maintainer_in_any_announcement( | 359 | async fn is_maintainer_in_any_announcement( |
| 121 | &self, | 360 | &self, |
| 122 | identifier: &str, | 361 | identifier: &str, |
| @@ -128,12 +367,26 @@ impl AnnouncementPolicy { | |||
| 128 | identifier.to_string(), | 367 | identifier.to_string(), |
| 129 | ); | 368 | ); |
| 130 | 369 | ||
| 131 | let announcements: Vec<Event> = match self.ctx.database.query(filter).await { | 370 | let db_announcements: Vec<Event> = match self.ctx.database.query(filter).await { |
| 132 | Ok(events) => events.into_iter().collect(), | 371 | Ok(events) => events.into_iter().collect(), |
| 133 | Err(e) => return Err(format!("Database query failed: {}", e)), | 372 | Err(e) => return Err(format!("Database query failed: {}", e)), |
| 134 | }; | 373 | }; |
| 135 | 374 | ||
| 136 | if announcements.is_empty() { | 375 | // Also collect purgatory announcements for this identifier |
| 376 | let purgatory_announcements: Vec<Event> = self | ||
| 377 | .ctx | ||
| 378 | .purgatory | ||
| 379 | .get_announcements_by_identifier(identifier) | ||
| 380 | .into_iter() | ||
| 381 | .map(|entry| entry.event) | ||
| 382 | .collect(); | ||
| 383 | |||
| 384 | let all_announcements: Vec<&Event> = db_announcements | ||
| 385 | .iter() | ||
| 386 | .chain(purgatory_announcements.iter()) | ||
| 387 | .collect(); | ||
| 388 | |||
| 389 | if all_announcements.is_empty() { | ||
| 137 | // No existing announcements for this identifier - author cannot be a maintainer | 390 | // No existing announcements for this identifier - author cannot be a maintainer |
| 138 | return Ok(false); | 391 | return Ok(false); |
| 139 | } | 392 | } |
| @@ -141,14 +394,14 @@ impl AnnouncementPolicy { | |||
| 141 | let author_hex = author.to_hex(); | 394 | let author_hex = author.to_hex(); |
| 142 | 395 | ||
| 143 | // Check each announcement to see if author is listed as a maintainer | 396 | // Check each announcement to see if author is listed as a maintainer |
| 144 | for event in &announcements { | 397 | for event in &all_announcements { |
| 145 | // Check if author is the owner of this announcement | 398 | // Check if author is the owner of this announcement |
| 146 | if event.pubkey == *author { | 399 | if event.pubkey == *author { |
| 147 | return Ok(true); | 400 | return Ok(true); |
| 148 | } | 401 | } |
| 149 | 402 | ||
| 150 | // Check if author is listed in the maintainers tag | 403 | // Check if author is listed in the maintainers tag |
| 151 | if let Ok(announcement) = RepositoryAnnouncement::from_event(event.clone()) { | 404 | if let Ok(announcement) = RepositoryAnnouncement::from_event((*event).clone()) { |
| 152 | if announcement.maintainers.contains(&author_hex) { | 405 | if announcement.maintainers.contains(&author_hex) { |
| 153 | return Ok(true); | 406 | return Ok(true); |
| 154 | } | 407 | } |
diff --git a/src/nostr/policy/deletion.rs b/src/nostr/policy/deletion.rs new file mode 100644 index 0000000..6457c90 --- /dev/null +++ b/src/nostr/policy/deletion.rs | |||
| @@ -0,0 +1,498 @@ | |||
| 1 | /// Deletion Policy - NIP-09 event deletion request handling | ||
| 2 | /// | ||
| 3 | /// Handles kind 5 (EventDeletion) events that request removal of purgatory entries | ||
| 4 | /// for repository announcements (kind 30617) and state events (kind 30618). | ||
| 5 | /// | ||
| 6 | /// ## NIP-09 Rules Enforced | ||
| 7 | /// | ||
| 8 | /// - Only the event author can delete their own events (pubkey must match) | ||
| 9 | /// - `e` tags reference specific event IDs to delete | ||
| 10 | /// - `a` tags reference addressable events by coordinate (`<kind>:<pubkey>:<d-identifier>`) | ||
| 11 | /// - When an `a` tag is used, all versions up to `created_at` of the deletion request | ||
| 12 | /// are considered deleted | ||
| 13 | /// | ||
| 14 | /// ## Purgatory Interaction | ||
| 15 | /// | ||
| 16 | /// - Kind 30617 (announcement) in purgatory: entry removed, bare repo deleted from disk | ||
| 17 | /// - Kind 30618 (state event) in purgatory: matching state event(s) removed by event ID | ||
| 18 | /// or by (author, identifier) coordinate | ||
| 19 | use nostr_relay_builder::prelude::{Event, WritePolicyResult}; | ||
| 20 | |||
| 21 | use super::PolicyContext; | ||
| 22 | |||
| 23 | /// Policy for handling NIP-09 event deletion requests | ||
| 24 | #[derive(Clone)] | ||
| 25 | pub struct DeletionPolicy { | ||
| 26 | ctx: PolicyContext, | ||
| 27 | } | ||
| 28 | |||
| 29 | impl DeletionPolicy { | ||
| 30 | pub fn new(ctx: PolicyContext) -> Self { | ||
| 31 | Self { ctx } | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Process a kind 5 (EventDeletion) event. | ||
| 35 | /// | ||
| 36 | /// Checks whether the deletion request targets any purgatory announcements | ||
| 37 | /// and removes them if so. The deletion event itself is always accepted | ||
| 38 | /// (relays should store deletion requests per NIP-09). | ||
| 39 | /// | ||
| 40 | /// Only the event author can delete their own events — this is enforced by | ||
| 41 | /// checking that the purgatory entry's owner matches `event.pubkey`. | ||
| 42 | pub async fn handle(&self, event: &Event) -> WritePolicyResult { | ||
| 43 | // Process purgatory removals synchronously (no async needed) | ||
| 44 | self.remove_purgatory_targets(event); | ||
| 45 | |||
| 46 | // Always accept the deletion event itself so it is stored and | ||
| 47 | // can prevent re-acceptance of the deleted event in the future. | ||
| 48 | WritePolicyResult::Accept | ||
| 49 | } | ||
| 50 | |||
| 51 | /// Remove any purgatory entries targeted by this deletion event. | ||
| 52 | /// | ||
| 53 | /// Handles both reference styles from NIP-09: | ||
| 54 | /// - `e` tags: event ID references — match against announcement or state event IDs | ||
| 55 | /// - `a` tags: addressable coordinate references — `30617:…` or `30618:…` | ||
| 56 | /// | ||
| 57 | /// Only removes entries where the purgatory entry's author matches the deletion | ||
| 58 | /// event's pubkey (enforces author-only deletion). | ||
| 59 | fn remove_purgatory_targets(&self, event: &Event) { | ||
| 60 | let author = &event.pubkey; | ||
| 61 | |||
| 62 | for tag in event.tags.iter() { | ||
| 63 | let tag_vec = tag.as_slice(); | ||
| 64 | if tag_vec.len() < 2 { | ||
| 65 | continue; | ||
| 66 | } | ||
| 67 | |||
| 68 | match tag_vec[0].as_str() { | ||
| 69 | "e" => { | ||
| 70 | // Event ID reference: find purgatory announcement with this event ID | ||
| 71 | let target_id = &tag_vec[1]; | ||
| 72 | self.remove_by_event_id(author, target_id, event.created_at.as_secs()); | ||
| 73 | } | ||
| 74 | "a" => { | ||
| 75 | // Addressable coordinate reference: `<kind>:<pubkey>:<d-identifier>` | ||
| 76 | let coord = &tag_vec[1]; | ||
| 77 | self.remove_by_coordinate(author, coord, event.created_at.as_secs()); | ||
| 78 | } | ||
| 79 | _ => {} | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | /// Remove a purgatory entry (announcement, state event, or PR event) matched by event ID. | ||
| 85 | /// | ||
| 86 | /// Checks in order: announcements (30617), state events (30618), PR/PR-update events. | ||
| 87 | /// Only removes entries whose author matches `author`. | ||
| 88 | fn remove_by_event_id( | ||
| 89 | &self, | ||
| 90 | author: &nostr_relay_builder::prelude::PublicKey, | ||
| 91 | target_id_hex: &str, | ||
| 92 | _deletion_created_at: u64, | ||
| 93 | ) { | ||
| 94 | // --- Check PR events (kind 1617/1618) first — O(1) direct lookup --- | ||
| 95 | // PR purgatory is keyed by event ID hex, so this is the cheapest check. | ||
| 96 | // Only remove if the entry has an actual event (not a placeholder) and the | ||
| 97 | // event's author matches the deletion request author. | ||
| 98 | if let Some(entry) = self.ctx.purgatory.find_pr(target_id_hex) { | ||
| 99 | if let Some(ref event) = entry.event { | ||
| 100 | if event.pubkey == *author { | ||
| 101 | tracing::info!( | ||
| 102 | event_id = %target_id_hex, | ||
| 103 | author = %author.to_hex(), | ||
| 104 | "Deletion request: removing purgatory PR event by event ID" | ||
| 105 | ); | ||
| 106 | self.ctx.purgatory.remove_pr(target_id_hex); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | // Entry exists but is a placeholder or wrong author — don't remove | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | // --- Check announcements (kind 30617) --- | ||
| 115 | // The DashMap doesn't expose a direct "find by event ID" method, so we use | ||
| 116 | // the announcements_for_sync snapshot to enumerate all (repo_id, _) pairs. | ||
| 117 | let all = self.ctx.purgatory.announcements_for_sync(); | ||
| 118 | for (repo_id, _) in all { | ||
| 119 | // repo_id format: "30617:{pubkey_hex}:{identifier}" | ||
| 120 | let parts: Vec<&str> = repo_id.splitn(3, ':').collect(); | ||
| 121 | if parts.len() != 3 { | ||
| 122 | continue; | ||
| 123 | } | ||
| 124 | let entry_pubkey_hex = parts[1]; | ||
| 125 | let identifier = parts[2]; | ||
| 126 | |||
| 127 | if entry_pubkey_hex != author.to_hex() { | ||
| 128 | continue; | ||
| 129 | } | ||
| 130 | |||
| 131 | if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { | ||
| 132 | if entry.event.id.to_hex() == target_id_hex { | ||
| 133 | tracing::info!( | ||
| 134 | event_id = %target_id_hex, | ||
| 135 | identifier = %identifier, | ||
| 136 | author = %author.to_hex(), | ||
| 137 | "Deletion request: removing purgatory announcement by event ID" | ||
| 138 | ); | ||
| 139 | self.evict_purgatory_entry(author, identifier); | ||
| 140 | return; // event IDs are unique | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | // --- Check state events (kind 30618) --- | ||
| 146 | // State events are keyed by identifier; scan all identifiers for a match. | ||
| 147 | let state_identifiers = self.ctx.purgatory.get_all_identifiers(); | ||
| 148 | for identifier in state_identifiers { | ||
| 149 | let entries = self.ctx.purgatory.find_state(&identifier); | ||
| 150 | for entry in entries { | ||
| 151 | if entry.author == *author && entry.event.id.to_hex() == target_id_hex { | ||
| 152 | tracing::info!( | ||
| 153 | event_id = %target_id_hex, | ||
| 154 | identifier = %identifier, | ||
| 155 | author = %author.to_hex(), | ||
| 156 | "Deletion request: removing purgatory state event by event ID" | ||
| 157 | ); | ||
| 158 | self.ctx.purgatory.remove_state_event(&identifier, &entry.event.id); | ||
| 159 | return; // event IDs are unique | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | /// Remove a purgatory entry matched by addressable coordinate. | ||
| 166 | /// | ||
| 167 | /// The coordinate format is `<kind>:<pubkey>:<d-identifier>`. | ||
| 168 | /// Handles kind 30617 (announcements) and kind 30618 (state events). | ||
| 169 | /// | ||
| 170 | /// Per NIP-09, all versions up to `deletion_created_at` are considered deleted. | ||
| 171 | fn remove_by_coordinate( | ||
| 172 | &self, | ||
| 173 | author: &nostr_relay_builder::prelude::PublicKey, | ||
| 174 | coordinate: &str, | ||
| 175 | deletion_created_at: u64, | ||
| 176 | ) { | ||
| 177 | // Parse coordinate: `<kind>:<pubkey>:<d-identifier>` | ||
| 178 | let parts: Vec<&str> = coordinate.splitn(3, ':').collect(); | ||
| 179 | if parts.len() != 3 { | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | |||
| 183 | let kind_str = parts[0]; | ||
| 184 | let coord_pubkey_hex = parts[1]; | ||
| 185 | let identifier = parts[2]; | ||
| 186 | |||
| 187 | // The coordinate pubkey must match the deletion event author | ||
| 188 | if coord_pubkey_hex != author.to_hex() { | ||
| 189 | tracing::debug!( | ||
| 190 | coord_pubkey = %coord_pubkey_hex, | ||
| 191 | deletion_author = %author.to_hex(), | ||
| 192 | "Ignoring deletion: coordinate pubkey does not match deletion author" | ||
| 193 | ); | ||
| 194 | return; | ||
| 195 | } | ||
| 196 | |||
| 197 | match kind_str { | ||
| 198 | "30617" => { | ||
| 199 | // Announcement purgatory entry | ||
| 200 | if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { | ||
| 201 | if entry.event.created_at.as_secs() <= deletion_created_at { | ||
| 202 | tracing::info!( | ||
| 203 | identifier = %identifier, | ||
| 204 | author = %author.to_hex(), | ||
| 205 | "Deletion request: removing purgatory announcement by coordinate" | ||
| 206 | ); | ||
| 207 | self.evict_purgatory_entry(author, identifier); | ||
| 208 | } else { | ||
| 209 | tracing::debug!( | ||
| 210 | identifier = %identifier, | ||
| 211 | author = %author.to_hex(), | ||
| 212 | "Ignoring deletion: purgatory announcement is newer than deletion request" | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | "30618" => { | ||
| 218 | // State event purgatory entries for this (author, identifier). | ||
| 219 | // Remove all entries authored by `author` with created_at ≤ deletion_created_at. | ||
| 220 | let entries = self.ctx.purgatory.find_state(identifier); | ||
| 221 | let mut removed = 0usize; | ||
| 222 | for entry in entries { | ||
| 223 | if entry.author == *author | ||
| 224 | && entry.event.created_at.as_secs() <= deletion_created_at | ||
| 225 | { | ||
| 226 | self.ctx.purgatory.remove_state_event(identifier, &entry.event.id); | ||
| 227 | removed += 1; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | if removed > 0 { | ||
| 231 | tracing::info!( | ||
| 232 | identifier = %identifier, | ||
| 233 | author = %author.to_hex(), | ||
| 234 | removed = %removed, | ||
| 235 | "Deletion request: removed purgatory state event(s) by coordinate" | ||
| 236 | ); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | _ => { | ||
| 240 | // Other kinds not handled | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | /// Remove a purgatory announcement and delete its bare repository from disk. | ||
| 246 | fn evict_purgatory_entry( | ||
| 247 | &self, | ||
| 248 | author: &nostr_relay_builder::prelude::PublicKey, | ||
| 249 | identifier: &str, | ||
| 250 | ) { | ||
| 251 | // Get repo path before removing | ||
| 252 | if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { | ||
| 253 | if entry.repo_path.exists() { | ||
| 254 | if let Err(e) = std::fs::remove_dir_all(&entry.repo_path) { | ||
| 255 | tracing::warn!( | ||
| 256 | path = %entry.repo_path.display(), | ||
| 257 | error = %e, | ||
| 258 | "Failed to delete bare repository during deletion request processing" | ||
| 259 | ); | ||
| 260 | } else { | ||
| 261 | tracing::info!( | ||
| 262 | path = %entry.repo_path.display(), | ||
| 263 | "Deleted bare repository for deletion-requested purgatory announcement" | ||
| 264 | ); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | self.ctx.purgatory.remove_announcement(author, identifier); | ||
| 270 | |||
| 271 | // Remove state events for this identifier only if no other owner's | ||
| 272 | // announcement remains in purgatory (state events are keyed by identifier alone) | ||
| 273 | let other_owners_remain = !self | ||
| 274 | .ctx | ||
| 275 | .purgatory | ||
| 276 | .get_announcements_by_identifier(identifier) | ||
| 277 | .is_empty(); | ||
| 278 | |||
| 279 | if !other_owners_remain { | ||
| 280 | self.ctx.purgatory.remove_state(identifier); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | #[cfg(test)] | ||
| 286 | mod tests { | ||
| 287 | use super::*; | ||
| 288 | use crate::nostr::policy::PolicyContext; | ||
| 289 | use crate::purgatory::Purgatory; | ||
| 290 | use nostr_relay_builder::prelude::*; | ||
| 291 | use std::collections::HashSet; | ||
| 292 | use std::path::PathBuf; | ||
| 293 | use std::sync::Arc; | ||
| 294 | |||
| 295 | fn make_context() -> PolicyContext { | ||
| 296 | let db = Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { | ||
| 297 | events: true, | ||
| 298 | max_events: None, | ||
| 299 | })); | ||
| 300 | let purgatory = Arc::new(Purgatory::new(PathBuf::new())); | ||
| 301 | let config = crate::config::Config::for_testing(); | ||
| 302 | PolicyContext::new("test.example.com", db, PathBuf::new(), purgatory, config) | ||
| 303 | } | ||
| 304 | |||
| 305 | fn make_announcement_event(keys: &Keys, identifier: &str) -> Event { | ||
| 306 | EventBuilder::new(Kind::GitRepoAnnouncement, "") | ||
| 307 | .tags(vec![ | ||
| 308 | Tag::identifier(identifier), | ||
| 309 | Tag::custom(TagKind::custom("clone"), vec!["https://example.com/repo.git"]), | ||
| 310 | ]) | ||
| 311 | .sign_with_keys(keys) | ||
| 312 | .unwrap() | ||
| 313 | } | ||
| 314 | |||
| 315 | fn add_to_purgatory(ctx: &PolicyContext, event: &Event, identifier: &str) { | ||
| 316 | ctx.purgatory.add_announcement( | ||
| 317 | event.clone(), | ||
| 318 | identifier.to_string(), | ||
| 319 | event.pubkey, | ||
| 320 | PathBuf::new(), | ||
| 321 | HashSet::new(), | ||
| 322 | ); | ||
| 323 | } | ||
| 324 | |||
| 325 | #[tokio::test] | ||
| 326 | async fn test_deletion_by_event_id_removes_purgatory_entry() { | ||
| 327 | let ctx = make_context(); | ||
| 328 | let keys = Keys::generate(); | ||
| 329 | let identifier = "my-repo"; | ||
| 330 | |||
| 331 | let announcement = make_announcement_event(&keys, identifier); | ||
| 332 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 333 | |||
| 334 | assert!(ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier)); | ||
| 335 | |||
| 336 | // Build kind 5 deletion event referencing the announcement by event ID | ||
| 337 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 338 | .tags(vec![ | ||
| 339 | Tag::event(announcement.id), | ||
| 340 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 341 | ]) | ||
| 342 | .sign_with_keys(&keys) | ||
| 343 | .unwrap(); | ||
| 344 | |||
| 345 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 346 | let result = policy.handle(&deletion).await; | ||
| 347 | |||
| 348 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 349 | assert!( | ||
| 350 | !ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), | ||
| 351 | "Purgatory entry should have been removed" | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | |||
| 355 | #[tokio::test] | ||
| 356 | async fn test_deletion_by_coordinate_removes_purgatory_entry() { | ||
| 357 | let ctx = make_context(); | ||
| 358 | let keys = Keys::generate(); | ||
| 359 | let identifier = "my-repo"; | ||
| 360 | |||
| 361 | let announcement = make_announcement_event(&keys, identifier); | ||
| 362 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 363 | |||
| 364 | assert!(ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier)); | ||
| 365 | |||
| 366 | // Build kind 5 deletion event referencing the announcement by coordinate | ||
| 367 | let coord = format!("30617:{}:{}", keys.public_key().to_hex(), identifier); | ||
| 368 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 369 | .tags(vec![ | ||
| 370 | Tag::custom(TagKind::custom("a"), vec![coord]), | ||
| 371 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 372 | ]) | ||
| 373 | .sign_with_keys(&keys) | ||
| 374 | .unwrap(); | ||
| 375 | |||
| 376 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 377 | let result = policy.handle(&deletion).await; | ||
| 378 | |||
| 379 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 380 | assert!( | ||
| 381 | !ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), | ||
| 382 | "Purgatory entry should have been removed" | ||
| 383 | ); | ||
| 384 | } | ||
| 385 | |||
| 386 | #[tokio::test] | ||
| 387 | async fn test_deletion_by_wrong_author_does_not_remove() { | ||
| 388 | let ctx = make_context(); | ||
| 389 | let owner_keys = Keys::generate(); | ||
| 390 | let attacker_keys = Keys::generate(); | ||
| 391 | let identifier = "my-repo"; | ||
| 392 | |||
| 393 | let announcement = make_announcement_event(&owner_keys, identifier); | ||
| 394 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 395 | |||
| 396 | // Attacker tries to delete by event ID | ||
| 397 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 398 | .tags(vec![ | ||
| 399 | Tag::event(announcement.id), | ||
| 400 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 401 | ]) | ||
| 402 | .sign_with_keys(&attacker_keys) | ||
| 403 | .unwrap(); | ||
| 404 | |||
| 405 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 406 | let result = policy.handle(&deletion).await; | ||
| 407 | |||
| 408 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 409 | assert!( | ||
| 410 | ctx.purgatory.has_purgatory_announcement(&owner_keys.public_key(), identifier), | ||
| 411 | "Purgatory entry should NOT have been removed by wrong author" | ||
| 412 | ); | ||
| 413 | } | ||
| 414 | |||
| 415 | #[tokio::test] | ||
| 416 | async fn test_deletion_by_coordinate_wrong_author_does_not_remove() { | ||
| 417 | let ctx = make_context(); | ||
| 418 | let owner_keys = Keys::generate(); | ||
| 419 | let attacker_keys = Keys::generate(); | ||
| 420 | let identifier = "my-repo"; | ||
| 421 | |||
| 422 | let announcement = make_announcement_event(&owner_keys, identifier); | ||
| 423 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 424 | |||
| 425 | // Attacker tries to delete by coordinate using owner's pubkey in coord | ||
| 426 | // but signs with their own key — coord pubkey != deletion author | ||
| 427 | let coord = format!("30617:{}:{}", owner_keys.public_key().to_hex(), identifier); | ||
| 428 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 429 | .tags(vec![ | ||
| 430 | Tag::custom(TagKind::custom("a"), vec![coord]), | ||
| 431 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 432 | ]) | ||
| 433 | .sign_with_keys(&attacker_keys) | ||
| 434 | .unwrap(); | ||
| 435 | |||
| 436 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 437 | let result = policy.handle(&deletion).await; | ||
| 438 | |||
| 439 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 440 | assert!( | ||
| 441 | ctx.purgatory.has_purgatory_announcement(&owner_keys.public_key(), identifier), | ||
| 442 | "Purgatory entry should NOT have been removed by wrong author" | ||
| 443 | ); | ||
| 444 | } | ||
| 445 | |||
| 446 | #[tokio::test] | ||
| 447 | async fn test_deletion_of_nonexistent_entry_is_accepted() { | ||
| 448 | let ctx = make_context(); | ||
| 449 | let keys = Keys::generate(); | ||
| 450 | |||
| 451 | // No purgatory entry exists — deletion should still be accepted | ||
| 452 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 453 | .tags(vec![ | ||
| 454 | Tag::custom(TagKind::custom("a"), vec![ | ||
| 455 | format!("30617:{}:nonexistent", keys.public_key().to_hex()) | ||
| 456 | ]), | ||
| 457 | ]) | ||
| 458 | .sign_with_keys(&keys) | ||
| 459 | .unwrap(); | ||
| 460 | |||
| 461 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 462 | let result = policy.handle(&deletion).await; | ||
| 463 | |||
| 464 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 465 | } | ||
| 466 | |||
| 467 | #[tokio::test] | ||
| 468 | async fn test_deletion_by_coordinate_respects_created_at() { | ||
| 469 | let ctx = make_context(); | ||
| 470 | let keys = Keys::generate(); | ||
| 471 | let identifier = "my-repo"; | ||
| 472 | |||
| 473 | // Create announcement with a future timestamp | ||
| 474 | let future_ts = Timestamp::now().as_secs() + 3600; // 1 hour in the future | ||
| 475 | let announcement = EventBuilder::new(Kind::GitRepoAnnouncement, "") | ||
| 476 | .tags(vec![Tag::identifier(identifier)]) | ||
| 477 | .custom_created_at(Timestamp::from(future_ts)) | ||
| 478 | .sign_with_keys(&keys) | ||
| 479 | .unwrap(); | ||
| 480 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 481 | |||
| 482 | // Deletion event with current timestamp (older than announcement) | ||
| 483 | let coord = format!("30617:{}:{}", keys.public_key().to_hex(), identifier); | ||
| 484 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 485 | .tags(vec![Tag::custom(TagKind::custom("a"), vec![coord])]) | ||
| 486 | .sign_with_keys(&keys) | ||
| 487 | .unwrap(); | ||
| 488 | |||
| 489 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 490 | let result = policy.handle(&deletion).await; | ||
| 491 | |||
| 492 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 493 | assert!( | ||
| 494 | ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), | ||
| 495 | "Purgatory entry should NOT be removed: entry is newer than deletion request" | ||
| 496 | ); | ||
| 497 | } | ||
| 498 | } | ||
diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index 1566b6c..f5b981a 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs | |||
| @@ -6,11 +6,13 @@ | |||
| 6 | /// - `PrEventPolicy` - PR/PR Update validation | 6 | /// - `PrEventPolicy` - PR/PR Update validation |
| 7 | /// - `RelatedEventPolicy` - Forward/backward reference checking | 7 | /// - `RelatedEventPolicy` - Forward/backward reference checking |
| 8 | mod announcement; | 8 | mod announcement; |
| 9 | mod deletion; | ||
| 9 | mod pr_event; | 10 | mod pr_event; |
| 10 | mod related; | 11 | mod related; |
| 11 | mod state; | 12 | mod state; |
| 12 | 13 | ||
| 13 | pub use announcement::{AnnouncementPolicy, AnnouncementResult}; | 14 | pub use announcement::{AnnouncementPolicy, AnnouncementResult}; |
| 15 | pub use deletion::DeletionPolicy; | ||
| 14 | pub use pr_event::PrEventPolicy; | 16 | pub use pr_event::PrEventPolicy; |
| 15 | pub use related::{ReferenceResult, RelatedEventPolicy}; | 17 | pub use related::{ReferenceResult, RelatedEventPolicy}; |
| 16 | pub use state::{StatePolicy, StateResult}; | 18 | pub use state::{StatePolicy, StateResult}; |
diff --git a/src/nostr/policy/pr_event.rs b/src/nostr/policy/pr_event.rs index 00e09c3..072e445 100644 --- a/src/nostr/policy/pr_event.rs +++ b/src/nostr/policy/pr_event.rs | |||
| @@ -127,6 +127,10 @@ impl PrEventPolicy { | |||
| 127 | .ok_or_else(|| anyhow::anyhow!("No identifier in PR event"))?; | 127 | .ok_or_else(|| anyhow::anyhow!("No identifier in PR event"))?; |
| 128 | 128 | ||
| 129 | // Fetch repository data | 129 | // Fetch repository data |
| 130 | // NOTE: Only fetch from database, NOT purgatory. Incoming PR events should | ||
| 131 | // only be accepted for announcements that have been promoted (validated). | ||
| 132 | // If the announcement is still in purgatory, the PR event should also go | ||
| 133 | // to purgatory and wait for the announcement to be promoted. | ||
| 130 | let db_repo_data = fetch_repository_data(&self.ctx.database, &identifier).await?; | 134 | let db_repo_data = fetch_repository_data(&self.ctx.database, &identifier).await?; |
| 131 | 135 | ||
| 132 | // Extract owner pubkey from source repo path | 136 | // Extract owner pubkey from source repo path |
| @@ -203,6 +207,10 @@ impl PrEventPolicy { | |||
| 203 | let identifier = parts[2]; | 207 | let identifier = parts[2]; |
| 204 | 208 | ||
| 205 | // 2. Fetch repo data | 209 | // 2. Fetch repo data |
| 210 | // NOTE: Only fetch from database, NOT purgatory. Incoming PR events should | ||
| 211 | // only be accepted for announcements that have been promoted (validated). | ||
| 212 | // If the announcement is still in purgatory, the PR event should also go | ||
| 213 | // to purgatory and wait for the announcement to be promoted. | ||
| 206 | let db_repo_data = fetch_repository_data(&self.ctx.database, identifier).await?; | 214 | let db_repo_data = fetch_repository_data(&self.ctx.database, identifier).await?; |
| 207 | 215 | ||
| 208 | // 3. Extract list of maintainers from "a 30617:<maintainer>:<identifier>" tags | 216 | // 3. Extract list of maintainers from "a 30617:<maintainer>:<identifier>" tags |
diff --git a/src/nostr/policy/related.rs b/src/nostr/policy/related.rs index 7ce87db..cfe04a7 100644 --- a/src/nostr/policy/related.rs +++ b/src/nostr/policy/related.rs | |||
| @@ -139,6 +139,11 @@ impl RelatedEventPolicy { | |||
| 139 | .push((addr, pubkey, identifier)); | 139 | .push((addr, pubkey, identifier)); |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | // NOTE: Intentionally only checks the database (promoted announcements), not purgatory. | ||
| 143 | // Related events should only be accepted once the repository announcement has been | ||
| 144 | // validated (promoted via git data). Events referencing purgatory-only repositories | ||
| 145 | // are correctly rejected as orphans and can be re-submitted after promotion. | ||
| 146 | |||
| 142 | // Query each kind group | 147 | // Query each kind group |
| 143 | for (kind, refs) in by_kind { | 148 | for (kind, refs) in by_kind { |
| 144 | let authors: Vec<PublicKey> = refs.iter().map(|(_, pk, _)| *pk).collect(); | 149 | let authors: Vec<PublicKey> = refs.iter().map(|(_, pk, _)| *pk).collect(); |
diff --git a/src/nostr/policy/state.rs b/src/nostr/policy/state.rs index 3411077..df743ae 100644 --- a/src/nostr/policy/state.rs +++ b/src/nostr/policy/state.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | use std::collections::HashSet; | ||
| 1 | use std::path::{Path, PathBuf}; | 2 | use std::path::{Path, PathBuf}; |
| 2 | 3 | ||
| 3 | use anyhow::{Context, Result}; | 4 | use anyhow::{Context, Result}; |
| @@ -10,7 +11,7 @@ use nostr_relay_builder::prelude::Event; | |||
| 10 | 11 | ||
| 11 | use super::PolicyContext; | 12 | use super::PolicyContext; |
| 12 | use crate::git; | 13 | use crate::git; |
| 13 | use crate::git::authorization::fetch_repository_data; | 14 | use crate::git::authorization::fetch_repository_data_with_purgatory; |
| 14 | use crate::nostr::events::{validate_state, RepositoryAnnouncement, RepositoryState}; | 15 | use crate::nostr::events::{validate_state, RepositoryAnnouncement, RepositoryState}; |
| 15 | 16 | ||
| 16 | /// Result of state policy evaluation | 17 | /// Result of state policy evaluation |
| @@ -76,7 +77,13 @@ impl StatePolicy { | |||
| 76 | } | 77 | } |
| 77 | 78 | ||
| 78 | // Get all repositories and state events from db with identifier | 79 | // Get all repositories and state events from db with identifier |
| 79 | let db_repo_data = fetch_repository_data(&self.ctx.database, &state.identifier).await?; | 80 | // Include purgatory announcements for authorization |
| 81 | let db_repo_data = fetch_repository_data_with_purgatory( | ||
| 82 | &self.ctx.database, | ||
| 83 | &self.ctx.purgatory, | ||
| 84 | &state.identifier, | ||
| 85 | ) | ||
| 86 | .await?; | ||
| 80 | 87 | ||
| 81 | // CRITICAL: Check if author is authorized via maintainer set | 88 | // CRITICAL: Check if author is authorized via maintainer set |
| 82 | // State events MUST be rejected if author is not in maintainer set of any accepted announcement | 89 | // State events MUST be rejected if author is not in maintainer set of any accepted announcement |
| @@ -139,6 +146,34 @@ impl StatePolicy { | |||
| 139 | "State event author authorized via maintainer set" | 146 | "State event author authorized via maintainer set" |
| 140 | ); | 147 | ); |
| 141 | 148 | ||
| 149 | // Extend expiry for any purgatory announcements for this identifier. | ||
| 150 | // | ||
| 151 | // Per design doc decision #4: state event arrival extends the purgatory | ||
| 152 | // announcement's expiry (reset the 30-minute protocol timer). This prevents | ||
| 153 | // premature expiry during slow sync operations — the repo is actively receiving | ||
| 154 | // metadata so it should stay alive. | ||
| 155 | // | ||
| 156 | // We extend for all owners that authorized this state event, since the state | ||
| 157 | // event proves the repo is active regardless of which owner's announcement | ||
| 158 | // authorized it. | ||
| 159 | for owner_hex in &authorized_owners { | ||
| 160 | if let Ok(owner_pk) = nostr_sdk::PublicKey::from_hex(owner_hex) { | ||
| 161 | if self.ctx.purgatory.has_purgatory_announcement(&owner_pk, &state.identifier) { | ||
| 162 | self.ctx.purgatory.extend_announcement_expiry( | ||
| 163 | &owner_pk, | ||
| 164 | &state.identifier, | ||
| 165 | std::time::Duration::from_secs(1800), | ||
| 166 | ); | ||
| 167 | tracing::debug!( | ||
| 168 | event_id = %event.id, | ||
| 169 | identifier = %state.identifier, | ||
| 170 | owner = %owner_hex, | ||
| 171 | "Extended purgatory announcement expiry due to state event arrival" | ||
| 172 | ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 142 | // Duplicate check in db | 177 | // Duplicate check in db |
| 143 | if db_repo_data.states.iter().any(|e| e.event.id.eq(&event.id)) { | 178 | if db_repo_data.states.iter().any(|e| e.event.id.eq(&event.id)) { |
| 144 | tracing::debug!("processed state event duplicate (in db): {}", event.id); | 179 | tracing::debug!("processed state event duplicate (in db): {}", event.id); |
| @@ -186,6 +221,42 @@ impl StatePolicy { | |||
| 186 | } | 221 | } |
| 187 | } | 222 | } |
| 188 | 223 | ||
| 224 | // After copying OIDs to other owner repos, promote any purgatory announcements | ||
| 225 | // for those repos. This handles the case where two maintainers push to the same | ||
| 226 | // identifier on the same relay with identical commit hashes: the second maintainer's | ||
| 227 | // announcement sits in purgatory, and when their state event arrives the relay copies | ||
| 228 | // commits from the first maintainer's repo — but without this call the announcement | ||
| 229 | // would stay in purgatory indefinitely. | ||
| 230 | let local_relay = self.ctx.get_local_relay(); | ||
| 231 | let empty_oids: HashSet<String> = HashSet::new(); | ||
| 232 | for announcement in &db_repo_data.announcements { | ||
| 233 | let target_repo_path = self.ctx.git_data_path.join(announcement.repo_path()); | ||
| 234 | if target_repo_path != repo_with_git_data { | ||
| 235 | // OIDs were copied to this repo by process_state_with_git_data; | ||
| 236 | // check if there's a purgatory announcement waiting for it. | ||
| 237 | if let Err(e) = crate::git::sync::process_newly_available_git_data( | ||
| 238 | &target_repo_path, | ||
| 239 | &empty_oids, | ||
| 240 | &self.ctx.database, | ||
| 241 | local_relay.as_ref(), | ||
| 242 | &self.ctx.purgatory, | ||
| 243 | &self.ctx.git_data_path, | ||
| 244 | None, | ||
| 245 | None, | ||
| 246 | ) | ||
| 247 | .await | ||
| 248 | { | ||
| 249 | tracing::warn!( | ||
| 250 | identifier = %state.identifier, | ||
| 251 | event_id = %event.id, | ||
| 252 | repo_path = %target_repo_path.display(), | ||
| 253 | error = %e, | ||
| 254 | "Failed to process purgatory announcements for target repo after git sync copy" | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 189 | // Event will be saved and broadcast by relay builder | 260 | // Event will be saved and broadcast by relay builder |
| 190 | Ok(WritePolicyResult::Accept) | 261 | Ok(WritePolicyResult::Accept) |
| 191 | } else { | 262 | } else { |