Skip to content

Commit 9c3c7a7

Browse files
committed
Add schema validator for Alpine linux
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 37b1eda commit 9c3c7a7

1 file changed

Lines changed: 43 additions & 10 deletions

File tree

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,75 @@
11
import itertools
22
import saneyaml
3+
from schema import Regex, Or, Schema
34
from urllib.request import urlopen
45
from io import BytesIO
56
from zipfile import ZipFile
67

7-
ALPINE_DB_URL = 'https://gitlab.alpinelinux.org/alpine/infra/alpine-secdb/-/\
8-
archive/master/alpine-secdb-master.zip'
8+
ALPINE_DB_URL = "https://gitlab.alpinelinux.org/alpine/infra/alpine-secdb/-/\
9+
archive/master/alpine-secdb-master.zip"
910

1011

1112
def alpine_advisories(url):
1213
with urlopen(url) as response:
1314
with ZipFile(BytesIO(response.read())) as zf:
1415
for path in zf.namelist():
15-
if path.endswith('main.yaml'):
16+
if path.endswith("main.yaml"):
1617
yield saneyaml.load(zf.open(path))
1718

1819

20+
def validate_schema(advisory_dict):
21+
scheme = {
22+
"distroversion": Regex(r"v\d.\d*"),
23+
"reponame": "main",
24+
"archs": list,
25+
"packages": [
26+
{
27+
"pkg": {
28+
"name": str,
29+
"secfixes": {
30+
str: Or(
31+
[
32+
Or(
33+
Regex(r"CVE.\d+-\d+"),
34+
Regex(r"XSA-\d{3}"),
35+
Regex(r"ZBX-\d{4}"),
36+
Regex(r"wnpa-sec-\d{4}-\d{2}"),
37+
)
38+
],
39+
"",
40+
),
41+
},
42+
}
43+
}
44+
],
45+
object: object,
46+
}
47+
Schema(scheme).validate(advisory_dict)
48+
49+
1950
def import_vulnerabilities():
2051
vulnerability_package_dicts = []
2152
for vulnerability in alpine_advisories(ALPINE_DB_URL):
22-
for pkg_details in vulnerability['packages']:
23-
package_name = pkg_details['pkg']['name']
24-
for version, fixed_cves in pkg_details['pkg']['secfixes'].items():
53+
validate_schema(vulnerability)
54+
for pkg_details in vulnerability["packages"]:
55+
package_name = pkg_details["pkg"]["name"]
56+
for version, fixed_cves in pkg_details["pkg"]["secfixes"].items():
2557
# ['CVE-2016-9932 XSA-200', 'CVE-2016-9815','CVE-????-?????'] after mapping
2658
# the split function to above list
2759
all_cves = list(map(lambda x: x.split(), fixed_cves))
2860
# it becomes [['CVE-2016-9932','XSA-200'], ['CVE-2016-9815'],['CVE-????-?????']]
2961
for index, vuln_grp in enumerate(all_cves):
3062
all_cves[index] = list(
31-
filter(lambda x: 'CVE-????-?????' not in x, vuln_grp))
63+
filter(lambda x: "CVE-????-?????" not in x, vuln_grp)
64+
)
3265
all_cves = [i for i in all_cves if i and len(i) <= 2]
3366
# this data consists lots of 'CVE-????-?????' to denote vulnerabilities
3467
# with unassigned CVE ids , we filter out these as well as other garbage data
3568
vulnerability_package_dicts.append(
3669
{
37-
'package_name': package_name,
38-
'vuln_ids': all_cves,
39-
'fixed_version': version,
70+
"package_name": package_name,
71+
"vuln_ids": all_cves,
72+
"fixed_version": version,
4073
}
4174
)
4275
return vulnerability_package_dicts

0 commit comments

Comments
 (0)