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(plugin): fall back on Python AST recursion errors (#1094)

wj6c4b791a340a
diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.py
index 7eed9270a..80d9a1de9 100644
--- a/plugins/codex-security/scripts/rank_preview.py
+++ b/plugins/codex-security/scripts/rank_preview.py
@@ -200,7 +200,7 @@ def python_arguments(node: ast.FunctionDef | ast.AsyncFunctionDef) -> str:
def python_outline(text: str) -> list[str]:
try:
tree = ast.parse(text)
- except (SyntaxError, ValueError, TypeError, MemoryError):
+ except (SyntaxError, ValueError, TypeError, MemoryError, RecursionError):
return []
outline: list[str] = []
diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.py
index 1107fce10..10247f786 100644
--- a/plugins/codex-security/tests/test_generate_rank_input.py
+++ b/plugins/codex-security/tests/test_generate_rank_input.py
@@ -173,6 +173,35 @@ def test_make_repo_rank_input_matches_golden_and_filters_noise(tmp_path: Path) -
)
+def test_make_repo_rank_input_keeps_python_with_ast_recursion(tmp_path: Path) -> None:
+ repo = tmp_path / "repo"
+ (repo / "src").mkdir(parents=True)
+ source = "value = " + " + ".join(["x"] * 10000) + "\n"
+ (repo / "src" / "generated.py").write_text(source, encoding="utf-8")
+ (repo / "src" / "normal.py").write_text("value = 1\n", encoding="utf-8")
+ output = tmp_path / "rank_input.jsonl"
+
+ run_cli(
+ "make-repo-rank-input",
+ "--repo",
+ str(repo),
+ "--scope",
+ "src",
+ "--preview-bytes",
+ "128",
+ "--out",
+ str(output),
+ )
+
+ rows = read_jsonl(output)
+ assert [row["path"] for row in rows] == ["src/generated.py", "src/normal.py"]
+ preview = rows[0]["preview"]
+ assert preview
+ assert source.startswith(preview)
+ assert len(preview.encode("utf-8")) <= 128
+ assert rows[1]["preview"] == "value = 1"
+
+
@pytest.mark.parametrize("scope", [".", "src", "src/large.py", "explicit", "overlap", "diff"])
def test_rank_input_bounds_large_text_reads(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, scope: str
diff --git a/plugins/codex-security/tests/test_rank_preview.py b/plugins/codex-security/tests/test_rank_preview.py
index 5d5b7bd01..8070f14fa 100644
--- a/plugins/codex-security/tests/test_rank_preview.py
+++ b/plugins/codex-security/tests/test_rank_preview.py
@@ -586,6 +586,27 @@ second_runtime_line()
]
+ "prefix", ["", "def visible():\n pass\n"], ids=["sampled-source", "simple-outline"]
+)
+def test_python_preview_falls_back_on_ast_recursion(tmp_path: Path, prefix: str) -> None:
+ source = prefix + "value = " + " + ".join(["x"] * 10000) + "\n"
+ path = tmp_path / "generated.py"
+ data = source.encode("utf-8")
+ path.write_bytes(data)
+
+ preview, is_binary = preview_for(path, DEFAULT_PREVIEW_BYTES)
+
+ assert not is_binary
+ assert preview
+ assert len(preview.encode("utf-8")) <= DEFAULT_PREVIEW_BYTES
+ if prefix:
+ assert preview == "function visible"
+ else:
+ assert source.startswith(preview)
+ assert preview_for_bytes(path, data, DEFAULT_PREVIEW_BYTES) == (preview, False)
+
+
def test_fallback_preview_uses_head_and_evenly_sampled_nonblank_lines(tmp_path: Path) -> None:
source = "\n\n".join(f"line_{index:02d} {{ color: red; }}" for index in range(40))