-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_csaf.py
More file actions
50 lines (41 loc) · 1.63 KB
/
Copy pathexample_csaf.py
File metadata and controls
50 lines (41 loc) · 1.63 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
import json
import sys
from pathlib import Path
from jsonpath_schema import Schema, validateJSONPath
CSAF_SCHEMA_URI = "https://docs.oasis-open.org/csaf/csaf/v2.1/schema/csaf.json"
def load_csaf_bundle(path: str | Path) -> dict[str, Schema]:
"""Load the bundle's store and add unambiguous, undated CVSS aliases.
Raises:
OSError: If the bundle cannot be opened or read.
UnicodeError: If the bundle is not valid UTF-8.
json.JSONDecodeError: If the bundle is not valid JSON.
KeyError: If the bundle has no ``schemas`` mapping.
ValueError: If an undated CVSS alias is ambiguous.
"""
with Path(path).open(encoding="utf-8") as source:
store = json.load(source)["schemas"]
aliases: dict[str, Schema] = {}
for uri, schema in store.items():
if uri.startswith("https://www.first.org/cvss/cvss-") and "?" in uri:
alias = uri.split("?", 1)[0]
aliases[alias] = schema
return store | aliases
def main() -> int:
store = load_csaf_bundle(Path("csaf-2.1-bundle.json"))
schema = store[CSAF_SCHEMA_URI]
paths = [
"$.document.title",
"$.vulnerabilities[0].metrics[0].content.cvss_v3.baseScore",
"$.vulnerabilities[0].metrics[0].content.cvss_v4.baseSeverity",
"$.document.unknown",
]
error_count = 0
for path in paths:
if not validateJSONPath(path, schema, store, strict=True):
print(path, "is not valid JSONPath in schema")
error_count += 1
else:
print(path, "is valid JSONPath in schema")
return error_count
if __name__ == "__main__":
sys.exit(main())