Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions src/cortex-execpolicy/src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ impl<'a> DetectionHelper<'a> {

/// Normalize a path for comparison.
pub fn normalize_path(path: &str) -> String {
let mut normalized = path.replace("//", "/");
let mut normalized = String::with_capacity(path.len());
let mut previous_was_slash = false;

for ch in path.chars() {
let ch = if ch == '\\' { '/' } else { ch };

if ch == '/' {
if previous_was_slash {
continue;
}
previous_was_slash = true;
} else {
previous_was_slash = false;
}

normalized.push(ch);
}

// Handle trailing slashes
while normalized.len() > 1 && normalized.ends_with('/') {
Expand All @@ -29,9 +45,6 @@ impl<'a> DetectionHelper<'a> {
normalized = normalized.replacen('~', "/home", 1);
}

// Handle Windows paths
normalized = normalized.replace('\\', "/");

normalized
}

Expand Down Expand Up @@ -994,3 +1007,33 @@ impl<'a> DetectionHelper<'a> {
}
}
}

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

#[test]
fn normalize_path_collapses_repeated_slashes() {
assert_eq!(
DetectionHelper::normalize_path("///etc/shadow"),
"/etc/shadow"
);
assert_eq!(
DetectionHelper::normalize_path("////etc//shadow///"),
"/etc/shadow"
);
assert_eq!(
DetectionHelper::normalize_path("C:\\\\Windows\\System32"),
"C:/Windows/System32"
);
}

#[test]
fn sensitive_path_detects_repeated_leading_slashes() {
let config = PolicyConfig::default();
let helper = DetectionHelper::new(&config);

assert!(helper.is_sensitive_path("///etc/shadow"));
assert!(helper.is_sensitive_path("////etc//shadow"));
}
}