Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/secfoo/report/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,35 @@ def classify_design_verdict(value: str | None) -> str | None:
if v.startswith("sound"):
return "approve"
return None


_VERDICT_LABELS = {"approve": "Sound", "conditions": "Sound with conditions", "reject": "Not sound"}
# Longest first: "sound" is a prefix of "sound with conditions".
_VERDICT_PREFIXES = ("sound with conditions", "not applicable", "not sound", "sound")


def split_design_verdict(value: str | None) -> tuple[str, str]:
"""A Design verdict line -> (short label, the rest of the sentence).

Agents write the verdict as a full sentence ("Not sound for any
deployment handling real users. It fits its declared purpose..."). The
label comes from classify_design_verdict, so it always agrees with the
badge colour and the gate-decision mix; the reason is whatever follows
the leading verdict phrase, for display beside the label.
"""
text = (value or "").strip()
lower = text.lower()
bucket = classify_design_verdict(text)
if bucket:
label = _VERDICT_LABELS[bucket]
elif lower.startswith(("not applicable", "n/a")):
label = "Not applicable"
else:
label = "Unclear"
rest = text
for prefix in _VERDICT_PREFIXES:
if lower.startswith(prefix):
rest = text[len(prefix):]
break
rest = rest.lstrip(" \t\u2014\u2013-:;,.").strip()
return label, (rest[:1].upper() + rest[1:]) if rest else ""
10 changes: 8 additions & 2 deletions src/secfoo/storage/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,16 @@ def activity_summary_by_project(self, skill_id: str) -> list[dict]:
# (diagram, design verdict) -- a failed/timeout run has no
# report to read, but shouldn't hide an earlier successful one.
latest_success = self._conn.execute(
"SELECT report_path FROM runs WHERE skill_id = ? AND project_id = ? "
"SELECT run_uuid, report_path, finished_at FROM runs WHERE skill_id = ? AND project_id = ? "
"AND status = 'success' AND report_path IS NOT NULL "
"ORDER BY started_at DESC LIMIT 1",
(skill_id, data["project_id"]),
).fetchone()
data["latest_success_report_path"] = latest_success["report_path"] if latest_success else None
# Which run that content came from, and when -- so a verdict links
# to the review it was read from, not a later failed run.
data["latest_success_run_uuid"] = latest_success["run_uuid"] if latest_success else None
data["latest_success_at"] = latest_success["finished_at"] if latest_success else None

summary.append(data)
return summary
Expand Down Expand Up @@ -1026,7 +1030,7 @@ def unreviewed_and_stale_projects(self, skill_id: str, *, stale_days: int = 365)
(never reviewed, then oldest) first.
"""
projects = self._conn.execute(
"SELECT DISTINCT projects.id, projects.display_name, projects.kind "
"SELECT DISTINCT projects.id, projects.display_name, projects.kind, projects.last_run_at "
"FROM projects JOIN assessments ON assessments.project_id = projects.id"
).fetchall()

Expand All @@ -1045,6 +1049,7 @@ def unreviewed_and_stale_projects(self, skill_id: str, *, stale_days: int = 365)
"project_display_name": project["display_name"],
"project_kind": project["kind"],
"state": "never_reviewed",
"last_activity_at": project["last_run_at"],
"last_reviewed_at": None,
"days_since_review": None,
}
Expand All @@ -1064,6 +1069,7 @@ def unreviewed_and_stale_projects(self, skill_id: str, *, stale_days: int = 365)
"project_display_name": project["display_name"],
"project_kind": project["kind"],
"state": "stale",
"last_activity_at": project["last_run_at"],
"last_reviewed_at": latest["started_at"],
"days_since_review": round(days_since),
}
Expand Down
14 changes: 12 additions & 2 deletions src/secfoo/web/architecture_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ class ArchitectureDashboard:
disposition_total: int = 0
exception_aging: dict[str, int] = field(default_factory=dict)
stale_projects: list[dict] = field(default_factory=list)
# The "Unreviewed & stale" panel shows only the most recently active
# few. The full list above still labels review status on the By project
# rows that are shown.
recent_stale_projects: list[dict] = field(default_factory=list)
root_causes: list[tuple[str, int]] = field(default_factory=list)
gate_mix: dict[str, int] = field(default_factory=dict)
top_standards: list[tuple[str, int]] = field(default_factory=list)


RECENT_STALE_LIMIT = 5


def build(repo: RunRepository) -> ArchitectureDashboard:
dash = ArchitectureDashboard()

Expand Down Expand Up @@ -166,8 +173,8 @@ def build(repo: RunRepository) -> ArchitectureDashboard:
)
dash.uncovered_ccm_domains = [name for code, name in CCM_DOMAINS if not ccm_assessed[code]]

dash.root_causes = root_cause_counter.most_common(8)
dash.top_standards = standards_counter.most_common(5)
dash.root_causes = root_cause_counter.most_common()
dash.top_standards = standards_counter.most_common()
dash.gate_mix = {
"approve": gate_counter.get("approve", 0),
"conditions": gate_counter.get("conditions", 0),
Expand All @@ -176,5 +183,8 @@ def build(repo: RunRepository) -> ArchitectureDashboard:

dash.stale_projects = repo.unreviewed_and_stale_projects(SKILL_ID, stale_days=STALE_DAYS)
append_boundary_changed_projects(repo, SKILL_ID, projects, dash.stale_projects)
dash.recent_stale_projects = sorted(
dash.stale_projects, key=lambda p: p.get("last_activity_at") or "", reverse=True
)[:RECENT_STALE_LIMIT]

return dash
1 change: 1 addition & 0 deletions src/secfoo/web/dashboard_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def append_boundary_changed_projects(
"project_kind": project["project_kind"],
"state": "boundary_changed",
"last_reviewed_at": project.get("latest_started_at"),
"last_activity_at": project.get("latest_started_at"),
"days_since_review": None,
}
)
66 changes: 64 additions & 2 deletions src/secfoo/web/routes/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
from fastapi import APIRouter, Form, HTTPException, Request
from fastapi.responses import RedirectResponse

from secfoo.report.architecture import count_trust_boundaries, extract_design_verdict, extract_mermaid_diagram
from secfoo.report.architecture import (
count_trust_boundaries,
extract_design_verdict,
extract_mermaid_diagram,
split_design_verdict,
)
from secfoo.skills.loader import SkillLoadError, load_skill
from secfoo.storage.repository import RunRepository
from secfoo.web import architecture_dashboard as arch_dashboard
Expand All @@ -35,6 +40,11 @@
router = APIRouter()


# Design verdicts and the architecture "By project" table show only the
# newest reviews. Stat cards above them still count every project.
RECENT_VERDICTS_LIMIT = 5
BY_PROJECT_RECENT_LIMIT = 6

_GATE_MIX_COLORS = {
"approve": "var(--color-success)",
"conditions": "var(--color-warning)",
Expand Down Expand Up @@ -81,6 +91,10 @@ def _attach_architecture_signals(projects: list[dict]) -> bool:
text = Path(report_path).read_text(encoding="utf-8", errors="replace")
project["diagram_source"] = extract_mermaid_diagram(text)
project["design_verdict"] = extract_design_verdict(text)
if project["design_verdict"]:
# Agents write the verdict as a full sentence; badges show only
# the short label and the rest is displayed as wrapping text.
project["verdict_label"], project["verdict_reason"] = split_design_verdict(project["design_verdict"])
project["boundary_count"] = count_trust_boundaries(text)
if project["diagram_source"]:
any_diagram = True
Expand Down Expand Up @@ -108,7 +122,12 @@ def _donut_gradient(counts: dict[str, int], color_map: dict[str, str]) -> str:


@router.get("/activities/{skill_id}")
def activity_detail(request: Request, skill_id: str, diagram_project: int | None = None):
def activity_detail(
request: Request,
skill_id: str,
diagram_project: int | None = None,
stride_project: int | None = None,
):
try:
skill = load_skill(skill_id)
except SkillLoadError as exc:
Expand All @@ -126,6 +145,12 @@ def activity_detail(request: Request, skill_id: str, diagram_project: int | None
)

diagrams_available = _attach_architecture_signals(projects)
verdict_count = sum(1 for p in projects if p.get("design_verdict"))
recent_verdicts = sorted(
(p for p in projects if p.get("design_verdict")),
key=lambda p: p.get("latest_success_at") or "",
reverse=True,
)[:RECENT_VERDICTS_LIMIT]

# A gallery rendering every project's diagram at once doesn't scale
# past a couple of projects -- instead show one at a time, picked via
Expand All @@ -150,6 +175,15 @@ def activity_detail(request: Request, skill_id: str, diagram_project: int | None
),
"run_count": sum(p["run_count"] for p in projects),
"diagrams_available": diagrams_available,
"recent_verdicts": recent_verdicts,
# Architecture only: the table shows the newest projects. Other
# activities keep the full list. Review state (stale / boundary
# changed) is still resolved for every project, so the rows that
# are shown can be labelled.
"by_project_rows": projects[:BY_PROJECT_RECENT_LIMIT] if dash is not None else projects,
"by_project_total": len(projects),
"review_states": {p["project_id"]: p for p in dash.stale_projects} if dash else None,
"verdict_count": verdict_count,
"diagram_projects": diagram_projects,
"selected_diagram_project": selected_diagram_project,
"mermaid_available": mermaid_bundle_available(),
Expand All @@ -164,6 +198,12 @@ def activity_detail(request: Request, skill_id: str, diagram_project: int | None
context["past_due_sla_days"] = arch_dashboard.PAST_DUE_SLA_DAYS
if tm_dash is not None:
context["past_due_sla_days"] = tm_dashboard.PAST_DUE_SLA_DAYS
# One project's STRIDE matrix at a time. The portfolio total above
# it still sums every project.
context["selected_stride_project"] = next(
(p for p in tm_dash.stride_by_project if p["project_id"] == stride_project),
None,
) or (tm_dash.stride_by_project[0] if tm_dash.stride_by_project else None)
if sca_dash is not None:
context["sca_severity_gradient"] = _donut_gradient(sca_dash.severity_counts, _SCA_SEVERITY_COLORS)
if sast_dash is not None:
Expand All @@ -174,6 +214,28 @@ def activity_detail(request: Request, skill_id: str, diagram_project: int | None
return templates.TemplateResponse(request, "activities/detail.html", context)


@router.get("/activities/threat-modeling/post-build")
def threat_modeling_post_build(request: Request):
"""Every threat recorded with `secfoo miss`. The dashboard card shows
only the newest handful."""
with RunRepository() as repo:
findings = repo.list_post_build_findings()
return templates.TemplateResponse(
request, "activities/threat_modeling_post_build.html", {"findings": findings}
)


@router.get("/activities/threat-modeling/assumptions")
def threat_modeling_assumptions(request: Request):
"""Every falsified assumption from the latest model of each project.
The dashboard card shows only the newest handful."""
with RunRepository() as repo:
assumptions = tm_dashboard.build(repo).falsified_assumptions
return templates.TemplateResponse(
request, "activities/threat_modeling_assumptions.html", {"assumptions": assumptions}
)


@router.get("/activities/sast/findings")
def sast_findings_list(
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion src/secfoo/web/sast_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def build(repo: RunRepository) -> SastDashboard:
exploitable_count += 1

dash.cvss_band_counts = {band: band_counter.get(band, 0) for band in _CVSS_BANDS}
dash.top_cwe_counts = cwe_counter.most_common(10)
dash.top_cwe_counts = cwe_counter.most_common()
dash.funnel = {
"findings": len(dash.open_findings),
"exploitable": exploitable_count,
Expand Down
Loading
Loading