From 49dedd13165c24df9953e03fee099b18ab896435 Mon Sep 17 00:00:00 2001 From: AmirNCI Date: Tue, 27 Oct 2026 20:39:06 +0000 Subject: [PATCH] SECFOO-22 Fix 422 error when "All projects" is selected on findings lists The SAST findings, SCA vulnerabilities and secret findings lists have an "All projects" option whose value is empty, so the form submits project_id=. The route declared project_id as `int | None`, which FastAPI rejects for an empty string with a 422 -- so picking "All projects", or pressing Search on the SAST list with it selected (the default), showed a raw JSON error instead of the list. The existing tests passed only because they built URLs by hand without the empty param. project_id is now taken as a string and parsed leniently: empty or non-numeric means "all projects". Regression test on each page. Co-Authored-By: Claude Opus 5.5 --- src/secfoo/web/routes/activities.py | 21 ++++++++++++++++--- tests/test_web/test_sast_finding_route.py | 8 +++++++ .../test_web/test_sca_vulnerability_route.py | 7 +++++++ tests/test_web/test_secret_scanning_route.py | 7 +++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/secfoo/web/routes/activities.py b/src/secfoo/web/routes/activities.py index d5e81f3..0044edc 100644 --- a/src/secfoo/web/routes/activities.py +++ b/src/secfoo/web/routes/activities.py @@ -32,6 +32,18 @@ router = APIRouter() + +def _project_id(value: str | None) -> int | None: + """The findings lists' "All projects" option submits project_id= + (empty). Declared as `int | None`, FastAPI rejected that with a 422, so + picking "All projects" -- or pressing Search with it selected, the + default -- showed a JSON error. Take the raw string instead; empty or + non-numeric means "all projects".""" + try: + return int((value or "").strip()) + except ValueError: + return None + _GATE_MIX_COLORS = { "approve": "var(--color-success)", "conditions": "var(--color-warning)", @@ -176,7 +188,7 @@ def sast_findings_list( request: Request, status: str = "open", q: str | None = None, - project_id: int | None = None, + project_id: str | None = None, ): """The full, filterable SAST findings register -- the program dashboard itself only ever shows a short "Top findings" preview (see @@ -184,6 +196,7 @@ def sast_findings_list( project filter the dashboard preview deliberately doesn't have (it's already cross-project by design). """ + project_id = _project_id(project_id) with RunRepository() as repo: projects = repo.activity_summary_by_project(sast_dashboard.SKILL_ID) findings = repo.list_sast_findings(project_id=project_id, status=status, search=q or None) @@ -219,7 +232,7 @@ def sast_finding_detail(request: Request, fingerprint: str): @router.get("/activities/{skill_id}/vulnerabilities") -def sca_vulnerabilities_list(request: Request, skill_id: str, project_id: int | None = None): +def sca_vulnerabilities_list(request: Request, skill_id: str, project_id: str | None = None): """The full SCA vulnerability register, filterable by project -- what the program dashboard's "Top vulnerabilities" preview links out to. Filtering happens post-aggregation, over sca_dashboard.build()'s @@ -230,6 +243,7 @@ def sca_vulnerabilities_list(request: Request, skill_id: str, project_id: int | """ if skill_id != sca_dashboard.SKILL_ID: raise HTTPException(status_code=404, detail="Not found") + project_id = _project_id(project_id) with RunRepository() as repo: projects = repo.activity_summary_by_project(sca_dashboard.SKILL_ID) @@ -273,11 +287,12 @@ def sca_vulnerability_detail(request: Request, skill_id: str, key: str): @router.get("/activities/secret-scanning/findings") -def secret_scanning_findings_list(request: Request, project_id: int | None = None): +def secret_scanning_findings_list(request: Request, project_id: str | None = None): """The full secret findings register, filterable by project -- what the program dashboard's "Top findings" preview links out to, same pattern as SAST's and SCA's findings-list pages. """ + project_id = _project_id(project_id) with RunRepository() as repo: projects = repo.activity_summary_by_project(secret_scanning_dashboard.SKILL_ID) dash = secret_scanning_dashboard.build(repo) diff --git a/tests/test_web/test_sast_finding_route.py b/tests/test_web/test_sast_finding_route.py index 260696e..0b80462 100644 --- a/tests/test_web/test_sast_finding_route.py +++ b/tests/test_web/test_sast_finding_route.py @@ -157,3 +157,11 @@ def test_sast_finding_detail_renders_for_a_real_finding(client): def test_sast_finding_detail_404s_for_unknown_fingerprint(client): resp = client.get("/activities/sast/findings/does-not-exist") assert resp.status_code == 404 + + +def test_sast_findings_list_accepts_the_all_projects_option(client): + # The "All projects" option submits project_id= (empty); this used to 422. + _seed_finding(fingerprint="fp-sqli", title="SQL Injection") + resp = client.get("/activities/sast/findings?status=open&project_id=&q=") + assert resp.status_code == 200 + assert "SQL Injection" in resp.text diff --git a/tests/test_web/test_sca_vulnerability_route.py b/tests/test_web/test_sca_vulnerability_route.py index e4ac48f..6a3b322 100644 --- a/tests/test_web/test_sca_vulnerability_route.py +++ b/tests/test_web/test_sca_vulnerability_route.py @@ -224,3 +224,10 @@ def test_arch_and_tm_dashboards_unaffected_by_sca_dashboard_addition(client, tmp tm_resp = client.get("/activities/threat-modeling") assert tm_resp.status_code == 200 assert "Program dashboard" in tm_resp.text + + +def test_sca_vulnerabilities_list_accepts_the_all_projects_option(client, tmp_path): + _seed(tmp_path) + resp = client.get("/activities/sca-reachability/vulnerabilities?project_id=") + assert resp.status_code == 200 + assert "aiohttp" in resp.text diff --git a/tests/test_web/test_secret_scanning_route.py b/tests/test_web/test_secret_scanning_route.py index 86baf32..eec0285 100644 --- a/tests/test_web/test_secret_scanning_route.py +++ b/tests/test_web/test_secret_scanning_route.py @@ -136,3 +136,10 @@ def test_secret_scanning_finding_detail_renders_for_a_real_finding(client, tmp_p def test_secret_scanning_finding_detail_404s_for_unknown_key(client, tmp_path): resp = client.get("/activities/secret-scanning/findings/deadbeefdeadbeef") assert resp.status_code == 404 + + +def test_secret_scanning_findings_list_accepts_the_all_projects_option(client, tmp_path): + _seed(tmp_path) + resp = client.get("/activities/secret-scanning/findings?project_id=") + assert resp.status_code == 200 + assert "AWS access key" in resp.text