Download scripts/ingest/normalize_osv.py from continuum-oss/ccrb: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/ingest/normalize_osv.py
- Command line
-
hf download hf://datasets/continuum-oss/ccrb/scripts/ingest/normalize_osv.py
-
curl -L -o normalize_osv.py https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/ingest/normalize_osv.py
3.56 kB
| """Normalize OSV / GitHub Security Advisory (GHSA) zip archives into CCRB advisory documents.""" | |
| from __future__ import annotations | |
| import json | |
| import re | |
| import zipfile | |
| from pathlib import Path | |
| from typing import Any | |
| def _clean_ws(text: str | None) -> str: | |
| if not text: | |
| return "" | |
| return re.sub(r"\s+", " ", text).strip() | |
| def parse_osv_zip( | |
| zip_path: Path, | |
| *, | |
| max_records: int | None = None, | |
| ) -> tuple[list[dict[str, Any]], list[dict[str, str]]]: | |
| """Parse an OSV ecosystem `all.zip` into advisory documents and cross-source edges.""" | |
| docs: list[dict[str, Any]] = [] | |
| edges: list[dict[str, str]] = [] | |
| with zipfile.ZipFile(zip_path, "r") as zf: | |
| json_names = sorted(n for n in zf.namelist() if n.lower().endswith(".json")) | |
| for name in json_names: | |
| if max_records is not None and len(docs) >= max_records: | |
| break | |
| with zf.open(name) as handle: | |
| obj = json.loads(handle.read().decode("utf-8")) | |
| if obj.get("withdrawn"): | |
| continue | |
| adv_id = str(obj.get("id", "")).strip() | |
| if not adv_id: | |
| continue | |
| summary = _clean_ws(obj.get("summary")) | |
| details = _clean_ws(obj.get("details")) | |
| if not summary and not details: | |
| continue | |
| packages: list[str] = [] | |
| for affected in obj.get("affected", []): | |
| pkg = affected.get("package", {}) | |
| eco = pkg.get("ecosystem", "") | |
| pkg_name = pkg.get("name", "") | |
| if pkg_name: | |
| packages.append(f"{eco}:{pkg_name}" if eco else pkg_name) | |
| aliases = [str(a).strip() for a in obj.get("aliases", []) if str(a).strip()] | |
| for alias in aliases: | |
| if alias.startswith("CVE-"): | |
| edges.append({"src": adv_id, "rel": "alias_cve", "dst": alias}) | |
| db_specific = obj.get("database_specific", {}) | |
| cwe_ids = [str(c).strip() for c in db_specific.get("cwe_ids", []) if str(c).strip()] | |
| for cwe in cwe_ids: | |
| edges.append({"src": adv_id, "rel": "has_cwe", "dst": cwe}) | |
| fix_urls = [ | |
| str(ref.get("url", "")).strip() | |
| for ref in obj.get("references", []) | |
| if ref.get("type") == "FIX" and ref.get("url") | |
| ] | |
| for fix_url in fix_urls: | |
| edges.append({"src": adv_id, "rel": "fix_url", "dst": fix_url}) | |
| body_parts = [summary, details] | |
| if packages: | |
| body_parts.append("Affected packages: " + ", ".join(sorted(set(packages))[:8])) | |
| if cwe_ids: | |
| body_parts.append("Weaknesses: " + ", ".join(cwe_ids)) | |
| safe_doc_suffix = re.sub(r"[^A-Za-z0-9._-]+", "_", adv_id) | |
| docs.append( | |
| { | |
| "document_id": f"adv_doc_{safe_doc_suffix}", | |
| "title": f"{adv_id}: {summary or adv_id}", | |
| "text": "\n\n".join(p for p in body_parts if p), | |
| "source": "OSV / GHSA", | |
| "track": "advisories", | |
| "source_id": adv_id, | |
| "license": "CC-BY-4.0", | |
| "published_date": str(obj.get("published", ""))[:10], | |
| "aliases": aliases, | |
| "fix_urls": fix_urls, | |
| "url": f"https://osv.dev/vulnerability/{adv_id}", | |
| } | |
| ) | |
| return docs, edges | |