File size: 1,977 Bytes
7cfea77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""The fixed schema a scan turns into. Matches the real shape
`scan_own_domains.py` and `functions/api/scan.js` already produce -
`{severity, title}` findings per target - this module doesn't invent a new
finding format, it wraps the existing one so it can be chained and diffed.
"""
from __future__ import annotations

from dataclasses import dataclass, field


@dataclass(frozen=True)
class Finding:
    severity: str   # "critical" | "high" | "medium" | "low" - as scan_own_domains.py emits
    title: str
    detail: str = ""

    @classmethod
    def from_dict(cls, d: dict) -> "Finding":
        return cls(severity=d["severity"], title=d["title"], detail=d.get("detail", ""))

    def to_dict(self) -> dict:
        d = {"severity": self.severity, "title": self.title}
        if self.detail:
            d["detail"] = self.detail
        return d


_SEVERITY_ORDER = {"critical": 3, "high": 2, "medium": 1, "low": 0}


@dataclass
class ScanRecord:
    target: str
    scanned_at: str          # ISO 8601, as produced by scan_own_domains.py / scan.js
    findings: list[Finding] = field(default_factory=list)

    def worst_severity(self) -> str | None:
        if not self.findings:
            return None
        return max(self.findings, key=lambda f: _SEVERITY_ORDER.get(f.severity, -1)).severity

    def severity_counts(self) -> dict:
        counts = {"critical": 0, "high": 0, "medium": 0, "low": 0}
        for f in self.findings:
            counts[f.severity] = counts.get(f.severity, 0) + 1
        return counts

    def to_dict(self) -> dict:
        return {
            "target": self.target,
            "scanned_at": self.scanned_at,
            "findings": [f.to_dict() for f in self.findings],
        }

    @classmethod
    def from_dict(cls, d: dict) -> "ScanRecord":
        return cls(
            target=d["target"],
            scanned_at=d["scanned_at"],
            findings=[Finding.from_dict(f) for f in d.get("findings", [])],
        )