OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(workbench): simplify command contexts and transactions (#1353)
mldangelo-oai45b23ed34302
diff --git a/plugins/codex-security/scripts/workbench/handoff.py b/plugins/codex-security/scripts/workbench/handoff.pyindex ccf92ceee..fa21ec8d5 100644--- a/plugins/codex-security/scripts/workbench/handoff.py+++ b/plugins/codex-security/scripts/workbench/handoff.py@@ -4,7 +4,6 @@ from __future__ import annotationsimport argparseimport sqlite3-from collections.abc import Callablefrom typing import Anyfrom workbench_validation import optional_text, require_uuid@@ -51,21 +50,17 @@ def validate_handoff_delivery_thread(def claim_handoff_delivery(+ wb: Any,connection: sqlite3.Connection,args: argparse.Namespace,- *,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- stale_claim_before: Callable[[], str],- workspace_state: Callable[[sqlite3.Connection, str], dict[str, Any]],) -> dict[str, Any]:scan_id = require_uuid(args.scan_id, "scan-id")claim_token = require_handoff_claim_token(args.claim_token)- timestamp = now()+ timestamp = wb.now()with connection:- scan = require_scan(connection, scan_id)+ scan = wb.require_scan(connection, scan_id)if scan["handoff_status"] != "pending" or scan["handoff_claim_token"] == claim_token:- return workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])updated = connection.execute("""UPDATE scans@@ -94,27 +89,24 @@ def claim_handoff_delivery(timestamp,scan["id"],int(args.take_over_stale),- stale_claim_before(),+ wb.stale_claim_before(),),)if updated.rowcount != 1:- return workspace_state(connection, scan["workspace_id"])- return workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])def release_handoff_delivery(+ wb: Any,connection: sqlite3.Connection,args: argparse.Namespace,- *,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- workspace_state: Callable[[sqlite3.Connection, str], dict[str, Any]],) -> dict[str, Any]:scan_id = require_uuid(args.scan_id, "scan-id")claim_token = require_handoff_claim_token(args.claim_token)- timestamp = now()+ timestamp = wb.now()with connection:- scan = require_scan(connection, scan_id)+ scan = wb.require_scan(connection, scan_id)connection.execute("""UPDATE scans@@ -126,16 +118,13 @@ def release_handoff_delivery(""",(timestamp, scan["id"], claim_token),)- return workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])def attach_scan_continuation_thread(+ wb: Any,connection: sqlite3.Connection,args: argparse.Namespace,- *,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- workspace_state: Callable[[sqlite3.Connection, str], dict[str, Any]],) -> dict[str, Any]:scan_id = require_uuid(args.scan_id, "scan-id")claim_token = require_handoff_claim_token(args.claim_token)@@ -143,9 +132,9 @@ def attach_scan_continuation_thread(if thread_id is None:raise SystemExit("Codex Security continuation thread ID is required.")connection.execute("BEGIN IMMEDIATE")- try:- timestamp = now()- scan = require_scan(connection, scan_id)+ with connection:+ timestamp = wb.now()+ scan = wb.require_scan(connection, scan_id)if scan["handoff_claim_token"] != claim_token:raise SystemExit("Codex Security continuation thread claim token does not match.")if scan["continuation_thread_id"] is not None:@@ -154,8 +143,8 @@ def attach_scan_continuation_thread("Codex Security scan continuation is owned by another continuation.")connection.commit()- return workspace_state(connection, scan["workspace_id"])- updated = connection.execute(+ return wb.workspace_state(connection, scan["workspace_id"])+ connection.execute("""UPDATE scansSET continuation_thread_id = ?,@@ -168,33 +157,23 @@ def attach_scan_continuation_thread(""",(thread_id, thread_id, timestamp, scan["id"], claim_token),)- if updated.rowcount != 1:- raise SystemExit("Codex Security continuation thread could not be attached.")- connection.commit()- except BaseException:- connection.rollback()- raise- return workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])def mark_handoff_delivered(+ wb: Any,connection: sqlite3.Connection,args: argparse.Namespace,- *,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- require_workspace: Callable[[sqlite3.Connection, str], sqlite3.Row],- workspace_state: Callable[[sqlite3.Connection, str], dict[str, Any]],) -> dict[str, Any]:scan_id = require_uuid(args.scan_id, "scan-id")claim_token = require_handoff_claim_token(args.claim_token)thread_id = optional_text(args.thread_id, maximum=512)connection.execute("BEGIN IMMEDIATE")- try:- timestamp = now()- scan = require_scan(connection, scan_id)+ with connection:+ timestamp = wb.now()+ scan = wb.require_scan(connection, scan_id)if thread_id is not None:- workspace = require_workspace(connection, scan["workspace_id"])+ workspace = wb.require_workspace(connection, scan["workspace_id"])validate_handoff_delivery_thread(scan["continuation_thread_id"] or workspace["thread_id"],thread_id,@@ -206,7 +185,7 @@ def mark_handoff_delivered("Codex Security handoff delivery is owned by another continuation.")connection.commit()- return workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])updated = connection.execute("""UPDATE scans@@ -231,8 +210,4 @@ def mark_handoff_delivered(""",(timestamp, scan["id"]),)- connection.commit()- except BaseException:- connection.rollback()- raise- return workspace_state(connection, scan["workspace_id"])+ return wb.workspace_state(connection, scan["workspace_id"])diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 2b22bdaa2..4a28a4e3c 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -1562,7 +1562,7 @@ def complete_scan_locked()cost_json = parse_scan_cost(scan_usage.measured_scan_cost_json(measured_usage))connection.execute("BEGIN IMMEDIATE")- try:+ with connection:timestamp = manifest["scan"]["completedAt"]scan = require_scan(connection, scan["id"])if scan["status"] == "complete":@@ -1614,10 +1614,6 @@ def complete_scan_locked()if updated.rowcount != 1:raise SystemExit("Only a running scan can be completed.")- connection.commit()- except BaseException:- connection.rollback()- raisecontext = scan_context(connection, scan["id"])context["targetWarnings"] = target_warningsreturn context@@ -3296,12 +3292,11 @@ def main() -> None:# Workbench callers send UTF-8 even when Windows uses a legacy code page.sys.stdin.reconfigure(encoding="utf-8")args = parse_args(__doc__)- deep_scan.configure(- SimpleNamespace(- **globals(),- preserve_stopped_results=preserve_stopped_results_after_transition,- )+ command_context = SimpleNamespace(+ **globals(),+ preserve_stopped_results=preserve_stopped_results_after_transition,)+ deep_scan.configure(command_context)if args.command == "resolve-scan-root":print(json.dumps({"scanRoot": str(resolve_scan_root(args.scan_root))}))return@@ -3395,15 +3390,10 @@ def main() -> None:scan = require_scan(connection, args.scan_id)try:result = scan_history.cli_scan_resume(+ command_context,connection,scan,require_workspace(connection, scan["workspace_id"]),- parse_scan_recipe=parse_scan_recipe,- scan_contract=scan_contract,- require_scan_directory=require_canonical_scan_directory,- artifact_path=artifact_path,- read_json_object=read_json_object,- workbench_completion_binding=workbench_completion_binding,)except SystemExit as exc:if not args.allow_unavailable:@@ -3433,10 +3423,10 @@ def main() -> None:result = native_indexes.list_repositories(connection, args)elif args.command == "list-findings":result = list_findings(connection, args)- elif args.command in {"update-progress", "update-scan-context"}:- result = progress.update(- connection, args, now, require_scan, require_workspace, scan_context- )+ elif args.command == "update-progress":+ result = progress.update_progress(command_context, connection, args)+ elif args.command == "update-scan-context":+ result = progress.update_context(command_context, connection, args)elif args.command in {"prepare-scan-completion", "complete-scan"}:result = complete_scan(connection, args, prepare_only=args.command == "prepare-scan-completion"@@ -3456,39 +3446,13 @@ def main() -> None:elif args.command == "save-scan-artifact":result = saved_results.save_scan_artifact(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "mark-handoff-delivered":- result = handoff.mark_handoff_delivered(- connection,- args,- now=now,- require_scan=require_scan,- require_workspace=require_workspace,- workspace_state=workspace_state,- )+ result = handoff.mark_handoff_delivered(command_context, connection, args)elif args.command == "claim-handoff-delivery":- result = handoff.claim_handoff_delivery(- connection,- args,- now=now,- require_scan=require_scan,- stale_claim_before=stale_claim_before,- workspace_state=workspace_state,- )+ result = handoff.claim_handoff_delivery(command_context, connection, args)elif args.command == "release-handoff-delivery":- result = handoff.release_handoff_delivery(- connection,- args,- now=now,- require_scan=require_scan,- workspace_state=workspace_state,- )+ result = handoff.release_handoff_delivery(command_context, connection, args)elif args.command == "attach-scan-continuation-thread":- result = handoff.attach_scan_continuation_thread(- connection,- args,- now=now,- require_scan=require_scan,- workspace_state=workspace_state,- )+ result = handoff.attach_scan_continuation_thread(command_context, connection, args)elif args.command == "set-finding-triage":result = set_finding_triage(connection, args)elif args.command == "request-finding-remediation":diff --git a/plugins/codex-security/scripts/workbench_progress.py b/plugins/codex-security/scripts/workbench_progress.pyindex 1d2066fe9..50dd55e80 100644--- a/plugins/codex-security/scripts/workbench_progress.py+++ b/plugins/codex-security/scripts/workbench_progress.py@@ -5,7 +5,7 @@ import jsonimport sqlite3import sysfrom pathlib import Path-from typing import Any, Callable+from typing import Anysys.path.insert(0, str(Path(__file__).resolve().parent))from deep_scan_workbench import require_current_coordinator@@ -71,22 +71,18 @@ def reportable_count(def update_context(+ wb: Any,connection: sqlite3.Connection,args: argparse.Namespace,- *,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- require_workspace: Callable[[sqlite3.Connection, str], sqlite3.Row],- scan_context: Callable[[sqlite3.Connection, str], dict[str, Any]],) -> dict[str, Any]:scan_id = require_uuid(args.scan_id, "scan-id")context = user_context_argument(args)connection.execute("BEGIN IMMEDIATE")with connection:- scan = require_scan(connection, scan_id)+ scan = wb.require_scan(connection, scan_id)if scan["status"] != "running" or scan["canceled_at"] is not None:raise SystemExit("Only a running scan can update context.")- workspace = require_workspace(connection, scan["workspace_id"])+ workspace = wb.require_workspace(connection, scan["workspace_id"])if args.workspace_id is not None:if args.claim_token is not None:raise SystemExit("claim-token is only valid with thread-id.")@@ -102,7 +98,7 @@ def update_context(args.claim_token,error_message="Scan context updates are owned by another continuation.",)- timestamp = now()+ timestamp = wb.now()connection.execute("UPDATE scans SET user_context = ?, updated_at = ? WHERE id = ?",(context, timestamp, scan["id"]),@@ -117,42 +113,13 @@ def update_context("UPDATE workspaces SET updated_at = ? WHERE id = ?",(timestamp, workspace["id"]),)- return scan_context(connection, scan_id)---def update(- connection: sqlite3.Connection,- args: argparse.Namespace,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- require_workspace: Callable[[sqlite3.Connection, str], sqlite3.Row],- scan_context: Callable[[sqlite3.Connection, str], dict[str, Any]],-) -> dict[str, Any]:- if args.command == "update-scan-context":- return update_context(- connection,- args,- now=now,- require_scan=require_scan,- require_workspace=require_workspace,- scan_context=scan_context,- )- return update_progress(- connection,- args,- now=now,- require_scan=require_scan,- scan_context=scan_context,- )+ return wb.scan_context(connection, scan_id)def update_progress(+ wb: Any,connection: sqlite3.Connection,args: argparse.Namespace,- *,- now: Callable[[], str],- require_scan: Callable[[sqlite3.Connection, str], sqlite3.Row],- scan_context: Callable[[sqlite3.Connection, str], dict[str, Any]],) -> dict[str, Any]:scan_id = require_uuid(args.scan_id, "scan-id")model = optional_text(args.model, maximum=200)@@ -163,8 +130,8 @@ def update_progress(serialized_preflight_issues = preflight_issues_json(preflight_issues)connection.execute("BEGIN IMMEDIATE")with connection:- timestamp = now()- scan = require_scan(connection, scan_id)+ timestamp = wb.now()+ scan = wb.require_scan(connection, scan_id)if scan["status"] != "running":raise SystemExit("Only a running scan can update progress.")if scan["mode"] == "deep":@@ -274,7 +241,7 @@ def update_progress(completed = progress["review_items_completed"]if completed > total:raise SystemExit("Completed review items cannot exceed total review items.")- updated = connection.execute(+ connection.execute("""UPDATE scansSET phase = COALESCE(?, phase), model = COALESCE(?, model),@@ -283,13 +250,11 @@ def update_progress(""",(args.phase, model, reasoning_effort, timestamp, scan["id"]),)- if updated.rowcount != 1:- raise SystemExit("Only a running scan can update progress.")connection.execute(f"UPDATE scan_progress SET {', '.join(updates)}, updated_at = ? WHERE scan_id = ?",(*values, timestamp, scan["id"]),)- return scan_context(connection, scan["id"])+ return wb.scan_context(connection, scan["id"])if __name__ == "__main__":diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.pyindex 8cc0ad786..9de4b04a2 100644--- a/plugins/codex-security/scripts/workbench_saved_results.py+++ b/plugins/codex-security/scripts/workbench_saved_results.py@@ -2526,7 +2526,7 @@ def fail_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:scan_id = db.require_uuid(args.scan_id, "scan-id")cost_json = db.parse_scan_cost(args.cost_json)connection.execute("BEGIN IMMEDIATE")- try:+ with connection:timestamp = db.now()scan = db.require_scan(connection, scan_id)if scan["status"] == "failed":@@ -2558,10 +2558,6 @@ def fail_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:)if progress_updated.rowcount != 1:raise SystemExit("Codex Security scan progress not found.")- connection.commit()- except BaseException:- connection.rollback()- raisepreserve_stopped_results_after_transition(db, connection, scan["id"])return db.scan_context(connection, scan["id"])@@ -2575,7 +2571,7 @@ def cancel_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:scan_id = db.require_uuid(args.scan_id, "scan-id")thread_id = db.optional_text(args.thread_id, maximum=512)connection.execute("BEGIN IMMEDIATE")- try:+ with connection:timestamp = db.now()scan = db.require_scan(connection, scan_id)workspace = db.require_workspace(connection, scan["workspace_id"])@@ -2604,10 +2600,6 @@ def cancel_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:)if progress_updated.rowcount != 1:raise SystemExit("Codex Security scan progress not found.")- connection.commit()- except BaseException:- connection.rollback()- raisepreserve_stopped_results_after_transition(db, connection, scan["id"])return db.workspace_state(connection, scan["workspace_id"])diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.pyindex fc145bc04..4dde1e2c4 100644--- a/plugins/codex-security/scripts/workbench_scan_history.py+++ b/plugins/codex-security/scripts/workbench_scan_history.py@@ -48,16 +48,10 @@ def preserve_sealed_completion(def cli_scan_resume(+ wb: Any,connection: sqlite3.Connection,scan: sqlite3.Row,workspace: sqlite3.Row,- *,- parse_scan_recipe: Callable[[str, Path], dict[str, Any]],- scan_contract: Callable[[sqlite3.Row], dict[str, Any]],- require_scan_directory: Callable[[Path], Path],- artifact_path: Callable[..., Path | None],- read_json_object: Callable[[Path], dict[str, Any]],- workbench_completion_binding: Callable[..., dict[str, Any]],) -> dict[str, Any]:if scan["mode"] != "deep" or scan["recipe_json"] is None:raise SystemExit("Resume requires a Deep Scan with a saved CLI launch recipe.")@@ -94,13 +88,13 @@ def cli_scan_resume(scan["target_inode"],):raise SystemExit("Cannot resume: the original checkout revision or contents changed.")- recipe = parse_scan_recipe(scan["recipe_json"], repository)- scan_dir = require_scan_directory(Path(scan["scan_dir"]))+ recipe = wb.parse_scan_recipe(scan["recipe_json"], repository)+ scan_dir = wb.require_canonical_scan_directory(Path(scan["scan_dir"]))progress = connection.execute("SELECT scope_file_count FROM scan_progress WHERE scan_id = ?", (scan["id"],)).fetchone()result = {- "contract": scan_contract(scan),+ "contract": wb.scan_contract(scan),"recipe": recipe,"scanDir": str(scan_dir),"scanId": scan["id"],@@ -114,16 +108,16 @@ def cli_scan_resume(# Active coordinators may still be writing drafts. Validate sealed results# before attaching to a coordinator that has finished.if run is not None and run["status"] == "succeeded":- manifest_path = artifact_path(scan_dir, ARTIFACTS["manifest"], required=False)+ manifest_path = wb.artifact_path(scan_dir, ARTIFACTS["manifest"], required=False)if manifest_path is not None:- manifest = read_json_object(manifest_path)+ manifest = wb.read_json_object(manifest_path)manifest_scan = manifest.get("scan")if isinstance(manifest_scan, dict) and (manifest_scan.get("sealedAt") is not Noneor manifest_scan.get("artifacts") is not None):try:- binding = workbench_completion_binding(scan, scan["started_at"], manifest)+ binding = wb.workbench_completion_binding(scan, scan["started_at"], manifest)_prepare_scan_finalization(scan_dir,expected_coverage_mode=binding["coverageMode"],