scanbridge 0.3.0

A unified, pluggable API for malware scanning with circuit breakers, policy enforcement, and audit logging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Circuit breaker implementation.

use crate::circuit_breaker::config::{CircuitBreakerConfig, FallbackBehavior};
use crate::circuit_breaker::state::{BreakerMetrics, BreakerState};
use crate::core::{FileInput, ScanError, ScanResult, Scanner};

use async_trait::async_trait;
use std::fmt;
use std::sync::RwLock;
use std::time::Instant;

/// A circuit breaker wrapper around a scanner.
///
/// The circuit breaker monitors failures and prevents cascading failures
/// by temporarily rejecting requests to unhealthy backends.
///
/// # States
///
/// - **Closed**: Normal operation. Requests pass through, failures are counted.
/// - **Open**: Backend is failing. Requests are rejected immediately.
/// - **Half-Open**: Probing. A limited number of requests are allowed through
///   to test if the backend has recovered.
///
/// # Example
///
/// ```rust,ignore
/// use scanbridge::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};
/// use scanbridge::backends::MockScanner;
///
/// let scanner = MockScanner::new();
/// let config = CircuitBreakerConfig::default();
/// let protected = CircuitBreaker::new(scanner, config);
///
/// // Use `protected` as a normal scanner
/// let result = protected.scan(&input).await;
/// ```
pub struct CircuitBreaker<S: Scanner> {
    /// The wrapped scanner.
    inner: S,
    /// Current state of the circuit.
    state: RwLock<BreakerState>,
    /// Configuration.
    config: CircuitBreakerConfig,
    /// Metrics.
    metrics: RwLock<BreakerMetrics>,
}

impl<S: Scanner> CircuitBreaker<S> {
    /// Creates a new circuit breaker with the given scanner and configuration.
    pub fn new(scanner: S, config: CircuitBreakerConfig) -> Self {
        Self {
            inner: scanner,
            state: RwLock::new(BreakerState::closed()),
            config,
            metrics: RwLock::new(BreakerMetrics::new()),
        }
    }

    /// Creates a new circuit breaker with default configuration.
    pub fn with_defaults(scanner: S) -> Self {
        Self::new(scanner, CircuitBreakerConfig::default())
    }

    /// Returns the current state of the circuit breaker.
    pub fn state(&self) -> BreakerState {
        self.state
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clone()
    }

    /// Returns a copy of the current metrics.
    pub fn metrics(&self) -> BreakerMetrics {
        self.metrics
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clone()
    }

    /// Forces the circuit into the open state.
    pub fn force_open(&self) {
        let until = Instant::now() + self.config.open_duration;
        *self
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = BreakerState::Open {
            opened_at: Instant::now(),
            until,
        };
        self.metrics
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .record_opened();
    }

    /// Forces the circuit into the closed state.
    pub fn force_close(&self) {
        *self
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = BreakerState::closed();
        self.metrics
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .record_closed();
    }

    /// Resets the circuit breaker state and metrics.
    pub fn reset(&self) {
        *self
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = BreakerState::closed();
        *self
            .metrics
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = BreakerMetrics::new();
    }

    /// Returns a reference to the wrapped scanner.
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Returns a reference to the configuration.
    pub fn config(&self) -> &CircuitBreakerConfig {
        &self.config
    }

    /// Checks if a request should be allowed through.
    fn should_allow_request(&self) -> Result<(), ScanError> {
        let mut state = self
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let now = Instant::now();

        match &*state {
            BreakerState::Closed { .. } => Ok(()),

            BreakerState::Open { until, .. } => {
                if now >= *until {
                    // Transition to half-open
                    *state = BreakerState::HalfOpen {
                        success_count: 0,
                        probe_count: 1,
                    };
                    Ok(())
                } else {
                    Err(ScanError::CircuitOpen {
                        engine: self.inner.name().to_string(),
                        recovery_hint: Some(format!("Circuit may recover in {:?}", *until - now)),
                    })
                }
            }

            BreakerState::HalfOpen {
                success_count,
                probe_count,
            } => {
                if *probe_count < self.config.half_open_max_probes {
                    // Allow this probe - preserve the current success_count
                    *state = BreakerState::HalfOpen {
                        success_count: *success_count,
                        probe_count: probe_count + 1,
                    };
                    Ok(())
                } else {
                    Err(ScanError::CircuitOpen {
                        engine: self.inner.name().to_string(),
                        recovery_hint: Some("Maximum probes in progress".to_string()),
                    })
                }
            }
        }
    }

    /// Records a successful request.
    fn record_success(&self) {
        let mut state = self
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        self.metrics
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .record_success();

        match &*state {
            BreakerState::Closed { .. } => {
                // Reset failure count on success
                *state = BreakerState::closed();
            }

            BreakerState::HalfOpen {
                success_count,
                probe_count,
            } => {
                let new_success_count = success_count + 1;
                if new_success_count >= self.config.success_threshold {
                    // Transition to closed
                    *state = BreakerState::closed();
                    self.metrics
                        .write()
                        .unwrap_or_else(|poisoned| poisoned.into_inner())
                        .record_closed();
                } else {
                    *state = BreakerState::HalfOpen {
                        success_count: new_success_count,
                        probe_count: *probe_count,
                    };
                }
            }

            BreakerState::Open { .. } => {
                // Shouldn't happen, but handle gracefully
            }
        }
    }

    /// Records a failed request.
    fn record_failure(&self, error: &ScanError) {
        // Check if this error should count as a failure
        if !self.config.failure_policy.should_count(error) {
            return;
        }

        let mut state = self
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        self.metrics
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .record_failure();

        match &*state {
            BreakerState::Closed { failure_count } => {
                let new_count = failure_count + 1;
                if new_count >= self.config.failure_threshold {
                    // Transition to open
                    let until = Instant::now() + self.config.open_duration;
                    *state = BreakerState::Open {
                        opened_at: Instant::now(),
                        until,
                    };
                    self.metrics
                        .write()
                        .unwrap_or_else(|poisoned| poisoned.into_inner())
                        .record_opened();
                } else {
                    *state = BreakerState::Closed {
                        failure_count: new_count,
                    };
                }
            }

            BreakerState::HalfOpen { .. } => {
                // Any failure in half-open reopens the circuit
                let until = Instant::now() + self.config.open_duration;
                *state = BreakerState::Open {
                    opened_at: Instant::now(),
                    until,
                };
                self.metrics
                    .write()
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .record_opened();
            }

            BreakerState::Open { .. } => {
                // Already open, nothing to do
            }
        }
    }

    /// Handles a request when the circuit is open.
    async fn handle_open_circuit(&self, input: &FileInput) -> Result<ScanResult, ScanError> {
        self.metrics
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .record_rejected();

        match &self.config.fallback_behavior {
            FallbackBehavior::FailClosed => Err(ScanError::CircuitOpen {
                engine: self.inner.name().to_string(),
                recovery_hint: Some("Circuit is open; scan rejected".to_string()),
            }),

            FallbackBehavior::FailOpen => {
                // Return a "clean" result with a warning
                // This is dangerous! Only use if availability > safety
                tracing::warn!(
                    engine = self.inner.name(),
                    "Circuit open, allowing file through (fail-open mode)"
                );

                use crate::core::{FileHash, FileHasher, FileMetadata, ScanContext, ScanOutcome};
                use std::time::Duration;

                let hasher = FileHasher::new();
                let hash = match input {
                    FileInput::Path(path) => hasher.hash_file(path)?,
                    FileInput::Bytes { data, .. } => hasher.hash_bytes(data),
                    FileInput::Stream { .. } => FileHash::new("unknown-stream"),
                };

                let metadata = FileMetadata::new(input.size_hint().unwrap_or(0), hash);
                let context = ScanContext::new();

                let mut result = ScanResult::new(
                    ScanOutcome::Clean,
                    metadata,
                    format!("{}-failopen", self.inner.name()),
                    Duration::ZERO,
                    context,
                );
                result.details.insert(
                    "warning".to_string(),
                    serde_json::Value::String(
                        "Scan skipped due to circuit breaker; file allowed through fail-open policy"
                            .to_string(),
                    ),
                );
                Ok(result)
            }

            FallbackBehavior::Fallback(fallback) => {
                tracing::info!(
                    primary = self.inner.name(),
                    fallback = fallback.name(),
                    "Using fallback scanner due to open circuit"
                );
                fallback.scan(input).await
            }
        }
    }
}

impl<S: Scanner> fmt::Debug for CircuitBreaker<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CircuitBreaker")
            .field("inner", &self.inner)
            .field(
                "state",
                &*self
                    .state
                    .read()
                    .unwrap_or_else(|poisoned| poisoned.into_inner()),
            )
            .field("config", &self.config)
            .finish()
    }
}

#[async_trait]
impl<S: Scanner> Scanner for CircuitBreaker<S> {
    fn name(&self) -> &str {
        self.inner.name()
    }

    async fn scan(&self, input: &FileInput) -> Result<ScanResult, ScanError> {
        // Check if we should allow this request
        if self.should_allow_request().is_err() {
            return self.handle_open_circuit(input).await;
        }

        // Perform the scan
        match self.inner.scan(input).await {
            Ok(result) => {
                self.record_success();
                Ok(result)
            }
            Err(e) => {
                self.record_failure(&e);
                Err(e)
            }
        }
    }

    async fn health_check(&self) -> Result<(), ScanError> {
        // Health check always goes through, but updates state
        match self.inner.health_check().await {
            Ok(()) => {
                // If we're half-open and health check succeeds, count it
                if self.state().is_half_open() {
                    self.record_success();
                }
                Ok(())
            }
            Err(e) => {
                if self.state().is_half_open() {
                    self.record_failure(&e);
                }
                Err(e)
            }
        }
    }

    fn max_file_size(&self) -> Option<u64> {
        self.inner.max_file_size()
    }

    async fn signature_version(&self) -> Option<String> {
        self.inner.signature_version().await
    }

    fn supports_streaming(&self) -> bool {
        self.inner.supports_streaming()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backends::MockScanner;
    use std::time::Duration;

    #[tokio::test]
    async fn test_circuit_breaker_passes_through() {
        let scanner = MockScanner::new_clean();
        let breaker = CircuitBreaker::with_defaults(scanner);

        let input = FileInput::from_bytes(b"test".to_vec());
        let result = breaker.scan(&input).await.unwrap();

        assert!(result.is_clean());
        assert!(breaker.state().is_closed());
    }

    #[tokio::test]
    async fn test_circuit_opens_on_failures() {
        let scanner = MockScanner::new().with_fail_rate(1.0);
        let config = CircuitBreakerConfig::default().with_failure_threshold(3);
        let breaker = CircuitBreaker::new(scanner, config);

        let input = FileInput::from_bytes(b"test".to_vec());

        // First 3 failures should open the circuit
        for _ in 0..3 {
            let _ = breaker.scan(&input).await;
        }

        assert!(breaker.state().is_open());
        assert_eq!(breaker.metrics().times_opened, 1);
    }

    #[tokio::test]
    async fn test_circuit_rejects_when_open() {
        let scanner = MockScanner::new_clean();
        let breaker = CircuitBreaker::with_defaults(scanner);

        // Force open
        breaker.force_open();
        assert!(breaker.state().is_open());

        let input = FileInput::from_bytes(b"test".to_vec());
        let result = breaker.scan(&input).await;

        assert!(matches!(result, Err(ScanError::CircuitOpen { .. })));
    }

    #[tokio::test]
    async fn test_circuit_transitions_to_half_open() {
        let scanner = MockScanner::new_clean();
        let config = CircuitBreakerConfig::default().with_open_duration(Duration::from_millis(10));
        let breaker = CircuitBreaker::new(scanner, config);

        breaker.force_open();
        assert!(breaker.state().is_open());

        // Wait for open duration
        tokio::time::sleep(Duration::from_millis(20)).await;

        // Next request should transition to half-open
        let input = FileInput::from_bytes(b"test".to_vec());
        let result = breaker.scan(&input).await;

        // Should succeed and transition states
        assert!(result.is_ok());
    }

    #[test]
    fn test_force_open_close() {
        let scanner = MockScanner::new_clean();
        let breaker = CircuitBreaker::with_defaults(scanner);

        assert!(breaker.state().is_closed());

        breaker.force_open();
        assert!(breaker.state().is_open());

        breaker.force_close();
        assert!(breaker.state().is_closed());
    }
}