-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_testcases.py
210 lines (194 loc) · 7.61 KB
/
check_testcases.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import inspect
import sys
import subprocess
import time
import traceback
# append the path of the parent directory
sys.path.append("..")
import testcases
from helpers.requestors import HttpxRequestor
from helpers.db_util import (
Activity,
setup_db,
db,
Site,
Url,
DirectTest,
ProbeTest,
RetroTest,
AddResp,
ReqResp,
)
from helpers.util import probes
from playhouse.shortcuts import model_to_dict
from dotenv import load_dotenv
load_dotenv()
def main(requestor):
"""Run all failing/passing responses for each test."""
db_name = "sanity_check"
try:
setup_db(db_name)
db.init(db_name)
db.connect()
db.create_tables(
[Site, Url, DirectTest, ProbeTest, RetroTest, AddResp, ReqResp]
)
p = subprocess.Popen(
[
"poetry",
"run",
"mitmdump",
"-s",
"conformance_checker.py",
"--set",
"validate_inbound_headers=false",
"--set",
"ssl_insecure=true",
"--set",
f"db_name={db_name}",
],
cwd="..",
)
time.sleep(5)
PROXIES = {"all://": "http://localhost:8080"}
requestor = requestor(PROXIES, set())
site = Site.get_or_create(
site_type="debug", description="sanity_check", rank=-1, site="localhost", bucket=-1, origin="localhost"
)[0]
for name, obj in inspect.getmembers(testcases):
if inspect.isclass(obj) and name not in [
"StrEnum",
"Activity",
"Violation",
"Level",
"datetime",
"DirectTest",
"ProbeTest",
"RetroTest",
"ReqResp",
"Url",
]:
obj = obj()
# Run proxy test with valid/invalid server example
if obj.activity == Activity.PROXY:
if obj.category == "HTTP/2" or obj.name in ["STS_header_after_upgrade_insecure_requests", "code_304_no_content", "transfer_encoding_http11"]:
base = "https://leaking.via:44333"
http2 = True
scheme = "https"
port = 44333
else:
base = "http://leaking.via:5001"
http2 = False
scheme = "http"
port = 5001
host = "leaking.via"
for val in ["valid", "invalid"]:
path = f"/{name}/{val}"
full_url = f"{base}{path}"
url = Url.get_or_create(
site=site,
full_url=full_url,
scheme=scheme,
host=host,
port=port,
path=path,
description="",
is_base=True,
)[0]
if obj.name == "post_invalid_response_codes":
method = "POST"
else:
method = "GET"
if obj.name == "close_option_in_final_response":
headers = {"connection": "close"}
else:
headers = {"upgrade-insecure-requests": "1"}
requestor.run(
f"{full_url}?url_id={url}",
method = method,
timeout=2,
verify=False,
http2=http2,
headers=headers
)
# Run direct test with valid/invalid example
if obj.activity in [Activity.DIRECT, Activity.DIRECT_BASE]:
base = "http://leaking.via:5001"
for url in [f"{base}/{name}/valid", f"{base}/{name}/invalid"]:
scheme, o = url.split("://")
host, o = o.split(":")
port, path = o.split("/", maxsplit=1)
port = int(port)
path = f"/{path}"
url = Url.get_or_create(
site=site,
full_url=url,
scheme=scheme,
host=host,
port=port,
path=path,
description="",
is_base=True,
)[0]
try:
res = obj.test(url)
except Exception as e:
DirectTest.create(
url=url, name=name, type=obj.type, test_error=e
)
print(f"{name} failed: {e}")
res = None
if res != None:
r_d = model_to_dict(res)
print(f"{url.path}: {r_d['violation'], r_d['extra']}\n")
# Run retro tests: first run all probes for both valid/invalid URL, then run the retro test
if obj.activity == Activity.RETRO:
base = "http://leaking.via:5001"
for url in [f"{base}/{name}/valid", f"{base}/{name}/invalid"]:
scheme, o = url.split("://")
host, o = o.split(":")
port, path = o.split("/", maxsplit=1)
port = int(port)
path = f"/{path}"
url = Url.get_or_create(
site=site,
full_url=url,
scheme=scheme,
host=host,
port=port,
path=path,
description="",
is_base=True,
)[0]
for probe_id, (method, headers, http2) in probes.items():
if http2:
continue
requestor.run(
f"{url.full_url}?url_id={url}&probe_id={probe_id}",
method=method,
headers=headers,
timeout=5,
verify=False,
http2=http2,
)
try:
req_resps = ReqResp.select().where(ReqResp.url == url)
res = obj.test(req_resps, url)
except Exception as e:
RetroTest.create(
url=url, name=name, type=obj.type, test_error=e
)
print(f"{name} failed: {e}")
res = None
if res != None:
r_d = model_to_dict(res)
print(f"{url.path}: {r_d['violation'], r_d['extra']}\n")
except Exception as e:
print(traceback.format_exc())
print(e)
finally:
db.close()
p.terminate()
requestor.close()
if __name__ == "__main__":
main(HttpxRequestor)