@openai/codex-security 0.1.23 → 0.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +24 -5
  2. package/_bundled_plugin/.mcp.json +1 -0
  3. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  4. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  5. package/_bundled_plugin/references/scan-contract.md +10 -0
  6. package/_bundled_plugin/scripts/deep_scan_workbench.py +6 -1
  7. package/_bundled_plugin/scripts/finalize_scan_contract.py +27 -78
  8. package/_bundled_plugin/scripts/generate_in_scope_files.py +2 -8
  9. package/_bundled_plugin/scripts/generate_rank_input.py +1 -5
  10. package/_bundled_plugin/scripts/rank_preview.py +1 -0
  11. package/_bundled_plugin/scripts/report_projection.py +8 -14
  12. package/_bundled_plugin/scripts/validate_scan_contract.py +1 -3
  13. package/_bundled_plugin/scripts/windows_scan_local_files.py +5 -15
  14. package/_bundled_plugin/scripts/workbench_cli.py +6 -0
  15. package/_bundled_plugin/scripts/workbench_dashboard.py +41 -18
  16. package/_bundled_plugin/scripts/workbench_db.py +83 -539
  17. package/_bundled_plugin/scripts/workbench_finding_workflows.py +31 -13
  18. package/_bundled_plugin/scripts/workbench_findings.py +8 -3
  19. package/_bundled_plugin/scripts/workbench_native_indexes.py +1 -2
  20. package/_bundled_plugin/scripts/workbench_publication.py +520 -0
  21. package/_bundled_plugin/scripts/workbench_remediation.py +37 -0
  22. package/_bundled_plugin/scripts/workbench_saved_results.py +298 -90
  23. package/_bundled_plugin/scripts/workbench_scan_history.py +1 -2
  24. package/_bundled_plugin/scripts/workbench_scan_usage.py +9 -33
  25. package/_bundled_plugin/scripts/workbench_schema.py +34 -21
  26. package/_bundled_plugin/scripts/workbench_target.py +10 -5
  27. package/_bundled_plugin/scripts/workbench_validation.py +1 -5
  28. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +1 -5
  29. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +6 -1
  30. package/dist/api.d.ts +3 -1
  31. package/dist/api.d.ts.map +1 -1
  32. package/dist/api.js +20 -0
  33. package/dist/api.js.map +1 -1
  34. package/dist/cli.d.ts.map +1 -1
  35. package/dist/cli.js +34 -4
  36. package/dist/cli.js.map +1 -1
  37. package/dist/deep-progress.d.ts +21 -0
  38. package/dist/deep-progress.d.ts.map +1 -0
  39. package/dist/deep-progress.js +108 -0
  40. package/dist/deep-progress.js.map +1 -0
  41. package/dist/index.d.ts +1 -0
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/runtime.d.ts.map +1 -1
  45. package/dist/runtime.js +6 -1
  46. package/dist/runtime.js.map +1 -1
  47. package/dist/scan-activity.d.ts.map +1 -1
  48. package/dist/scan-activity.js +12 -2
  49. package/dist/scan-activity.js.map +1 -1
  50. package/dist/server/index.js +2 -28
  51. package/dist/server/index.js.map +1 -1
  52. package/dist/server/serve.d.ts +2 -0
  53. package/dist/server/serve.d.ts.map +1 -0
  54. package/dist/server/serve.js +28 -0
  55. package/dist/server/serve.js.map +1 -0
  56. package/package.json +3 -3
@@ -4,10 +4,8 @@
4
4
  from __future__ import annotations
5
5
 
6
6
  import argparse
7
- import csv
8
7
  import errno
9
8
  import hashlib
10
- import io
11
9
  import json
12
10
  import os
13
11
  import re
@@ -21,7 +19,6 @@ from contextlib import closing, contextmanager
21
19
  from datetime import datetime, timedelta, timezone
22
20
  from pathlib import Path, PurePosixPath
23
21
  from typing import Any
24
- from urllib.parse import quote
25
22
 
26
23
  try:
27
24
  import fcntl as posix_file_lock
@@ -37,11 +34,14 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))
37
34
  import deep_scan_workbench as deep_scan
38
35
  import workbench_native_indexes as native_indexes
39
36
  import workbench_progress as progress
37
+ import workbench_publication as publication
40
38
  import workbench_remediation as remediation
41
39
  import workbench_saved_results as saved_results
42
40
  import workbench_scan_history as scan_history
43
41
  import workbench_scan_usage as scan_usage
44
- from filesystem_identity import serialize_filesystem_identity as serialize_filesystem_identity
42
+ from filesystem_identity import (
43
+ serialize_filesystem_identity as serialize_filesystem_identity,
44
+ )
45
45
  from filesystem_identity import (
46
46
  stored_filesystem_identity_matches as stored_filesystem_identity_matches,
47
47
  )
@@ -51,11 +51,9 @@ from finalize_scan_contract import (
51
51
  RecoverableContractError,
52
52
  _prepare_scan_finalization,
53
53
  _write_prepared_scan_finalization,
54
- csv_cell,
55
54
  finalize_scan,
56
55
  finding_candidate_id,
57
56
  open_scan_local_file_descriptor,
58
- write_sarif_projection,
59
57
  write_scan_local_bytes,
60
58
  )
61
59
  from finding_preview import bounded_finding_details
@@ -80,8 +78,8 @@ from workbench_constants import (
80
78
  PATCH_PREVIEW_BYTES,
81
79
  SQLITE_RETRY_ATTEMPTS,
82
80
  )
83
- from workbench_feedback import get_scan_feedback
84
81
  from workbench_dashboard import dashboard
82
+ from workbench_feedback import get_scan_feedback
85
83
  from workbench_finding_index import index_findings
86
84
  from workbench_finding_workflows import finding_workflow, register_workflow_scan
87
85
  from workbench_findings import (
@@ -1023,7 +1021,10 @@ def _start_prompt_driven_scan(
1023
1021
  ).fetchone()
1024
1022
  if existing is not None:
1025
1023
  connection.commit()
1026
- return {**scan_context(connection, existing["id"]), "startDisposition": "joined"}
1024
+ return {
1025
+ **scan_context(connection, existing["id"]),
1026
+ "startDisposition": "joined",
1027
+ }
1027
1028
  target_root.mkdir(parents=True, exist_ok=True)
1028
1029
  workspace_id = str(uuid.uuid4())
1029
1030
  scan_id = str(uuid.uuid4())
@@ -1134,7 +1135,10 @@ def pin_legacy_manifest_digest(
1134
1135
 
1135
1136
 
1136
1137
  def complete_scan(
1137
- connection: sqlite3.Connection, args: argparse.Namespace, *, prepare_only: bool = False
1138
+ connection: sqlite3.Connection,
1139
+ args: argparse.Namespace,
1140
+ *,
1141
+ prepare_only: bool = False,
1138
1142
  ) -> dict[str, Any]:
1139
1143
  scan_id = require_uuid(args.scan_id, "scan-id")
1140
1144
  cost_json = None if prepare_only else parse_scan_cost(args.cost_json)
@@ -1347,8 +1351,7 @@ def budget_exhausted_draft(
1347
1351
  existing_deferred = {
1348
1352
  item.get("candidateId", item.get("id"))
1349
1353
  for item in coverage["deferred"]
1350
- if isinstance(item, dict)
1351
- and isinstance(item.get("candidateId", item.get("id")), str)
1354
+ if isinstance(item, dict) and isinstance(item.get("candidateId", item.get("id")), str)
1352
1355
  }
1353
1356
  existing_surfaces = {
1354
1357
  item.get("id")
@@ -1551,9 +1554,7 @@ def complete_scan_locked(
1551
1554
  wrote = True
1552
1555
  manifest, findings, _ = _write_prepared_scan_finalization(prepared)
1553
1556
  except ContractError as exc:
1554
- if wrote or (
1555
- scan["mode"] == "deep" and not isinstance(exc, RecoverableContractError)
1556
- ):
1557
+ if wrote or (scan["mode"] == "deep" and not isinstance(exc, RecoverableContractError)):
1557
1558
  args = argparse.Namespace(claim_token=claim_token, cost_json=cost_json)
1558
1559
  args.message, args.scan_id = str(exc), scan_id
1559
1560
  fail_scan_locked(connection, args)
@@ -1861,20 +1862,10 @@ def preserve_scan_results_locked(connection: sqlite3.Connection, scan_id: str) -
1861
1862
  return saved_results.preserve_scan_results_locked(_WORKBENCH_DB_CONTEXT, connection, scan_id)
1862
1863
 
1863
1864
 
1864
- def refresh_stopped_scan_results(
1865
- connection: sqlite3.Connection, scan_id: str, *, strict: bool = False
1866
- ) -> None:
1867
- saved_results.refresh_stopped_scan_results(
1868
- _WORKBENCH_DB_CONTEXT, connection, scan_id, strict=strict
1869
- )
1870
-
1871
-
1872
- def refresh_all_stopped_scan_results(connection: sqlite3.Connection) -> None:
1873
- scan_ids = [
1874
- row["id"] for row in connection.execute("SELECT id FROM scans WHERE status = 'failed'")
1875
- ]
1876
- for scan_id in scan_ids:
1877
- refresh_stopped_scan_results(connection, scan_id)
1865
+ def recover_scan_results(
1866
+ connection: sqlite3.Connection, args: argparse.Namespace
1867
+ ) -> dict[str, Any]:
1868
+ return saved_results.recover_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)
1878
1869
 
1879
1870
 
1880
1871
  def preserve_scan_results(
@@ -1969,7 +1960,14 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
1969
1960
  id, occurrence_id, status, close_reason, note, created_at
1970
1961
  ) VALUES (?, ?, ?, ?, ?, ?)
1971
1962
  """,
1972
- (str(uuid.uuid4()), occurrence["id"], args.status, close_reason, note, timestamp),
1963
+ (
1964
+ str(uuid.uuid4()),
1965
+ occurrence["id"],
1966
+ args.status,
1967
+ close_reason,
1968
+ note,
1969
+ timestamp,
1970
+ ),
1973
1971
  )
1974
1972
  connection.execute(
1975
1973
  """
@@ -2330,8 +2328,8 @@ def set_finding_remediation(
2330
2328
  )
2331
2329
  if current["pending_action_claim_token"] != action_token:
2332
2330
  raise SystemExit("This remediation host request is owned by a different action token.")
2333
- require_remediation_transition(current["state"], args.state)
2334
- require_pending_remediation_action(current, args.state)
2331
+ remediation.require_transition(current["state"], args.state)
2332
+ remediation.require_pending_action(current, args.state)
2335
2333
  patch_path = current["patch_path"]
2336
2334
  if args.patch_path is not None:
2337
2335
  requested_patch_path = require_scan_relative_file(scan, args.patch_path)
@@ -2432,494 +2430,15 @@ def set_finding_remediation(
2432
2430
  return scan_context(connection, occurrence["scan_id"])
2433
2431
 
2434
2432
 
2435
- def linear_publication_input(
2436
- args: argparse.Namespace, *, recording: bool
2437
- ) -> tuple[dict[str, Any], dict[str, str], list[dict[str, str]]]:
2438
- payload = read_json_object(Path(args.input_file))
2439
- required = {"scanId", "scanDirectory", "destination", "findings"}
2440
- if recording:
2441
- required.add("publications")
2442
- if set(payload) != required:
2443
- raise SystemExit("Linear publication input contains unexpected or missing fields.")
2444
-
2445
- scan_id = payload["scanId"]
2446
- scan_directory = payload["scanDirectory"]
2447
- destination = payload["destination"]
2448
- findings = payload["findings"]
2449
- if not isinstance(scan_id, str) or not isinstance(scan_directory, str):
2450
- raise SystemExit("Linear publication input must identify the exact completed scan.")
2451
- if (
2452
- not isinstance(destination, dict)
2453
- or not {"type", "teamId"}.issubset(destination)
2454
- or not set(destination).issubset({"type", "teamId", "projectId"})
2455
- or destination.get("type") != "linear"
2456
- or not isinstance(destination.get("teamId"), str)
2457
- or not destination["teamId"].strip()
2458
- or (
2459
- "projectId" in destination
2460
- and (
2461
- not isinstance(destination["projectId"], str)
2462
- or not destination["projectId"].strip()
2463
- )
2464
- )
2465
- ):
2466
- raise SystemExit("Linear publication input must identify the exact team and optional project.")
2467
- if not isinstance(findings, list):
2468
- raise SystemExit("Linear publication input must include the planned scan findings.")
2469
-
2470
- seen_finding_ids: set[str] = set()
2471
- seen_occurrence_ids: set[str] = set()
2472
- for finding in findings:
2473
- if (
2474
- not isinstance(finding, dict)
2475
- or set(finding) != {"findingId", "occurrenceId"}
2476
- or not isinstance(finding.get("findingId"), str)
2477
- or not finding["findingId"].strip()
2478
- or not isinstance(finding.get("occurrenceId"), str)
2479
- or not finding["occurrenceId"].strip()
2480
- ):
2481
- raise SystemExit("Linear publication input contains an invalid finding identity.")
2482
- if (
2483
- finding["findingId"] in seen_finding_ids
2484
- or finding["occurrenceId"] in seen_occurrence_ids
2485
- ):
2486
- raise SystemExit("Linear publication input repeats a finding or occurrence.")
2487
- seen_finding_ids.add(finding["findingId"])
2488
- seen_occurrence_ids.add(finding["occurrenceId"])
2489
-
2490
- return payload, destination, findings
2491
-
2492
-
2493
- def verify_linear_publication_scan(
2494
- connection: sqlite3.Connection,
2495
- payload: dict[str, Any],
2496
- findings: list[dict[str, str]],
2497
- ) -> sqlite3.Row:
2498
- try:
2499
- scan = require_scan(connection, payload["scanId"])
2500
- except SystemExit as exc:
2501
- raise SystemExit(
2502
- "The completed scan is not present in the local Codex Security scan-history database. "
2503
- "Use the state directory where the scan was completed."
2504
- ) from exc
2505
- if scan["id"] != payload["scanId"]:
2506
- raise SystemExit("Linear publication must use the exact completed scan identifier.")
2507
- if scan["status"] != "complete":
2508
- raise SystemExit("Only completed scans can publish findings to Linear.")
2509
-
2510
- requested_directory = require_canonical_scan_directory(Path(payload["scanDirectory"]))
2511
- recorded_directory = require_canonical_scan_directory(Path(scan["scan_dir"]))
2512
- if os.path.normcase(requested_directory) != os.path.normcase(recorded_directory):
2513
- raise SystemExit(
2514
- "The selected scan directory does not match its local Codex Security scan history."
2515
- )
2516
- if "seal_manifest_digest" in scan.keys():
2517
- require_recorded_manifest_digest(scan, recorded_directory)
2518
-
2519
- stored_findings = {
2520
- row["id"]: row["finding_id"]
2521
- for row in connection.execute(
2522
- "SELECT id, finding_id FROM finding_occurrences WHERE scan_id = ?",
2523
- (scan["id"],),
2524
- )
2525
- }
2526
- for finding in findings:
2527
- if stored_findings.get(finding["occurrenceId"]) != finding["findingId"]:
2528
- raise SystemExit(
2529
- "A selected finding or occurrence does not belong to the completed scan "
2530
- "in local Codex Security scan history."
2531
- )
2532
- if len(stored_findings) != len(findings):
2533
- raise SystemExit(
2534
- "The completed scan findings do not exactly match local Codex Security scan history."
2535
- )
2536
- return scan
2433
+ _WORKBENCH_PUBLICATION_CONTEXT: publication.WorkbenchPublicationContext
2537
2434
 
2538
2435
 
2539
2436
  def inspect_linear_publication(args: argparse.Namespace) -> dict[str, Any]:
2540
- payload, destination, findings = linear_publication_input(args, recording=False)
2541
- database_uri = f"file:{quote(str(database_path()), safe='')}?mode=ro"
2542
- with closing(sqlite3.connect(database_uri, uri=True, timeout=5)) as connection:
2543
- connection.row_factory = sqlite3.Row
2544
- connection.execute("BEGIN")
2545
- scan = verify_linear_publication_scan(connection, payload, findings)
2546
- recorded: dict[str, dict[str, str]] = {}
2547
- if connection.execute(
2548
- "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'finding_publications'"
2549
- ).fetchone():
2550
- for row in connection.execute(
2551
- """
2552
- SELECT finding_id, occurrence_id, external_id, external_url
2553
- FROM finding_publications
2554
- WHERE scan_id = ? AND destination_type = ? AND team_id = ? AND project_id IS ?
2555
- ORDER BY created_at, external_id
2556
- """,
2557
- (
2558
- scan["id"],
2559
- destination["type"],
2560
- destination["teamId"],
2561
- destination.get("projectId"),
2562
- ),
2563
- ):
2564
- recorded.setdefault(
2565
- row["occurrence_id"],
2566
- {
2567
- "findingId": row["finding_id"],
2568
- "occurrenceId": row["occurrence_id"],
2569
- "issueIdentifier": row["external_id"],
2570
- **({"url": row["external_url"]} if row["external_url"] is not None else {}),
2571
- },
2572
- )
2573
- return {
2574
- "scanId": scan["id"],
2575
- "destination": destination,
2576
- "findingCount": len(findings),
2577
- "recorded": [
2578
- recorded[finding["occurrenceId"]]
2579
- for finding in findings
2580
- if finding["occurrenceId"] in recorded
2581
- ],
2582
- }
2583
-
2584
-
2585
- def prepare_linear_publication(
2586
- connection: sqlite3.Connection, args: argparse.Namespace
2587
- ) -> dict[str, Any]:
2588
- payload, destination, findings = linear_publication_input(args, recording=False)
2589
- connection.execute("BEGIN IMMEDIATE")
2590
- try:
2591
- scan = verify_linear_publication_scan(connection, payload, findings)
2592
- result = {
2593
- "scanId": scan["id"],
2594
- "destination": destination,
2595
- "findingCount": len(findings),
2596
- }
2597
- connection.commit()
2598
- except BaseException:
2599
- connection.rollback()
2600
- raise
2601
- return result
2602
-
2603
-
2604
- def record_linear_publications(
2605
- connection: sqlite3.Connection, args: argparse.Namespace
2606
- ) -> dict[str, Any]:
2607
- payload, destination, findings = linear_publication_input(args, recording=True)
2608
- publications = payload["publications"]
2609
- if not isinstance(publications, list):
2610
- raise SystemExit("Linear publication results must be an array.")
2611
- planned = {finding["findingId"]: finding["occurrenceId"] for finding in findings}
2612
- current: dict[str, dict[str, str]] = {}
2613
- external_ids: set[str] = set()
2614
- for publication in publications:
2615
- if (
2616
- not isinstance(publication, dict)
2617
- or not {"findingId", "occurrenceId", "issueIdentifier"}.issubset(publication)
2618
- or not set(publication).issubset(
2619
- {"findingId", "occurrenceId", "issueIdentifier", "url"}
2620
- )
2621
- or not isinstance(publication.get("findingId"), str)
2622
- or not isinstance(publication.get("occurrenceId"), str)
2623
- or not isinstance(publication.get("issueIdentifier"), str)
2624
- or not publication["issueIdentifier"].strip()
2625
- or (
2626
- "url" in publication
2627
- and (
2628
- not isinstance(publication["url"], str)
2629
- or not publication["url"].strip()
2630
- )
2631
- )
2632
- ):
2633
- raise SystemExit("Linear publication results contain an invalid issue association.")
2634
- finding_id = publication["findingId"]
2635
- issue_identifier = publication["issueIdentifier"]
2636
- if planned.get(finding_id) != publication["occurrenceId"]:
2637
- raise SystemExit(
2638
- "A created Linear issue does not match its planned finding and occurrence."
2639
- )
2640
- if finding_id in current or issue_identifier in external_ids:
2641
- raise SystemExit("Linear publication results repeat a finding or issue identifier.")
2642
- current[finding_id] = publication
2643
- external_ids.add(issue_identifier)
2644
-
2645
- connection.execute("BEGIN IMMEDIATE")
2646
- try:
2647
- scan = verify_linear_publication_scan(connection, payload, findings)
2648
- timestamp = now()
2649
- for publication in publications:
2650
- conflicting = connection.execute(
2651
- """
2652
- SELECT occurrence_id, external_url
2653
- FROM finding_publications
2654
- WHERE destination_type = ? AND team_id = ? AND project_id IS ?
2655
- AND external_id = ?
2656
- """,
2657
- (
2658
- destination["type"],
2659
- destination["teamId"],
2660
- destination.get("projectId"),
2661
- publication["issueIdentifier"],
2662
- ),
2663
- ).fetchone()
2664
- if conflicting is not None and conflicting["occurrence_id"] != publication[
2665
- "occurrenceId"
2666
- ]:
2667
- raise SystemExit("This Linear issue is already associated with a different finding.")
2668
- if (
2669
- conflicting is not None
2670
- and "url" in publication
2671
- and conflicting["external_url"] != publication["url"]
2672
- ):
2673
- raise SystemExit("This Linear issue is already associated with a different URL.")
2674
-
2675
- connection.execute(
2676
- """
2677
- INSERT INTO finding_publications (
2678
- scan_id, finding_id, occurrence_id, destination_type,
2679
- team_id, project_id, external_id, external_url, created_at
2680
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
2681
- ON CONFLICT DO NOTHING
2682
- """,
2683
- (
2684
- scan["id"],
2685
- publication["findingId"],
2686
- publication["occurrenceId"],
2687
- destination["type"],
2688
- destination["teamId"],
2689
- destination.get("projectId"),
2690
- publication["issueIdentifier"],
2691
- publication.get("url"),
2692
- timestamp,
2693
- ),
2694
- )
2695
-
2696
- created = []
2697
- for finding in findings:
2698
- publication = current.get(finding["findingId"])
2699
- if publication is None:
2700
- continue
2701
- row = connection.execute(
2702
- """
2703
- SELECT finding_id, occurrence_id, external_id, external_url
2704
- FROM finding_publications
2705
- WHERE scan_id = ? AND occurrence_id = ? AND destination_type = ?
2706
- AND team_id = ? AND project_id IS ? AND external_id = ?
2707
- """,
2708
- (
2709
- scan["id"],
2710
- publication["occurrenceId"],
2711
- destination["type"],
2712
- destination["teamId"],
2713
- destination.get("projectId"),
2714
- publication["issueIdentifier"],
2715
- ),
2716
- ).fetchone()
2717
- if row is None:
2718
- raise SystemExit("A created Linear issue could not be read from scan history.")
2719
- created.append(
2720
- {
2721
- "findingId": row["finding_id"],
2722
- "occurrenceId": row["occurrence_id"],
2723
- "issueIdentifier": row["external_id"],
2724
- **({"url": row["external_url"]} if row["external_url"] is not None else {}),
2725
- }
2726
- )
2727
- result = {"scanId": scan["id"], "destination": destination, "created": created}
2728
- connection.commit()
2729
- except BaseException:
2730
- connection.rollback()
2731
- raise
2732
- return result
2437
+ return publication.inspect_linear_publication(_WORKBENCH_PUBLICATION_CONTEXT, args)
2733
2438
 
2734
2439
 
2735
2440
  def export_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2736
- refresh_stopped_scan_results(connection, args.scan_id, strict=True)
2737
- scan = require_scan(connection, args.scan_id)
2738
- if scan["status"] != "complete" and not (
2739
- scan["status"] == "failed" and scan["seal_manifest_digest"]
2740
- ):
2741
- raise SystemExit(
2742
- "Findings can be exported after the scan completes or preserves stopped results."
2743
- )
2744
- scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
2745
- require_recorded_manifest_digest(scan, scan_dir)
2746
- verify_manifest_binding(scan, read_json_object(scan_dir / ARTIFACTS["manifest"]))
2747
- try:
2748
- manifest, _, _ = finalize_scan(
2749
- scan_dir,
2750
- expected_coverage_mode=expected_coverage_mode(scan),
2751
- )
2752
- except ContractError as exc:
2753
- raise SystemExit(str(exc)) from exc
2754
- verify_manifest_binding(scan, manifest)
2755
- manifest_digest = published_manifest_digest(scan_dir, manifest)
2756
- pin_legacy_manifest_digest(connection, scan["id"], manifest_digest)
2757
- if args.format == "json":
2758
- path = artifact_path(scan_dir, ARTIFACTS["findings"], required=True)
2759
- elif args.format == "sarif":
2760
- try:
2761
- write_sarif_projection(scan_dir)
2762
- except ContractError as exc:
2763
- raise SystemExit(str(exc)) from exc
2764
- path = artifact_path(scan_dir, "exports/results.sarif", required=True)
2765
- else:
2766
- path = write_csv_export(connection, scan)
2767
- if path is None:
2768
- raise SystemExit(f"Could not export Codex Security findings as {args.format.upper()}.")
2769
- return {
2770
- "export": {"format": args.format, "path": str(path)},
2771
- "scan": scan_result(connection, scan),
2772
- "workspace": workspace_state(connection, scan["workspace_id"]),
2773
- }
2774
-
2775
-
2776
- def write_csv_export(connection: sqlite3.Connection, scan: sqlite3.Row) -> Path:
2777
- scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
2778
- output = io.StringIO(newline="")
2779
- writer = csv.writer(output)
2780
- deep_scan = scan["mode"] == "deep"
2781
- candidate_ids_by_occurrence: dict[str, str] = {}
2782
- if deep_scan:
2783
- findings_document = read_json_object(scan_dir / ARTIFACTS["findings"])
2784
- findings = findings_document.get("findings")
2785
- if not isinstance(findings, list):
2786
- raise SystemExit("findings.json must contain a findings array.")
2787
- for finding in findings:
2788
- if not isinstance(finding, dict):
2789
- raise SystemExit("findings.json entries must be objects.")
2790
- occurrence_id = finding.get("occurrenceId")
2791
- candidate_id = finding_candidate_id(finding)
2792
- if isinstance(occurrence_id, str) and isinstance(candidate_id, str):
2793
- candidate_ids_by_occurrence[occurrence_id] = candidate_id
2794
- columns = (
2795
- "occurrence_id",
2796
- "finding_id",
2797
- *(("candidate_id",) if deep_scan else ()),
2798
- "title",
2799
- "summary",
2800
- "severity",
2801
- "confidence",
2802
- "status",
2803
- "close_reason",
2804
- "note",
2805
- "remediation",
2806
- "path",
2807
- "start_line",
2808
- "end_line",
2809
- )
2810
- writer.writerow(columns)
2811
- for row in finding_export_rows(connection, scan["id"]):
2812
- writer.writerow(
2813
- (
2814
- csv_cell(row["occurrence_id"]),
2815
- csv_cell(row["finding_id"]),
2816
- *(
2817
- (csv_cell(candidate_ids_by_occurrence.get(row["occurrence_id"])),)
2818
- if deep_scan
2819
- else ()
2820
- ),
2821
- csv_cell(row["title"]),
2822
- csv_cell(row["summary"]),
2823
- csv_cell(row["severity"]),
2824
- csv_cell(row["confidence"]),
2825
- csv_cell(row["status"]),
2826
- csv_cell(row["close_reason"]),
2827
- csv_cell(row["note"]),
2828
- csv_cell(row["remediation"]),
2829
- csv_cell(row["relative_path"]),
2830
- row["start_line"],
2831
- row["end_line"],
2832
- )
2833
- )
2834
- try:
2835
- write_scan_local_bytes(
2836
- scan_dir,
2837
- "exports/findings.csv",
2838
- output.getvalue().encode("utf-8"),
2839
- )
2840
- except ContractError as exc:
2841
- raise SystemExit(
2842
- "exports: expected a regular directory inside the scan directory."
2843
- ) from exc
2844
- destination = scan_dir / "exports" / "findings.csv"
2845
- path = available_artifact_path(scan_dir, destination)
2846
- if path is None:
2847
- raise SystemExit("findings.csv: expected a regular file inside the scan directory.")
2848
- return path
2849
-
2850
-
2851
- def finding_export_rows(connection: sqlite3.Connection, scan_id: str) -> sqlite3.Cursor:
2852
- return connection.execute(
2853
- """
2854
- SELECT
2855
- occurrences.id AS occurrence_id,
2856
- occurrences.finding_id,
2857
- occurrences.title,
2858
- occurrences.summary,
2859
- occurrences.severity,
2860
- occurrences.confidence,
2861
- occurrences.remediation,
2862
- COALESCE(triage.status, 'open') AS status,
2863
- triage.close_reason,
2864
- triage.note,
2865
- locations.relative_path,
2866
- locations.start_line,
2867
- locations.end_line
2868
- FROM finding_occurrences AS occurrences
2869
- LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
2870
- LEFT JOIN finding_locations AS locations
2871
- ON locations.occurrence_id = occurrences.id
2872
- AND locations.sort_order = (
2873
- SELECT primary_location.sort_order
2874
- FROM finding_locations AS primary_location
2875
- WHERE primary_location.occurrence_id = occurrences.id
2876
- ORDER BY
2877
- CASE WHEN primary_location.role = 'root_control' THEN 0 ELSE 1 END,
2878
- primary_location.sort_order
2879
- LIMIT 1
2880
- )
2881
- WHERE occurrences.scan_id = ?
2882
- ORDER BY occurrences.created_at, occurrences.id
2883
- """,
2884
- (scan_id,),
2885
- )
2886
-
2887
-
2888
- def require_remediation_transition(current: str, requested: str) -> None:
2889
- allowed = {
2890
- "requested": {"requested", "generated", "failed"},
2891
- "generated": {"generated", "applied", "failed"},
2892
- "applied": {"applied", "verifying", "failed"},
2893
- "verifying": {"verifying", "verified", "failed"},
2894
- "verified": {"verifying", "verified"},
2895
- "failed": {"generated", "applied", "verifying", "verified", "failed"},
2896
- }
2897
- if requested not in allowed.get(current, set()):
2898
- raise SystemExit(f"Finding remediation cannot move from {current} to {requested}.")
2899
-
2900
-
2901
- def require_pending_remediation_action(current: sqlite3.Row, requested: str) -> None:
2902
- pending_action = current["pending_action"]
2903
- if pending_action is not None:
2904
- allowed = {
2905
- "generate": {"generated", "failed"},
2906
- "apply": {"applied", "failed"},
2907
- "verify": {"verifying", "verified", "failed"},
2908
- }
2909
- if requested not in allowed[pending_action]:
2910
- raise SystemExit(
2911
- f"Pending remediation action {pending_action} cannot record state {requested}."
2912
- )
2913
- return
2914
- required_action = {
2915
- ("requested", "generated"): "generate",
2916
- ("generated", "applied"): "apply",
2917
- ("applied", "verifying"): "verify",
2918
- }.get((current["state"], requested))
2919
- if required_action is not None:
2920
- raise SystemExit(
2921
- f"Request {required_action} before recording remediation state {requested}."
2922
- )
2441
+ return publication.export_findings(_WORKBENCH_PUBLICATION_CONTEXT, connection, args)
2923
2442
 
2924
2443
 
2925
2444
  def require_reviewed_patch_applied(
@@ -3090,8 +2609,8 @@ def workspace_state(
3090
2609
  workspace_id: str,
3091
2610
  *,
3092
2611
  result_scan_id: str | None = None,
2612
+ result_scan: dict[str, Any] | None = None,
3093
2613
  thread_id: str | None = None,
3094
- refresh_stopped: bool = False,
3095
2614
  ) -> dict[str, Any]:
3096
2615
  workspace = require_workspace(connection, workspace_id)
3097
2616
  if thread_id is not None and workspace["thread_id"] != optional_text(thread_id, maximum=512):
@@ -3112,11 +2631,11 @@ def workspace_state(
3112
2631
  }
3113
2632
  selected_scan_id = result_scan_id or workspace["active_scan_id"]
3114
2633
  if selected_scan_id:
3115
- if refresh_stopped:
3116
- refresh_stopped_scan_results(connection, selected_scan_id)
3117
2634
  selected_scan = require_scan(connection, selected_scan_id)
3118
2635
  result["userContext"] = selected_scan["user_context"]
3119
- result["results"] = scan_result(connection, selected_scan)
2636
+ result["results"] = (
2637
+ result_scan if result_scan is not None else scan_result(connection, selected_scan)
2638
+ )
3120
2639
  return result
3121
2640
 
3122
2641
  target_metadata = None
@@ -3158,14 +2677,17 @@ def scan_context(
3158
2677
  occurrence_id: str | None = None,
3159
2678
  ) -> dict[str, Any]:
3160
2679
  scan = require_scan(connection, scan_id)
3161
- workspace = workspace_state(connection, scan["workspace_id"], result_scan_id=scan["id"])
2680
+ result = scan_result(connection, scan, occurrence_id=occurrence_id)
2681
+ workspace_result = result if occurrence_id is None else scan_result(connection, scan)
2682
+ workspace = workspace_state(
2683
+ connection,
2684
+ scan["workspace_id"],
2685
+ result_scan_id=scan["id"],
2686
+ result_scan=workspace_result,
2687
+ )
3162
2688
  context = {
3163
2689
  "otherRunningDeepScans": deep_scan.other_running_deep_scans(connection, scan["id"]),
3164
- "scan": (
3165
- workspace["results"]
3166
- if occurrence_id is None
3167
- else scan_result(connection, scan, occurrence_id=occurrence_id)
3168
- ),
2690
+ "scan": result,
3169
2691
  "workspace": workspace,
3170
2692
  }
3171
2693
  if scan["recipe_json"] is not None:
@@ -3175,7 +2697,6 @@ def scan_context(
3175
2697
 
3176
2698
 
3177
2699
  def list_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
3178
- refresh_stopped_scan_results(connection, args.scan_id)
3179
2700
  scan = require_scan(connection, args.scan_id)
3180
2701
  backfill_legacy_finding_details(connection, scan)
3181
2702
  limit = min(args.limit, FINDINGS_PAGE_MAX)
@@ -3293,6 +2814,7 @@ def scan_result(
3293
2814
  progress_result["independentReviews"] = {
3294
2815
  "active": independent_reviews["active"],
3295
2816
  "completed": independent_reviews["completed"],
2817
+ "maximum": independent_reviews["maximum"],
3296
2818
  "consolidating": independent_reviews["consolidating"],
3297
2819
  }
3298
2820
  return {
@@ -3317,6 +2839,9 @@ def scan_result(
3317
2839
  "remediationAvailable": remediation_available,
3318
2840
  "remediationUnavailableReason": remediation_unavailable_reason,
3319
2841
  "reportAvailable": "markdownReport" in artifacts,
2842
+ "resultsRecoveryNeeded": saved_results.scan_results_recovery_needed(
2843
+ _WORKBENCH_DB_CONTEXT, connection, scan
2844
+ ),
3320
2845
  "scanDir": scan["scan_dir"],
3321
2846
  "scanId": scan["id"],
3322
2847
  "scope": scan["scope"],
@@ -3768,9 +3293,7 @@ def require_canonical_scan_directory(scan_dir: Path) -> Path:
3768
3293
  # Re-check privacy so a shared parent cannot be used to substitute forged artifacts.
3769
3294
  if os.name != "nt":
3770
3295
  if stat.S_IMODE(metadata.st_mode) & 0o077:
3771
- raise SystemExit(
3772
- "Scan directory must not be accessible to other users (chmod 700)."
3773
- )
3296
+ raise SystemExit("Scan directory must not be accessible to other users (chmod 700).")
3774
3297
  geteuid = getattr(os, "geteuid", None)
3775
3298
  effective_uid = geteuid() if geteuid is not None else None
3776
3299
  if effective_uid is not None and metadata.st_uid != effective_uid:
@@ -3780,9 +3303,7 @@ def require_canonical_scan_directory(scan_dir: Path) -> Path:
3780
3303
  parent_metadata = parent.lstat()
3781
3304
  except OSError as exc:
3782
3305
  raise SystemExit("Scan output parent could not be inspected.") from exc
3783
- if not stat.S_ISDIR(parent_metadata.st_mode) or stat.S_ISLNK(
3784
- parent_metadata.st_mode
3785
- ):
3306
+ if not stat.S_ISDIR(parent_metadata.st_mode) or stat.S_ISLNK(parent_metadata.st_mode):
3786
3307
  raise SystemExit("Scan output parent must be a non-symlink directory.")
3787
3308
  if effective_uid is not None and parent_metadata.st_uid not in {
3788
3309
  0,
@@ -3816,6 +3337,25 @@ def reject_non_finite_json(value: str) -> None:
3816
3337
  raise ValueError(f"non-finite JSON number {value!r} is not supported")
3817
3338
 
3818
3339
 
3340
+ _WORKBENCH_PUBLICATION_CONTEXT = publication.WorkbenchPublicationContext(
3341
+ ARTIFACTS=ARTIFACTS,
3342
+ artifact_path=artifact_path,
3343
+ available_artifact_path=available_artifact_path,
3344
+ database_path=database_path,
3345
+ expected_coverage_mode=expected_coverage_mode,
3346
+ now=now,
3347
+ pin_legacy_manifest_digest=pin_legacy_manifest_digest,
3348
+ published_manifest_digest=published_manifest_digest,
3349
+ read_json_object=read_json_object,
3350
+ require_canonical_scan_directory=require_canonical_scan_directory,
3351
+ require_recorded_manifest_digest=require_recorded_manifest_digest,
3352
+ require_scan=require_scan,
3353
+ scan_result=scan_result,
3354
+ verify_manifest_binding=verify_manifest_binding,
3355
+ workspace_state=workspace_state,
3356
+ )
3357
+
3358
+
3819
3359
  _WORKBENCH_DB_CONTEXT = saved_results.WorkbenchDbContext(
3820
3360
  ARTIFACTS=ARTIFACTS,
3821
3361
  artifact_path=artifact_path,
@@ -3884,7 +3424,6 @@ def main() -> None:
3884
3424
  connection,
3885
3425
  args.workspace_id,
3886
3426
  thread_id=args.thread_id,
3887
- refresh_stopped=True,
3888
3427
  )
3889
3428
  elif args.command == "save-workspace":
3890
3429
  result = save_workspace(connection, args)
@@ -3913,12 +3452,10 @@ def main() -> None:
3913
3452
  elif args.command == "record-deep-scan-publication-failure":
3914
3453
  result = deep_scan.record_deep_scan_publication_failure(connection, args)
3915
3454
  elif args.command == "get-scan":
3916
- refresh_stopped_scan_results(connection, args.scan_id)
3917
3455
  result = scan_context(connection, args.scan_id, args.occurrence_id)
3918
3456
  elif args.command == "get-scan-feedback":
3919
3457
  result = get_scan_feedback(connection, require_scan(connection, args.scan_id))
3920
3458
  elif args.command == "list-scans":
3921
- refresh_all_stopped_scan_results(connection)
3922
3459
  result = scan_history.list_scans(connection, args)
3923
3460
  elif args.command == "list-unmatched-scan-pairs":
3924
3461
  result = scan_history.list_unmatched_scan_pairs(
@@ -3952,10 +3489,8 @@ def main() -> None:
3952
3489
  read_coverage=coverage_for_comparison,
3953
3490
  )
3954
3491
  elif args.command == "list-global-findings":
3955
- refresh_all_stopped_scan_results(connection)
3956
3492
  result = native_indexes.list_global_findings(connection, args)
3957
3493
  elif args.command == "list-repositories":
3958
- refresh_all_stopped_scan_results(connection)
3959
3494
  result = native_indexes.list_repositories(connection, args)
3960
3495
  elif args.command == "list-findings":
3961
3496
  result = list_findings(connection, args)
@@ -3975,6 +3510,8 @@ def main() -> None:
3975
3510
  result = fail_scan(connection, args)
3976
3511
  elif args.command == "preserve-scan-results":
3977
3512
  result = preserve_scan_results(connection, args)
3513
+ elif args.command == "recover-scan-results":
3514
+ result = recover_scan_results(connection, args)
3978
3515
  elif args.command == "write-scan-draft":
3979
3516
  result = write_scan_draft(connection, args)
3980
3517
  elif args.command == "mark-handoff-delivered":
@@ -4025,14 +3562,19 @@ def main() -> None:
4025
3562
  result = release_finding_remediation_claim(connection, args)
4026
3563
  elif args.command == "cancel-finding-remediation-request":
4027
3564
  result = scan_context(
4028
- connection, remediation.cancel_finding_remediation_request(connection, args)
3565
+ connection,
3566
+ remediation.cancel_finding_remediation_request(connection, args),
4029
3567
  )
4030
3568
  elif args.command == "set-finding-remediation":
4031
3569
  result = set_finding_remediation(connection, args)
4032
3570
  elif args.command == "prepare-linear-publication":
4033
- result = prepare_linear_publication(connection, args)
3571
+ result = publication.prepare_linear_publication(
3572
+ _WORKBENCH_PUBLICATION_CONTEXT, connection, args
3573
+ )
4034
3574
  elif args.command == "record-linear-publications":
4035
- result = record_linear_publications(connection, args)
3575
+ result = publication.record_linear_publications(
3576
+ _WORKBENCH_PUBLICATION_CONTEXT, connection, args
3577
+ )
4036
3578
  elif args.command == "export-findings":
4037
3579
  result = export_findings(connection, args)
4038
3580
  elif args.command == "database-info":
@@ -4043,7 +3585,9 @@ def main() -> None:
4043
3585
  result = dashboard(connection, json.load(sys.stdin))
4044
3586
  elif args.command == "store-findings":
4045
3587
  payload = json.load(sys.stdin)
4046
- result = store_findings(connection, payload["entries"], now(), payload.get("repositoryId"))
3588
+ result = store_findings(
3589
+ connection, payload["entries"], now(), payload.get("repositoryId")
3590
+ )
4047
3591
  elif args.command == "find-potential-duplicates":
4048
3592
  result = find_potential_duplicates(connection, args.finding_id, args.repository_id)
4049
3593
  elif args.command == "store-dedupe-groups":