| """cvelistV5 full release -> _intermediate/cve5_*.parquet (one row per CVE record).""" | |
| import sys, os, zipfile, orjson, multiprocessing as mp | |
| import pyarrow as pa, pyarrow.parquet as pq | |
| sys.path.insert(0, os.path.dirname(__file__)) | |
| from hubcommon import INTER, ROOT, one, norm_cve | |
| OUTER = one("cvelistV5/*/*_all_CVEs_at_midnight.zip.zip") | |
| INNER = f"{ROOT}/scratch/cvelist/cves.zip" # extracted from OUTER (scratch, deletable) | |
| CVSS_T = pa.struct([("vector", pa.string()), ("base_score", pa.float64()), ("source", pa.string())]) | |
| SCHEMA = pa.schema([ | |
| ("cve_id", pa.string()), ("state", pa.string()), ("assigner", pa.string()), | |
| ("date_reserved", pa.string()), ("date_published", pa.string()), ("date_updated", pa.string()), | |
| ("title", pa.string()), ("description", pa.string()), ("cwe_ids", pa.list_(pa.string())), | |
| ("cvss_v2", CVSS_T), ("cvss_v3", CVSS_T), ("cvss_v4", CVSS_T), | |
| ("adp_providers", pa.list_(pa.string())), ("n_references", pa.int32()), | |
| ]) | |
| def en(descs): | |
| if not descs: return None | |
| for d in descs: | |
| if d.get("lang", "").lower().startswith("en"): return d.get("value") | |
| return descs[0].get("value") | |
| def cvss_from(metrics, provider): | |
| out = {} | |
| for m in metrics or []: | |
| for key, ver in (("cvssV4_0", "v4"), ("cvssV3_1", "v3"), ("cvssV3_0", "v3"), ("cvssV2_0", "v2")): | |
| c = m.get(key) | |
| if c and ver not in out: | |
| out[ver] = {"vector": c.get("vectorString"), "base_score": c.get("baseScore"), "source": provider} | |
| return out | |
| def cwes(problem_types): | |
| s = [] | |
| for pt in problem_types or []: | |
| for d in pt.get("descriptions", []): | |
| c = d.get("cweId") | |
| if not c: | |
| v = d.get("description", "") | |
| if v.startswith("CWE-"): c = v.split()[0].rstrip(":") | |
| if c and c.startswith("CWE-") and c[4:].isdigit() and c not in s: s.append(c) | |
| return s | |
| def work(args): | |
| idx, names = args | |
| z = zipfile.ZipFile(INNER) | |
| rows = [] | |
| for n in names: | |
| try: j = orjson.loads(z.read(n)) | |
| except Exception as e: | |
| print("bad json", n, e, flush=True); continue | |
| md = j.get("cveMetadata", {}); cont = j.get("containers", {}); cna = cont.get("cna", {}) or {} | |
| adps = cont.get("adp", []) or [] | |
| desc = en(cna.get("descriptions")) or en(cna.get("rejectedReasons")) | |
| cw = cwes(cna.get("problemTypes")) | |
| cv = cvss_from(cna.get("metrics"), "cna:" + (cna.get("providerMetadata", {}).get("shortName") or "")) | |
| provs = [] | |
| for a in adps: | |
| p = a.get("providerMetadata", {}).get("shortName") or "adp"; provs.append(p) | |
| for c in cwes(a.get("problemTypes")): | |
| if c not in cw: cw.append(c) | |
| for k, v in cvss_from(a.get("metrics"), "adp:" + p).items(): cv.setdefault(k, v) | |
| nref = len(cna.get("references") or []) + sum(len(a.get("references") or []) for a in adps) | |
| rows.append(dict(cve_id=norm_cve(md.get("cveId")), state=md.get("state"), assigner=md.get("assignerShortName"), | |
| date_reserved=md.get("dateReserved"), date_published=md.get("datePublished"), date_updated=md.get("dateUpdated"), | |
| title=cna.get("title"), description=desc, cwe_ids=cw, cvss_v2=cv.get("v2"), cvss_v3=cv.get("v3"), | |
| cvss_v4=cv.get("v4"), adp_providers=provs, n_references=nref)) | |
| pq.write_table(pa.Table.from_pylist(rows, schema=SCHEMA), f"{INTER}/cve5_{idx:03d}.parquet", compression="zstd") | |
| return len(rows) | |
| if __name__ == "__main__": | |
| if not os.path.exists(INNER): | |
| os.makedirs(os.path.dirname(INNER), exist_ok=True) | |
| zipfile.ZipFile(OUTER).extract("cves.zip", os.path.dirname(INNER)) | |
| names = [n for n in zipfile.ZipFile(INNER).namelist() if n.endswith(".json") and "/CVE-" in n] | |
| chunks = [(i, names[i::24]) for i in range(24)] | |
| with mp.Pool(6) as p: | |
| tot = sum(p.imap_unordered(work, chunks)) | |
| print("cve5 rows", tot, "of", len(names)) | |
Xet Storage Details
- Size:
- 3.98 kB
- Xet hash:
- 68d5a4c4d714ebbce032b733a9c2398775c072cb34ae76a5ebce1ecaee2c2c52
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.