Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(security): rescan every file when the scanner version changes (#3086)
Aman31dbb3ab0a0b
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/command.py b/packages/cli/src/repowise/cli/commands/init_cmd/command.pyindex ea2f0dcdb..83a9d9e74 100644--- a/packages/cli/src/repowise/cli/commands/init_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/init_cmd/command.py@@ -79,6 +79,7 @@ from repowise.cli.ui import ()from repowise.core.analysis.health import HEALTH_ANALYZER_VERSIONfrom repowise.core.analysis.health.coverage import PARSERS as COVERAGE_PARSERS+from repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSIONfrom repowise.core.docs_mode import docs_mode_state_fields, resolve_docs_modefrom repowise.core.generation.languages import SUPPORTED_LANGUAGESfrom repowise.core.generation.styles import DEFAULT_STYLE, list_styles, resolve_style@@ -1934,6 +1935,9 @@ def init_command(# their stamp. Without it `health_analyzer_changed` reads absent-as-# unchanged and the version trigger never fires for them.base_state["health_analyzer_version"] = HEALTH_ANALYZER_VERSION+ # Same reasoning for the security scanner (#3072): this run just+ # scanned every file, so start tracking its version here too.+ base_state["security_scanner_version"] = SECURITY_SCANNER_VERSION# This run just scored every file, so the periodic re-score cadence# starts now. Without the stamp the gate reads "never re-scored" and the# very next update re-scores the whole repo init had only just scored.diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.pyindex 7321295e3..f412dae2c 100644--- a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py@@ -27,6 +27,7 @@ from repowise.cli.helpers import ()from repowise.cli.state_persistence import build_kg_state, save_knowledge_graph_jsonfrom repowise.core.analysis.health import HEALTH_ANALYZER_VERSION+from repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSIONfrom repowise.core.docs_mode import docs_mode_state_fieldsfrom repowise.core.generation.models import count_stub_fallbacksfrom repowise.core.index_scope import (@@ -525,6 +526,9 @@ def save_full_state_and_config(# tracking it here — otherwise a fresh install carries no stamp and the# first analyzer change after it cannot tell it needs a re-score.state["health_analyzer_version"] = HEALTH_ANALYZER_VERSION+ # Same reasoning for the security scanner (#3072): this run just scanned+ # every file, so start tracking its version here too.+ state["security_scanner_version"] = SECURITY_SCANNER_VERSION# This run just scored every file, so the periodic re-score cadence starts# now. Without the stamp the gate reads "never re-scored" and the very next# update re-scores the whole repo that init had only just finished scoring.diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.pyindex 859a126cd..4cac417dd 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py@@ -2483,6 +2483,19 @@ def run_update("fingerprint was retained so the next update retries.")+ # A scanner version bump means every unchanged file's stored findings may+ # be stale (#3072): the normal persist above only rescanned the changed+ # files. Does its own full parse, since unlike health there is no stored+ # structural fact to replay from the DB for a security finding.+ from repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSION++ from .persistence import run_full_security_rescan, security_scanner_changed++ if security_scanner_changed(state):+ with timed(timings, "security_rescan"):+ if run_full_security_rescan(repo_path, exclude_patterns):+ state["security_scanner_version"] = SECURITY_SCANNER_VERSION+# ---- Editor project files (best-effort) ----with timed(timings, "editor_files"):_refresh_editor_stamp(repo_path, agents_md, degraded)diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex c6e4d5f3e..a05e4fd96 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -28,6 +28,7 @@ from repowise.cli.helpers import (save_state,)from repowise.core.analysis.health import HEALTH_ANALYZER_VERSION+from repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSIONfrom repowise.core.pipeline import PhaseTimings, timedfrom .incremental import _build_repo_graph@@ -500,11 +501,20 @@ def _persist_index_only_update("fingerprint was retained so the next update retries.")+ # Same drift check for the security scanner (#3072): a version bump means+ # every unchanged file's stored findings may be stale, since the normal+ # persist above only rescanned the changed files.+ security_scanner_version = state.get("security_scanner_version")+ if security_scanner_changed(state):+ with timed(timings, "security_rescan"):+ if run_full_security_rescan(repo_path, exclude_patterns or []):+ security_scanner_version = SECURITY_SCANNER_VERSIONnew_state = {**state,"last_sync_commit": head,"last_full_rescore_at": last_full_rescore_at,"health_analyzer_version": health_analyzer_version,+ "security_scanner_version": security_scanner_version,"config_fingerprint": config_fingerprint(repo_path),"config_dependency_fingerprints": (dependency_fingerprints@@ -1699,6 +1709,17 @@ def health_analyzer_changed(state: dict) -> bool:return stored is not None and stored != HEALTH_ANALYZER_VERSION+def security_scanner_changed(state: dict) -> bool:+ """Whether the stored security findings were written by a different scanner.++ Mirrors :func:`health_analyzer_changed`: a legacy state file with no stamp+ is not a change, it picks the stamp up on its first full security rescan+ (#3072).+ """+ stored = state.get("security_scanner_version")+ return stored is not None and stored != SECURITY_SCANNER_VERSION++def full_rescore_due(state: dict, head_ts: float | None) -> bool:"""Whether a full health re-score is due this update.@@ -1745,3 +1766,58 @@ def run_decay_health_rescore(except Exception as exc:console.print(f"[yellow]Idle-file health re-score skipped: {exc}[/yellow]")return False+++def run_full_security_rescan(repo_path: Any, exclude_patterns: list[str]) -> bool:+ """Rescan every tracked working-tree file against the current scanner (#3072).++ Unlike the health decay rescore, there is no stored structural fact to+ replay from the DB: a security finding comes from the raw source text, so+ this does its own full parse (``collect_sources=True``) rather than reusing+ the update's changed-files-only ``parsed_files``. Best-effort: returns True+ on success so the caller can stamp ``security_scanner_version``; a failure+ is logged and leaves the stamp so the next update retries.+ """+ try:+ run_async(_security_rescan_from_full_parse(repo_path, exclude_patterns))+ return True+ except Exception as exc:+ console.print(f"[yellow]Full security rescan skipped: {exc}[/yellow]")+ return False+++async def _security_rescan_from_full_parse(repo_path: Any, exclude_patterns: list[str]) -> None:+ """Full-replace security rescan over every tracked working-tree file.++ Symbol-name findings (``SYMBOL_NAME_KINDS``) need the parsed symbols, so+ this does its own full repo parse with ``collect_sources=True`` rather+ than the changed-files-only ``parsed_files`` the rest of ``update`` works+ from, the same way ``init`` has the full source available for its first+ scan. ``replace_findings`` only ever deletes/replaces working-tree rows+ (``commit_sha`` empty); history rows from ``scan --history`` are untouched.+ """+ from repowise.cli.helpers import get_db_url_for_repo+ from repowise.core.analysis.security_scan import SecurityScanner, scan_source_map+ from repowise.core.persistence import (+ create_engine,+ create_session_factory,+ get_session,+ init_db,+ upsert_repository,+ )++ parsed_files, source_map, _graph_builder, _repo_structure, _file_count = _build_repo_graph(+ repo_path, exclude_patterns, collect_sources=True+ )++ url = get_db_url_for_repo(repo_path)+ engine = create_engine(url)+ try:+ await init_db(engine)+ sf = create_session_factory(engine)+ async with get_session(sf) as session:+ repo = await upsert_repository(session, name=repo_path.name, local_path=str(repo_path))+ findings_by_file, scanned_paths = scan_source_map(parsed_files, source_map)+ await SecurityScanner(session, repo.id).replace_findings(findings_by_file, scanned_paths)+ finally:+ await engine.dispose()diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.pyindex 81244688b..3592ba489 100644--- a/packages/core/src/repowise/core/analysis/security_scan.py+++ b/packages/core/src/repowise/core/analysis/security_scan.py@@ -34,6 +34,15 @@ from repowise.core.test_paths import is_test_related_pathlogger = logging.getLogger(__name__)+# Bump whenever a change alters what the scanner reports: a new/removed+# pattern, a changed severity, a precision fix to an existing rule. ``update``+# compares this against the value stamped in state.json and, on a mismatch,+# rescans every tracked working-tree file instead of only the changed ones,+# the same way ``HEALTH_ANALYZER_VERSION`` forces a full health re-score+# (#3072). Without this, a scanner fix only ever reaches a file a user+# happens to edit.+SECURITY_SCANNER_VERSION = 1+_CREDENTIAL_EXACT_PLACEHOLDERS: frozenset[str] = frozenset({"password", "changeit"})_CREDENTIAL_SUBSTRING_PLACEHOLDERS: tuple[str, ...] = (diff --git a/tests/unit/cli/test_security_rescan_gate.py b/tests/unit/cli/test_security_rescan_gate.pynew file mode 100644index 000000000..8960aadb7--- /dev/null+++ b/tests/unit/cli/test_security_rescan_gate.py@@ -0,0 +1,127 @@+"""The gate that decides when `repowise update` rescans security findings+in full (#3072), and the full rescan itself.++Mirrors ``test_health_rescore_gate.py``'s ``TestHealthAnalyzerChanged``: a+scanner version bump is the only trigger here, there is no time cadence,+since security findings do not decay the way health scores do.+"""++from __future__ import annotations++from pathlib import Path++import pytest+from sqlalchemy import text++from repowise.cli.commands.update_cmd.persistence import (+ run_full_security_rescan,+ security_scanner_changed,+)+from repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSION+++class TestSecurityScannerChanged:+ def test_absent_stamp_is_not_a_change(self):+ """A legacy state file must not read as drift (mirrors health's rule)."""+ assert security_scanner_changed({}) is False++ def test_matching_stamp_is_not_a_change(self):+ assert (+ security_scanner_changed({"security_scanner_version": SECURITY_SCANNER_VERSION})+ is False+ )++ @pytest.mark.parametrize(+ "stored", [SECURITY_SCANNER_VERSION - 1, SECURITY_SCANNER_VERSION + 1]+ )+ def test_any_different_stamp_is_a_change(self, stored):+ """Not `<`: a downgrade also leaves rows this scanner did not write."""+ assert security_scanner_changed({"security_scanner_version": stored}) is True+++async def _rows(repo_path: Path) -> list[tuple]:+ from repowise.cli.helpers import get_db_url_for_repo+ from repowise.core.persistence import create_engine, create_session_factory, get_session++ engine = create_engine(get_db_url_for_repo(repo_path))+ sf = create_session_factory(engine)+ async with get_session(sf) as session:+ res = await session.execute(+ text(+ "SELECT file_path, kind, line_number, commit_sha FROM security_findings "+ "ORDER BY file_path, line_number, kind"+ )+ )+ rows = [tuple(r) for r in res.fetchall()]+ await engine.dispose()+ return rows+++async def _seed_repo_with_a_stale_row(tmp_path: Path, *, with_history_row: bool) -> None:+ """A tiny repo with one file the current scanner has no finding for, plus+ a row as if an older scanner version wrote it there (the issue's repro)."""+ from repowise.core.persistence import (+ create_engine,+ create_session_factory,+ get_session,+ init_db,+ )+ from repowise.core.persistence.models import Repository++ (tmp_path / ".repowise").mkdir(exist_ok=True)+ (tmp_path / "a.py").write_text("def b():\n return 2\n", encoding="utf-8")++ from repowise.cli.helpers import get_db_url_for_repo++ engine = create_engine(get_db_url_for_repo(tmp_path))+ await init_db(engine)+ sf = create_session_factory(engine)+ async with get_session(sf) as session:+ session.add(Repository(id="repo-1", name="t", local_path=str(tmp_path)))+ await session.flush()+ await session.execute(+ text(+ "INSERT INTO security_findings "+ "(repository_id, file_path, kind, severity, snippet, line_number, "+ "commit_sha, detected_at) "+ "VALUES ('repo-1', 'a.py', 'hardcoded_secret', 'high', 'x', 1, "+ "'', datetime('now'))"+ )+ )+ if with_history_row:+ await session.execute(+ text(+ "INSERT INTO security_findings "+ "(repository_id, file_path, kind, severity, snippet, line_number, "+ "commit_sha, detected_at) "+ "VALUES ('repo-1', 'a.py', 'hardcoded_secret', 'high', 'x', 1, "+ "'deadbeef', datetime('now'))"+ )+ )+ await engine.dispose()+++class TestFullSecurityRescan:+ def test_a_stale_working_tree_row_is_replaced(self, tmp_path: Path) -> None:+ import asyncio++ asyncio.run(_seed_repo_with_a_stale_row(tmp_path, with_history_row=False))+ before = asyncio.run(_rows(tmp_path))+ assert ("a.py", "hardcoded_secret", 1, "") in before++ assert run_full_security_rescan(tmp_path, []) is True++ after = asyncio.run(_rows(tmp_path))+ # The stale row named a finding the current scanner does not make for+ # this file's actual content; the rescan must not leave it standing.+ assert ("a.py", "hardcoded_secret", 1, "") not in after++ def test_a_history_row_survives_the_full_rescan(self, tmp_path: Path) -> None:+ import asyncio++ asyncio.run(_seed_repo_with_a_stale_row(tmp_path, with_history_row=True))+ assert run_full_security_rescan(tmp_path, []) is True++ after = asyncio.run(_rows(tmp_path))+ # replace_findings only ever touches commit_sha='' (working-tree) rows.+ assert ("a.py", "hardcoded_secret", 1, "deadbeef") in after