raisfast 0.2.23

The last backend you'll ever need. Rust-powered headless CMS with built-in blog, ecommerce, wallet, payment and 4 plugin engines.
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
497
498
499
500
501
//! Plugin virtual filesystem
//!
//! Each plugin has an isolated sandbox directory; virtual paths map under `{vfs_root}/{plugin_id}/`.
//! Provides path escape protection, file size limits, and total quota limits.

use std::fs;
use std::path::{Path, PathBuf};

use crate::config::app::AppConfig;
use crate::plugins::Permissions;

/// Virtual filesystem errors
#[derive(Debug, thiserror::Error)]
pub enum VfsError {
    /// Path contains `..` or other escape attempts
    #[error("path escape detected")]
    PathEscape,
    /// Exceeds single file size limit
    #[error("file exceeds max size ({max} bytes)")]
    FileTooLarge { max: usize },
    /// Exceeds total storage quota
    #[error("quota exceeded (used {used}/{max}, need {need})")]
    QuotaExceeded {
        used: usize,
        max: usize,
        need: usize,
    },
    /// Permission denied (read-only / no access)
    #[error("filesystem permission denied")]
    PermissionDenied,
    /// IO error
    #[error("{0}")]
    Io(#[from] std::io::Error),
}

/// Virtual filesystem instance
///
/// One instance per plugin, bound to the `{vfs_root}/{plugin_id}/` directory.
pub struct VirtualFs {
    root: PathBuf,
    max_file_size: usize,
    max_total_size: usize,
    can_write: bool,
    can_read: bool,
}

impl VirtualFs {
    /// Create a VFS instance from config and permissions
    ///
    /// Automatically creates the sandbox root directory.
    #[must_use]
    pub fn new(config: &AppConfig, plugin_id: &str, permissions: &Permissions) -> Self {
        let root = PathBuf::from(&config.plugin_vfs_root).join(plugin_id);
        let can_read = permissions
            .filesystem
            .iter()
            .any(|p| p == "read" || p == "read-write" || p == "*");
        let can_write = permissions
            .filesystem
            .iter()
            .any(|p| p == "write" || p == "read-write" || p == "*");
        Self {
            root,
            max_file_size: config.plugin_vfs_max_file_size,
            max_total_size: config.plugin_vfs_max_total_size,
            can_read,
            can_write,
        }
    }

    /// Resolve virtual path to real path, detecting escapes
    fn resolve(&self, path: &str) -> Result<PathBuf, VfsError> {
        let cleaned = path.replace('\\', "/");
        let parts: Vec<&str> = cleaned.split('/').filter(|s| !s.is_empty()).collect();
        for part in &parts {
            if *part == ".." {
                return Err(VfsError::PathEscape);
            }
        }
        if parts.is_empty() {
            return Err(VfsError::PathEscape);
        }
        let virtual_path = parts.join("/");
        Ok(self.root.join(&virtual_path))
    }

    /// Calculate current used storage size
    fn used_size(&self) -> usize {
        dir_size(&self.root).unwrap_or(0)
    }

    /// Read file contents
    pub fn read_file(&self, path: &str) -> Result<String, VfsError> {
        if !self.can_read {
            return Err(VfsError::PermissionDenied);
        }
        let real = self.resolve(path)?;
        fs::read_to_string(&real).map_err(VfsError::Io)
    }

    /// Write file (auto-creates intermediate directories)
    pub fn write_file(&self, path: &str, content: &str) -> Result<(), VfsError> {
        if !self.can_write {
            return Err(VfsError::PermissionDenied);
        }
        let content_len = content.len();
        if content_len > self.max_file_size {
            return Err(VfsError::FileTooLarge {
                max: self.max_file_size,
            });
        }
        let real = self.resolve(path)?;
        let used = self.used_size();
        let existing_len = fs::metadata(&real).map(|m| m.len() as usize).unwrap_or(0);
        let new_used = used.saturating_sub(existing_len) + content_len;
        if new_used > self.max_total_size {
            return Err(VfsError::QuotaExceeded {
                used,
                max: self.max_total_size,
                need: content_len,
            });
        }
        if let Some(parent) = real.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(&real, content)?;
        Ok(())
    }

    /// Delete file
    pub fn delete_file(&self, path: &str) -> Result<(), VfsError> {
        if !self.can_write {
            return Err(VfsError::PermissionDenied);
        }
        let real = self.resolve(path)?;
        if real.exists() {
            fs::remove_file(&real)?;
        }
        Ok(())
    }

    /// Check if a file exists
    pub fn exists(&self, path: &str) -> Result<bool, VfsError> {
        if !self.can_read {
            return Err(VfsError::PermissionDenied);
        }
        let real = self.resolve(path)?;
        Ok(real.exists())
    }

    /// List files and subdirectories under a directory
    pub fn list_dir(&self, path: &str) -> Result<Vec<String>, VfsError> {
        if !self.can_read {
            return Err(VfsError::PermissionDenied);
        }
        let dir_path = if path.is_empty() || path == "/" {
            self.root.clone()
        } else {
            self.resolve(path)?
        };
        if !dir_path.exists() {
            return Ok(vec![]);
        }
        let mut entries = Vec::new();
        let entries_iter = fs::read_dir(&dir_path)?;
        for entry in entries_iter {
            let entry = entry?;
            let name = entry.file_name().to_string_lossy().to_string();
            let file_type = entry.file_type()?;
            if file_type.is_dir() {
                entries.push(format!("{name}/"));
            } else {
                entries.push(name);
            }
        }
        entries.sort();
        Ok(entries)
    }

    /// Get file metadata
    pub fn stat(&self, path: &str) -> Result<VfsFileInfo, VfsError> {
        if !self.can_read {
            return Err(VfsError::PermissionDenied);
        }
        let real = self.resolve(path)?;
        let meta = fs::metadata(&real)?;
        Ok(VfsFileInfo {
            size: meta.len() as usize,
            is_dir: meta.is_dir(),
            modified: meta
                .modified()
                .ok()
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| d.as_secs()),
        })
    }

    /// Return sandbox root directory path
    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }
}

/// File metadata
#[derive(Debug, serde::Serialize)]
pub struct VfsFileInfo {
    pub size: usize,
    pub is_dir: bool,
    pub modified: Option<u64>,
}

/// Recursively calculate directory size
fn dir_size(path: &Path) -> Result<usize, std::io::Error> {
    if !path.exists() {
        return Ok(0);
    }
    let mut total = 0usize;
    dir_size_recursive(path, &mut total)?;
    Ok(total)
}

fn dir_size_recursive(path: &Path, total: &mut usize) -> Result<(), std::io::Error> {
    for entry in fs::read_dir(path)? {
        let entry = entry?;
        let meta = entry.metadata()?;
        if meta.is_dir() {
            dir_size_recursive(&entry.path(), total)?;
        } else {
            *total += meta.len() as usize;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    fn make_test_config() -> Arc<AppConfig> {
        let mut config = AppConfig::test_defaults();
        config.plugin_vfs_root = tempfile::tempdir()
            .unwrap()
            .path()
            .to_string_lossy()
            .to_string();
        config.plugin_vfs_max_file_size = 1024;
        config.plugin_vfs_max_total_size = 4096;
        Arc::new(config)
    }

    fn make_rw_perms() -> Permissions {
        Permissions {
            filesystem: vec!["read-write".into()],
            ..Permissions::default()
        }
    }

    fn make_ro_perms() -> Permissions {
        Permissions {
            filesystem: vec!["read".into()],
            ..Permissions::default()
        }
    }

    fn make_wo_perms() -> Permissions {
        Permissions {
            filesystem: vec!["write".into()],
            ..Permissions::default()
        }
    }

    #[test]
    fn write_and_read_roundtrip() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("hello.txt", "hello world").unwrap();
        let content = vfs.read_file("hello.txt").unwrap();
        assert_eq!(content, "hello world");
    }

    #[test]
    fn write_creates_subdirectories() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("templates/index.html", "<h1>Hi</h1>")
            .unwrap();
        let content = vfs.read_file("templates/index.html").unwrap();
        assert_eq!(content, "<h1>Hi</h1>");
    }

    #[test]
    fn path_escape_blocked() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        assert!(vfs.resolve("../etc/passwd").is_err());
        assert!(vfs.resolve("foo/../../bar").is_err());
        assert!(vfs.write_file("../escape.txt", "nope").is_err());
    }

    #[test]
    fn file_too_large() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        let big = "x".repeat(2048);
        let result = vfs.write_file("big.txt", &big);
        assert!(matches!(result, Err(VfsError::FileTooLarge { .. })));
    }

    #[test]
    fn quota_exceeded() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("a.txt", &"a".repeat(1000)).unwrap();
        vfs.write_file("b.txt", &"b".repeat(1000)).unwrap();
        vfs.write_file("c.txt", &"c".repeat(1000)).unwrap();
        vfs.write_file("d.txt", &"d".repeat(1000)).unwrap();
        let result = vfs.write_file("e.txt", &"e".repeat(100));
        match &result {
            Err(VfsError::QuotaExceeded { .. }) => {}
            other => panic!("expected QuotaExceeded, got: {other:?}"),
        }
    }

    #[test]
    fn overwrite_within_quota() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("data.txt", &"x".repeat(1000)).unwrap();
        vfs.write_file("data.txt", &"y".repeat(500)).unwrap();
        assert_eq!(vfs.read_file("data.txt").unwrap(), "y".repeat(500));
    }

    #[test]
    fn delete_file() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("temp.txt", "temp").unwrap();
        assert!(vfs.exists("temp.txt").unwrap());
        vfs.delete_file("temp.txt").unwrap();
        assert!(!vfs.exists("temp.txt").unwrap());
    }

    #[test]
    fn delete_nonexistent_ok() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        assert!(vfs.delete_file("nope.txt").is_ok());
    }

    #[test]
    fn exists_check() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        assert!(!vfs.exists("missing.txt").unwrap());
        vfs.write_file("found.txt", "yes").unwrap();
        assert!(vfs.exists("found.txt").unwrap());
    }

    #[test]
    fn list_dir_contents() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("a.txt", "a").unwrap();
        vfs.write_file("b.txt", "b").unwrap();
        vfs.write_file("sub/c.txt", "c").unwrap();
        let entries = vfs.list_dir("").unwrap();
        assert!(entries.contains(&"a.txt".to_string()));
        assert!(entries.contains(&"b.txt".to_string()));
        assert!(entries.contains(&"sub/".to_string()));
    }

    #[test]
    fn list_nonexistent_dir_returns_empty() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        let entries = vfs.list_dir("nonexistent").unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn stat_file() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "test-plugin", &make_rw_perms());
        vfs.write_file("info.txt", "hello").unwrap();
        let info = vfs.stat("info.txt").unwrap();
        assert_eq!(info.size, 5);
        assert!(!info.is_dir);
        assert!(info.modified.is_some());
    }

    #[test]
    fn read_without_permission_denied() {
        let config = make_test_config();
        let vfs_rw = VirtualFs::new(&config, "p1", &make_rw_perms());
        vfs_rw.write_file("secret.txt", "data").unwrap();
        let vfs_ro = VirtualFs::new(&config, "p1", &make_wo_perms());
        assert!(matches!(
            vfs_ro.read_file("secret.txt"),
            Err(VfsError::PermissionDenied)
        ));
    }

    #[test]
    fn write_without_permission_denied() {
        let config = make_test_config();
        let vfs = VirtualFs::new(&config, "p1", &make_ro_perms());
        assert!(matches!(
            vfs.write_file("file.txt", "data"),
            Err(VfsError::PermissionDenied)
        ));
    }

    #[test]
    fn delete_without_permission_denied() {
        let config = make_test_config();
        let vfs_rw = VirtualFs::new(&config, "p1", &make_rw_perms());
        vfs_rw.write_file("f.txt", "x").unwrap();
        let vfs_ro = VirtualFs::new(&config, "p1", &make_ro_perms());
        assert!(matches!(
            vfs_ro.delete_file("f.txt"),
            Err(VfsError::PermissionDenied)
        ));
    }

    #[test]
    fn wildcard_permission_allows_all() {
        let config = make_test_config();
        let perms = Permissions {
            filesystem: vec!["*".into()],
            ..Permissions::default()
        };
        let vfs = VirtualFs::new(&config, "p1", &perms);
        vfs.write_file("w.txt", "ok").unwrap();
        assert_eq!(vfs.read_file("w.txt").unwrap(), "ok");
    }

    #[test]
    fn no_filesystem_permission_blocks_all() {
        let config = make_test_config();
        let vfs_rw = VirtualFs::new(&config, "p1", &make_rw_perms());
        vfs_rw.write_file("f.txt", "x").unwrap();
        let vfs_none = VirtualFs::new(&config, "p1", &Permissions::default());
        assert!(matches!(
            vfs_none.read_file("f.txt"),
            Err(VfsError::PermissionDenied)
        ));
        assert!(matches!(
            vfs_none.write_file("f.txt", "y"),
            Err(VfsError::PermissionDenied)
        ));
        assert!(matches!(
            vfs_none.exists("f.txt"),
            Err(VfsError::PermissionDenied)
        ));
        assert!(matches!(
            vfs_none.list_dir(""),
            Err(VfsError::PermissionDenied)
        ));
        assert!(matches!(
            vfs_none.stat("f.txt"),
            Err(VfsError::PermissionDenied)
        ));
    }

    #[test]
    fn isolation_between_plugins() {
        let config = make_test_config();
        let vfs_a = VirtualFs::new(&config, "plugin-a", &make_rw_perms());
        let vfs_b = VirtualFs::new(&config, "plugin-b", &make_rw_perms());
        vfs_a.write_file("data.txt", "from-a").unwrap();
        vfs_b.write_file("data.txt", "from-b").unwrap();
        assert_eq!(vfs_a.read_file("data.txt").unwrap(), "from-a");
        assert_eq!(vfs_b.read_file("data.txt").unwrap(), "from-b");
    }

    #[test]
    fn error_display_human_readable() {
        assert!(VfsError::PathEscape.to_string().contains("escape"));
        assert!(
            VfsError::PermissionDenied
                .to_string()
                .contains("permission")
        );
        assert!(
            VfsError::FileTooLarge { max: 1024 }
                .to_string()
                .contains("1024")
        );
        assert!(
            VfsError::QuotaExceeded {
                used: 100,
                max: 200,
                need: 150,
            }
            .to_string()
            .contains("quota")
        );
    }
}