aether/sandbox/
context.rs1use super::PathValidator;
6use std::cell::RefCell;
7
8thread_local! {
10 static FILESYSTEM_VALIDATOR: RefCell<Option<PathValidator>> = const { RefCell::new(None) };
11}
12
13pub fn set_filesystem_validator(validator: Option<PathValidator>) {
15 FILESYSTEM_VALIDATOR.with(|v| *v.borrow_mut() = validator);
16}
17
18pub fn get_filesystem_validator() -> Option<PathValidator> {
20 FILESYSTEM_VALIDATOR.with(|v| v.borrow().clone())
21}
22
23pub struct ScopedValidator {
25 _private: (),
26}
27
28impl ScopedValidator {
29 pub fn set(validator: PathValidator) -> Self {
31 set_filesystem_validator(Some(validator));
32 Self { _private: () }
33 }
34}
35
36impl Drop for ScopedValidator {
37 fn drop(&mut self) {
38 set_filesystem_validator(None);
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_validator_context() {
48 let validator = PathValidator::with_root_dir(std::path::PathBuf::from("/safe"));
49
50 set_filesystem_validator(Some(validator.clone()));
52
53 let retrieved = get_filesystem_validator().unwrap();
55 assert_eq!(
56 retrieved.restriction().root_dir,
57 validator.restriction().root_dir
58 );
59
60 set_filesystem_validator(None);
62 assert!(get_filesystem_validator().is_none());
63 }
64
65 #[test]
66 fn test_scoped_validator() {
67 assert!(get_filesystem_validator().is_none());
68
69 {
70 let validator = PathValidator::with_root_dir(std::path::PathBuf::from("/safe"));
71 let _scope = ScopedValidator::set(validator);
72
73 assert!(get_filesystem_validator().is_some());
75 }
76
77 assert!(get_filesystem_validator().is_none());
79 }
80}