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): include C++ headers in scan inventories (#1079)

wjba8486e17bde
diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.py
index df53af77f..7eed9270a 100644
--- a/plugins/codex-security/scripts/rank_preview.py
+++ b/plugins/codex-security/scripts/rank_preview.py
@@ -32,9 +32,11 @@ TEXT_CODE_EXTENSIONS = {
".go",
".graphql",
".h",
+ ".hh",
".hpp",
".hs",
".html",
+ ".hxx",
".java",
".js",
".json",
@@ -71,7 +73,19 @@ TEXT_CODE_EXTENSIONS = {
}
JAVASCRIPT_EXTENSIONS = {".cjs", ".cts", ".js", ".jsx", ".mjs", ".mts", ".ts", ".tsx", ".vue"}
-JAVA_LIKE_EXTENSIONS = {".c", ".cc", ".cpp", ".cs", ".cxx", ".h", ".hpp", ".java", ".mm"}
+JAVA_LIKE_EXTENSIONS = {
+ ".c",
+ ".cc",
+ ".cpp",
+ ".cs",
+ ".cxx",
+ ".h",
+ ".hh",
+ ".hpp",
+ ".hxx",
+ ".java",
+ ".mm",
+}
BRACE_LANGUAGE_EXTENSIONS = {
*JAVASCRIPT_EXTENSIONS,
*JAVA_LIKE_EXTENSIONS,
@@ -459,7 +473,7 @@ def match_type_declaration(line: str, suffix: str) -> tuple[str, str] | None:
match = re.search(r"\b(class|struct|enum|protocol|actor|extension)\s+([A-Za-z_]\w*)", line)
elif suffix == ".dart":
match = re.search(r"\b(class|mixin|enum|extension)\s+([A-Za-z_]\w*)", line)
- elif suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".mm"}:
+ elif suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm"}:
match = re.search(
r"\b(class|struct|union|enum(?:\s+class)?)\s+([A-Za-z_]\w*)",
line,
@@ -511,12 +525,20 @@ def match_java_like_function(
prefix = before[: name_match.start()].strip()
if not prefix and name != (type_name or ""):
return None
- if suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".mm"} and prefix.startswith(
- ("typedef", "using")
- ):
+ if suffix in {
+ ".c",
+ ".cc",
+ ".cpp",
+ ".cxx",
+ ".h",
+ ".hh",
+ ".hpp",
+ ".hxx",
+ ".mm",
+ } and prefix.startswith(("typedef", "using")):
return None
if (
- suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".mm"}
+ suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm"}
and type_name is None
and line.endswith(";")
and not re.search(
diff --git a/plugins/codex-security/tests/test_generate_in_scope_files.py b/plugins/codex-security/tests/test_generate_in_scope_files.py
index 4dbf9d8de..711ea05f5 100644
--- a/plugins/codex-security/tests/test_generate_in_scope_files.py
+++ b/plugins/codex-security/tests/test_generate_in_scope_files.py
@@ -463,6 +463,39 @@ def test_diff_inventory_includes_changed_objective_c(tmp_path: Path, mode: str)
assert output.read_text(encoding="utf-8").splitlines() == sorted(sources)
[email protected]("mode", ["revisions", "staged", "unstaged"])
+def test_diff_inventory_includes_changed_cpp_headers(tmp_path: Path, mode: str) -> None:
+ repository = make_repository(tmp_path)
+ names = [
+ "include/base.h",
+ "include/base.hpp",
+ "include/lower.hh",
+ "include/lower.hxx",
+ "include/upper.HH",
+ "include/upper.HXX",
+ ]
+ for name in names:
+ write_file(repository, name, b"inline int answer() { return 1; }\n")
+ git(repository, "add", ".")
+ git(repository, "commit", "-qm", "base")
+ base = git(repository, "rev-parse", "HEAD")
+ for name in names:
+ write_file(repository, name, b"inline int answer() { return 2; }\n")
+ arguments = ["--diff-base", base, "--diff-mode", "local-patch"]
+ if mode in {"revisions", "staged"}:
+ git(repository, "add", ".")
+ if mode == "revisions":
+ git(repository, "commit", "-qm", "change")
+ arguments = ["--diff-base", base, "--diff-head", git(repository, "rev-parse", "HEAD")]
+ git(repository, "checkout", "-q", base)
+ output = tmp_path / "in_scope_files.txt"
+
+ result = run_inventory(repository, ".", output, arguments=arguments)
+
+ assert result.returncode == 0, result.stderr
+ assert output.read_text(encoding="utf-8").splitlines() == sorted(names)
+
+
@pytest.mark.parametrize("mode", ["revisions", "local-patch"])
def test_diff_inventory_includes_changed_solidity(tmp_path: Path, mode: str) -> None:
repository = make_repository(tmp_path)
diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.py
index fabf00b1e..1107fce10 100644
--- a/plugins/codex-security/tests/test_generate_rank_input.py
+++ b/plugins/codex-security/tests/test_generate_rank_input.py
@@ -295,6 +295,52 @@ def test_rank_input_includes_objective_c(tmp_path: Path, mode: str) -> None:
]
[email protected]("mode", ["repo", "revisions", "staged", "unstaged"])
+def test_rank_input_includes_cpp_headers(tmp_path: Path, mode: str) -> None:
+ repo = tmp_path / "repo"
+ include = repo / "include"
+ include.mkdir(parents=True)
+ initialize_repo(repo)
+ names = ["base.h", "base.hpp", "lower.hh", "lower.hxx", "upper.HH", "upper.HXX"]
+ for name in names:
+ (include / name).write_text("inline int before() { return 1; }\n", encoding="utf-8")
+ git(repo, "add", ".")
+ git(repo, "commit", "-qm", "base")
+ base = git(repo, "rev-parse", "HEAD")
+ for name in names:
+ (include / name).write_text("inline int after() { return 2; }\n", encoding="utf-8")
+ output = tmp_path / "rank_input.jsonl"
+
+ if mode in {"revisions", "staged"}:
+ git(repo, "add", ".")
+ if mode == "repo":
+ arguments = ["make-repo-rank-input", "--repo", str(repo), "--scope", "include"]
+ else:
+ diff_mode = "revisions" if mode == "revisions" else "local-patch"
+ arguments = [
+ "make-diff-rank-input",
+ "--repo",
+ str(repo),
+ "--base",
+ base,
+ "--mode",
+ diff_mode,
+ ]
+ if mode == "revisions":
+ git(repo, "commit", "-qm", "change")
+ arguments.extend(["--head", git(repo, "rev-parse", "HEAD")])
+ git(repo, "checkout", "-q", base)
+
+ run_cli(*arguments, "--out", str(output))
+
+ rows = read_jsonl(output)
+ assert [row["path"] for row in rows] == [f"include/{name}" for name in sorted(names)]
+ for row in rows:
+ assert row["area"] == ("include" if mode == "repo" else "diff")
+ assert "function after" in row["preview"]
+ assert "before" not in row["preview"]
+
+
@pytest.mark.parametrize("mode", ["repo", "revisions", "local-patch"])
def test_rank_input_includes_solidity(tmp_path: Path, mode: str) -> None:
repo = tmp_path / "repo"
diff --git a/plugins/codex-security/tests/test_rank_preview.py b/plugins/codex-security/tests/test_rank_preview.py
index 3d3726950..5d5b7bd01 100644
--- a/plugins/codex-security/tests/test_rank_preview.py
+++ b/plugins/codex-security/tests/test_rank_preview.py
@@ -398,6 +398,33 @@ def test_enterprise_language_previews_list_declarations(
assert "function widget" not in preview
[email protected]("suffix", [".h", ".hpp", ".hh", ".hxx", ".HH", ".HXX"])
+def test_cpp_headers_use_structural_previews(tmp_path: Path, suffix: str) -> None:
+ source = """typedef void Callback();
+Widget widget(options);
+template <typename T>
+class Box {
+public:
+ Box() {}
+ T get() const { return value; }
+};
+inline int answer() { return 42; }
+"""
+ path = tmp_path / f"box{suffix}"
+ preview = generate_preview(tmp_path, path.name, source)
+
+ assert "class Box" in preview
+ assert "method Box.Box" in preview
+ assert "method Box.get" in preview
+ assert "function answer" in preview
+ assert "function Callback" not in preview
+ assert "function widget" not in preview
+ assert preview_for_bytes(path, source.encode("utf-8"), DEFAULT_PREVIEW_BYTES) == (
+ preview,
+ False,
+ )
+
+
def test_expression_bodied_function_does_not_consume_next_type_body(tmp_path: Path) -> None:
source = """fun answer(): Int = 42
class Service {