File size: 1,465 Bytes
2ef05ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
"""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] = {
    # CVSS:3.1 base strings (base score in comment)
    "Critical": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",   # 9.8
    "High":     "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",   # 8.8
    "Medium":   "CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:L/A:N",   # 6.1
    "Low":      "CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:U/C:L/I:N/A:N",   # 2.6
}

# Flags that escalate a vector toward the higher-severity default.
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"
    # Escalate High->Critical when clearly RCE-class and network-exploitable.
    tags = set(record.get("tags", []))
    if sev == "High" and (tags & CRITICAL_IF):
        return SEVERITY_DEFAULTS["Critical"]
    return SEVERITY_DEFAULTS[sev]