Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix: reuse captured source hashes in SARIF results (#1721)

Michael D'Angeloe0803544002f
diff --git a/plugins/codex-security/references/sarif-adapter.md b/plugins/codex-security/references/sarif-adapter.md
index c7b68a391..7d8ee6c0a 100644
--- a/plugins/codex-security/references/sarif-adapter.md
+++ b/plugins/codex-security/references/sarif-adapter.md
@@ -14,6 +14,7 @@ The adapter:
- keeps the root-control location first for GitHub annotation when available and emits every distinct affected or code-evidence location in `locations`, so vulnerable sinks remain matchable
- preserves the semantic fingerprint under `codexSecurity/v1`
- emits GitHub's source-line `primaryLocationLineHash` when it can safely hash a bounded regular non-symlink source file inside the available source root
+- captures optional source-line hashes in one eager pass and reuses those captured values; later source-root changes do not trigger rereads
- maps categorical severity to SARIF `level`
- sets GitHub's rule-level `security-severity` to the highest finding score. Unscored critical, high, medium, low, and informational findings use 9.5, 8.0, 5.0, 2.0, and 0.0. A rule with no positive score omits this field. These defaults are display values, not calculated CVSS scores.
- preserves a deep scan's canonical `candidateId` under each child result's properties so consumers can group results without changing the original SARIF result presentation
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index e659441d8..8acbfde69 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -2501,31 +2501,6 @@ def _sarif_locations(finding: dict[str, Any]) -> list[dict[str, Any]]:
return list(unique.values())
-def _github_primary_location_line_hash(
- finding: dict[str, Any],
- source_root: Path | None,
- line_hash_cache: dict[tuple[Path, int], str | None] | None = None,
-) -> str | None:
- if source_root is None:
- return None
- primary_location = _sarif_primary_location(finding)
- try:
- source_root = source_root.resolve(strict=True)
- except (OSError, RuntimeError):
- return None
- relative_path = _require_safe_relative_path(primary_location["path"], "SARIF source location")
- source_path = source_root / relative_path
- start_line = primary_location["startLine"]
- cache_key = (source_path, start_line)
- if line_hash_cache is not None and cache_key in line_hash_cache:
- return line_hash_cache[cache_key]
- line_hashes = _github_line_hashes_for_source(source_root, relative_path, {start_line})
- line_hash = None if line_hashes is None else line_hashes.get(start_line)
- if line_hash_cache is not None:
- line_hash_cache[cache_key] = line_hash
- return line_hash
-
-
def _github_line_hash_cache(
findings: list[dict[str, Any]], source_root: Path | None
) -> dict[tuple[Path, int], str | None]:
@@ -2545,7 +2520,7 @@ def _github_line_hash_cache(
line_hash_cache: dict[tuple[Path, int], str | None] = {}
for relative_path, requested_lines in requested_lines_by_path.items():
line_hashes = _github_line_hashes_for_source(source_root, relative_path, requested_lines)
- source_path = source_root / relative_path
+ source_path = Path(relative_path)
for line_number in requested_lines:
line_hash_cache[(source_path, line_number)] = (
None if line_hashes is None else line_hashes.get(line_number)
@@ -2573,7 +2548,6 @@ def _sarif_location(location: dict[str, Any]) -> dict[str, Any]:
def _sarif_result(
finding: dict[str, Any],
rule_index: int,
- source_root: Path | None = None,
line_hash_cache: dict[tuple[Path, int], str | None] | None = None,
) -> dict[str, Any]:
properties = {
@@ -2590,7 +2564,8 @@ def _sarif_result(
partial_fingerprints = {
"codexSecurity/v1": finding["fingerprints"]["primary"],
}
- line_hash = _github_primary_location_line_hash(finding, source_root, line_hash_cache)
+ primary = _sarif_primary_location(finding)
+ line_hash = (line_hash_cache or {}).get((Path(primary["path"]), primary["startLine"]))
if line_hash is not None:
partial_fingerprints["primaryLocationLineHash"] = line_hash
return {
@@ -2628,7 +2603,7 @@ def build_sarif(
},
"automationDetails": {"id": scan["id"]},
"results": [
- _sarif_result(finding, rule_index[finding["ruleId"]], source_root, line_hash_cache)
+ _sarif_result(finding, rule_index[finding["ruleId"]], line_hash_cache)
for finding in ordered_findings
],
"properties": {
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.py
index 08b1ed165..0528537c5 100644
--- a/plugins/codex-security/tests/test_finalize_scan_contract.py
+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py
@@ -199,6 +199,11 @@ The extraction root is not enforced.
)
return source_root
+ def sarif_result(self, finding: dict, source_root: Path) -> dict:
+ return FINALIZER.build_sarif(self.manifest, {"findings": [finding]}, source_root)["runs"][
+ 0
+ ]["results"][0]
+
def export_csv(self) -> csv.DictReader:
self.findings["findings"] = [copy.deepcopy(self.finding)]
self.write_sealed_scan()
@@ -3046,7 +3051,7 @@ The extraction root is not enforced.
def test_sarif_emits_github_line_hash_when_source_is_available(self) -> None:
source_root = self.write_line_hash_source()
sarif_finding = self.sarif_finding("semantic-fingerprint")
- result = FINALIZER._sarif_result(sarif_finding, 0, source_root)
+ result = self.sarif_result(sarif_finding, source_root)
fingerprints = result["partialFingerprints"]
self.assertEqual(fingerprints["codexSecurity/v1"], "semantic-fingerprint")
self.assertEqual(fingerprints["primaryLocationLineHash"], "614150a715d5311d:1")
@@ -3061,7 +3066,7 @@ The extraction root is not enforced.
mock.patch.object(FINALIZER, "_is_windows", return_value=True),
mock.patch.object(FINALIZER, "_windows_scan_local_files", return_value=backend),
):
- result = FINALIZER._sarif_result(sarif_finding, 0, source_root)
+ result = self.sarif_result(sarif_finding, source_root)
backend.open_read_fd.assert_called_once_with(
source_root, "src/extract.py", "source file src/extract.py"
@@ -3081,14 +3086,14 @@ The extraction root is not enforced.
sarif_finding = self.sarif_finding("semantic-fingerprint")
sarif_finding["locations"] = [
{"path": "src/route.py", "startLine": 1, "role": "entrypoint/wrapper"},
- {"path": "src/control.py", "startLine": 1, "role": "root_control"},
+ {"path": "./src//control.py", "startLine": 1, "role": "root_control"},
]
- result = FINALIZER._sarif_result(sarif_finding, 0, source_root)
+ result = self.sarif_result(sarif_finding, source_root)
self.assertEqual(
result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"],
- "src/control.py",
+ "./src//control.py",
)
self.assertEqual(
result["partialFingerprints"]["primaryLocationLineHash"], "41499a4b14756ba2:1"
@@ -3100,7 +3105,7 @@ The extraction root is not enforced.
source_path.parent.mkdir(parents=True)
source_path.write_bytes(b"\n" * 40 + b"invalid: \xff\n")
sarif_finding = self.sarif_finding("semantic-fingerprint")
- result = FINALIZER._sarif_result(sarif_finding, 0, source_root)
+ result = self.sarif_result(sarif_finding, source_root)
self.assertIn("primaryLocationLineHash", result["partialFingerprints"])
def test_sarif_line_hash_skips_symlink_source(self) -> None:
@@ -3114,7 +3119,7 @@ The extraction root is not enforced.
)
source_path.symlink_to(external_path)
sarif_finding = self.sarif_finding("semantic-fingerprint")
- result = FINALIZER._sarif_result(sarif_finding, 0, source_root)
+ result = self.sarif_result(sarif_finding, source_root)
self.assertNotIn("primaryLocationLineHash", result["partialFingerprints"])
@skipUnless(hasattr(os, "mkfifo"), "requires FIFO support")
@@ -3175,7 +3180,7 @@ The extraction root is not enforced.
source_root = self.write_line_hash_source()
sarif_finding = self.sarif_finding("semantic-fingerprint")
with mock.patch.object(FINALIZER, "_github_line_hashes", side_effect=PermissionError):
- result = FINALIZER._sarif_result(sarif_finding, 0, source_root)
+ result = self.sarif_result(sarif_finding, source_root)
self.assertNotIn("primaryLocationLineHash", result["partialFingerprints"])
def test_sarif_line_hash_streams_source(self) -> None:
@@ -3209,6 +3214,54 @@ The extraction root is not enforced.
for result in results:
self.assertIn("primaryLocationLineHash", result["partialFingerprints"])
+ def test_sarif_keeps_captured_hash_when_source_root_changes(self) -> None:
+ read_hashes = FINALIZER._github_line_hashes_for_source
+ for change in ("disappear", "retarget"):
+ with self.subTest(change=change):
+ source_root = self.write_line_hash_source()
+ moved_root = source_root.with_name(f"moved-source-{change}")
+
+ def read_then_move(
+ *args, source_root=source_root, moved_root=moved_root, change=change
+ ):
+ hashes = read_hashes(*args)
+ source_root.rename(moved_root)
+ if change == "retarget":
+ replacement = self.scan_dir / "replacement"
+ (replacement / "src").mkdir(parents=True)
+ (replacement / "src" / "extract.py").write_text(
+ "replacement source\n" * 41, encoding="utf-8"
+ )
+ source_root.symlink_to(replacement, target_is_directory=True)
+ return hashes
+
+ with mock.patch.object(
+ FINALIZER, "_github_line_hashes_for_source", side_effect=read_then_move
+ ) as source_reads:
+ result = self.sarif_result(
+ self.sarif_finding("semantic-fingerprint"), source_root
+ )
+
+ self.assertEqual(
+ result["partialFingerprints"]["primaryLocationLineHash"], "614150a715d5311d:1"
+ )
+ self.assertEqual(source_reads.call_count, 1)
+
+ def test_sarif_does_not_retry_source_root_that_appears_after_capture(self) -> None:
+ source_root = self.scan_dir / "source"
+ sarif_rule = FINALIZER._sarif_rule
+
+ def create_source_then_render_rule(*args):
+ self.write_line_hash_source()
+ return sarif_rule(*args)
+
+ with mock.patch.object(
+ FINALIZER, "_sarif_rule", side_effect=create_source_then_render_rule
+ ):
+ result = self.sarif_result(self.sarif_finding("semantic-fingerprint"), source_root)
+
+ self.assertNotIn("primaryLocationLineHash", result["partialFingerprints"])
+
def test_sarif_caches_line_hashes_per_source_file(self) -> None:
source_root = self.write_line_hash_source()
findings = []