Skip to content

Commit 1b562d9

Browse files
committed
remove leading slashes for check path start
1 parent 3dc4610 commit 1b562d9

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

aikido_zen/sinks/tests/os_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,42 @@
22
from pathlib import Path, PurePath
33
from unittest.mock import patch
44
import aikido_zen.sinks.os
5+
from aikido_zen.context import Context
6+
from aikido_zen.errors import AikidoPathTraversal
7+
from aikido_zen.sinks.tests.clickhouse_driver_test import set_blocking_to_true
58

69
kind = "path_traversal"
710

811

12+
def set_context(param):
13+
wsgi_request = {
14+
"REQUEST_METHOD": "GET",
15+
"HTTP_HEADER_1": "header 1 value",
16+
"HTTP_HEADER_2": "Header 2 value",
17+
"RANDOM_VALUE": "Random value",
18+
"HTTP_COOKIE": "sessionId=abc123xyz456;",
19+
"wsgi.url_scheme": "http",
20+
"HTTP_HOST": "localhost:8080",
21+
"PATH_INFO": "/hello",
22+
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
23+
"CONTENT_TYPE": "application/json",
24+
"REMOTE_ADDR": "198.51.100.23",
25+
}
26+
context = Context(
27+
req=wsgi_request,
28+
body={
29+
"param": param,
30+
},
31+
source="flask",
32+
)
33+
context.set_as_current_context()
34+
35+
36+
@pytest.fixture(autouse=True)
37+
def set_blocking_to_true(monkeypatch):
38+
monkeypatch.setenv("AIKIDO_BLOCK", "1")
39+
40+
941
def test_ospath_commands():
1042
with patch(
1143
"aikido_zen.vulnerabilities.run_vulnerability_scan"
@@ -39,6 +71,16 @@ def test_ospath_commands():
3971
mock_run_vulnerability_scan.assert_any_call(kind=kind, op=op, args=args)
4072

4173

74+
def test_os_create_path_with_multiple_slashes():
75+
import os
76+
77+
file_path = "////etc/passwd"
78+
set_context(file_path)
79+
with pytest.raises(AikidoPathTraversal):
80+
full_path = Path("flaskr/resources/blogs/") / file_path
81+
open(full_path, "r").close()
82+
83+
4284
def test_ospath_command_absolute_path():
4385
with patch(
4486
"aikido_zen.vulnerabilities.run_vulnerability_scan"

aikido_zen/vulnerabilities/path_traversal/unsafe_path_start.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@
2828

2929
def starts_with_unsafe_path(file_path, user_input):
3030
"""Check if the file path starts with any dangerous paths and the user input."""
31-
lower_case_path = file_path.lower()
32-
lower_case_user_input = user_input.lower()
31+
path_parsed = trim_leading_slashes(file_path.lower())
32+
input_parsed = trim_leading_slashes(user_input.lower())
3333

3434
for dangerous_start in dangerous_path_starts:
35-
if lower_case_path.startswith(dangerous_start) and lower_case_path.startswith(
36-
lower_case_user_input
35+
if path_parsed.startswith(dangerous_start) and path_parsed.startswith(
36+
input_parsed
3737
):
3838
return True
3939

4040
return False
41+
42+
43+
def trim_leading_slashes(path: str) -> str:
44+
return "/" + path.lstrip("/")

0 commit comments

Comments
 (0)