pub mod registry;
use std::fmt;
use std::path::{Path, PathBuf};
use crate::limits::NseExecutionLimits;
use crate::profile::{NseModulePolicy, NseScriptPolicy};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NseModuleName(String);
impl NseModuleName {
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
}
impl fmt::Display for NseModuleName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
const MAX_MODULE_NAME_LEN: usize = 256;
pub fn validate_nse_module_name(name: &str) -> Result<NseModuleName, NseLoadError> {
if name.is_empty() {
return Err(NseLoadError::InvalidModuleName {
name: name.to_string(),
reason: "module name is empty".to_string(),
});
}
if name.len() > MAX_MODULE_NAME_LEN {
return Err(NseLoadError::InvalidModuleName {
name: name.to_string(),
reason: format!(
"module name exceeds maximum length ({} > {})",
name.len(),
MAX_MODULE_NAME_LEN
),
});
}
if name.starts_with('.') {
return Err(NseLoadError::InvalidModuleName {
name: name.to_string(),
reason: "module name must not start with '.'".to_string(),
});
}
if name.contains("..") {
return Err(NseLoadError::InvalidModuleName {
name: name.to_string(),
reason: "module name must not contain '..'".to_string(),
});
}
for ch in name.chars() {
if !ch.is_ascii_alphanumeric() && ch != '_' && ch != '-' && ch != '.' {
return Err(NseLoadError::InvalidModuleName {
name: name.to_string(),
reason: format!("module name contains forbidden character '{}'", ch),
});
}
}
Ok(NseModuleName(name.to_string()))
}
#[derive(Debug, Clone)]
pub enum NseScriptSource {
Builtin { name: String },
TrustedRegistry { name: String },
File { path: PathBuf },
InlineManual { label: String, content: String },
}
impl fmt::Display for NseScriptSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Builtin { name } => write!(f, "builtin:{}", name),
Self::TrustedRegistry { name } => write!(f, "registry:{}", name),
Self::File { path } => write!(f, "file:{}", path.display()),
Self::InlineManual { label, .. } => write!(f, "inline:{}", label),
}
}
}
#[derive(Debug, Clone)]
pub enum NseLoadError {
NotFound { source: NseScriptSource },
BlockedByPolicy {
source: NseScriptSource,
reason: String,
},
OutsideRoot {
path: PathBuf,
approved_roots: Vec<PathBuf>,
},
SymlinkEscape { path: PathBuf, resolved: PathBuf },
InvalidExtension { path: PathBuf, extension: String },
Oversized {
source: NseScriptSource,
size: usize,
limit: usize,
},
InvalidModuleName { name: String, reason: String },
IoError { path: PathBuf, error: String },
EvalError {
source: NseScriptSource,
error: String,
},
}
impl fmt::Display for NseLoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotFound { source } => write!(f, "not found: {}", source),
Self::BlockedByPolicy { source, reason } => {
write!(f, "blocked by policy: {} ({})", source, reason)
}
Self::OutsideRoot {
path,
approved_roots,
} => write!(
f,
"path outside approved roots: {} (roots: {:?})",
path.display(),
approved_roots
),
Self::SymlinkEscape { path, resolved } => write!(
f,
"symlink escapes approved roots: {} -> {}",
path.display(),
resolved.display()
),
Self::InvalidExtension { path, extension } => {
write!(f, "invalid extension '{}': {}", extension, path.display())
}
Self::Oversized {
source,
size,
limit,
} => write!(
f,
"oversized content from {}: {} bytes (limit: {})",
source, size, limit
),
Self::InvalidModuleName { name, reason } => {
write!(f, "invalid module name '{}': {}", name, reason)
}
Self::IoError { path, error } => {
write!(f, "I/O error reading {}: {}", path.display(), error)
}
Self::EvalError { source, error } => {
write!(f, "eval error for {}: {}", source, error)
}
}
}
}
impl std::error::Error for NseLoadError {}
#[cfg(feature = "nse")]
impl From<NseLoadError> for mlua::Error {
fn from(e: NseLoadError) -> Self {
mlua::Error::RuntimeError(e.to_string())
}
}
#[derive(Debug, Clone)]
pub enum NseLoadDiagnostic {
Resolved {
source: NseScriptSource,
bytes: usize,
},
Blocked {
source: NseScriptSource,
reason: String,
},
OutsideRoot { path: PathBuf, root: PathBuf },
SymlinkRejected { path: PathBuf, resolved: PathBuf },
ModuleNameRejected { name: String, reason: String },
OversizedRejected {
source: NseScriptSource,
size: usize,
limit: usize,
},
ModuleLoadFailed {
name: String,
path: PathBuf,
error: String,
},
}
#[derive(Debug, Clone)]
pub struct ResolvedNseScript {
pub source: NseScriptSource,
pub content: String,
pub size: usize,
}
#[derive(Debug, Clone)]
pub struct ResolvedNseModule {
pub name: NseModuleName,
pub content: String,
pub size: usize,
pub path: Option<PathBuf>,
}
const ALLOWED_SCRIPT_EXTENSIONS: &[&str] = &["lua", "nse"];
const ALLOWED_MODULE_EXTENSIONS: &[&str] = &["lua", "nse"];
fn has_allowed_extension(path: &Path, allowed: &[&str]) -> bool {
path.extension()
.and_then(|e| e.to_str())
.map(|ext| allowed.contains(&ext))
.unwrap_or(false)
}
fn try_canonicalize(path: &Path) -> Option<PathBuf> {
path.canonicalize().ok()
}
fn canonicalize_root(path: &Path) -> Option<PathBuf> {
path.canonicalize().ok()
}
fn is_under_root(canonical_path: &Path, canonical_root: &Path) -> bool {
canonical_path.strip_prefix(canonical_root).is_ok()
}
fn validate_existing_path_under_roots(
path: &Path,
approved_roots: &[PathBuf],
) -> Result<PathBuf, NseLoadError> {
let canonical = try_canonicalize(path).ok_or_else(|| NseLoadError::IoError {
path: path.to_path_buf(),
error: "path does not exist or cannot be canonicalized".to_string(),
})?;
for root in approved_roots {
if let Some(canonical_root) = canonicalize_root(root) {
if is_under_root(&canonical, &canonical_root) {
return Ok(canonical);
}
}
}
Err(NseLoadError::OutsideRoot {
path: path.to_path_buf(),
approved_roots: approved_roots.to_vec(),
})
}
#[allow(dead_code)]
fn validate_parent_under_roots(
path: &Path,
approved_roots: &[PathBuf],
) -> Result<PathBuf, NseLoadError> {
let parent = path.parent().ok_or_else(|| NseLoadError::OutsideRoot {
path: path.to_path_buf(),
approved_roots: approved_roots.to_vec(),
})?;
let canonical_parent = try_canonicalize(parent).ok_or_else(|| NseLoadError::OutsideRoot {
path: path.to_path_buf(),
approved_roots: approved_roots.to_vec(),
})?;
for root in approved_roots {
if let Some(canonical_root) = canonicalize_root(root) {
if is_under_root(&canonical_parent, &canonical_root) {
return Ok(canonical_parent.join(path.file_name().unwrap_or_default()));
}
}
}
Err(NseLoadError::OutsideRoot {
path: path.to_path_buf(),
approved_roots: approved_roots.to_vec(),
})
}
fn validate_symlink_containment(
path: &Path,
approved_roots: &[PathBuf],
) -> Result<PathBuf, NseLoadError> {
let canonical = path.canonicalize().map_err(|e| NseLoadError::IoError {
path: path.to_path_buf(),
error: e.to_string(),
})?;
for root in approved_roots {
if let Some(canonical_root) = canonicalize_root(root) {
if is_under_root(&canonical, &canonical_root) {
return Ok(canonical);
}
}
}
Err(NseLoadError::SymlinkEscape {
path: path.to_path_buf(),
resolved: canonical,
})
}
const BUILTIN_SCRIPT_NAMES: &[&str] = &[
"default",
"discovery",
"banner",
"http-headers",
"dns-check",
"ssl-cert",
];
pub fn is_builtin_script(name: &str) -> bool {
BUILTIN_SCRIPT_NAMES.contains(&name)
}
pub struct ScriptResolver {
script_policy: NseScriptPolicy,
module_policy: NseModulePolicy,
limits: NseExecutionLimits,
diagnostics: Vec<NseLoadDiagnostic>,
}
impl ScriptResolver {
pub fn new(
script_policy: NseScriptPolicy,
module_policy: NseModulePolicy,
limits: NseExecutionLimits,
) -> Self {
Self {
script_policy,
module_policy,
limits,
diagnostics: Vec::new(),
}
}
pub fn diagnostics(&self) -> &[NseLoadDiagnostic] {
&self.diagnostics
}
pub fn take_diagnostics(&mut self) -> Vec<NseLoadDiagnostic> {
std::mem::take(&mut self.diagnostics)
}
pub fn clear_diagnostics(&mut self) {
self.diagnostics.clear();
}
pub fn resolve_script(
&mut self,
source: NseScriptSource,
) -> Result<ResolvedNseScript, NseLoadError> {
match &source {
NseScriptSource::Builtin { name } => self.resolve_builtin(name.clone()),
NseScriptSource::TrustedRegistry { name: _ } => {
Err(NseLoadError::NotFound {
source: source.clone(),
})
}
NseScriptSource::File { path } => self.resolve_script_file(path.clone(), source),
NseScriptSource::InlineManual { label, content } => {
self.resolve_inline(label.clone(), content.clone(), source)
}
}
}
fn resolve_builtin(&mut self, name: String) -> Result<ResolvedNseScript, NseLoadError> {
if !self.script_policy.allow_builtin_scripts {
return Err(NseLoadError::BlockedByPolicy {
source: NseScriptSource::Builtin { name: name.clone() },
reason: "builtin scripts not allowed by profile".to_string(),
});
}
let source = NseScriptSource::Builtin { name };
let content = String::new(); let size = 0;
self.diagnostics.push(NseLoadDiagnostic::Resolved {
source: source.clone(),
bytes: size,
});
Ok(ResolvedNseScript {
source,
content,
size,
})
}
fn resolve_script_file(
&mut self,
path: PathBuf,
source: NseScriptSource,
) -> Result<ResolvedNseScript, NseLoadError> {
if !self.script_policy.allow_script_files {
self.diagnostics.push(NseLoadDiagnostic::Blocked {
source: source.clone(),
reason: "script files not allowed by profile".to_string(),
});
return Err(NseLoadError::BlockedByPolicy {
source: source.clone(),
reason: "script files not allowed by profile".to_string(),
});
}
if !path.exists() {
return Err(NseLoadError::NotFound {
source: source.clone(),
});
}
if !has_allowed_extension(&path, ALLOWED_SCRIPT_EXTENSIONS) {
let ext = path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_string();
return Err(NseLoadError::InvalidExtension {
path: path.clone(),
extension: ext,
});
}
if !self.script_policy.allowed_script_roots.is_empty() {
validate_existing_path_under_roots(&path, &self.script_policy.allowed_script_roots)?;
}
let canonical = if self.script_policy.allowed_script_roots.is_empty() {
path.canonicalize().map_err(|e| NseLoadError::IoError {
path: path.clone(),
error: e.to_string(),
})?
} else {
validate_symlink_containment(&path, &self.script_policy.allowed_script_roots)?
};
let content = std::fs::read_to_string(&canonical).map_err(|e| NseLoadError::IoError {
path: canonical.clone(),
error: e.to_string(),
})?;
let size = content.len();
if let Some(max) = self.limits.max_script_bytes {
if size > max {
self.diagnostics.push(NseLoadDiagnostic::OversizedRejected {
source: source.clone(),
size,
limit: max,
});
return Err(NseLoadError::Oversized {
source: source.clone(),
size,
limit: max,
});
}
}
if let Some(max) = self.script_policy.max_script_bytes {
if size > max {
self.diagnostics.push(NseLoadDiagnostic::OversizedRejected {
source: source.clone(),
size,
limit: max,
});
return Err(NseLoadError::Oversized {
source: source.clone(),
size,
limit: max,
});
}
}
self.diagnostics.push(NseLoadDiagnostic::Resolved {
source: source.clone(),
bytes: size,
});
Ok(ResolvedNseScript {
source,
content,
size,
})
}
fn resolve_inline(
&mut self,
_label: String,
content: String,
source: NseScriptSource,
) -> Result<ResolvedNseScript, NseLoadError> {
let size = content.len();
if let Some(max) = self.limits.max_script_bytes {
if size > max {
self.diagnostics.push(NseLoadDiagnostic::OversizedRejected {
source: source.clone(),
size,
limit: max,
});
return Err(NseLoadError::Oversized {
source: source.clone(),
size,
limit: max,
});
}
}
if let Some(max) = self.script_policy.max_script_bytes {
if size > max {
self.diagnostics.push(NseLoadDiagnostic::OversizedRejected {
source: source.clone(),
size,
limit: max,
});
return Err(NseLoadError::Oversized {
source: source.clone(),
size,
limit: max,
});
}
}
self.diagnostics.push(NseLoadDiagnostic::Resolved {
source: source.clone(),
bytes: size,
});
Ok(ResolvedNseScript {
source,
content,
size,
})
}
pub fn resolve_module(
&mut self,
name: &str,
) -> Result<Option<ResolvedNseModule>, NseLoadError> {
let validated_name = validate_nse_module_name(name).inspect_err(|e| {
self.diagnostics
.push(NseLoadDiagnostic::ModuleNameRejected {
name: name.to_string(),
reason: e.to_string(),
});
})?;
if !self.module_policy.allow_filesystem_modules {
return Ok(None);
}
if self.module_policy.allowed_module_roots.is_empty() {
return Ok(None);
}
for root in &self.module_policy.allowed_module_roots {
let Some(canonical_root) = canonicalize_root(root) else {
continue;
};
for ext in ALLOWED_MODULE_EXTENSIONS {
let candidate = canonical_root.join(format!("{}.{}", validated_name.as_str(), ext));
if !candidate.exists() {
continue;
}
match validate_existing_path_under_roots(
&candidate,
&self.module_policy.allowed_module_roots,
) {
Ok(canonical_path) => {
match validate_symlink_containment(
&canonical_path,
&self.module_policy.allowed_module_roots,
) {
Ok(safe_path) => {
match std::fs::read_to_string(&safe_path) {
Ok(content) => {
let size = content.len();
if let Some(max) = self.limits.max_required_module_bytes {
if size > max {
self.diagnostics.push(
NseLoadDiagnostic::OversizedRejected {
source: NseScriptSource::File {
path: safe_path.clone(),
},
size,
limit: max,
},
);
return Err(NseLoadError::Oversized {
source: NseScriptSource::File {
path: safe_path.clone(),
},
size,
limit: max,
});
}
}
if let Some(max) = self.module_policy.max_module_bytes {
if size > max {
self.diagnostics.push(
NseLoadDiagnostic::OversizedRejected {
source: NseScriptSource::File {
path: safe_path.clone(),
},
size,
limit: max,
},
);
return Err(NseLoadError::Oversized {
source: NseScriptSource::File {
path: safe_path.clone(),
},
size,
limit: max,
});
}
}
self.diagnostics.push(NseLoadDiagnostic::Resolved {
source: NseScriptSource::File {
path: safe_path.clone(),
},
bytes: size,
});
return Ok(Some(ResolvedNseModule {
name: validated_name,
content,
size,
path: Some(safe_path),
}));
}
Err(e) => {
self.diagnostics.push(
NseLoadDiagnostic::ModuleLoadFailed {
name: name.to_string(),
path: safe_path,
error: e.to_string(),
},
);
}
}
}
Err(NseLoadError::SymlinkEscape { path, resolved }) => {
self.diagnostics
.push(NseLoadDiagnostic::SymlinkRejected { path, resolved });
}
Err(e) => return Err(e),
}
}
Err(NseLoadError::OutsideRoot {
path,
approved_roots,
}) => {
self.diagnostics.push(NseLoadDiagnostic::OutsideRoot {
path,
root: approved_roots.first().cloned().unwrap_or_default(),
});
}
Err(e) => return Err(e),
}
}
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_module_name_valid() {
assert!(validate_nse_module_name("stdnse").is_ok());
assert!(validate_nse_module_name("http").is_ok());
assert!(validate_nse_module_name("my-module").is_ok());
assert!(validate_nse_module_name("my_module").is_ok());
assert!(validate_nse_module_name("module.v2").is_ok());
assert!(validate_nse_module_name("a1b2c3").is_ok());
}
#[test]
fn test_validate_module_name_empty() {
assert!(matches!(
validate_nse_module_name(""),
Err(NseLoadError::InvalidModuleName { .. })
));
}
#[test]
fn test_validate_module_name_starts_with_dot() {
assert!(matches!(
validate_nse_module_name(".hidden"),
Err(NseLoadError::InvalidModuleName { .. })
));
}
#[test]
fn test_validate_module_name_traversal() {
assert!(matches!(
validate_nse_module_name("..etc/passwd"),
Err(NseLoadError::InvalidModuleName { .. })
));
assert!(matches!(
validate_nse_module_name("foo/../bar"),
Err(NseLoadError::InvalidModuleName { .. })
));
}
#[test]
fn test_validate_module_name_forbidden_chars() {
assert!(validate_nse_module_name("foo/bar").is_err());
assert!(validate_nse_module_name("foo\\bar").is_err());
assert!(validate_nse_module_name("foo:bar").is_err());
assert!(validate_nse_module_name("foo~bar").is_err());
assert!(validate_nse_module_name("foo\0bar").is_err());
assert!(validate_nse_module_name("foo bar").is_err());
assert!(validate_nse_module_name("foo*bar").is_err());
assert!(validate_nse_module_name("foo?bar").is_err());
assert!(validate_nse_module_name("foo{bar").is_err());
}
#[test]
fn test_validate_module_name_too_long() {
let long_name = "a".repeat(257);
assert!(matches!(
validate_nse_module_name(&long_name),
Err(NseLoadError::InvalidModuleName { .. })
));
}
#[test]
fn test_validate_module_name_max_length() {
let max_name = "a".repeat(256);
assert!(validate_nse_module_name(&max_name).is_ok());
}
#[test]
fn test_script_source_display() {
assert_eq!(
NseScriptSource::Builtin {
name: "default".to_string()
}
.to_string(),
"builtin:default"
);
assert_eq!(
NseScriptSource::File {
path: PathBuf::from("/tmp/test.lua")
}
.to_string(),
"file:/tmp/test.lua"
);
assert_eq!(
NseLoadError::NotFound {
source: NseScriptSource::Builtin {
name: "x".to_string()
}
}
.to_string(),
"not found: builtin:x"
);
}
#[test]
fn test_is_builtin_script() {
assert!(is_builtin_script("default"));
assert!(is_builtin_script("discovery"));
assert!(is_builtin_script("banner"));
assert!(is_builtin_script("http-headers"));
assert!(is_builtin_script("dns-check"));
assert!(is_builtin_script("ssl-cert"));
assert!(!is_builtin_script("custom"));
assert!(!is_builtin_script(""));
}
#[test]
fn test_has_allowed_extension() {
assert!(has_allowed_extension(
Path::new("test.lua"),
ALLOWED_SCRIPT_EXTENSIONS
));
assert!(has_allowed_extension(
Path::new("test.nse"),
ALLOWED_SCRIPT_EXTENSIONS
));
assert!(!has_allowed_extension(
Path::new("test.txt"),
ALLOWED_SCRIPT_EXTENSIONS
));
assert!(!has_allowed_extension(
Path::new("test"),
ALLOWED_SCRIPT_EXTENSIONS
));
}
#[test]
fn test_resolver_rejects_script_file_when_policy_blocks() {
let profile = crate::profile::ResolvedNseExecutionProfile::agent_safe("10.0.0.1", &[]);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let result = resolver.resolve_script(NseScriptSource::File {
path: PathBuf::from("/tmp/test.lua"),
});
assert!(result.is_err());
match result.unwrap_err() {
NseLoadError::BlockedByPolicy { reason, .. } => {
assert!(reason.contains("not allowed"));
}
other => panic!("Expected BlockedByPolicy, got {:?}", other),
}
}
#[test]
fn test_resolver_rejects_oversized_inline_script() {
let profile = crate::profile::ResolvedNseExecutionProfile::agent_safe("10.0.0.1", &[]);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let large_content = "x".repeat(1024 * 1024 + 1);
let result = resolver.resolve_script(NseScriptSource::InlineManual {
label: "test".to_string(),
content: large_content,
});
assert!(result.is_err());
match result.unwrap_err() {
NseLoadError::Oversized { size, limit, .. } => {
assert!(size > limit);
}
other => panic!("Expected Oversized, got {:?}", other),
}
}
#[test]
fn test_resolver_allows_inline_script_within_limits() {
let profile = crate::profile::ResolvedNseExecutionProfile::manual_permissive(None);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let result = resolver.resolve_script(NseScriptSource::InlineManual {
label: "test".to_string(),
content: "return 1".to_string(),
});
assert!(result.is_ok());
let resolved = result.unwrap();
assert_eq!(resolved.content, "return 1");
}
#[test]
fn test_resolver_rejects_module_name_traversal() {
let profile = crate::profile::ResolvedNseExecutionProfile::agent_safe("10.0.0.1", &[]);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let result = resolver.resolve_module("../../etc/passwd");
assert!(result.is_err());
}
#[test]
fn test_resolver_rejects_module_name_slash() {
let profile = crate::profile::ResolvedNseExecutionProfile::agent_safe("10.0.0.1", &[]);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let result = resolver.resolve_module("foo/bar");
assert!(result.is_err());
}
#[test]
fn test_resolver_returns_none_for_module_when_filesystem_disallowed() {
let profile = crate::profile::ResolvedNseExecutionProfile::agent_safe("10.0.0.1", &[]);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let result = resolver.resolve_module("stdnse").unwrap();
assert!(result.is_none());
}
#[test]
fn test_resolver_allows_builtin_script() {
let profile = crate::profile::ResolvedNseExecutionProfile::manual_permissive(None);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let result = resolver.resolve_script(NseScriptSource::Builtin {
name: "default".to_string(),
});
assert!(result.is_ok());
}
#[test]
fn test_resolver_rejects_builtin_when_policy_blocks() {
let mut script_policy = crate::profile::NseScriptPolicy {
allow_builtin_scripts: false,
allow_script_files: false,
allowed_script_roots: Vec::new(),
allow_conventional_nmap_paths: false,
max_script_bytes: None,
};
let mut resolver = ScriptResolver::new(
script_policy,
crate::profile::NseModulePolicy {
allow_builtin_modules: true,
allow_filesystem_modules: false,
allowed_module_roots: Vec::new(),
max_module_bytes: None,
},
NseExecutionLimits::default(),
);
let result = resolver.resolve_script(NseScriptSource::Builtin {
name: "default".to_string(),
});
assert!(result.is_err());
}
#[test]
fn test_diagnostics_accumulate() {
let profile = crate::profile::ResolvedNseExecutionProfile::agent_safe("10.0.0.1", &[]);
let mut resolver =
ScriptResolver::new(profile.script_policy, profile.module_policy, profile.limits);
let _ = resolver.resolve_script(NseScriptSource::File {
path: PathBuf::from("/tmp/test.lua"),
});
let diags = resolver.take_diagnostics();
assert!(!diags.is_empty());
}
#[test]
fn test_is_under_root_component_semantics() {
let root = PathBuf::from("/tmp/root");
let child = PathBuf::from("/tmp/root2/file.nse");
assert!(!is_under_root(&child, &root));
let child = PathBuf::from("/tmp/root/file.nse");
assert!(is_under_root(&child, &root));
let child = PathBuf::from("/tmp/root/sub/file.nse");
assert!(is_under_root(&child, &root));
}
#[test]
fn test_validate_existing_path_under_roots_rejects_sibling_prefix() {
let roots = vec![PathBuf::from("/tmp/root")];
let result = validate_existing_path_under_roots(Path::new("/tmp/root2/file.nse"), &roots);
assert!(matches!(result, Err(NseLoadError::IoError { .. })));
}
#[test]
fn test_validate_existing_path_under_roots_rejects_sibling_prefix_existing_file() {
let tmp = std::env::temp_dir().join("eggsec-nse-sibling-prefix-test");
let root = tmp.join("root");
let evil = tmp.join("root2");
let _ = std::fs::create_dir_all(&root);
let _ = std::fs::create_dir_all(&evil);
std::fs::write(evil.join("real.nse"), "-- content").unwrap();
let result = validate_existing_path_under_roots(&evil.join("real.nse"), &[root.clone()]);
assert!(matches!(result, Err(NseLoadError::OutsideRoot { .. })));
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn test_validate_existing_path_under_roots_rejects_nonexistent_path_outside_root() {
let roots = vec![PathBuf::from("/tmp/root")];
let result =
validate_existing_path_under_roots(Path::new("/tmp/outside/secret.lua"), &roots);
assert!(matches!(result, Err(NseLoadError::IoError { .. })));
}
#[test]
fn test_validate_existing_path_under_roots_rejects_nonexistent_path_inside_root() {
let tmp = std::env::temp_dir().join("eggsec-nse-read-nonexistent-test");
let _ = std::fs::create_dir_all(&tmp);
let roots = vec![tmp.clone()];
let missing = tmp.join("does-not-exist.nse");
let result = validate_existing_path_under_roots(&missing, &roots);
assert!(
matches!(result, Err(NseLoadError::IoError { .. })),
"Expected IoError for non-existent file, got: {:?}",
result
);
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn test_validate_existing_path_under_roots_accepts_existing_inside_root() {
let tmp = std::env::temp_dir().join("eggsec-nse-read-existing-test");
let _ = std::fs::create_dir_all(&tmp);
let file = tmp.join("real.nse");
std::fs::write(&file, "-- script").unwrap();
let roots = vec![tmp.clone()];
let result = validate_existing_path_under_roots(&file, &roots);
assert!(result.is_ok());
assert_eq!(result.unwrap(), file.canonicalize().unwrap());
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn test_validate_existing_path_under_roots_rejects_existing_outside_root() {
let tmp = std::env::temp_dir().join("eggsec-nse-read-outside-test");
let root = tmp.join("root");
let evil = tmp.join("root_evil");
let _ = std::fs::create_dir_all(&root);
let _ = std::fs::create_dir_all(&evil);
let file = evil.join("real.nse");
std::fs::write(&file, "-- script").unwrap();
let roots = vec![root.clone()];
let result = validate_existing_path_under_roots(&file, &roots);
assert!(matches!(result, Err(NseLoadError::OutsideRoot { .. })));
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn test_validate_symlink_containment_rejects_escape() {
let tmp = std::env::temp_dir().join("eggsec-nse-symlink-test");
let allowed = tmp.join("allowed");
let outside = tmp.join("outside");
let _ = std::fs::create_dir_all(&allowed);
let _ = std::fs::create_dir_all(&outside);
std::fs::write(outside.join("secret.txt"), "secret").unwrap();
let link = allowed.join("escape.txt");
#[cfg(unix)]
std::os::unix::fs::symlink(outside.join("secret.txt"), &link).unwrap();
let result = validate_symlink_containment(&link, &[allowed.clone()]);
assert!(result.is_err());
match result.unwrap_err() {
NseLoadError::SymlinkEscape { path, resolved } => {
assert_eq!(path, link);
assert!(resolved.starts_with(&outside));
}
other => panic!("Expected SymlinkEscape, got {:?}", other),
}
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn test_validate_symlink_containment_allows_internal_symlink() {
let tmp = std::env::temp_dir().join("eggsec-nse-symlink-ok-test");
let allowed = tmp.join("allowed");
let _ = std::fs::create_dir_all(allowed.join("sub"));
std::fs::write(allowed.join("real.txt"), "content").unwrap();
let link = allowed.join("sub").join("link.txt");
#[cfg(unix)]
std::os::unix::fs::symlink(allowed.join("real.txt"), &link).unwrap();
let result = validate_symlink_containment(&link, &[allowed.clone()]);
assert!(result.is_ok());
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn test_validate_existing_path_under_roots_rejects_absolute_traversal() {
let roots = vec![PathBuf::from("/tmp/root")];
let result = validate_existing_path_under_roots(Path::new("/etc/passwd"), &roots);
assert!(result.is_err());
}
#[test]
fn test_canonicalize_root_rejects_nonexistent() {
let result = canonicalize_root(Path::new("/tmp/nonexistent-root-eggsec-nse"));
assert!(result.is_none());
}
#[test]
fn test_canonicalize_root_accepts_existing() {
let tmp = std::env::temp_dir().join("eggsec-nse-root-test");
let _ = std::fs::create_dir_all(&tmp);
let result = canonicalize_root(&tmp);
assert!(result.is_some());
let _ = std::fs::remove_dir_all(&tmp);
}
}