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
//! Filesystem-based quarantine storage implementation.

use crate::core::error::QuarantineError;
use crate::core::FileInput;
use crate::quarantine::record::{QuarantineFilter, QuarantineId, QuarantineRecord};
use crate::quarantine::traits::QuarantineStore;

use async_trait::async_trait;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::RwLock;

/// Filesystem-based quarantine storage.
///
/// Stores quarantined files in a designated directory with their metadata.
/// Files are stored with obfuscated names to prevent accidental execution.
///
/// # Directory Structure
///
/// ```text
/// quarantine/
/// ├── data/
/// │   └── {id}.qdata          # Quarantined file (obfuscated)
/// └── meta/
///     └── {id}.json           # Metadata
/// ```
#[derive(Debug)]
pub struct FilesystemQuarantine {
    /// Base directory for quarantine storage.
    base_path: PathBuf,
    /// In-memory index of records (for fast lookups).
    index: RwLock<HashMap<String, QuarantineRecord>>,
}

impl FilesystemQuarantine {
    /// Creates a new filesystem quarantine at the given path.
    ///
    /// Creates the directory structure if it doesn't exist.
    pub fn new(base_path: impl Into<PathBuf>) -> Result<Self, QuarantineError> {
        let base_path = base_path.into();

        // Create directory structure
        let data_dir = base_path.join("data");
        let meta_dir = base_path.join("meta");

        std::fs::create_dir_all(&data_dir).map_err(|e| QuarantineError::StoreFailed {
            reason: format!("Failed to create data directory: {}", e),
        })?;

        std::fs::create_dir_all(&meta_dir).map_err(|e| QuarantineError::StoreFailed {
            reason: format!("Failed to create meta directory: {}", e),
        })?;

        let store = Self {
            base_path,
            index: RwLock::new(HashMap::new()),
        };

        // Load existing records into index
        store.load_index()?;

        Ok(store)
    }

    /// Returns the path to the quarantine data directory.
    pub fn data_dir(&self) -> PathBuf {
        self.base_path.join("data")
    }

    /// Returns the path to the quarantine metadata directory.
    pub fn meta_dir(&self) -> PathBuf {
        self.base_path.join("meta")
    }

    /// Returns the data file path for a given ID.
    fn data_path(&self, id: &QuarantineId) -> PathBuf {
        self.data_dir().join(format!("{}.qdata", id.as_str()))
    }

    /// Returns the metadata file path for a given ID.
    fn meta_path(&self, id: &QuarantineId) -> PathBuf {
        self.meta_dir().join(format!("{}.json", id.as_str()))
    }

    /// Loads existing records into the in-memory index.
    fn load_index(&self) -> Result<(), QuarantineError> {
        let meta_dir = self.meta_dir();
        if !meta_dir.exists() {
            return Ok(());
        }

        let entries =
            std::fs::read_dir(&meta_dir).map_err(|e| QuarantineError::RetrieveFailed {
                reason: format!("Failed to read meta directory: {}", e),
            })?;

        let mut index = self
            .index
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        for entry in entries.filter_map(|e| e.ok()) {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some("json") {
                if let Ok(content) = std::fs::read_to_string(&path) {
                    if let Ok(record) = serde_json::from_str::<QuarantineRecord>(&content) {
                        index.insert(record.id.0.clone(), record);
                    }
                }
            }
        }

        tracing::debug!(count = index.len(), "Loaded quarantine index");
        Ok(())
    }

    /// Saves a record's metadata to disk.
    fn save_metadata(&self, record: &QuarantineRecord) -> Result<(), QuarantineError> {
        let path = self.meta_path(&record.id);
        let content =
            serde_json::to_string_pretty(record).map_err(|e| QuarantineError::StoreFailed {
                reason: format!("Failed to serialize metadata: {}", e),
            })?;

        std::fs::write(&path, content).map_err(|e| QuarantineError::StoreFailed {
            reason: format!("Failed to write metadata: {}", e),
        })?;

        Ok(())
    }

    /// Reads file data from a FileInput.
    async fn read_input_data(&self, input: &FileInput) -> Result<Vec<u8>, QuarantineError> {
        match input {
            FileInput::Path(path) => {
                #[cfg(feature = "tokio-runtime")]
                {
                    tokio::fs::read(path)
                        .await
                        .map_err(|e| QuarantineError::StoreFailed {
                            reason: format!("Failed to read file: {}", e),
                        })
                }
                #[cfg(not(feature = "tokio-runtime"))]
                {
                    std::fs::read(path).map_err(|e| QuarantineError::StoreFailed {
                        reason: format!("Failed to read file: {}", e),
                    })
                }
            }
            FileInput::Bytes { data, .. } => Ok(data.clone()),
            FileInput::Stream { .. } => Err(QuarantineError::StoreFailed {
                reason: "Stream input not supported for filesystem quarantine".into(),
            }),
        }
    }
}

#[async_trait]
impl QuarantineStore for FilesystemQuarantine {
    async fn store(
        &self,
        input: &FileInput,
        record: QuarantineRecord,
    ) -> Result<QuarantineId, QuarantineError> {
        let id = record.id.clone();

        // Read the file data
        let data = self.read_input_data(input).await?;

        // Verify hash matches
        let hasher = crate::core::FileHasher::new();
        let computed_hash = hasher.hash_bytes(&data);
        if computed_hash.blake3 != record.file_hash.blake3 {
            return Err(QuarantineError::IntegrityCheckFailed {
                expected: record.file_hash.blake3.clone(),
                actual: computed_hash.blake3,
            });
        }

        // Write data file
        let data_path = self.data_path(&id);
        #[cfg(feature = "tokio-runtime")]
        {
            tokio::fs::write(&data_path, &data).await.map_err(|e| {
                QuarantineError::StoreFailed {
                    reason: format!("Failed to write data file: {}", e),
                }
            })?;
        }
        #[cfg(not(feature = "tokio-runtime"))]
        {
            std::fs::write(&data_path, &data).map_err(|e| QuarantineError::StoreFailed {
                reason: format!("Failed to write data file: {}", e),
            })?;
        }

        // Save metadata
        self.save_metadata(&record)?;

        // Update index
        self.index
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert(id.0.clone(), record);

        tracing::info!(
            quarantine_id = %id,
            file_hash = %computed_hash.blake3,
            "File quarantined"
        );

        Ok(id)
    }

    async fn retrieve(
        &self,
        id: &QuarantineId,
    ) -> Result<(Vec<u8>, QuarantineRecord), QuarantineError> {
        // Get record from index
        let record = self
            .index
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(&id.0)
            .cloned()
            .ok_or_else(|| QuarantineError::NotFound { id: id.to_string() })?;

        // Read data file
        let data_path = self.data_path(id);
        #[cfg(feature = "tokio-runtime")]
        let data =
            tokio::fs::read(&data_path)
                .await
                .map_err(|e| QuarantineError::RetrieveFailed {
                    reason: format!("Failed to read data file: {}", e),
                })?;
        #[cfg(not(feature = "tokio-runtime"))]
        let data = std::fs::read(&data_path).map_err(|e| QuarantineError::RetrieveFailed {
            reason: format!("Failed to read data file: {}", e),
        })?;

        // Verify integrity
        let hasher = crate::core::FileHasher::new();
        let computed_hash = hasher.hash_bytes(&data);
        if computed_hash.blake3 != record.file_hash.blake3 {
            return Err(QuarantineError::IntegrityCheckFailed {
                expected: record.file_hash.blake3.clone(),
                actual: computed_hash.blake3,
            });
        }

        Ok((data, record))
    }

    async fn delete(&self, id: &QuarantineId) -> Result<(), QuarantineError> {
        // Remove from index
        self.index
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .remove(&id.0);

        // Delete files
        let data_path = self.data_path(id);
        let meta_path = self.meta_path(id);

        #[cfg(feature = "tokio-runtime")]
        {
            let _ = tokio::fs::remove_file(&data_path).await;
            let _ = tokio::fs::remove_file(&meta_path).await;
        }
        #[cfg(not(feature = "tokio-runtime"))]
        {
            let _ = std::fs::remove_file(&data_path);
            let _ = std::fs::remove_file(&meta_path);
        }

        tracing::info!(quarantine_id = %id, "Quarantine record deleted");

        Ok(())
    }

    async fn list(
        &self,
        filter: QuarantineFilter,
    ) -> Result<Vec<QuarantineRecord>, QuarantineError> {
        let index = self
            .index
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let mut records: Vec<_> = index
            .values()
            .filter(|r| filter.matches(r))
            .cloned()
            .collect();

        // Sort by quarantine date (newest first)
        records.sort_by(|a, b| b.quarantined_at.cmp(&a.quarantined_at));

        // Apply pagination
        if let Some(offset) = filter.offset {
            if offset < records.len() {
                records = records.into_iter().skip(offset).collect();
            } else {
                records.clear();
            }
        }

        if let Some(limit) = filter.limit {
            records.truncate(limit);
        }

        Ok(records)
    }

    async fn count(&self) -> Result<usize, QuarantineError> {
        Ok(self
            .index
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .len())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{FileHash, FileMetadata, ScanContext, ScanOutcome, ScanResult};
    use std::time::Duration;
    use tempfile::TempDir;

    fn make_test_record(hash: FileHash) -> QuarantineRecord {
        let metadata = FileMetadata::new(4, hash.clone());
        let result = ScanResult::new(
            ScanOutcome::Infected { threats: vec![] },
            metadata,
            "test",
            Duration::from_millis(10),
            ScanContext::new(),
        );
        QuarantineRecord::new(hash, 4, "test", result)
    }

    #[tokio::test]
    async fn test_filesystem_quarantine_roundtrip() {
        let temp_dir = TempDir::new().unwrap();
        let store = FilesystemQuarantine::new(temp_dir.path()).unwrap();

        // Create test data
        let data = b"test";
        let hasher = crate::core::FileHasher::new();
        let hash = hasher.hash_bytes(data);
        let record = make_test_record(hash);

        let input = FileInput::from_bytes(data.to_vec());

        // Store
        let id = store.store(&input, record.clone()).await.unwrap();

        // Retrieve
        let (retrieved_data, retrieved_record) = store.retrieve(&id).await.unwrap();
        assert_eq!(retrieved_data, data);
        assert_eq!(retrieved_record.file_hash.blake3, record.file_hash.blake3);

        // Count
        assert_eq!(store.count().await.unwrap(), 1);

        // Delete
        store.delete(&id).await.unwrap();
        assert_eq!(store.count().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_filesystem_quarantine_list() {
        let temp_dir = TempDir::new().unwrap();
        let store = FilesystemQuarantine::new(temp_dir.path()).unwrap();

        // Store multiple files
        for i in 0..5 {
            let data = format!("test{}", i);
            let hasher = crate::core::FileHasher::new();
            let hash = hasher.hash_bytes(data.as_bytes());
            let mut record = make_test_record(hash);
            record.file_size = data.len() as u64;
            record = record.with_tenant_id(format!("tenant-{}", i % 2));

            let input = FileInput::from_bytes(data.into_bytes());
            store.store(&input, record).await.unwrap();
        }

        // List all
        let all = store.list(QuarantineFilter::new()).await.unwrap();
        assert_eq!(all.len(), 5);

        // List with filter
        let filtered = store
            .list(QuarantineFilter::new().with_tenant_id("tenant-0"))
            .await
            .unwrap();
        assert_eq!(filtered.len(), 3); // 0, 2, 4

        // List with pagination
        let paginated = store
            .list(QuarantineFilter::new().with_pagination(2, 1))
            .await
            .unwrap();
        assert_eq!(paginated.len(), 2);
    }
}