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
fix(plugin): read source excerpts from the scanned target (#1063)
mldangelo-oai0c4bf047aeca
diff --git a/plugins/codex-security/scripts/workbench_source_excerpt.py b/plugins/codex-security/scripts/workbench_source_excerpt.pyindex 63e9fbe25..87d4199c8 100644--- a/plugins/codex-security/scripts/workbench_source_excerpt.py+++ b/plugins/codex-security/scripts/workbench_source_excerpt.py@@ -71,7 +71,12 @@ def scanned_source_text(scan: sqlite3.Row, target: Path, path: str) -> str | Nonsnapshot_digest = scan["target_snapshot_digest"]if snapshot_digest is not None and snapshot_digest != clean_worktree_content_digest():return None- object_name = f"{revision}:{path}"+ if (+ scan["diff_target_kind"] == "working_tree"+ and scan["diff_content_digest"] != clean_worktree_content_digest()+ ):+ return None+ object_name = f"{revision}:./{path}"content = git_bytes(target, "cat-file", "blob", object_name)return content.decode("utf-8", errors="replace") if content is not None else Nonediff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex b21d854e2..cd0cfacbf 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -2248,7 +2248,14 @@ def test_workbench_rejects_working_tree_target_after_contents_change(tmp_path: Pdef test_workbench_warns_after_working_tree_changes(tmp_path: Path) -> None:state_dir = tmp_path / "state"target = tmp_path / "target"- revision = initialize_git_repository(target)+ initialize_git_repository(target)+ (target / "README.md").write_text("committed source\n" * 50)+ subprocess.run(["git", "add", "README.md"], cwd=target, check=True)+ subprocess.run(["git", "commit", "-qm", "Add source fixture"], cwd=target, check=True)+ revision = subprocess.run(+ ["git", "rev-parse", "HEAD"], cwd=target, check=True, capture_output=True, text=True+ ).stdout.strip()+ (target / "README.md").write_text("scanned working-tree source\n" * 50)(target / "new-file.txt").write_text("selected content\n")workspace_id = str(uuid.uuid4())create_saved_working_tree_workspace(state_dir, target, workspace_id, revision)@@ -2266,6 +2273,7 @@ def test_workbench_warns_after_working_tree_changes(tmp_path: Path) -> None:scan_dir,scan_id,target,+ relative_path="README.md",target_kind="git_diff",diff_base_revision=revision,diff_head_revision=revision,@@ -2276,6 +2284,9 @@ def test_workbench_warns_after_working_tree_changes(tmp_path: Path) -> None:completed = scan_command(state_dir, "complete-scan", scan_id)assert completed["scan"]["progress"]["status"] == "complete"assert completed["scan"]["warnings"] == [WORKTREE_CHANGED_WARNING]+ finding = completed["scan"]["findings"][0]+ assert finding["locations"][0]["path"] == "README.md"+ assert "sourceExcerpt" not in findingmanifest = json.loads((scan_dir / "scan-manifest.json").read_text())assert manifest["scan"]["target"]["snapshotDigest"] == snapshot_digestassert json.loads((scan_dir / "coverage.json").read_text())["completeness"] == "complete"@@ -2938,19 +2949,26 @@ def test_completed_finding_projects_writeup_and_poc_artifact_paths(][email protected]("nested", [False, True])def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt(tmp_path: Path,+ nested: bool,) -> None:state_dir = tmp_path / "state"- target = tmp_path / "target"- initialize_git_repository(target)+ repository = tmp_path / "target"+ initialize_git_repository(repository)+ target = repository+ if nested:+ (repository / "README.md").write_text("different root source\n" * 50)+ target = repository / "nested"+ target.mkdir()(target / "README.md").write_text("\n".join(f"source line {line_number}" for line_number in range(1, 51))+ "\n"+ "x" * (1024 * 1024))- subprocess.run(["git", "add", "README.md"], cwd=target, check=True)- subprocess.run(["git", "commit", "-qm", "Add source fixture"], cwd=target, check=True)+ subprocess.run(["git", "add", "."], cwd=repository, check=True)+ subprocess.run(["git", "commit", "-qm", "Add source fixture"], cwd=repository, check=True)revision = subprocess.run(["git", "rev-parse", "HEAD"],cwd=target,@@ -3021,7 +3039,7 @@ def test_source_excerpt_breaks_lines_only_at_newlines(tmp_path: Path, separator:)excerpt = finding_source_excerpt(- {"target_revision": revision, "target_snapshot_digest": None},+ {"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None},target,[{"path": "README.md", "startLine": 5, "endLine": 5}],)@@ -3043,7 +3061,7 @@ def test_source_excerpt_numbers_standard_line_endings(tmp_path: Path, line_endin)excerpt = finding_source_excerpt(- {"target_revision": revision, "target_snapshot_digest": None},+ {"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None},target,[{"path": "README.md", "startLine": 5, "endLine": 5}],)@@ -3073,7 +3091,7 @@ def test_source_excerpt_preserves_final_lines(finding_source_excerpt = namespace["finding_source_excerpt"]target = tmp_path / "target"revision = commit_source_fixture(target, source)- scan = {"target_revision": revision, "target_snapshot_digest": None}+ scan = {"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None}excerpt = finding_source_excerpt(scan,diff --git a/sdk/typescript/tests-ts/plugin-report-limits.test.ts b/sdk/typescript/tests-ts/plugin-report-limits.test.tsindex 1356466bb..7f1b044d5 100644--- a/sdk/typescript/tests-ts/plugin-report-limits.test.ts+++ b/sdk/typescript/tests-ts/plugin-report-limits.test.ts@@ -22,7 +22,7 @@ describe("bundled scan report and source limits", () => {" source = b'x' * (1024 * 1024 + 1)"," excerpts.git_bytes = lambda *args: source"," target = pathlib.Path(directory).resolve()",- " excerpt = excerpts.scanned_source_text({'target_revision': 'deadbeef', 'target_snapshot_digest': None}, target, 'large.py')",+ " excerpt = excerpts.scanned_source_text({'target_revision': 'deadbeef', 'target_snapshot_digest': None, 'diff_target_kind': None}, target, 'large.py')"," hashes = finalizer._github_line_hashes(io.StringIO('line\\n' * 100001), {100001})"," print(json.dumps({'documentBytes': len(document), 'sourceBytes': len(excerpt), 'lateSourceLine': 100001 in hashes, 'unsafePathRejected': excerpts.safe_source_path(target, '../outside') is None}))",].join("\n");