Download scripts/ingest/build_corpus.py from continuum-oss/ccrb: direct link, hf CLI and curl.
- Browser
- Download file 6.05 kB
-
https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/ingest/build_corpus.py
- Command line
-
hf download hf://datasets/continuum-oss/ccrb/scripts/ingest/build_corpus.py
-
curl -L -o build_corpus.py https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/ingest/build_corpus.py
6.05 kB
| """Build normalized CCRB document corpora and cross-source relationship graph from data/raw/.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| from collections import Counter, defaultdict | |
| from pathlib import Path | |
| from typing import Any | |
| _REPO_ROOT = Path(__file__).resolve().parent.parent.parent | |
| if str(_REPO_ROOT) not in sys.path: | |
| sys.path.insert(0, str(_REPO_ROOT)) | |
| from scripts.ingest.normalize_kev_nvd import parse_cisa_kev, parse_nvd_page | |
| from scripts.ingest.normalize_mitre import ( | |
| parse_attack_stix, | |
| parse_capec_xml, | |
| parse_cwe_xml, | |
| ) | |
| from scripts.ingest.normalize_osv import parse_osv_zip | |
| from scripts.ingest.normalize_owasp_sigma import ( | |
| parse_owasp_cheatsheets_zip, | |
| parse_sigma_rules_zip, | |
| ) | |
| from scripts.pipeline_utils import TRACKS | |
| def _write_jsonl_dedup(path: Path, records: list[dict[str, Any]], id_key: str) -> int: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| seen: set[str] = set() | |
| written = 0 | |
| with path.open("w", encoding="utf-8") as handle: | |
| for rec in records: | |
| rec_id = str(rec.get(id_key, "")) | |
| if not rec_id or rec_id in seen: | |
| continue | |
| seen.add(rec_id) | |
| handle.write(json.dumps(rec, ensure_ascii=False) + "\n") | |
| written += 1 | |
| return written | |
| def build_corpus( | |
| raw_dir: Path, | |
| data_dir: Path, | |
| *, | |
| max_osv_per_ecosystem: int | None = 15000, | |
| ) -> dict[str, Any]: | |
| """Normalize available raw feeds into data/documents/<track>.jsonl and data/graph/edges.jsonl.""" | |
| by_track: dict[str, list[dict[str, Any]]] = defaultdict(list) | |
| all_edges: list[dict[str, str]] = [] | |
| cwe_file = raw_dir / "cwec_latest.xml.zip" | |
| code_query_seeds: list[dict[str, Any]] = [] | |
| if cwe_file.exists(): | |
| cwe_docs, cwe_rem_docs, cwe_code_docs, cwe_edges = parse_cwe_xml(cwe_file) | |
| by_track["cwe"].extend(cwe_docs) | |
| by_track["remediation"].extend(cwe_rem_docs) | |
| for cdoc in cwe_code_docs: | |
| if cdoc.get("track") == "code_query_seed": | |
| code_query_seeds.append(cdoc) | |
| else: | |
| by_track["code"].append(cdoc) | |
| all_edges.extend(cwe_edges) | |
| capec_file = raw_dir / "capec_latest.xml" | |
| if capec_file.exists(): | |
| capec_docs, capec_edges = parse_capec_xml(capec_file) | |
| by_track["attack"].extend(capec_docs) | |
| all_edges.extend(capec_edges) | |
| stix_files = [ | |
| raw_dir / name | |
| for name in ("enterprise-attack.json", "ics-attack.json", "mobile-attack.json") | |
| if (raw_dir / name).exists() | |
| ] | |
| if stix_files: | |
| att_docs, att_rem_docs, att_edges = parse_attack_stix(stix_files) | |
| by_track["attack"].extend(att_docs) | |
| by_track["remediation"].extend(att_rem_docs) | |
| all_edges.extend(att_edges) | |
| kev_cves: set[str] = set() | |
| kev_file = raw_dir / "cisa_kev.json" | |
| if kev_file.exists(): | |
| kev_docs, kev_edges, kev_cves = parse_cisa_kev(kev_file) | |
| by_track["cve"].extend(kev_docs) | |
| all_edges.extend(kev_edges) | |
| nvd_dir = raw_dir / "nvd" | |
| if nvd_dir.exists(): | |
| for page_path in sorted(nvd_dir.glob("nvd_page_*.json")): | |
| nvd_docs, nvd_edges = parse_nvd_page(page_path, kev_cves=kev_cves) | |
| by_track["cve"].extend(nvd_docs) | |
| all_edges.extend(nvd_edges) | |
| for osv_zip in sorted(raw_dir.glob("osv_*_all.zip")): | |
| adv_docs, adv_edges = parse_osv_zip(osv_zip, max_records=max_osv_per_ecosystem) | |
| by_track["advisories"].extend(adv_docs) | |
| all_edges.extend(adv_edges) | |
| owasp_zip = raw_dir / "owasp_cheatsheets.zip" | |
| if owasp_zip.exists(): | |
| owasp_rems, owasp_code = parse_owasp_cheatsheets_zip(owasp_zip) | |
| by_track["remediation"].extend(owasp_rems) | |
| by_track["code"].extend(owasp_code) | |
| sigma_zip = raw_dir / "sigma_rules.zip" | |
| if sigma_zip.exists(): | |
| sigma_code, sigma_edges = parse_sigma_rules_zip(sigma_zip) | |
| by_track["code"].extend(sigma_code) | |
| all_edges.extend(sigma_edges) | |
| doc_counts: dict[str, int] = {} | |
| docs_dir = data_dir / "documents" | |
| for track in TRACKS: | |
| out_path = docs_dir / f"{track}.jsonl" | |
| doc_counts[track] = _write_jsonl_dedup(out_path, by_track[track], "document_id") | |
| graph_dir = data_dir / "graph" | |
| graph_dir.mkdir(parents=True, exist_ok=True) | |
| _write_jsonl_dedup(graph_dir / "code_query_seeds.jsonl", code_query_seeds, "document_id") | |
| edge_path = graph_dir / "edges.jsonl" | |
| edge_counts: Counter[str] = Counter() | |
| seen_edges: set[tuple[str, str, str]] = set() | |
| with edge_path.open("w", encoding="utf-8") as handle: | |
| for edge in all_edges: | |
| key = (edge["src"], edge["rel"], edge["dst"]) | |
| if key in seen_edges: | |
| continue | |
| seen_edges.add(key) | |
| edge_counts[edge["rel"]] += 1 | |
| handle.write(json.dumps(edge) + "\n") | |
| summary = { | |
| "documents_by_track": doc_counts, | |
| "total_documents": sum(doc_counts.values()), | |
| "total_edges": sum(edge_counts.values()), | |
| "edges_by_relation": dict(sorted(edge_counts.items())), | |
| } | |
| (graph_dir / "summary.json").write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8") | |
| return summary | |
| def main(argv: list[str] | None = None) -> int: | |
| parser = argparse.ArgumentParser(description="Normalize raw feeds into CCRB document corpora.") | |
| parser.add_argument("--raw-dir", type=Path, default=Path("data/raw")) | |
| parser.add_argument("--data-dir", type=Path, default=Path("data")) | |
| parser.add_argument( | |
| "--max-osv-per-ecosystem", | |
| type=int, | |
| default=15000, | |
| help="Cap OSV records per ecosystem zip (0 for unlimited).", | |
| ) | |
| args = parser.parse_args(argv) | |
| cap = None if args.max_osv_per_ecosystem <= 0 else args.max_osv_per_ecosystem | |
| summary = build_corpus(args.raw_dir, args.data_dir, max_osv_per_ecosystem=cap) | |
| print(json.dumps(summary, indent=2)) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |