generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathtester.py
More file actions
executable file
·174 lines (147 loc) · 6.08 KB
/
tester.py
File metadata and controls
executable file
·174 lines (147 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright 2025 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests
import sys
import urllib.parse
def test_health_check(base_url):
"""
Tests the health check endpoint.
"""
url = f"{base_url}/"
try:
print(f"--- Testing Health Check endpoint ---")
print(f"Sending GET request to {url}")
response = requests.get(url)
response.raise_for_status()
print("Health check successful!")
print("Response JSON:", response.json())
assert response.json()["status"] == "ok"
except (requests.exceptions.RequestException, AssertionError) as e:
print(f"An error occurred during health check: {e}")
sys.exit(1)
def test_execute(base_url):
"""
Tests the execute endpoint.
"""
url = f"{base_url}/execute"
payload = {"command": "echo 'hello world'"}
try:
print(f"\n--- Testing Execute endpoint ---")
print(f"Sending POST request to {url} with payload: {payload}")
response = requests.post(url, json=payload)
response.raise_for_status() # Raise an exception for bad status codes
print("Execute command successful!")
print("Response JSON:", response.json())
assert response.json()["stdout"] == "hello world\n"
except (requests.exceptions.RequestException, AssertionError) as e:
print(f"An error occurred during execute command: {e}")
sys.exit(1)
def test_list_files(base_url):
"""
Tests the list files endpoint.
"""
url = f"{base_url}/list/."
try:
print(f"\n--- Testing List Files endpoint ---")
print(f"Sending GET request to {url}")
response = requests.get(url)
response.raise_for_status()
print("List files successful!")
print("Response JSON:", response.json())
assert isinstance(response.json(), list)
except (requests.exceptions.RequestException, AssertionError) as e:
print(f"An error occurred during list files: {e}")
sys.exit(1)
def test_exists(base_url):
"""
Tests the exists endpoint.
"""
url = f"{base_url}/exists/."
try:
print(f"\n--- Testing Exists endpoint ---")
print(f"Sending GET request to {url}")
response = requests.get(url)
response.raise_for_status()
print("Exists check successful!")
print("Response JSON:", response.json())
assert response.json()["path"] == ""
assert response.json()["exists"] is True
url = f"{base_url}/exists/does_not_exist"
print(f"Sending GET request to {url}")
response = requests.get(url)
response.raise_for_status()
print("Exists check (negative) successful!")
print("Response JSON:", response.json())
assert response.json()["path"] == "does_not_exist"
assert response.json()["exists"] is False
except (requests.exceptions.RequestException, AssertionError) as e:
print(f"An error occurred during exists check: {e}")
sys.exit(1)
def test_path_traversal(base_url):
"""
Tests that relative path traversal attempts are blocked.
"""
# Try to access /etc/passwd via relative path traversal
unsafe_path = "../../etc/passwd"
# We must encode slashes so the web server passes them to the application
# instead of resolving them itself.
encoded_path = urllib.parse.quote(unsafe_path, safe='')
url = f"{base_url}/exists/{encoded_path}"
try:
print(f"\n--- Testing Path Traversal ---")
print(f"Sending GET request to {url}")
response = requests.get(url)
print(f"Response status code: {response.status_code}")
print("Response JSON:", response.json())
assert response.status_code == 403
assert response.json()["message"] == "Access denied"
print("Path traversal blocked successfully!")
except (requests.exceptions.RequestException, AssertionError) as e:
print(f"An error occurred during path traversal check: {e}")
sys.exit(1)
def test_absolute_path_traversal(base_url):
"""
Tests that absolute path traversal attempts are blocked.
"""
# Try to access /etc/passwd via absolute path traversal.
# Note: The server strips leading slashes, effectively re-rooting absolute paths to /app.
# To test the 'outside /app' check, we must use '..' to traverse up from /app.
unsafe_path = "/../etc/passwd"
encoded_path = urllib.parse.quote(unsafe_path, safe='')
url = f"{base_url}/exists/{encoded_path}"
try:
print(f"\n--- Testing Absolute Path Traversal ---")
print(f"Sending GET request to {url}")
response = requests.get(url)
print(f"Response status code: {response.status_code}")
print("Response JSON:", response.json())
assert response.status_code == 403
assert response.json()["message"] == "Access denied"
print("Absolute path traversal blocked successfully!")
except (requests.exceptions.RequestException, AssertionError) as e:
print(f"An error occurred during absolute path traversal check: {e}")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python tester.py <server_ip> <server_port>")
sys.exit(1)
ip = sys.argv[1]
port = sys.argv[2]
base_url = f"http://{ip}:{port}"
test_health_check(base_url)
test_execute(base_url)
test_list_files(base_url)
test_exists(base_url)
test_path_traversal(base_url)
test_absolute_path_traversal(base_url)