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): preserve source excerpt line numbering (#1098)
林SO75ec0608d28e
diff --git a/plugins/codex-security/scripts/workbench_source_excerpt.py b/plugins/codex-security/scripts/workbench_source_excerpt.pyindex cea25e23f..06c4075c7 100644--- a/plugins/codex-security/scripts/workbench_source_excerpt.py+++ b/plugins/codex-security/scripts/workbench_source_excerpt.py@@ -40,7 +40,10 @@ def finding_source_excerpt(source = scanned_source_text(scan, target, path)if not source or "\0" in source:return None- lines = source.splitlines()+ # Match location validation and SARIF hashing: CR, LF, and CRLF are line breaks.+ # Keep form feeds and Unicode separators within their original source lines.+ normalized = source.replace("\r\n", "\n").replace("\r", "\n")+ lines = normalized.removesuffix("\n").split("\n")if start_line < 1 or start_line > len(lines):return Nonelast_affected_line = end_line if isinstance(end_line, int) else start_linediff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex 5e8c8a9c1..1c5954311 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -4129,6 +4129,110 @@ def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt(assert refreshed["scan"]["findings"][0]["sourceExcerpt"] == excerpt+def commit_source_fixture(target: Path, source: bytes) -> str:+ initialize_git_repository(target)+ (target / "README.md").write_bytes(source)+ # Stage the bytes verbatim so hosts that enable core.autocrlf keep the fixture line endings.+ subprocess.run(["git", "-c", "core.autocrlf=false", "add", "README.md"], cwd=target, check=True)+ subprocess.run(["git", "commit", "-qm", "Add source fixture"], cwd=target, check=True)+ return subprocess.run(+ ["git", "rev-parse", "HEAD"],+ cwd=target,+ check=True,+ capture_output=True,+ text=True,+ ).stdout.strip()+++ "separator", ["\f", "\v", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"]+)+def test_source_excerpt_breaks_lines_only_at_newlines(tmp_path: Path, separator: str) -> None:+ namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")+ finding_source_excerpt = namespace["finding_source_excerpt"]+ target = tmp_path / "target"+ revision = commit_source_fixture(+ target,+ "".join(+ f"source line {line_number}{separator if line_number == 2 else ''}\n"+ for line_number in range(1, 11)+ ).encode(),+ )++ excerpt = finding_source_excerpt(+ {"target_revision": revision, "target_snapshot_digest": None},+ target,+ [{"path": "README.md", "startLine": 5, "endLine": 5}],+ )++ assert excerpt == "\n".join(+ f"{line_number} source line {line_number}{separator if line_number == 2 else ''}"+ for line_number in range(2, 9)+ )++[email protected]("line_ending", ["\n", "\r\n", "\r"])+def test_source_excerpt_numbers_standard_line_endings(tmp_path: Path, line_ending: str) -> None:+ namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")+ finding_source_excerpt = namespace["finding_source_excerpt"]+ target = tmp_path / "target"+ revision = commit_source_fixture(+ target,+ "".join(f"source line {line_number}{line_ending}" for line_number in range(1, 11)).encode(),+ )++ excerpt = finding_source_excerpt(+ {"target_revision": revision, "target_snapshot_digest": None},+ target,+ [{"path": "README.md", "startLine": 5, "endLine": 5}],+ )++ assert excerpt == "\n".join(+ f"{line_number} source line {line_number}" for line_number in range(2, 9)+ )+++ ("source", "expected_lines"),+ [+ (b"first\nsecond", ["first", "second"]),+ (b"first\nsecond\n", ["first", "second"]),+ (b"first\rsecond\r", ["first", "second"]),+ (b"first\r\nsecond\r\n", ["first", "second"]),+ (b"first\r\nsecond\rthird\nfourth", ["first", "second", "third", "fourth"]),+ (b"first\n\n", ["first", ""]),+ (b"first\r\r\n", ["first", ""]),+ (b"\r\n", [""]),+ ],+)+def test_source_excerpt_preserves_final_lines(+ tmp_path: Path, source: bytes, expected_lines: list[str]+) -> None:+ namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")+ 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}++ excerpt = finding_source_excerpt(+ scan,+ target,+ [{"path": "README.md", "startLine": len(expected_lines)}],+ )++ assert excerpt == "\n".join(+ f"{line_number} {line}" for line_number, line in enumerate(expected_lines, start=1)+ )+ assert (+ finding_source_excerpt(+ scan,+ target,+ [{"path": "README.md", "startLine": len(expected_lines) + 1}],+ )+ is None+ )++def test_workbench_preserves_in_flight_git_scan_during_migration_normalization(tmp_path: Path,) -> None: