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): preserve declarations around C++ raw strings (#1279)

wjf1ff761ee5c9
diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.py
index 6b87179fd..086401d38 100644
--- a/plugins/codex-security/scripts/rank_preview.py
+++ b/plugins/codex-security/scripts/rank_preview.py
@@ -102,6 +102,7 @@ BRACE_LANGUAGE_EXTENSIONS = {
".swift",
}
NESTED_BLOCK_COMMENT_EXTENSIONS = {".kt", ".kts", ".rs", ".scala", ".swift"}
+CPP_RAW_STRING_RE = re.compile(r'(?<![\w\x80-\U0010ffff])(?:u8|u|U|L)?R"([^\s()\\]{0,16})\(')
RUST_RAW_STRING_RE = re.compile(r'(?:br|r)(#{0,16})"')
RUST_LIFETIME_RE = re.compile(r"'[A-Za-z_][A-Za-z0-9_]*")
PHP_HEREDOC_RE = re.compile(r"<<<\s*['\"]?([A-Za-z_]\w*)['\"]?")
@@ -357,6 +358,14 @@ def mask_c_style_source(text: str, suffix: str) -> str:
quote = ""
index += 1
continue
+ if suffix in {".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm"}:
+ raw_match = CPP_RAW_STRING_RE.match(text, index)
+ if raw_match:
+ token = raw_match.group(0)
+ masked.extend(" " * len(token))
+ raw_terminator = f'){raw_match.group(1)}"'
+ index += len(token)
+ continue
if suffix == ".rs":
raw_match = RUST_RAW_STRING_RE.match(text, index)
if raw_match:
diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.py
index 019663da3..fdcf39a6f 100644
--- a/plugins/codex-security/tests/test_generate_rank_input.py
+++ b/plugins/codex-security/tests/test_generate_rank_input.py
@@ -173,6 +173,25 @@ def test_make_repo_rank_input_matches_golden_and_filters_noise(tmp_path: Path) -
)
+def test_make_repo_rank_input_keeps_declarations_after_cpp_raw_strings(tmp_path: Path) -> None:
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ (repo / "sample.cpp").write_text(
+ """void before() {}
+const char* text = R"tag("{)tag";
+void after() {}
+""",
+ encoding="utf-8",
+ )
+ output = tmp_path / "rank_input.jsonl"
+
+ run_cli("make-repo-rank-input", "--repo", str(repo), "--out", str(output))
+
+ rows = read_jsonl(output)
+ assert [row["path"] for row in rows] == ["sample.cpp"]
+ assert rows[0]["preview"] == "function before\nfunction after"
+
+
def test_make_repo_rank_input_keeps_python_with_ast_recursion(tmp_path: Path) -> None:
repo = tmp_path / "repo"
(repo / "src").mkdir(parents=True)
diff --git a/plugins/codex-security/tests/test_rank_preview.py b/plugins/codex-security/tests/test_rank_preview.py
index b1856c495..4eb740368 100644
--- a/plugins/codex-security/tests/test_rank_preview.py
+++ b/plugins/codex-security/tests/test_rank_preview.py
@@ -425,6 +425,60 @@ inline int answer() { return 42; }
)
+ "suffix", [".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx", ".mm", ".CPP"]
+)
+def test_cpp_raw_string_does_not_hide_following_declarations(tmp_path: Path, suffix: str) -> None:
+ source = """void before() {}
+const char* text = R"tag("{)tag";
+class Service {
+public:
+ const char* value() { return R"("{)"; }
+ void visible() {}
+};
+void after() {}
+"""
+
+ preview = generate_preview(tmp_path, f"sample{suffix}", source)
+
+ assert preview.splitlines() == [
+ "function before",
+ "class Service",
+ "method Service.value",
+ "method Service.visible",
+ "function after",
+ ]
+
+
[email protected]("prefix", ["", "u8", "u", "U", "L"])
[email protected]("delimiter", ["", "tag", "abcdefghijklmnop"])
+def test_cpp_raw_string_body_is_not_code(tmp_path: Path, prefix: str, delimiter: str) -> None:
+ source = f'''void before() {{}}
+const auto text = {prefix}R"{delimiter}(
+void fake() {{}}
+" {{ /*
+){delimiter}";
+void after() {{}}
+'''
+
+ preview = generate_preview(tmp_path, "sample.cpp", source)
+
+ assert preview.splitlines() == ["function before", "function after"]
+
+
[email protected]("macro", ["ERROR", "FORMAT_u8R", "x\u0301R"])
+def test_cpp_macro_before_string_is_not_a_raw_string(tmp_path: Path, macro: str) -> None:
+ source = f"""#define {macro} "error: "
+void before() {{}}
+const char* text = {macro}"(connection failed";
+void after() {{}}
+"""
+
+ preview = generate_preview(tmp_path, "sample.cpp", source)
+
+ assert preview.splitlines() == ["function before", "function after"]
+
+
def test_expression_bodied_function_does_not_consume_next_type_body(tmp_path: Path) -> None:
source = """fun answer(): Int = 42
class Service {