| """OSV all.zip and GHSA advisory-database clone -> CVE links, ecosystems, fix commits. | |
| Outputs _intermediate/{osv,ghsa}link_*.parquet (cve_id, adv_id, ecosystems, cwe_ids) and {osv,ghsa}fix_*.parquet.""" | |
| import sys, os, glob, 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, RAW, one, norm_cve, parse_commit_url, norm_repo | |
| LINK_SCHEMA = pa.schema([("cve_id", pa.string()), ("adv_id", pa.string()), ("modified", pa.string()), | |
| ("ecosystems", pa.list_(pa.string())), ("cwe_ids", pa.list_(pa.string())), ("via", pa.string())]) | |
| FIX_SCHEMA = pa.schema([("cve_id", pa.string()), ("adv_id", pa.string()), ("repo_url", pa.string()), ("sha", pa.string()), | |
| ("source", pa.string()), ("evidence", pa.string()), ("score", pa.float64()), ("ref_url", pa.string())]) | |
| # score = heuristic confidence that (repo, sha) is a fix commit for the CVE | |
| SCORES = {"range_fixed": 0.95, "ref:FIX": 0.85, "ref:WEB": 0.5, "ref:ADVISORY": 0.5, "ref:REPORT": 0.4, "ref:ARTICLE": 0.4, "ref:PACKAGE": 0.3} | |
| def extract(j, source): | |
| aid = j.get("id", "") | |
| cves, via = [], {} | |
| c = norm_cve(aid) | |
| if c: cves.append(c); via[c] = "id" | |
| for k in ("aliases", "upstream"): | |
| for a in j.get(k) or []: | |
| c = norm_cve(a) | |
| if c and c not in cves: cves.append(c); via[c] = k | |
| if not cves: return [], [] | |
| ecos = [] | |
| fixes = {} | |
| for aff in j.get("affected") or []: | |
| e = ((aff.get("package") or {}).get("ecosystem") or "").split(":")[0] | |
| if e and e not in ecos: ecos.append(e) | |
| for r in aff.get("ranges") or []: | |
| if r.get("type") != "GIT": continue | |
| repo = norm_repo(r.get("repo")) | |
| for ev in r.get("events") or []: | |
| sha = (ev.get("fixed") or "").lower() | |
| if repo and sha and len(sha) >= 7 and all(ch in "0123456789abcdef" for ch in sha): | |
| fixes.setdefault((repo, sha), ("range_fixed", None)) | |
| for ref in j.get("references") or []: | |
| pc = parse_commit_url(ref.get("url")) | |
| if pc: | |
| ev = "ref:" + (ref.get("type") or "WEB") | |
| key = (norm_repo(pc[0]), pc[1]) | |
| old = fixes.get(key) | |
| if old is None or SCORES.get(ev, 0.3) > SCORES.get(old[0], 0.3): fixes[key] = (ev, ref.get("url")) | |
| cwe = list((j.get("database_specific") or {}).get("cwe_ids") or []) | |
| links = [dict(cve_id=c, adv_id=aid, modified=j.get("modified"), ecosystems=ecos, cwe_ids=cwe, via=via[c]) for c in cves] | |
| fx = [dict(cve_id=c, adv_id=aid, repo_url=k[0], sha=k[1], source=source, evidence=f"{source}_{v[0]}", | |
| score=SCORES.get(v[0], 0.3), ref_url=v[1]) for c in cves for k, v in fixes.items()] | |
| return links, fx | |
| def write(prefix, idx, links, fixes): | |
| pq.write_table(pa.Table.from_pylist(links, schema=LINK_SCHEMA), f"{INTER}/{prefix}link_{idx:03d}.parquet", compression="zstd") | |
| pq.write_table(pa.Table.from_pylist(fixes, schema=FIX_SCHEMA), f"{INTER}/{prefix}fix_{idx:03d}.parquet", compression="zstd") | |
| def osv_work(args): | |
| idx, zpath, names = args | |
| z = zipfile.ZipFile(zpath); L, F = [], []; bad = 0 | |
| for n in names: | |
| try: j = orjson.loads(z.read(n)) | |
| except Exception: bad += 1; continue | |
| l, f = extract(j, "osv"); L += l; F += f | |
| write("osv", idx, L, F); return len(names), len(L), len(F), bad | |
| def ghsa_work(args): | |
| idx, paths = args | |
| L, F = [], []; bad = 0 | |
| for p in paths: | |
| try: j = orjson.loads(open(p, "rb").read()) | |
| except Exception: bad += 1; continue | |
| l, f = extract(j, "ghsa") | |
| reviewed = "/github-reviewed/" in p | |
| for x in l: x["via"] = ("reviewed:" if reviewed else "unreviewed:") + x["via"] | |
| L += l; F += f | |
| write("ghsa", idx, L, F); return len(paths), len(L), len(F), bad | |
| if __name__ == "__main__": | |
| which = sys.argv[1] | |
| N = 48 | |
| if which == "osv": | |
| zpath = one("osv/*/all.zip") | |
| names = [n for n in zipfile.ZipFile(zpath).namelist() if n.endswith(".json")] | |
| jobs, fn = [(i, zpath, names[i::N]) for i in range(N)], osv_work | |
| else: | |
| root = one("ghsa/*/advisory-database") | |
| paths = glob.glob(f"{root}/advisories/**/*.json", recursive=True) | |
| jobs, fn = [(i, paths[i::N]) for i in range(N)], ghsa_work | |
| tot = [0, 0, 0, 0] | |
| with mp.Pool(6) as p: | |
| for r in p.imap_unordered(fn, jobs): | |
| tot = [a + b for a, b in zip(tot, r)] | |
| print(which, "records=%d links=%d fixes=%d bad_json=%d" % tuple(tot)) | |
Xet Storage Details
- Size:
- 4.59 kB
- Xet hash:
- 179cc0d059d6f92df8f6eb3fa41bd8219fa8815d56d7fe8581c1f91f0eae53d4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.