-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
197 lines (149 loc) · 6.08 KB
/
convert.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
"""
Parse findings in mixbytes public repo
Usage:
convert [options]
Options:
-h help
--config=FILE [default: config.json]
--index=FILE root md file with a table of reports
--path=PATH base path with reports
--debug=FILE run in debug mode over a single report
--list list projects only
--list-errors list only projects with parsing errors
"""
from docopt import docopt
import json
import re
import os
import md
def extract_href_from_md_link(text):
link_pattern = r'\[link\]\((https?://\S+)\)'
return re.findall(link_pattern, text)[0]
def extract_file_path_from_link(link):
relative_path = link.replace("https://github.com/mixbytes/audits_public/blob/master/", "").replace("%20", " ")
return relative_path
def clean_title(text):
text = text.lower()
text = re.sub("[^a-z]", " ", text)
text = re.sub("\s+", " ", text)
text = text.strip()
return text
def find(section, title):
if clean_title(section["title"]) == clean_title(title):
return section
for section in section["subsections"]:
if found := find(section, title):
return found
def parse(text, md_link="", pdf_link=None, protocol_name="", report_date=""):
sections = md.parse_md(text)
for urgency in config["urgency_map"]:
if findings := find(sections, urgency):
findings = findings["subsections"]
for finding in findings:
if clean_title(finding["title"]) == "not found" and not finding["subsections"]:
continue
finding_github_link = md_link + md.generate_hash_link(finding["title"])
debug_info = finding_github_link
if (
re.match("^[0-9]+\. ?https://github.com/", finding["title"], flags=re.I)
or
re.match(r"^([0-9]+\. ?)?\[\w+\.sol\\?#?[L0-9-]+\]\(https://github.com/",
finding["title"], flags=re.I)
):
raise Exception("Report '%s' have bad vulnerability title" % finding_github_link)
if not finding["subsections"] and finding["content"]:
content = md.to_md({**finding, "level": 0})
else:
description = find(finding, "description")
assert description, f"Description section not found in {debug_info}"
recommendation = find(finding, "recommendation")
assert recommendation, f"Recommendation section not found in {debug_info}"
content = md.to_md(description) + "\n" + md.to_md(recommendation)
yield {
"title": re.sub(r'^[0-9]+\.\s+', '', finding["title"]),
"content": content,
"protocol": protocol_name,
"report_date": report_date,
"impact": config["urgency_map"][urgency],
"finders": [],
"github_link": finding_github_link,
"pdf_link": pdf_link,
}
def fix(text, md_link):
for md_link_to_fix, rules in config.get("fix", {}).items():
if md_link == md_link_to_fix:
if "sub" in rules:
for pattern, repl in rules["sub"].items():
text = re.sub(pattern, repl, text, flags=re.M)
elif "file" in rules:
return open(rules["file"]).read()
return text
def run_from_index(path):
table = md.parse_table(open(path).read())
base_path = config.get("base_path", "")
outputs = []
for report in table:
md_link = extract_href_from_md_link(report["MD Report"])
path = extract_file_path_from_link(md_link)
path = os.path.join(base_path, path)
if report["PDF Report"] == "N/A":
pdf_link = None
else:
pdf_link = extract_href_from_md_link(report["PDF Report"])
assert os.path.exists(path), f"Report '{md_link}' cannot be found in '{path}'"
text = open(path).read()
text = fix(text, md_link)
outputs.extend(parse(
text,
md_link=md_link,
protocol_name=report["Project"],
report_date=report["Release Date (YYYY-MM-DD)"],
pdf_link=pdf_link,
))
print(json.dumps(outputs, indent=4))
def run_single_report(path):
print(json.dumps(list(parse(open(path).read())), indent=4))
def get_config(args):
config = args["--config"] or "config.json"
assert os.path.exists(config), "Config not found"
config = json.load(open(config))
if args["--index"]:
config["index"] = args["--index"]
if args["--path"]:
config["base_path"] = args["--path"]
return config
if __name__ == "__main__":
args = docopt(__doc__)
config = get_config(args)
if args["--list"]:
table = md.parse_table(open(config["index"]).read())
base_path = config.get("base_path", "")
outputs = []
for report in table:
md_link = extract_href_from_md_link(report["MD Report"])
print(md_link)
elif args["--list-errors"]:
table = md.parse_table(open(config["index"]).read())
base_path = config.get("base_path", "")
outputs = []
for report in table:
md_link = extract_href_from_md_link(report["MD Report"])
try:
path = extract_file_path_from_link(md_link)
path = os.path.join(base_path, path)
assert os.path.exists(path), f"Report '{md_link}' cannot be found in '{path}'"
text = open(path).read()
text = fix(text, md_link)
list(parse(
text,
md_link=md_link,
protocol_name=report["Project"],
report_date=report["Release Date (YYYY-MM-DD)"],
pdf_link="",
))
except:
print(md_link)
elif args["--debug"]:
run_single_report(args["--debug"])
else:
run_from_index(config["index"])