threatflux-cache 0.1.8

A flexible async cache library for Rust with pluggable backends and serialization
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
//! Filesystem storage backend

use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::Hash;
use std::path::{Path, PathBuf};
use tokio::fs::{self, File};
use tokio::io::AsyncWriteExt;

use crate::{storage::SerializationFormat, CacheEntry, EntryMetadata, Result, StorageBackend};

/// Type alias for complex phantom data type
type PhantomTypes<K, V, M> = std::marker::PhantomData<(K, V, M)>;

/// Filesystem storage backend
#[allow(clippy::type_complexity)]
pub struct FilesystemBackend<K, V, M = ()>
where
    K: Hash + Eq + Clone + Send + Sync,
    V: Clone + Send + Sync,
    M: Clone + Send + Sync,
{
    base_path: PathBuf,
    format: SerializationFormat,
    _phantom: PhantomTypes<K, V, M>,
}

impl<K, V, M> FilesystemBackend<K, V, M>
where
    K: Hash + Eq + Clone + Send + Sync,
    V: Clone + Send + Sync,
    M: Clone + Send + Sync,
{
    /// Create a new filesystem backend with the given base path
    pub async fn new<P: AsRef<Path>>(base_path: P) -> Result<Self> {
        let base_path = base_path.as_ref().to_path_buf();
        fs::create_dir_all(&base_path).await?;

        Ok(Self {
            base_path,
            #[cfg(feature = "json-serialization")]
            format: SerializationFormat::Json,
            #[cfg(all(not(feature = "json-serialization"), feature = "bincode-serialization"))]
            format: SerializationFormat::Bincode,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Set the serialization format
    pub fn with_format(mut self, format: SerializationFormat) -> Self {
        self.format = format;
        self
    }

    /// Sanitize a filename by removing or replacing dangerous characters
    fn sanitize_filename(filename: &str) -> String {
        // Replace path separators and other dangerous characters with safe alternatives
        let mut result = filename
            .chars()
            .map(|c| match c {
                '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
                c if c.is_control() => '_', // Replace control characters
                c => c,
            })
            .collect::<String>();

        // Replace leading dots to prevent hidden files
        if result.starts_with('.') {
            result = result.replacen('.', "_", 1);
        }

        // Clean up trailing dots and whitespace
        result.trim_matches('.').trim().to_string()
    }

    /// Get the path for a cache file
    fn get_cache_file_path(&self, key: &str) -> PathBuf {
        let sanitized_key = Self::sanitize_filename(key);
        // Ensure the filename isn't empty after sanitization
        let safe_key = if sanitized_key.is_empty() {
            "cache_entry".to_string()
        } else {
            sanitized_key
        };

        self.base_path
            .join(format!("{}.{}", safe_key, self.format.extension()))
    }

    /// Get the metadata file path
    fn get_metadata_path(&self) -> PathBuf {
        self.base_path
            .join(format!("metadata.{}", self.format.extension()))
    }
}

#[async_trait]
impl<K, V, M> StorageBackend for FilesystemBackend<K, V, M>
where
    K: Serialize + DeserializeOwned + Hash + Eq + Clone + Send + Sync + std::fmt::Display + 'static,
    V: Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
    M: Serialize + DeserializeOwned + Clone + Send + Sync + EntryMetadata,
{
    type Key = K;
    type Value = V;
    type Metadata = M;

    async fn save(&self, entries: &HashMap<K, Vec<CacheEntry<K, V, M>>>) -> Result<()> {
        // Save each key's entries to a separate file
        for (key, entry_vec) in entries {
            let file_path = self.get_cache_file_path(&key.to_string());
            let data = self.format.serialize(entry_vec)?;

            let mut file = File::create(&file_path).await?;
            file.write_all(&data).await?;
            file.flush().await?;
        }

        // Save metadata about the cache
        let metadata = CacheMetadata {
            total_keys: entries.len(),
            last_updated: chrono::Utc::now(),
        };

        let metadata_path = self.get_metadata_path();
        let data = self.format.serialize(&metadata)?;

        let mut file = File::create(&metadata_path).await?;
        file.write_all(&data).await?;
        file.flush().await?;

        Ok(())
    }

    async fn load(&self) -> Result<HashMap<K, Vec<CacheEntry<K, V, M>>>> {
        let mut entries = HashMap::new();

        // Read all cache files
        let mut dir_entries = fs::read_dir(&self.base_path).await?;

        while let Some(entry) = dir_entries.next_entry().await? {
            let path = entry.path();

            // Skip non-cache files
            if path.extension().and_then(|s| s.to_str()) != Some(self.format.extension()) {
                continue;
            }

            // Skip metadata file
            if path.file_stem().and_then(|s| s.to_str()) == Some("metadata") {
                continue;
            }

            // Read and deserialize the file
            match fs::read(&path).await {
                Ok(data) => {
                    match self.format.deserialize::<Vec<CacheEntry<K, V, M>>>(&data) {
                        Ok(entry_vec) => {
                            if let Some(first_entry) = entry_vec.first() {
                                entries.insert(first_entry.key.clone(), entry_vec);
                            }
                        }
                        Err(e) => {
                            // Log error but continue loading other files
                            eprintln!("Failed to deserialize cache file {:?}: {}", path, e);
                        }
                    }
                }
                Err(e) => {
                    // Log error but continue loading other files
                    eprintln!("Failed to read cache file {:?}: {}", path, e);
                }
            }
        }

        Ok(entries)
    }

    async fn remove(&self, key: &K) -> Result<()> {
        let file_path = self.get_cache_file_path(&key.to_string());
        if file_path.exists() {
            fs::remove_file(&file_path).await?;
        }
        Ok(())
    }

    async fn clear(&self) -> Result<()> {
        let mut dir_entries = fs::read_dir(&self.base_path).await?;

        while let Some(entry) = dir_entries.next_entry().await? {
            let path = entry.path();

            // Only remove cache files
            if path.extension().and_then(|s| s.to_str()) == Some(self.format.extension()) {
                fs::remove_file(&path).await?;
            }
        }

        Ok(())
    }

    async fn contains(&self, key: &K) -> Result<bool> {
        let file_path = self.get_cache_file_path(&key.to_string());
        Ok(file_path.exists())
    }

    async fn size_bytes(&self) -> Result<u64> {
        let mut total_size = 0u64;
        let mut dir_entries = fs::read_dir(&self.base_path).await?;

        while let Some(entry) = dir_entries.next_entry().await? {
            if let Ok(metadata) = entry.metadata().await {
                total_size += metadata.len();
            }
        }

        Ok(total_size)
    }

    async fn compact(&self) -> Result<()> {
        // For filesystem backend, compaction could involve:
        // - Removing expired entries
        // - Consolidating small files
        // - Rewriting files with compression
        // For now, just a no-op
        Ok(())
    }
}

/// Metadata about the cache stored on filesystem
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CacheMetadata {
    total_keys: usize,
    last_updated: chrono::DateTime<chrono::Utc>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_filesystem_backend_operations() {
        let temp_dir = TempDir::new().unwrap();
        let backend: FilesystemBackend<String, String> =
            FilesystemBackend::new(temp_dir.path()).await.unwrap();

        // Test empty state
        let loaded = backend.load().await.unwrap();
        assert!(loaded.is_empty());

        // Test save and load
        let mut entries = HashMap::new();
        let entry = CacheEntry::new("key1".to_string(), "value1".to_string());
        entries.insert("key1".to_string(), vec![entry]);

        backend.save(&entries).await.unwrap();
        let loaded = backend.load().await.unwrap();
        assert_eq!(loaded.len(), 1);
        assert!(loaded.contains_key("key1"));

        // Test contains
        assert!(backend.contains(&"key1".to_string()).await.unwrap());
        assert!(!backend.contains(&"key2".to_string()).await.unwrap());

        // Test remove
        backend.remove(&"key1".to_string()).await.unwrap();
        assert!(!backend.contains(&"key1".to_string()).await.unwrap());

        // Test clear
        backend.save(&entries).await.unwrap();
        backend.clear().await.unwrap();
        let loaded = backend.load().await.unwrap();
        assert!(loaded.is_empty());
    }

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

        // Save data with one backend instance
        {
            let backend: FilesystemBackend<String, String> =
                FilesystemBackend::new(&path).await.unwrap();

            let mut entries = HashMap::new();
            let entry =
                CacheEntry::new("persistent_key".to_string(), "persistent_value".to_string());
            entries.insert("persistent_key".to_string(), vec![entry]);

            backend.save(&entries).await.unwrap();
        }

        // Load data with a new backend instance
        {
            let backend: FilesystemBackend<String, String> =
                FilesystemBackend::new(&path).await.unwrap();

            let loaded = backend.load().await.unwrap();
            assert_eq!(loaded.len(), 1);
            assert!(loaded.contains_key("persistent_key"));

            let entries = &loaded["persistent_key"];
            assert_eq!(entries[0].value, "persistent_value");
        }
    }

    #[tokio::test]
    async fn test_filesystem_backend_size() {
        let temp_dir = TempDir::new().unwrap();
        let backend: FilesystemBackend<String, String> =
            FilesystemBackend::new(temp_dir.path()).await.unwrap();

        // Save some data
        let mut entries = HashMap::new();
        for i in 0..5 {
            let entry = CacheEntry::new(format!("key{}", i), format!("value{}", i));
            entries.insert(format!("key{}", i), vec![entry]);
        }

        backend.save(&entries).await.unwrap();

        // Check size is non-zero
        let size = backend.size_bytes().await.unwrap();
        assert!(size > 0);
    }

    #[tokio::test]
    async fn test_path_traversal_protection() {
        let temp_dir = TempDir::new().unwrap();
        let backend: FilesystemBackend<String, String> =
            FilesystemBackend::new(temp_dir.path()).await.unwrap();

        // Test malicious keys that could attempt path traversal
        let malicious_keys = vec![
            "../etc/passwd",
            "..\\windows\\system32\\config\\sam",
            "/etc/shadow",
            "C:\\Windows\\System32\\config\\SAM",
            "../../sensitive_file",
            "./../../../etc/hosts",
            "../",
            "..",
            "test/../../../etc/passwd",
            "normal_file/../../../etc/passwd",
        ];

        for malicious_key in malicious_keys {
            let path = backend.get_cache_file_path(malicious_key);

            // Ensure the path is within the base directory
            assert!(
                path.starts_with(&backend.base_path),
                "Malicious key '{}' resulted in path outside base directory: {:?}",
                malicious_key,
                path
            );

            // Ensure the filename doesn't contain path separators
            let filename = path.file_name().unwrap().to_str().unwrap();
            assert!(
                !filename.contains('/') && !filename.contains('\\'),
                "Filename '{}' contains path separators for key '{}'",
                filename,
                malicious_key
            );
        }
    }

    #[test]
    fn test_filename_sanitization() {
        // Test various dangerous characters
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("../etc/passwd"),
            "_._etc_passwd"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file\\name"),
            "file_name"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file:name"),
            "file_name"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file*name"),
            "file_name"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file?name"),
            "file_name"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file\"name"),
            "file_name"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file<name>"),
            "file_name_"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("file|name"),
            "file_name"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename(".hidden"),
            "_hidden"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("..."),
            "_"
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename(""),
            ""
        );
        assert_eq!(
            FilesystemBackend::<String, String>::sanitize_filename("   "),
            ""
        );

        // Test the most important security aspect: no path traversal
        let result = FilesystemBackend::<String, String>::sanitize_filename("../etc/passwd");
        assert!(!result.contains('/'));
        assert!(!result.contains('\\'));
        assert!(!result.starts_with('.'));
    }
}