@openai/codex-security 0.1.13 → 0.1.15

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 (102) hide show
  1. package/README.md +90 -29
  2. package/_bundled_plugin/.app.json +5 -0
  3. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  4. package/_bundled_plugin/examples/completed-scan/report.md +96 -0
  5. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  6. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  7. package/_bundled_plugin/references/config-preflight.md +5 -1
  8. package/_bundled_plugin/references/core-scan.md +6 -15
  9. package/_bundled_plugin/references/final-report.md +1 -1
  10. package/_bundled_plugin/references/scan-artifacts.md +1 -1
  11. package/_bundled_plugin/references/threat-model.md +64 -0
  12. package/_bundled_plugin/schemas/findings.schema.json +327 -2
  13. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +286 -4
  14. package/_bundled_plugin/scripts/config_preflight.py +20 -1
  15. package/_bundled_plugin/scripts/deep_scan_workbench.py +12 -6
  16. package/_bundled_plugin/scripts/finalize_scan_contract.py +322 -33
  17. package/_bundled_plugin/scripts/finding_preview.py +81 -19
  18. package/_bundled_plugin/scripts/report_projection.py +253 -33
  19. package/_bundled_plugin/scripts/validate_scan_contract.py +5 -2
  20. package/_bundled_plugin/scripts/workbench_cli.py +7 -1
  21. package/_bundled_plugin/scripts/workbench_remediation.py +2 -0
  22. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +4 -0
  23. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +2 -2
  24. package/_bundled_plugin/skills/security-scan/SKILL.md +1 -1
  25. package/_bundled_plugin/skills/threat-model/SKILL.md +6 -49
  26. package/dist/api.d.ts +5 -73
  27. package/dist/api.d.ts.map +1 -1
  28. package/dist/api.js +266 -211
  29. package/dist/api.js.map +1 -1
  30. package/dist/bulk-scan-discovery.d.ts +3 -3
  31. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  32. package/dist/bulk-scan-discovery.js +8 -7
  33. package/dist/bulk-scan-discovery.js.map +1 -1
  34. package/dist/cli.d.ts +18 -2
  35. package/dist/cli.d.ts.map +1 -1
  36. package/dist/cli.js +785 -121
  37. package/dist/cli.js.map +1 -1
  38. package/dist/codex-prompt.d.ts +4 -0
  39. package/dist/codex-prompt.d.ts.map +1 -0
  40. package/dist/codex-prompt.js +14 -0
  41. package/dist/codex-prompt.js.map +1 -0
  42. package/dist/config.d.ts +1 -0
  43. package/dist/config.d.ts.map +1 -1
  44. package/dist/config.js +8 -1
  45. package/dist/config.js.map +1 -1
  46. package/dist/contract.d.ts.map +1 -1
  47. package/dist/contract.js +198 -2
  48. package/dist/contract.js.map +1 -1
  49. package/dist/cost.d.ts +7 -0
  50. package/dist/cost.d.ts.map +1 -1
  51. package/dist/cost.js +68 -47
  52. package/dist/cost.js.map +1 -1
  53. package/dist/errors.d.ts +4 -0
  54. package/dist/errors.d.ts.map +1 -1
  55. package/dist/errors.js +9 -0
  56. package/dist/errors.js.map +1 -1
  57. package/dist/index.d.ts +2 -2
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +1 -1
  60. package/dist/index.js.map +1 -1
  61. package/dist/linear.d.ts +19 -0
  62. package/dist/linear.d.ts.map +1 -0
  63. package/dist/linear.js +106 -0
  64. package/dist/linear.js.map +1 -0
  65. package/dist/models.d.ts +89 -2
  66. package/dist/models.d.ts.map +1 -1
  67. package/dist/models.js.map +1 -1
  68. package/dist/multiscan.d.ts.map +1 -1
  69. package/dist/multiscan.js +7 -6
  70. package/dist/multiscan.js.map +1 -1
  71. package/dist/patch-tui.d.ts +28 -0
  72. package/dist/patch-tui.d.ts.map +1 -0
  73. package/dist/patch-tui.js +384 -0
  74. package/dist/patch-tui.js.map +1 -0
  75. package/dist/publish.d.ts +2 -2
  76. package/dist/publish.d.ts.map +1 -1
  77. package/dist/publish.js +4 -8
  78. package/dist/publish.js.map +1 -1
  79. package/dist/runtime.d.ts +10 -7
  80. package/dist/runtime.d.ts.map +1 -1
  81. package/dist/runtime.js +63 -35
  82. package/dist/runtime.js.map +1 -1
  83. package/dist/scan-comparison.d.ts +2 -1
  84. package/dist/scan-comparison.d.ts.map +1 -1
  85. package/dist/scan-comparison.js +17 -15
  86. package/dist/scan-comparison.js.map +1 -1
  87. package/dist/scan-dashboard.d.ts +2 -1
  88. package/dist/scan-dashboard.d.ts.map +1 -1
  89. package/dist/scan-dashboard.js +219 -11
  90. package/dist/scan-dashboard.js.map +1 -1
  91. package/dist/targets.d.ts +1 -0
  92. package/dist/targets.d.ts.map +1 -1
  93. package/dist/targets.js +4 -2
  94. package/dist/targets.js.map +1 -1
  95. package/dist/trusted-executable.d.ts.map +1 -1
  96. package/dist/trusted-executable.js +26 -11
  97. package/dist/trusted-executable.js.map +1 -1
  98. package/dist/version.d.ts +1 -1
  99. package/dist/version.js +1 -1
  100. package/dist/version.js.map +1 -1
  101. package/package.json +7 -3
  102. package/_bundled_plugin/skills/threat-model/references/threat-model-guidance.md +0 -48
@@ -20,7 +20,16 @@ except ModuleNotFoundError: # pragma: no cover - Python 3.10 only
20
20
  PLUGIN_ROOT = Path(__file__).resolve().parents[1]
21
21
  DEFAULT_REGISTRY = PLUGIN_ROOT / "preflight" / "capability-profiles.toml"
22
22
  DEFAULT_CODEX_HOME = Path(os.environ.get("CODEX_HOME", "~/.codex")).expanduser()
23
- SYSTEM_CONFIG = Path("/etc/codex/config.toml")
23
+
24
+
25
+ def default_system_config() -> Path:
26
+ if os.name == "nt":
27
+ program_data = os.environ.get("ProgramData", r"C:\ProgramData")
28
+ return Path(program_data) / "OpenAI" / "Codex" / "config.toml"
29
+ return Path("/etc/codex/config.toml")
30
+
31
+
32
+ SYSTEM_CONFIG = default_system_config()
24
33
  DEFAULT_CONFIG = DEFAULT_CODEX_HOME / "config.toml"
25
34
  VALID_SEVERITIES = {"block", "warn", "suggest"}
26
35
  VALID_MULTI_AGENT_OWNERS = {"native", "codex-bridge"}
@@ -244,6 +253,16 @@ def project_trust_level(
244
253
  if not isinstance(projects, dict):
245
254
  continue
246
255
  project = projects.get(str(project_root))
256
+ if not isinstance(project, dict) and os.name == "nt":
257
+ project_key = os.path.normcase(os.path.realpath(project_root))
258
+ project = next(
259
+ (
260
+ value
261
+ for path, value in projects.items()
262
+ if os.path.normcase(os.path.realpath(path)) == project_key
263
+ ),
264
+ None,
265
+ )
247
266
  if not isinstance(project, dict):
248
267
  continue
249
268
  trust_level = project.get("trust_level")
@@ -160,6 +160,12 @@ def now() -> str:
160
160
  return dependencies().now()
161
161
 
162
162
 
163
+ def _parse_timestamp(value: str) -> datetime:
164
+ if isinstance(value, str) and value.endswith(("Z", "z")):
165
+ value = value[:-1] + "+00:00"
166
+ return datetime.fromisoformat(value)
167
+
168
+
163
169
  def state_dir() -> Path:
164
170
  return dependencies().state_dir()
165
171
 
@@ -219,7 +225,7 @@ def require_deep_scan_run(connection: sqlite3.Connection, scan_id: str) -> sqlit
219
225
 
220
226
 
221
227
  def deep_scan_deadline_reached(run: sqlite3.Row) -> bool:
222
- elapsed = datetime.fromisoformat(now()) - datetime.fromisoformat(str(run["created_at"]))
228
+ elapsed = _parse_timestamp(now()) - _parse_timestamp(str(run["created_at"]))
223
229
  return elapsed.total_seconds() / 3600 >= run["max_time_hours"]
224
230
 
225
231
 
@@ -880,12 +886,12 @@ def coordinator_lease_is_live(
880
886
  """,
881
887
  (run["scan_id"],),
882
888
  ).fetchone()
883
- return active_worker is not None and datetime.fromisoformat(
889
+ return active_worker is not None and _parse_timestamp(
884
890
  str(run["updated_at"])
885
- ) > datetime.fromisoformat(timestamp) - timedelta(
891
+ ) > _parse_timestamp(timestamp) - timedelta(
886
892
  seconds=DEEP_SCAN_LEGACY_COORDINATOR_GRACE_SECONDS
887
893
  )
888
- heartbeat_time = datetime.fromisoformat(str(run["updated_at"]))
894
+ heartbeat_time = _parse_timestamp(str(run["updated_at"]))
889
895
  heartbeat_path = (
890
896
  Path(scan["scan_dir"])
891
897
  / "artifacts"
@@ -895,10 +901,10 @@ def coordinator_lease_is_live(
895
901
  try:
896
902
  heartbeat = json.loads(heartbeat_path.read_text(encoding="utf-8"))
897
903
  if heartbeat["coordinatorGeneration"] == run["coordinator_generation"]:
898
- heartbeat_time = max(heartbeat_time, datetime.fromisoformat(heartbeat["updatedAt"]))
904
+ heartbeat_time = max(heartbeat_time, _parse_timestamp(heartbeat["updatedAt"]))
899
905
  except (OSError, KeyError, TypeError, ValueError):
900
906
  pass
901
- current_time = datetime.fromisoformat(timestamp)
907
+ current_time = _parse_timestamp(timestamp)
902
908
  return heartbeat_time > current_time - timedelta(seconds=DEEP_SCAN_COORDINATOR_LEASE_SECONDS)
903
909
 
904
910
 
@@ -694,7 +694,7 @@ def _finding_strength(finding: dict[str, Any]) -> tuple[int, int, int]:
694
694
  return (
695
695
  ("informational", "low", "medium", "high", "critical").index(finding["severity"]["level"]),
696
696
  ("low", "medium", "high").index(finding["confidence"]["level"]),
697
- len(finding.get("codeEvidence") or []),
697
+ _finding_evidence_strength(finding),
698
698
  )
699
699
 
700
700
 
@@ -735,6 +735,12 @@ def _recover_unsealed_findings(
735
735
  try:
736
736
  if not isinstance(finding, dict):
737
737
  raise ContractError(f"{context}: expected an object")
738
+ compatible_findings = _legacy_sealed_findings_for_validation(
739
+ {"findings": [finding]}
740
+ )["findings"]
741
+ compatible_finding = compatible_findings[0]
742
+ normalized_legacy_details = compatible_finding != finding
743
+ finding = compatible_finding
738
744
  identity = _require_dict(finding, "identity", context)
739
745
  fields: list[tuple[dict[str, Any], str, str, str]] = [
740
746
  (finding, "ruleId", context, "rule identifier"),
@@ -742,7 +748,9 @@ def _recover_unsealed_findings(
742
748
  ]
743
749
  if "instance" in identity:
744
750
  fields.append((identity, "instance", f"{context}.identity", "instance"))
745
- normalized_fields = []
751
+ normalized_fields = (
752
+ ["legacy finding details"] if normalized_legacy_details else []
753
+ )
746
754
  for parent, field, field_context, label in fields:
747
755
  value = _require_str(parent, field, field_context)
748
756
  if SLUG_RE.fullmatch(value):
@@ -1222,12 +1230,14 @@ def _validate_finding(finding: dict[str, Any], context: str) -> None:
1222
1230
  _validate_location(location, f"{context}.locations[{index}]")
1223
1231
 
1224
1232
  evidence_ids: set[str] = set()
1225
- code_evidence = finding.get("codeEvidence")
1226
- if code_evidence is not None:
1233
+ for evidence_key in ("codeEvidence", "code_evidence"):
1234
+ if evidence_key not in finding:
1235
+ continue
1236
+ code_evidence = finding[evidence_key]
1227
1237
  if not isinstance(code_evidence, list):
1228
- raise ContractError(f"{context}.codeEvidence: expected an array")
1238
+ raise ContractError(f"{context}.{evidence_key}: expected an array")
1229
1239
  for index, evidence in enumerate(code_evidence):
1230
- evidence_context = f"{context}.codeEvidence[{index}]"
1240
+ evidence_context = f"{context}.{evidence_key}[{index}]"
1231
1241
  if not isinstance(evidence, dict):
1232
1242
  raise ContractError(f"{evidence_context}: expected an object")
1233
1243
  evidence_id = _require_str(evidence, "id", evidence_context)
@@ -1236,19 +1246,33 @@ def _validate_finding(finding: dict[str, Any], context: str) -> None:
1236
1246
  evidence_ids.add(evidence_id)
1237
1247
  _require_str(evidence, "code", evidence_context)
1238
1248
 
1239
- for section_name in ("rootCause", "validation", "attackPath"):
1240
- section = finding.get(section_name)
1241
- if not isinstance(section, dict) or "evidenceRefs" not in section:
1249
+ referenced_sections = [
1250
+ (section_name, finding.get(section_name))
1251
+ for section_name in ("rootCause", "root_cause", "validation", "attackPath")
1252
+ ]
1253
+ attack_path = finding.get("attackPath")
1254
+ if isinstance(attack_path, dict):
1255
+ referenced_sections.extend(
1256
+ (f"attackPath.{section_name}", attack_path.get(section_name))
1257
+ for section_name in ("dataFlow", "dataflow", "data_flow", "reachability")
1258
+ )
1259
+ for section_name, section in referenced_sections:
1260
+ if not isinstance(section, dict):
1242
1261
  continue
1243
- refs = section["evidenceRefs"]
1244
- if not isinstance(refs, list) or any(not isinstance(ref, str) or not ref for ref in refs):
1245
- raise ContractError(f"{context}.{section_name}.evidenceRefs: expected strings")
1246
- unknown_refs = sorted(set(refs) - evidence_ids)
1247
- if unknown_refs:
1248
- raise ContractError(
1249
- f"{context}.{section_name}.evidenceRefs: unknown code-evidence ids: "
1250
- + ", ".join(unknown_refs)
1251
- )
1262
+ for refs_key in ("evidenceRefs", "evidence_refs"):
1263
+ if refs_key not in section:
1264
+ continue
1265
+ refs = section[refs_key]
1266
+ if not isinstance(refs, list) or any(
1267
+ not isinstance(ref, str) or not ref for ref in refs
1268
+ ):
1269
+ raise ContractError(f"{context}.{section_name}.{refs_key}: expected strings")
1270
+ unknown_refs = sorted(set(refs) - evidence_ids)
1271
+ if unknown_refs:
1272
+ raise ContractError(
1273
+ f"{context}.{section_name}.{refs_key}: unknown code-evidence ids: "
1274
+ + ", ".join(unknown_refs)
1275
+ )
1252
1276
 
1253
1277
  provenance = _require_dict(finding, "provenance", context)
1254
1278
  _require_str(provenance, "source", f"{context}.provenance")
@@ -1466,6 +1490,190 @@ def validate_against_schema(payload: dict[str, Any], schema_path: Path) -> None:
1466
1490
  _validate_schema_node(payload, schema, schema_path.stem)
1467
1491
 
1468
1492
 
1493
+ def _filter_unknown_legacy_evidence_refs(
1494
+ section: dict[str, Any], evidence_ids: set[str]
1495
+ ) -> None:
1496
+ for refs_field in ("evidenceRefs", "evidence_refs"):
1497
+ refs = section.get(refs_field)
1498
+ if isinstance(refs, list):
1499
+ section[refs_field] = [
1500
+ ref
1501
+ for ref in refs
1502
+ if isinstance(ref, str) and ref.strip() and ref in evidence_ids
1503
+ ]
1504
+
1505
+
1506
+ def _normalize_legacy_string_list_fields(
1507
+ section: dict[str, Any], fields: tuple[str, ...]
1508
+ ) -> None:
1509
+ for field in fields:
1510
+ if field not in section:
1511
+ continue
1512
+ value = section[field]
1513
+ if isinstance(value, str):
1514
+ normalized = [value] if value.strip() else []
1515
+ elif isinstance(value, list):
1516
+ normalized = [
1517
+ item for item in value if isinstance(item, str) and item.strip()
1518
+ ]
1519
+ else:
1520
+ normalized = []
1521
+ if normalized:
1522
+ section[field] = normalized
1523
+ else:
1524
+ section.pop(field)
1525
+
1526
+
1527
+ def _remove_unsupported_legacy_scalar_fields(
1528
+ section: dict[str, Any], fields: tuple[str, ...]
1529
+ ) -> None:
1530
+ for field in fields:
1531
+ if field in section and (
1532
+ not isinstance(section[field], str) or section[field] == ""
1533
+ ):
1534
+ section.pop(field)
1535
+
1536
+
1537
+ def _legacy_sealed_findings_for_validation(findings: dict[str, Any]) -> dict[str, Any]:
1538
+ compatible = copy.deepcopy(findings)
1539
+ finding_items = compatible.get("findings")
1540
+ if not isinstance(finding_items, list):
1541
+ return compatible
1542
+ for finding in finding_items:
1543
+ if not isinstance(finding, dict):
1544
+ continue
1545
+ canonical_evidence = finding.get("codeEvidence")
1546
+ canonical_evidence = canonical_evidence if isinstance(canonical_evidence, list) else []
1547
+ canonical_evidence_ids = {
1548
+ evidence["id"]
1549
+ for evidence in canonical_evidence
1550
+ if isinstance(evidence, dict)
1551
+ and isinstance(evidence.get("id"), str)
1552
+ and evidence["id"]
1553
+ }
1554
+ legacy_evidence = finding.get("code_evidence")
1555
+ if isinstance(legacy_evidence, list):
1556
+ compatible_legacy_evidence = []
1557
+ seen_evidence_ids = set(canonical_evidence_ids)
1558
+ for evidence in legacy_evidence:
1559
+ if not isinstance(evidence, dict):
1560
+ continue
1561
+ evidence_id = evidence.get("id")
1562
+ evidence_code = evidence.get("code")
1563
+ if (
1564
+ not isinstance(evidence_id, str)
1565
+ or not evidence_id.strip()
1566
+ or not isinstance(evidence_code, str)
1567
+ or not evidence_code.strip()
1568
+ ):
1569
+ continue
1570
+ if evidence_id in seen_evidence_ids:
1571
+ continue
1572
+ seen_evidence_ids.add(evidence_id)
1573
+ compatible_legacy_evidence.append(evidence)
1574
+ finding["code_evidence"] = compatible_legacy_evidence
1575
+ elif "code_evidence" in finding:
1576
+ finding.pop("code_evidence")
1577
+ compatible_legacy_evidence = finding.get("code_evidence")
1578
+ compatible_legacy_evidence = (
1579
+ compatible_legacy_evidence if isinstance(compatible_legacy_evidence, list) else []
1580
+ )
1581
+ evidence_ids = canonical_evidence_ids | {
1582
+ evidence["id"]
1583
+ for evidence in compatible_legacy_evidence
1584
+ if isinstance(evidence, dict)
1585
+ and isinstance(evidence.get("id"), str)
1586
+ and evidence["id"]
1587
+ }
1588
+ for section_name, list_fields in (
1589
+ ("root_cause", ("evidenceRefs", "evidence_refs")),
1590
+ (
1591
+ "validation",
1592
+ (
1593
+ "assertions",
1594
+ "counterEvidence",
1595
+ "evidence",
1596
+ "evidenceRefs",
1597
+ "evidence_refs",
1598
+ "limitations",
1599
+ ),
1600
+ ),
1601
+ (
1602
+ "attackPath",
1603
+ (
1604
+ "assumptions",
1605
+ "blindspots",
1606
+ "controls",
1607
+ "evidenceRefs",
1608
+ "evidence_refs",
1609
+ "limitations",
1610
+ "preconditions",
1611
+ "steps",
1612
+ ),
1613
+ ),
1614
+ ):
1615
+ section = finding.get(section_name)
1616
+ if not isinstance(section, dict):
1617
+ continue
1618
+ _normalize_legacy_string_list_fields(section, list_fields)
1619
+ _filter_unknown_legacy_evidence_refs(section, evidence_ids)
1620
+ legacy_root_cause = finding.get("root_cause")
1621
+ if isinstance(legacy_root_cause, dict):
1622
+ _remove_unsupported_legacy_scalar_fields(
1623
+ legacy_root_cause, ("summary", "code", "language")
1624
+ )
1625
+ elif (
1626
+ "root_cause" in finding
1627
+ and legacy_root_cause is not None
1628
+ and (not isinstance(legacy_root_cause, str) or legacy_root_cause == "")
1629
+ ):
1630
+ finding.pop("root_cause")
1631
+ validation = finding.get("validation")
1632
+ if isinstance(validation, dict):
1633
+ _remove_unsupported_legacy_scalar_fields(
1634
+ validation, ("method", "status", "summary", "disposition", "result")
1635
+ )
1636
+ attack_path = finding.get("attackPath")
1637
+ if not isinstance(attack_path, dict):
1638
+ continue
1639
+ _remove_unsupported_legacy_scalar_fields(attack_path, ("summary",))
1640
+ for field in ("dataFlow", "data_flow", "dataflow", "reachability"):
1641
+ if field not in attack_path:
1642
+ continue
1643
+ detail = attack_path.get(field)
1644
+ if detail is None:
1645
+ attack_path.pop(field)
1646
+ continue
1647
+ if not isinstance(detail, (str, dict)):
1648
+ attack_path.pop(field)
1649
+ continue
1650
+ if isinstance(detail, str):
1651
+ if detail == "":
1652
+ attack_path.pop(field)
1653
+ continue
1654
+ detail_scalar_fields = ("summary", "source", "sink", "outcome")
1655
+ if field == "reachability":
1656
+ detail_scalar_fields += ("attacker", "entrypoint")
1657
+ _remove_unsupported_legacy_scalar_fields(detail, detail_scalar_fields)
1658
+ _normalize_legacy_string_list_fields(
1659
+ detail, ("evidenceRefs", "evidence_refs", "transformations")
1660
+ )
1661
+ _filter_unknown_legacy_evidence_refs(detail, evidence_ids)
1662
+ if field == "reachability":
1663
+ _normalize_legacy_string_list_fields(detail, ("preconditions",))
1664
+ for field in ("impact", "likelihood"):
1665
+ detail = attack_path.get(field)
1666
+ if isinstance(detail, dict):
1667
+ _remove_unsupported_legacy_scalar_fields(
1668
+ detail, ("level", "rationale", "why")
1669
+ )
1670
+ elif detail is not None and (
1671
+ not isinstance(detail, str) or detail == ""
1672
+ ):
1673
+ attack_path.pop(field)
1674
+ return compatible
1675
+
1676
+
1469
1677
  def _validate_canonical_schemas_before_projection(
1470
1678
  manifest: dict[str, Any],
1471
1679
  findings: dict[str, Any],
@@ -1608,21 +1816,94 @@ def _sarif_primary_location(finding: dict[str, Any]) -> dict[str, Any]:
1608
1816
  )
1609
1817
 
1610
1818
 
1819
+ def _merged_code_evidence(finding: dict[str, Any]) -> list[dict[str, Any]]:
1820
+ catalog: dict[str, dict[str, Any]] = {}
1821
+ for evidence_key in ("codeEvidence", "code_evidence"):
1822
+ code_evidence = finding.get(evidence_key)
1823
+ if not isinstance(code_evidence, list):
1824
+ continue
1825
+ for evidence in code_evidence:
1826
+ if not isinstance(evidence, dict):
1827
+ continue
1828
+ evidence_id = evidence.get("id")
1829
+ if isinstance(evidence_id, str) and evidence_id:
1830
+ catalog.setdefault(evidence_id, evidence)
1831
+ return list(catalog.values())
1832
+
1833
+
1834
+ def _finding_evidence_strength(finding: dict[str, Any]) -> int:
1835
+ evidence = _merged_code_evidence(finding)
1836
+ seen_ids = {
1837
+ item["id"] for item in evidence if isinstance(item.get("id"), str) and item["id"].strip()
1838
+ }
1839
+ seen_codes = {
1840
+ item["code"]
1841
+ for item in evidence
1842
+ if isinstance(item.get("code"), str) and item["code"].strip()
1843
+ }
1844
+ strength = len(evidence)
1845
+ for section_name in ("rootCause", "root_cause"):
1846
+ section = finding.get(section_name)
1847
+ if not isinstance(section, dict):
1848
+ continue
1849
+ for evidence_name in ("codeEvidence", "code_evidence"):
1850
+ embedded = section.get(evidence_name)
1851
+ if not isinstance(embedded, list):
1852
+ continue
1853
+ for item in embedded:
1854
+ if not isinstance(item, dict):
1855
+ continue
1856
+ code = item.get("code")
1857
+ if not isinstance(code, str) or not code.strip() or code in seen_codes:
1858
+ continue
1859
+ evidence_id = item.get("id")
1860
+ if isinstance(evidence_id, str) and evidence_id.strip():
1861
+ if evidence_id in seen_ids:
1862
+ continue
1863
+ seen_ids.add(evidence_id)
1864
+ seen_codes.add(code)
1865
+ strength += 1
1866
+ code = section.get("code")
1867
+ if isinstance(code, str) and code.strip() and code not in seen_codes:
1868
+ seen_codes.add(code)
1869
+ strength += 1
1870
+ return strength
1871
+
1872
+
1611
1873
  def _sarif_locations(finding: dict[str, Any]) -> list[dict[str, Any]]:
1612
1874
  primary = _sarif_primary_location(finding)
1613
1875
  locations = [
1614
1876
  primary,
1615
1877
  *(location for location in finding["locations"] if location is not primary),
1616
1878
  ]
1617
- locations.extend(
1618
- {
1619
- "path": evidence["path"],
1620
- "startLine": evidence["startLine"],
1621
- "endLine": evidence.get("endLine", evidence["startLine"]),
1622
- "role": f"evidence:{evidence['id']}",
1623
- }
1624
- for evidence in finding.get("codeEvidence", [])
1625
- )
1879
+ for evidence in _merged_code_evidence(finding):
1880
+ path = evidence.get("path")
1881
+ start_line = evidence.get("startLine")
1882
+ if (
1883
+ not isinstance(path, str)
1884
+ or not isinstance(start_line, int)
1885
+ or isinstance(start_line, bool)
1886
+ or start_line < 1
1887
+ ):
1888
+ continue
1889
+ try:
1890
+ path = _require_safe_relative_path(path, "SARIF evidence location")
1891
+ except ContractError:
1892
+ continue
1893
+ locations.append(
1894
+ {
1895
+ "path": path,
1896
+ "startLine": start_line,
1897
+ "endLine": (
1898
+ evidence["endLine"]
1899
+ if isinstance(evidence.get("endLine"), int)
1900
+ and not isinstance(evidence["endLine"], bool)
1901
+ and evidence["endLine"] >= start_line
1902
+ else start_line
1903
+ ),
1904
+ "role": f"evidence:{evidence['id']}",
1905
+ }
1906
+ )
1626
1907
  unique: dict[tuple[str, int, int], dict[str, Any]] = {}
1627
1908
  for location in locations:
1628
1909
  key = (
@@ -1894,11 +2175,12 @@ def _read_sealed_scan(
1894
2175
  },
1895
2176
  )
1896
2177
  _validate_manifest(manifest)
1897
- _validate_findings(manifest, findings)
2178
+ findings_for_validation = _legacy_sealed_findings_for_validation(findings)
2179
+ _validate_findings(manifest, findings_for_validation)
1898
2180
  _validate_coverage(manifest, coverage, scan_dir)
1899
2181
  _validate_sealed_coverage_receipts(scan, coverage)
1900
2182
  validate_against_schema(manifest, schema_dir / "scan-manifest.schema.json")
1901
- validate_against_schema(findings, schema_dir / "findings.schema.json")
2183
+ validate_against_schema(findings_for_validation, schema_dir / "findings.schema.json")
1902
2184
  validate_against_schema(coverage, schema_dir / "coverage.schema.json")
1903
2185
  _validate_derived_finding_identities(manifest, findings)
1904
2186
  return manifest, findings, coverage, findings_bytes
@@ -2196,8 +2478,11 @@ def _prepare_scan_finalization(
2196
2478
  scan["sealedAt"] = _require_str(scan, "completedAt", "manifest.scan")
2197
2479
  _validate_target(_require_dict(scan, "target", "manifest.scan"))
2198
2480
  _validate_completion_binding(manifest, findings, coverage, completion_binding)
2481
+ findings_for_validation = (
2482
+ _legacy_sealed_findings_for_validation(findings) if was_sealed else findings
2483
+ )
2199
2484
  if was_sealed:
2200
- _validate_findings(manifest, findings)
2485
+ _validate_findings(manifest, findings_for_validation)
2201
2486
  _validate_derived_finding_identities(manifest, findings)
2202
2487
  elif completion_warnings is not None:
2203
2488
  discarded_findings = _recover_unsealed_findings(
@@ -2209,16 +2494,20 @@ def _prepare_scan_finalization(
2209
2494
  _recover_unsealed_hardening(manifest, scan_dir, completion_warnings)
2210
2495
  else:
2211
2496
  _populate_unsealed_finding_identities(manifest, findings)
2212
- _validate_findings(manifest, findings)
2497
+ _validate_findings(manifest, findings_for_validation)
2213
2498
  _validate_coverage(manifest, coverage, scan_dir)
2214
- _validate_canonical_schemas_before_projection(manifest, findings, coverage, schema_dir)
2499
+ _validate_canonical_schemas_before_projection(
2500
+ manifest, findings_for_validation, coverage, schema_dir
2501
+ )
2215
2502
  _require_derived_writeup_files(scan_dir, findings)
2216
2503
  _require_hardening_portfolio_file(scan_dir, scan)
2217
2504
  if was_sealed:
2218
2505
  _validate_sealed_coverage_receipts(scan, coverage)
2219
2506
  _validate_manifest(manifest)
2220
2507
  validate_against_schema(manifest, schema_dir / "scan-manifest.schema.json")
2221
- validate_against_schema(findings, schema_dir / "findings.schema.json")
2508
+ validate_against_schema(
2509
+ findings_for_validation, schema_dir / "findings.schema.json"
2510
+ )
2222
2511
  validate_against_schema(coverage, schema_dir / "coverage.schema.json")
2223
2512
  report_markdown_bytes = _generate_report_projection(manifest, findings, coverage)
2224
2513
  _validate_report_output_paths(scan_dir)