From 5ecd8d6a434f97da94daef2f59166086fbaf5a6b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 9 Jan 2026 17:04:06 +0000 Subject: feat: implement state event authorization per GRASP-01 spec Add comprehensive authorization checks to ensure state events are only accepted from maintainers of accepted repository announcements. This implements the core GRASP-01 requirement that pushes must match the latest state announcement "respecting the maintainer set." Changes: 1. StatePolicy authorization (src/nostr/policy/state.rs): - Check authorization BEFORE git data validation (fail-fast) - Reject if no announcement exists for repository - Reject if author not in maintainer set - Use existing helpers: fetch_repository_data() and pubkey_authorised_for_repo_owners() - Structured logging for all rejections 2. Purgatory invalidation (src/nostr/builder.rs): - New method: check_purgatory_state_events_for_identifier() - Called when announcements accepted (Accept and AcceptMaintainer) - Re-evaluates state events in purgatory for the identifier - Processes newly-authorized events (releases from purgatory) - Keeps unauthorized events for natural expiry (30 min) - Enables retroactive authorization when announcements arrive late 3. Purgatory sync authorization (src/git/sync.rs): - Check authorization BEFORE processing git data - Remove unauthorized events from purgatory (permanent rejection) - Prevents processing even if git data arrives first - Structured logging for monitoring 4. Rejected events tracking (src/sync/rejected_index.rs): - Add support for tracking rejected state events - New methods: add_state(), contains_state() - Separate metrics for state rejections - Enables sync to avoid re-fetching rejected states 5. Sync metrics (src/sync/metrics.rs, src/sync/mod.rs): - Add state-specific metrics (hot cache, cold index) - Track rejected states separately from announcements - Support monitoring of authorization rejections 6. Comprehensive tests (tests/state_authorization.rs): - test_reject_state_without_announcement - test_reject_state_from_unauthorized_author - test_accept_state_from_announcement_author - test_accept_state_from_maintainer Security Impact: - Before: State events could be published by anyone - After: Only maintainers can publish state events - Defense-in-depth: Authorization checked at 3 points: 1. On arrival (StatePolicy) 2. On announcement acceptance (purgatory re-evaluation) 3. On git data arrival (purgatory sync) All tests pass: - 248 unit tests - 51 NIP-34 announcement tests - 4 new state authorization tests - 9 rejected index tests Closes: State authorization requirement from GRASP-01 spec --- src/sync/metrics.rs | 103 ++++++++++++++++++++++++++++++++++++++++++ src/sync/mod.rs | 40 ++++++++++++++++- src/sync/rejected_index.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 249 insertions(+), 3 deletions(-) (limited to 'src/sync') diff --git a/src/sync/metrics.rs b/src/sync/metrics.rs index a175210..7d6d42d 100644 --- a/src/sync/metrics.rs +++ b/src/sync/metrics.rs @@ -56,6 +56,22 @@ pub struct SyncMetrics { rejected_announcements_cold_index_expired_total: IntCounter, /// Total invalidations (maintainer announcements invalidated) rejected_announcements_invalidated_total: IntCounter, + + // === Rejected States Index Metrics === + /// Current number of state events in hot cache + rejected_states_hot_cache_current: IntGauge, + /// Total hot cache hits (state events re-processed from cache) + rejected_states_hot_cache_hits_total: IntCounter, + /// Total hot cache misses (state events not in cache) + rejected_states_hot_cache_misses_total: IntCounter, + /// Total expired state events removed from hot cache + rejected_states_hot_cache_expired_total: IntCounter, + /// Current number of state event entries in cold index + rejected_states_cold_index_current: IntGauge, + /// Total state event cold index entries expired and removed + rejected_states_cold_index_expired_total: IntCounter, + /// Total state event invalidations + rejected_states_invalidated_total: IntCounter, } impl SyncMetrics { @@ -172,6 +188,49 @@ impl SyncMetrics { ))?; registry.register(Box::new(rejected_announcements_invalidated_total.clone()))?; + // Rejected states metrics + let rejected_states_hot_cache_current = IntGauge::with_opts(Opts::new( + "ngit_sync_rejected_states_hot_cache_current", + "Current number of state events in hot cache (full events, 2 min expiry)", + ))?; + registry.register(Box::new(rejected_states_hot_cache_current.clone()))?; + + let rejected_states_hot_cache_hits_total = IntCounter::with_opts(Opts::new( + "ngit_sync_rejected_states_hot_cache_hits_total", + "Total hot cache hits (state events re-processed from cache)", + ))?; + registry.register(Box::new(rejected_states_hot_cache_hits_total.clone()))?; + + let rejected_states_hot_cache_misses_total = IntCounter::with_opts(Opts::new( + "ngit_sync_rejected_states_hot_cache_misses_total", + "Total hot cache misses (state events not in cache when invalidated)", + ))?; + registry.register(Box::new(rejected_states_hot_cache_misses_total.clone()))?; + + let rejected_states_hot_cache_expired_total = IntCounter::with_opts(Opts::new( + "ngit_sync_rejected_states_hot_cache_expired_total", + "Total expired state events removed from hot cache", + ))?; + registry.register(Box::new(rejected_states_hot_cache_expired_total.clone()))?; + + let rejected_states_cold_index_current = IntGauge::with_opts(Opts::new( + "ngit_sync_rejected_states_cold_index_current", + "Current number of state event entries in cold index (metadata only, 7 day expiry)", + ))?; + registry.register(Box::new(rejected_states_cold_index_current.clone()))?; + + let rejected_states_cold_index_expired_total = IntCounter::with_opts(Opts::new( + "ngit_sync_rejected_states_cold_index_expired_total", + "Total state event cold index entries expired and removed", + ))?; + registry.register(Box::new(rejected_states_cold_index_expired_total.clone()))?; + + let rejected_states_invalidated_total = IntCounter::with_opts(Opts::new( + "ngit_sync_rejected_states_invalidated_total", + "Total state event invalidations (when announcements accepted)", + ))?; + registry.register(Box::new(rejected_states_invalidated_total.clone()))?; + Ok(Self { relay_connected, connection_attempts_total, @@ -188,6 +247,13 @@ impl SyncMetrics { rejected_announcements_cold_index_current, rejected_announcements_cold_index_expired_total, rejected_announcements_invalidated_total, + rejected_states_hot_cache_current, + rejected_states_hot_cache_hits_total, + rejected_states_hot_cache_misses_total, + rejected_states_hot_cache_expired_total, + rejected_states_cold_index_current, + rejected_states_cold_index_expired_total, + rejected_states_invalidated_total, }) } @@ -396,6 +462,43 @@ impl SyncMetrics { pub fn record_invalidation(&self, count: usize) { self.rejected_announcements_invalidated_total.inc_by(count as u64); } + + // === Rejected States Recording Methods === + + /// Update state events hot cache current size gauge. + pub fn update_states_hot_cache_size(&self, size: usize) { + self.rejected_states_hot_cache_current.set(size as i64); + } + + /// Record state event hot cache hit (event re-processed from cache). + pub fn record_states_hot_cache_hit(&self) { + self.rejected_states_hot_cache_hits_total.inc(); + } + + /// Record state event hot cache miss (event not in cache when invalidated). + pub fn record_states_hot_cache_miss(&self) { + self.rejected_states_hot_cache_misses_total.inc(); + } + + /// Record state event hot cache expired entries. + pub fn record_states_hot_cache_expired(&self, count: usize) { + self.rejected_states_hot_cache_expired_total.inc_by(count as u64); + } + + /// Update state events cold index current size gauge. + pub fn update_states_cold_index_size(&self, size: usize) { + self.rejected_states_cold_index_current.set(size as i64); + } + + /// Record state event cold index expired entries. + pub fn record_states_cold_index_expired(&self, count: usize) { + self.rejected_states_cold_index_expired_total.inc_by(count as u64); + } + + /// Record state event invalidation. + pub fn record_states_invalidation(&self, count: usize) { + self.rejected_states_invalidated_total.inc_by(count as u64); + } } #[cfg(test)] diff --git a/src/sync/mod.rs b/src/sync/mod.rs index f296c0f..93b0e38 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -367,12 +367,14 @@ async fn run_daily_timer( /// Run the combined health and metrics checker /// /// This function runs in a loop with a 2-second interval, performing three tasks: -/// Background task for cleaning up expired entries from the rejected events index +/// Background task for cleaning up expired entries from the rejected events indexes /// /// This task runs two cleanup operations at different intervals: /// 1. **Hot cache cleanup (60s)**: Remove events older than 2 minutes from hot cache /// 2. **Cold index cleanup (daily)**: Remove metadata older than 7 days from cold index /// +/// Cleans up both the announcements index and the states index. +/// /// The hot cache cleanup runs frequently to keep memory usage low (events expire quickly). /// The cold index cleanup runs daily since metadata is small and expires slowly. async fn run_rejected_index_cleanup( @@ -397,6 +399,8 @@ async fn run_rejected_index_cleanup( tokio::select! { _ = hot_cache_timer.tick() => { let manager = sync_manager.lock().await; + + // Clean up announcements index let (hot_expired, _) = manager.rejected_events_index.cleanup_expired(); if hot_expired > 0 { tracing::debug!( @@ -404,9 +408,20 @@ async fn run_rejected_index_cleanup( hot_expired ); } + + // Clean up states index + let (states_hot_expired, _) = manager.rejected_states_index.cleanup_states_expired(); + if states_hot_expired > 0 { + tracing::debug!( + "Cleaned up {} expired entries from rejected states hot cache", + states_hot_expired + ); + } } _ = cold_index_timer.tick() => { let manager = sync_manager.lock().await; + + // Clean up announcements index let (_, cold_expired) = manager.rejected_events_index.cleanup_expired(); if cold_expired > 0 { tracing::info!( @@ -414,6 +429,15 @@ async fn run_rejected_index_cleanup( cold_expired ); } + + // Clean up states index + let (_, states_cold_expired) = manager.rejected_states_index.cleanup_states_expired(); + if states_cold_expired > 0 { + tracing::info!( + "Cleaned up {} expired entries from rejected states cold index", + states_cold_expired + ); + } } _ = shutdown_rx.recv() => { tracing::info!("Rejected index cleanup received shutdown signal"); @@ -507,6 +531,8 @@ pub struct SyncManager { pending_sync_index: PendingSyncIndex, /// Rejected announcement events (30617/30618) - two-tier storage for re-processing rejected_events_index: Arc, + /// Rejected state events (30618) - two-tier storage for re-processing + rejected_states_index: Arc, /// Active relay connections - keyed by relay URL connections: HashMap, /// Health tracker for relay connection state @@ -571,6 +597,18 @@ impl SyncManager { Duration::from_secs(config.rejected_cold_index_expiry_secs), ) }), + rejected_states_index: Arc::new(if let Some(ref metrics) = sync_metrics { + RejectedEventsIndex::with_metrics( + Duration::from_secs(config.rejected_hot_cache_duration_secs), + Duration::from_secs(config.rejected_cold_index_expiry_secs), + metrics.clone(), + ) + } else { + RejectedEventsIndex::new( + Duration::from_secs(config.rejected_hot_cache_duration_secs), + Duration::from_secs(config.rejected_cold_index_expiry_secs), + ) + }), connections: HashMap::new(), health_tracker: Arc::new(RelayHealthTracker::new(config)), next_batch_id: 0, diff --git a/src/sync/rejected_index.rs b/src/sync/rejected_index.rs index 4733d80..f5ffef4 100644 --- a/src/sync/rejected_index.rs +++ b/src/sync/rejected_index.rs @@ -355,7 +355,7 @@ impl RejectedEventsIndex { index } - /// Update metrics with current sizes + /// Update metrics with current sizes (for announcements) fn update_metrics(&self) { if let Some(ref metrics) = self.metrics { metrics.update_hot_cache_size(self.hot_cache.len()); @@ -363,6 +363,14 @@ impl RejectedEventsIndex { } } + /// Update metrics with current sizes (for states) + fn update_states_metrics(&self) { + if let Some(ref metrics) = self.metrics { + metrics.update_states_hot_cache_size(self.hot_cache.len()); + metrics.update_states_cold_index_size(self.cold_index.len()); + } + } + /// Add rejected announcement to both tiers /// /// # Arguments @@ -393,6 +401,36 @@ impl RejectedEventsIndex { self.update_metrics(); } + /// Add rejected state event to both tiers + /// + /// # Arguments + /// + /// * `event` - Full event object (stored in hot cache) + /// * `pubkey` - Author's public key + /// * `identifier` - Repository identifier (d tag) + /// * `reason` - Why the state event was rejected + pub fn add_state( + &self, + event: Event, + pubkey: PublicKey, + identifier: String, + reason: RejectionReason, + ) { + // Add to hot cache (full event) + self.hot_cache.add( + event.clone(), + pubkey, + identifier.clone(), + reason, + ); + + // Add to cold index (metadata only) + self.cold_index.add(event.id, pubkey, identifier, reason); + + // Update metrics (using states metrics) + self.update_states_metrics(); + } + /// Check if event is already rejected (in either tier) pub fn contains(&self, event_id: &EventId) -> bool { self.hot_cache.contains(event_id) || self.cold_index.contains(event_id) @@ -442,7 +480,51 @@ impl RejectedEventsIndex { (removed, events) } - /// Clean up expired entries from both tiers + /// Invalidate state events and get events for immediate re-processing + /// + /// This is called when an announcement is accepted that authorizes state events. + /// It removes the cold index entries (so they can be re-fetched on next sync) and + /// returns any events still in the hot cache for immediate re-processing. + /// + /// # Returns + /// + /// Tuple of (number of cold index entries removed, events from hot cache) + pub fn invalidate_and_get_state_events( + &self, + maintainer_pubkey: &PublicKey, + identifier: &str, + ) -> (usize, Vec) { + // Remove from cold index (prevents re-fetch) + let removed = self + .cold_index + .invalidate_maintainer_announcements(maintainer_pubkey, identifier); + + // Get from hot cache (for immediate re-processing) + let events = self + .hot_cache + .get_maintainer_events(maintainer_pubkey, identifier); + + // Track metrics (using states metrics) + if let Some(ref metrics) = self.metrics { + if removed > 0 { + metrics.record_states_invalidation(removed); + } + if events.is_empty() { + metrics.record_states_hot_cache_miss(); + } else { + for _ in &events { + metrics.record_states_hot_cache_hit(); + } + } + } + + // Update size metrics (using states metrics) + self.update_states_metrics(); + + (removed, events) + } + + /// Clean up expired entries from both tiers (for announcements) /// /// Returns tuple of (hot cache expired, cold index expired) pub fn cleanup_expired(&self) -> (usize, usize) { @@ -465,6 +547,29 @@ impl RejectedEventsIndex { (hot_expired, cold_expired) } + /// Clean up expired entries from both tiers (for states) + /// + /// Returns tuple of (hot cache expired, cold index expired) + pub fn cleanup_states_expired(&self) -> (usize, usize) { + let hot_expired = self.hot_cache.cleanup_expired(); + let cold_expired = self.cold_index.cleanup_expired(); + + // Track metrics (using states metrics) + if let Some(ref metrics) = self.metrics { + if hot_expired > 0 { + metrics.record_states_hot_cache_expired(hot_expired); + } + if cold_expired > 0 { + metrics.record_states_cold_index_expired(cold_expired); + } + } + + // Update size metrics (using states metrics) + self.update_states_metrics(); + + (hot_expired, cold_expired) + } + /// Get current number of entries in hot cache pub fn hot_cache_len(&self) -> usize { self.hot_cache.len() -- cgit v1.2.3