use crate::security::fs_view::FsView;
use crate::security::fs_view::HostFs;
use std::marker::PhantomData;
pub struct ValidationContext<V: FsView = HostFs> {
symlink_seen: bool,
symlinks_allowed: bool,
view: PhantomData<fn() -> V>,
}
impl<V: FsView> ValidationContext<V> {
#[must_use]
pub fn new(symlinks_allowed: bool) -> Self {
Self {
symlink_seen: false,
symlinks_allowed,
view: PhantomData,
}
}
pub fn mark_symlink_seen(&mut self) {
self.symlink_seen = true;
}
#[inline]
pub fn needs_full_canonicalize(&self) -> bool {
self.symlinks_allowed || self.symlink_seen
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_context_no_optimizations() {
let ctx = ValidationContext::<HostFs>::new(false);
assert!(!ctx.needs_full_canonicalize());
}
#[test]
fn test_context_with_symlinks_allowed() {
let ctx = ValidationContext::<HostFs>::new(true);
assert!(ctx.needs_full_canonicalize());
}
#[test]
fn test_context_symlink_seen_enables_canonicalize() {
let mut ctx = ValidationContext::<HostFs>::new(false);
assert!(!ctx.needs_full_canonicalize());
ctx.mark_symlink_seen();
assert!(ctx.needs_full_canonicalize());
}
}