forked from mozilla/foundation-security-advisories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
320 lines (274 loc) · 9.92 KB
/
common.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import codecs
import fnmatch
import os
import re
from glob import glob
from subprocess import check_output
from dataclasses import dataclass, field
import yaml
from markdown import markdown
GIT = os.getenv("GIT_BIN", "git")
ADVISORIES_DIR = "announce"
HOF_DIR = "bug-bounty-hof"
MFSA_FILENAME_RE = re.compile("mfsa(\d{4}-\d{2,3})\.(md|yml)$")
HOF_FILENAME_RE = re.compile("bug-bounty-hof/\w+\.yml$")
HTML_BR_TAG_RE = re.compile(r"<br */?>")
HTML_CODE_TAG_RE = re.compile(r"</?code>")
HTML_TAG_RE = re.compile(r"<[^>]+>")
HTML_NEWLINE_RE = re.compile(r"\n")
HTML_DOUBLE_NEWLINE_RE = re.compile(r"\n\n")
def mfsa_id_from_filename(filename):
match = MFSA_FILENAME_RE.search(filename)
if match:
return match.group(1)
return None
def git_diff(staged):
"""
Return the modified files in the repo.
:param staged: boolean return only those changes staged in git
:return: list modified file names.
"""
command = [GIT, "diff", "--name-only"]
if staged:
command.append("--cached")
git_out = check_output(command, universal_newlines=True).split()
return [
fn
for fn in git_out
if MFSA_FILENAME_RE.search(fn) or HOF_FILENAME_RE.search(fn)
]
def get_modified_files(staged_only):
"""
Return the modified file names in the repo.
:param staged_only: boolean include all changes or only staged.
:return: list modified file names.
"""
staged_files = git_diff(staged=True)
if staged_only:
return staged_files
modified_files = set(staged_files)
modified_files.update(git_diff(staged=False))
return list(modified_files)
def get_all_files():
"""
Return all advisory file names in the repo.
:return: generator of file names.
"""
for root, dirnames, filenames in os.walk(ADVISORIES_DIR):
for filename in fnmatch.filter(filenames, "mfsa*.*"):
yield os.path.join(root, filename)
for filename in glob("{}/*.yml".format(HOF_DIR)):
yield filename
def parse_md_front_matter(lines):
"""Return the YAML and MD sections.
:param: lines iterator
:return: str YAML, str Markdown
"""
# fm_count: 0: init, 1: in YAML, 2: in Markdown
fm_count = 0
yaml_lines = []
md_lines = []
for line in lines:
# first line we care about is FM start
if fm_count < 2 and line.strip() == "---":
fm_count += 1
continue
if fm_count == 1:
yaml_lines.append(line)
if fm_count == 2:
md_lines.append(line)
if fm_count < 2:
raise ValueError("Front Matter not found.")
return "".join(yaml_lines), "".join(md_lines)
def parse_yml_file(file_name):
"""Return the YAML data for file_name."""
with codecs.open(file_name, encoding="utf8") as fh:
data = yaml.safe_load(fh)
if "mfsa_id" not in data:
mfsa_id = mfsa_id_from_filename(file_name)
if mfsa_id:
data["mfsa_id"] = mfsa_id
return data
def parse_md_file(file_name):
"""Return the YAML and MD sections for file_name."""
with codecs.open(file_name, encoding="utf8") as fh:
yamltext, mdtext = parse_md_front_matter(fh)
data = yaml.safe_load(yamltext)
if "mfsa_id" not in data:
mfsa_id = mfsa_id_from_filename(file_name)
if mfsa_id:
data["mfsa_id"] = mfsa_id
# run it through parser in case of exception
markdown(mdtext)
return data
def remove_newlines(content: str | None):
"""Removes markdown-style newlines. Replaces '\\n\\n' with '<br />' and '\\n' with a space ' '."""
if not content:
return None
content = HTML_DOUBLE_NEWLINE_RE.sub("<br />", content)
content = HTML_NEWLINE_RE.sub(" ", content)
return content
def remove_html_tags(content: str | None):
"""Executes `remove_newlines` and replaces <br> tags with '\\n', <code> tags with '`' and removes all other tags."""
if not content:
return None
content = remove_newlines(content)
content = HTML_BR_TAG_RE.sub("\n", content)
content = HTML_CODE_TAG_RE.sub("`", content)
content = HTML_TAG_RE.sub("", content)
return content
def comma_separated(sequence: list[str], conjunction="and"):
"""
Returns the given string list comma separated. For example: \n
["a","b","c","d"] -> "a, b, c, and d" \n
["a","b"] -> "a and b" \n
["a"] -> "a"
"""
if len(sequence) > 2:
return f"{', '.join(sequence[:-1])}, {conjunction} {sequence[-1]}"
elif len(sequence) == 2:
return f"{sequence[0]} {conjunction} {sequence[-1]}"
else:
return sequence[0]
@dataclass
class CVEAdvisory:
"""A collection of `CVEAdvisoryInstance`s with the same CVE-ID."""
id: str
year: int
instances: list["CVEAdvisoryInstance"] = field(default_factory=list)
@property
def newest_instance(self):
"""
Returns the last modified instance of this CVE advisory (determined by git commit time).
Useful for when only one of the instances is being updated with the latest information.
"""
greatest_last_modified = 0
newest_instance: CVEAdvisoryInstance = None
for instance in self.instances:
if instance.file_last_modified > greatest_last_modified:
greatest_last_modified = instance.file_last_modified
newest_instance = instance
return newest_instance
@property
def full_description(self):
return (
self.newest_instance.description.strip()
+ " This vulnerability affects "
+ comma_separated(
[
f"{instance.product} < {instance.version_fixed}"
for instance in self.instances
],
)
+ "."
)
def to_json(self):
"""
Convert advisory in yml format into
[CVE JSON](https://cveproject.github.io/cve-schema/schema/docs/) format.
"""
return {
"containers": {
"cna": {
"affected": [
{
"product": instance.product,
"vendor": "Mozilla",
"versions": [
{
"lessThan": instance.version_fixed,
"status": "affected",
"version": "unspecified",
"versionType": "custom",
}
],
}
for instance in self.instances
],
"descriptions": [
{
"lang": "en",
"value": remove_html_tags(self.full_description),
"supportingMedia": [
{
"type": "text/html",
"base64": False,
"value": remove_newlines(self.full_description),
}
],
}
],
**(
{
"problemTypes": [
{
"descriptions": [
{
"description": remove_html_tags(
self.newest_instance.title
),
"lang": "en",
"type": "text",
}
]
}
]
}
if self.newest_instance.title
else {}
),
"references": [
{
"url": f"https://www.mozilla.org/security/advisories/mfsa{mfsa_id}/"
}
for mfsa_id in sorted(
set([instance.mfsa_id for instance in self.instances])
)
]
+ [
{
"url": url,
**({"name": desc} if desc else {}),
}
for url, desc in self.newest_instance.references
],
**(
{
"credits": [
{
"lang": "en",
"value": remove_html_tags(
self.newest_instance.reporter
),
}
],
}
if self.newest_instance.reporter
else {}
),
}
},
"dataType": "CVE_RECORD",
"dataVersion": "5.1",
}
@dataclass
class CVEAdvisoryInstance:
"""
A manifestation of a CVE advisory in this repository.
Objects of this class correspond to the entries in the
`advisories:` section of the advisory YAML format.
"""
parent: CVEAdvisory
title: str
description: str
reporter: str | None
references: list[(str, str | None)]
mfsa_id: str
product: str
version_fixed: str
file_name: str
file_last_modified: int