Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Ap6pack_malwar /src /malwar /models /scan.py
| """Scan request and result models.""" | |
| from __future__ import annotations | |
| from datetime import UTC, datetime | |
| from pydantic import BaseModel, Field, computed_field | |
| from malwar.core.constants import ( | |
| SEVERITY_WEIGHTS, | |
| ScanStatus, | |
| Severity, | |
| is_fragile_rule_set, | |
| ) | |
| from malwar.models.finding import Finding | |
| class ScanRequest(BaseModel): | |
| """Input for initiating a scan.""" | |
| target: str = Field(description="File path, directory path, or URL") | |
| layers: list[str] = Field( | |
| default=["rule_engine", "url_crawler", "llm_analyzer", "threat_intel"], | |
| ) | |
| follow_urls: bool = True | |
| use_llm: bool = True | |
| max_url_depth: int = Field(default=2, ge=0, le=5) | |
| class ScanResult(BaseModel): | |
| """Complete result of scanning a single SKILL.md.""" | |
| scan_id: str | |
| target: str | |
| status: ScanStatus = ScanStatus.PENDING | |
| started_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) | |
| completed_at: datetime | None = None | |
| duration_ms: int | None = None | |
| findings: list[Finding] = Field(default_factory=list) | |
| skill_sha256: str = "" | |
| skill_name: str | None = None | |
| skill_author: str | None = None | |
| layers_executed: list[str] = Field(default_factory=list) | |
| errors: list[str] = Field(default_factory=list) | |
| ml_risk_score: float | None = Field( | |
| default=None, | |
| description="ML model risk probability [0.0, 1.0], None if ML disabled", | |
| ) | |
| # type: ignore[prop-decorator] | |
| def risk_score(self) -> int: | |
| # Delegates to the single calibrated implementation so the CLI, SDK, | |
| # API and registry monitor can never diverge on scoring. | |
| from malwar.scanner.severity import compute_risk_score | |
| return compute_risk_score(self.findings) | |
| # type: ignore[prop-decorator] | |
| def fragile_detection(self) -> bool: | |
| """True when the verdict rests on a single high-false-positive rule. | |
| Surfaced so callers (reports, the registry monitor, API consumers) can | |
| tell "capped pending verification" apart from a genuinely mid-range | |
| score, and can prioritise these for a second opinion. | |
| """ | |
| active = [f for f in self.findings if not f.suppressed] | |
| return bool(active) and is_fragile_rule_set({f.rule_id for f in active}) | |
| # type: ignore[prop-decorator] | |
| def overall_severity(self) -> Severity: | |
| active = [f for f in self.findings if not f.suppressed] | |
| if not active: | |
| return Severity.INFO | |
| return max(active, key=lambda f: SEVERITY_WEIGHTS[f.severity]).severity | |
| # type: ignore[prop-decorator] | |
| def verdict(self) -> str: | |
| if self.risk_score >= 75: | |
| return "MALICIOUS" | |
| if self.risk_score >= 40: | |
| return "SUSPICIOUS" | |
| if self.risk_score >= 15: | |
| return "CAUTION" | |
| return "CLEAN" | |
| # type: ignore[prop-decorator] | |
| def finding_count_by_severity(self) -> dict[str, int]: | |
| counts: dict[str, int] = {} | |
| for f in self.findings: | |
| if f.suppressed: | |
| continue | |
| counts[f.severity] = counts.get(f.severity, 0) + 1 | |
| return counts | |
| # type: ignore[prop-decorator] | |
| def active_finding_count(self) -> int: | |
| """Findings that count toward risk scoring (i.e. not suppressed).""" | |
| return sum(1 for f in self.findings if not f.suppressed) | |
| # type: ignore[prop-decorator] | |
| def suppressed_finding_count(self) -> int: | |
| """Findings a later layer identified as false positives.""" | |
| return sum(1 for f in self.findings if f.suppressed) | |
Xet Storage Details
- Size:
- 3.8 kB
- Xet hash:
- 363e53b7e6c5a7915b296ca85dd68e0ebc2ef5e6a09372e268f9d0f8608b89f5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.