Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,29 @@ def test_does_not_absolute_path_inside_another_folder():

def test_disable_checkPathStart():
assert detect_path_traversal("/etc/passwd", "/etc/passwd", False) is False


def test_current_directory_references():
"""/./ should be normalized to /"""
assert detect_path_traversal("/./etc/passwd", "/./etc") is True
assert detect_path_traversal("/etc/./passwd", "/etc") is True
assert detect_path_traversal("/etc/./passwd", "/etc/./") is True
assert detect_path_traversal("/./etc/passwd", "/./etc/passwd") is True
assert detect_path_traversal("/etc/./passwd", "/etc/./passwd") is True
assert detect_path_traversal("/./etc/./passwd", "/./etc/./passwd") is True
# Multiple /./ sequences
assert detect_path_traversal("/././etc/passwd", "/././etc") is True
assert detect_path_traversal("/etc/././passwd", "/etc/././passwd") is True


def test_path_normalization():
"""Paths with multiple slashes and /./ should be normalized and detected"""
assert detect_path_traversal("//etc//passwd", "/etc") is True
assert detect_path_traversal("/./etc/./passwd", "/etc") is True
assert detect_path_traversal("/././etc/passwd", "/etc") is True
# Paths without leading slash are not unsafe
assert detect_path_traversal("etc/passwd", "etc") is False
assert detect_path_traversal("", "") is False
# Combined slashes and dot: ///.///etc/passwd should normalize to /etc/passwd
assert detect_path_traversal("///.///etc/passwd", "///.///etc") is True
assert detect_path_traversal("///.///etc/passwd", "///.///etc/passwd") is True
25 changes: 19 additions & 6 deletions aikido_zen/vulnerabilities/path_traversal/unsafe_path_start.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Exports the function starts_with_unsafe_path"""

import regex as re

CURRENT_DIR_PATTERN = re.compile(r"/(\./)+")

linux_root_folders = [
"/bin/",
"/boot/",
Expand Down Expand Up @@ -28,8 +32,8 @@

def starts_with_unsafe_path(file_path, user_input):
"""Check if the file path starts with any dangerous paths and the user input."""
path_parsed = ensure_one_leading_slash(file_path.lower())
input_parsed = ensure_one_leading_slash(user_input.lower())
path_parsed = normalize_path(file_path)
input_parsed = normalize_path(user_input)

for dangerous_start in dangerous_path_starts:
if path_parsed.startswith(dangerous_start) and path_parsed.startswith(
Expand All @@ -40,7 +44,16 @@ def starts_with_unsafe_path(file_path, user_input):
return False


def ensure_one_leading_slash(path: str) -> str:
if path.startswith("/"):
return "/" + path.lstrip("/")
return path
def normalize_path(path: str) -> str:
"""Normalizes a path by lowercasing, removing /./ and removing consecutive slashes"""
if not path:
return path

normalized = path.lower()

# Matches /./ or /././ or /./././ etc. (one or more ./ sequences after a /)
normalized = CURRENT_DIR_PATTERN.sub("/", normalized)

# Merge consecutive slashes since these don't change where you are in the path.
normalized = re.sub(r"/+", "/", normalized)
return normalized