diff --git a/src/secfoo/storage/repository.py b/src/secfoo/storage/repository.py index 39347a3..77e320c 100644 --- a/src/secfoo/storage/repository.py +++ b/src/secfoo/storage/repository.py @@ -1,6 +1,7 @@ from __future__ import annotations import json +import re import sqlite3 import uuid from contextlib import contextmanager @@ -165,21 +166,76 @@ def _migrate(self) -> None: existing = {row["name"] for row in self._conn.execute("PRAGMA table_info(runs)").fetchall()} for column, ddl in _RUNS_MIGRATIONS.items(): if column not in existing: - self._conn.execute(ddl) + self._add_column(ddl) existing_assessment_cols = { row["name"] for row in self._conn.execute("PRAGMA table_info(assessments)").fetchall() } for column, ddl in _ASSESSMENTS_MIGRATIONS.items(): if column not in existing_assessment_cols: - self._conn.execute(ddl) + self._add_column(ddl) existing_sast_cols = { row["name"] for row in self._conn.execute("PRAGMA table_info(sast_findings)").fetchall() } for column, ddl in _SAST_FINDINGS_MIGRATIONS.items(): if column not in existing_sast_cols: - self._conn.execute(ddl) + self._add_column(ddl) self._conn.commit() self._migrate_attachment_kind_check() + self._migrate_runs_status_check() + + def _add_column(self, ddl: str) -> None: + # Skills run in parallel threads, each opening its own connection, so + # two connections can both see a column missing on an old database + # and race to add it; the loser's "duplicate column" is success. + try: + self._conn.execute(ddl) + except sqlite3.OperationalError as exc: + if "duplicate column name" not in str(exc): + raise + + def _migrate_runs_status_check(self) -> None: + """Widens runs.status's CHECK to allow 'not_configured' (the api agent + with no key) on a database created before that status existed; saving + such a run otherwise fails with an IntegrityError. + + Same 12-step rebuild as _migrate_attachment_kind_check, but `runs` is + referenced by other tables and has columns appended by + _RUNS_MIGRATIONS, so the new table is built from the existing table's + own DDL (column order preserved) with only the CHECK list widened, and + foreign keys are switched off for the swap as SQLite's ALTER TABLE + documentation requires. Indexes are recreated by schema_indexes.sql + right after _migrate(). + """ + def needs_rebuild() -> str | None: + row = self._conn.execute("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'runs'").fetchone() + sql = row["sql"] if row else "" + return sql if "'binary_not_found'" in sql and "'not_configured'" not in sql else None + + if needs_rebuild() is None: + return + self._conn.commit() + self._conn.execute("PRAGMA foreign_keys = OFF") # a no-op inside a transaction, so set it first + try: + self._conn.execute("BEGIN IMMEDIATE") + old_sql = needs_rebuild() # re-read under the write lock: another connection may have migrated + if old_sql is None: + self._conn.rollback() + return + new_sql = old_sql.replace("'binary_not_found'", "'binary_not_found','not_configured'", 1) + new_sql = re.sub(r'^\s*CREATE TABLE\s+(IF NOT EXISTS\s+)?["`\[]?runs["`\]]?', "CREATE TABLE runs_new", + new_sql, count=1, flags=re.IGNORECASE) + columns = ", ".join(f'"{row["name"]}"' for row in self._conn.execute("PRAGMA table_info(runs)")) + self._conn.execute(new_sql) + self._conn.execute(f"INSERT INTO runs_new ({columns}) SELECT {columns} FROM runs") + self._conn.execute("DROP TABLE runs") + # Every row keeps its id, so references from other tables stay valid. + self._conn.execute("ALTER TABLE runs_new RENAME TO runs") + self._conn.commit() + except BaseException: + self._conn.rollback() + raise + finally: + self._conn.execute("PRAGMA foreign_keys = ON") def _migrate_attachment_kind_check(self) -> None: """Widens assessment_attachments.attachment_kind's CHECK constraint diff --git a/src/secfoo/storage/schema.sql b/src/secfoo/storage/schema.sql index d05b5be..d687ba4 100644 --- a/src/secfoo/storage/schema.sql +++ b/src/secfoo/storage/schema.sql @@ -46,7 +46,7 @@ CREATE TABLE IF NOT EXISTS runs ( model TEXT, -- requested model; NULL = CLI default/unknown confluence_urls TEXT NOT NULL DEFAULT '[]', status TEXT NOT NULL CHECK (status IN - ('pending','running','success','failed','timeout','binary_not_found')), + ('pending','running','success','failed','timeout','binary_not_found','not_configured')), exit_code INTEGER, started_at TEXT NOT NULL, finished_at TEXT, diff --git a/src/secfoo/web/app.py b/src/secfoo/web/app.py index eb7f67f..31ab689 100644 --- a/src/secfoo/web/app.py +++ b/src/secfoo/web/app.py @@ -42,6 +42,12 @@ def create_app(*, bind_host: str = "127.0.0.1", allow_insecure_runtime: bool = F app.state.bind_host = bind_host app.state.allow_insecure_runtime = allow_insecure_runtime + @app.exception_handler(OverflowError) + async def id_out_of_range(request, exc): + # A path id wider than SQLite's 64-bit INTEGER (/assessments/99999999999999999999) + # cannot name a row; sqlite3 raises OverflowError binding it, which was a 500. + return PlainTextResponse("Not Found", status_code=404) + @app.middleware("http") async def revalidate_static(request, call_next): # Make the browser re-check stylesheets/scripts on each load (a cheap diff --git a/tests/test_storage/test_run_status_migration.py b/tests/test_storage/test_run_status_migration.py new file mode 100644 index 0000000..f17d612 --- /dev/null +++ b/tests/test_storage/test_run_status_migration.py @@ -0,0 +1,155 @@ +"""runs.status must accept every status an adapter can return (agents.base.Status).""" +from __future__ import annotations + +import sqlite3 +import threading +import typing + +import pytest + +from secfoo.agents.api import KEY_VARS, ApiAdapter +from secfoo.agents.base import Status +from secfoo.runner import execute_runs +from secfoo.storage.repository import RunRepository + +# The runs table as created before 'not_configured' existed, plus a column a +# later _RUNS_MIGRATIONS entry appended (so column order differs from schema.sql). +_OLD_RUNS = """ +CREATE TABLE IF NOT EXISTS projects ( + id INTEGER PRIMARY KEY AUTOINCREMENT, identifier TEXT NOT NULL UNIQUE, display_name TEXT NOT NULL, + kind TEXT NOT NULL CHECK (kind IN ('local','github')), first_seen_at TEXT NOT NULL, last_run_at TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS runs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + run_uuid TEXT NOT NULL UNIQUE, + project_id INTEGER NOT NULL REFERENCES projects(id), + skill_id TEXT NOT NULL, + skill_name TEXT NOT NULL, + agent_id TEXT NOT NULL, + confluence_urls TEXT NOT NULL DEFAULT '[]', + status TEXT NOT NULL CHECK (status IN + ('pending','running','success','failed','timeout','binary_not_found')), + exit_code INTEGER, + started_at TEXT NOT NULL, + finished_at TEXT, + duration_seconds REAL, + report_path TEXT, + prompt_path TEXT, + stderr_excerpt TEXT +); +ALTER TABLE runs ADD COLUMN cost_usd REAL; +CREATE TABLE IF NOT EXISTS findings ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + run_id INTEGER NOT NULL REFERENCES runs(id), + title TEXT NOT NULL +); +""" + + +def _old_db(path): + conn = sqlite3.connect(path) + conn.executescript(_OLD_RUNS) + conn.execute("INSERT INTO projects (identifier, display_name, kind, first_seen_at, last_run_at) " + "VALUES ('/p', 'p', 'local', 't', 't')") + conn.execute("INSERT INTO runs (run_uuid, project_id, skill_id, skill_name, agent_id, status, started_at, cost_usd) " + "VALUES ('old-run', 1, 'sast', 'SAST', 'claude', 'success', 't', 0.25)") + conn.execute("INSERT INTO findings (run_id, title) VALUES (1, 'kept')") + conn.commit() + conn.close() + + +def _runs_sql(path): + conn = sqlite3.connect(path) + try: + return conn.execute("SELECT sql FROM sqlite_master WHERE name = 'runs'").fetchone()[0] + finally: + conn.close() + + +def test_every_adapter_status_is_allowed_by_a_fresh_schema(tmp_path): + with RunRepository(db_path=tmp_path / "secfoo.db") as repo: + project = repo.upsert_project("/p", "p", "local") + for status in typing.get_args(Status): + run_uuid = repo.create_run(project_id=project, skill_id="sast", skill_name="SAST", + agent_id="api", confluence_urls=[]) + repo.complete_run(run_uuid, status=status, exit_code=None, duration_seconds=0.0, + report_path=None, prompt_path=None, stderr_excerpt=None) + assert repo.get_run(run_uuid).status == status + + +def test_api_agent_without_a_key_is_saved_as_not_configured(tmp_path, monkeypatch): + """Was an IntegrityError traceback: the first thing a user without an agent CLI sees.""" + for var in KEY_VARS: + monkeypatch.delenv(var, raising=False) + monkeypatch.setattr("secfoo.runner.run_dir", lambda run_uuid: tmp_path / "reports" / run_uuid) + monkeypatch.setattr("secfoo.runner.ensure_store_dirs", lambda: None) + monkeypatch.setattr("secfoo.runner.get_adapter", lambda agent_id: ApiAdapter()) + target = tmp_path / "target" + target.mkdir() + (target / "app.py").write_text("print('hi')\n") + with RunRepository(db_path=tmp_path / "secfoo.db") as repo: + [outcome] = execute_runs(skill_ids=["sast"], target=str(target), confluence_urls=[], agent_id="api", repo=repo) + run = repo.get_run(outcome.run_uuid) + assert outcome.status == "not_configured" + assert run.status == "not_configured" + assert "No API key set" in (run.stderr_excerpt or "") + + +def test_migration_widens_runs_status_check_and_keeps_rows_and_references(tmp_path): + db = tmp_path / "old.db" + _old_db(db) + with RunRepository(db_path=db) as repo: + old = repo.get_run("old-run") + assert old.status == "success" and old.cost_usd == 0.25 + new = repo.create_run(project_id=1, skill_id="sast", skill_name="SAST", agent_id="api", + confluence_urls=[]) + repo.complete_run(new, status="not_configured", exit_code=None, duration_seconds=0.0, + report_path=None, prompt_path=None, stderr_excerpt=None) + assert repo.get_run(new).status == "not_configured" + conn = repo._conn + assert conn.execute("SELECT title FROM findings WHERE run_id = 1").fetchone()[0] == "kept" + assert conn.execute("PRAGMA foreign_keys").fetchone()[0] == 1 + assert conn.execute("PRAGMA foreign_key_check").fetchall() == [] + indexes = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='runs'")} + assert "idx_runs_project_id" in indexes + with pytest.raises(sqlite3.IntegrityError): # the CHECK itself survived, only widened + conn.execute("UPDATE runs SET status = 'bogus' WHERE run_uuid = ?", (new,)) + with pytest.raises(sqlite3.IntegrityError): # and foreign keys still bind to the rebuilt table + conn.execute("DELETE FROM runs WHERE id = 1") + assert "runs_new" not in _runs_sql(db) and "'not_configured'" in _runs_sql(db) + + +def test_runs_status_migration_is_idempotent_and_safe_under_concurrent_opens(tmp_path): + """Skills run in parallel threads with their own connections, so the first + run after an upgrade opens an old database several times at once.""" + db = tmp_path / "old.db" + _old_db(db) + errors = [] + + def open_repo(): + try: + RunRepository(db_path=db).close() + except Exception as exc: # noqa: BLE001 -- surfaced by the assert below + errors.append(exc) + + threads = [threading.Thread(target=open_repo) for _ in range(4)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + assert errors == [] + sql = _runs_sql(db) + assert sql.count("'not_configured'") == 1 + RunRepository(db_path=db).close() + assert _runs_sql(db) == sql + + +def test_runs_without_any_status_check_are_left_alone(tmp_path): + """The oldest databases had no CHECK on status at all; nothing to widen.""" + db = tmp_path / "oldest.db" + conn = sqlite3.connect(db) + conn.executescript(_OLD_RUNS.replace( + "CHECK (status IN\n ('pending','running','success','failed','timeout','binary_not_found'))", "")) + conn.close() + RunRepository(db_path=db).close() + assert "CHECK (status" not in _runs_sql(db) diff --git a/tests/test_web/test_assessment_routes.py b/tests/test_web/test_assessment_routes.py index 511ea74..faf62d3 100644 --- a/tests/test_web/test_assessment_routes.py +++ b/tests/test_web/test_assessment_routes.py @@ -882,3 +882,10 @@ def test_assessments_list_rows_have_selection_checkboxes(client): html = client.get("/assessments").text assert f'name="ids" value="{a1}" form="bulk-status-form"' in html assert 'data-select-all="ids"' in html + + +@pytest.mark.parametrize("path", ["/assessments/99999999999999999999", "/assessments/99999999999999999999/runs/new", + "/runs/99999999999999999999"]) +def test_ids_wider_than_sqlite_integer_are_404_not_500(client, path): + resp = client.get(path) + assert resp.status_code == 404