| |
| """CVSS 3.1 vector helper for SecureCodePairs. |
| |
| Records may optionally carry an explicit ``cvss_vector``. When absent, a |
| defensible default is derived from the severity bucket and a few flags |
| (network-exploitable, requires privileges, user interaction, scope change). |
| This keeps the dataset consistent and machine-readable without hand-authoring |
| 200+ vectors. |
| """ |
| from typing import Dict, Optional |
|
|
| SEVERITY_DEFAULTS: Dict[str, str] = { |
| |
| "Critical": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H", |
| "High": "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H", |
| "Medium": "CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:L/A:N", |
| "Low": "CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:U/C:L/I:N/A:N", |
| } |
|
|
| |
| CRITICAL_IF = {"rce", "deserialization", "command-injection", "ssrf", "sqli", "auth-bypass"} |
|
|
|
|
| def compute_cvss(record: dict) -> str: |
| explicit = record.get("cvss_vector") |
| if explicit and explicit.startswith("CVSS:"): |
| return explicit |
| sev = record.get("severity", "Medium") |
| if sev not in SEVERITY_DEFAULTS: |
| sev = "Medium" |
| |
| tags = set(record.get("tags", [])) |
| if sev == "High" and (tags & CRITICAL_IF): |
| return SEVERITY_DEFAULTS["Critical"] |
| return SEVERITY_DEFAULTS[sev] |
|
|