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): close C# raw strings on the matching quote count (#1346)

ilaigoldf88b7f578cdf
diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.py
index f174c67ba..2f1b989bc 100644
--- a/plugins/codex-security/scripts/rank_preview.py
+++ b/plugins/codex-security/scripts/rank_preview.py
@@ -110,6 +110,7 @@ BRACE_LANGUAGE_EXTENSIONS = {
}
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})\(')
+CSHARP_RAW_STRING_RE = re.compile(r'"{3,}')
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*)['\"]?")
@@ -370,6 +371,14 @@ def mask_c_style_source(text: str, suffix: str) -> str | None:
quote = '@"'
index += 2
continue
+ if suffix == ".cs":
+ raw_match = CSHARP_RAW_STRING_RE.match(text, index)
+ if raw_match:
+ token = raw_match.group(0)
+ masked.extend(" " * len(token))
+ raw_terminator = token
+ index += len(token)
+ continue
if suffix == ".go" and char == "`":
masked.append(" ")
raw_terminator = "`"
diff --git a/plugins/codex-security/tests/test_rank_preview.py b/plugins/codex-security/tests/test_rank_preview.py
index d21577259..f0505d0d2 100644
--- a/plugins/codex-security/tests/test_rank_preview.py
+++ b/plugins/codex-security/tests/test_rank_preview.py
@@ -803,6 +803,34 @@ func After() {}
assert "function After" in preview
+ "literal",
+ [
+ '"""\n { "a": "b" }\n """',
+ '""""\n { "a": """ }\n """"',
+ '""""" contains """" and { """""',
+ ],
+ ids=["three-quotes", "four-quotes", "five-quotes"],
+)
+def test_csharp_raw_string_closes_on_matching_quote_count(tmp_path: Path, literal: str) -> None:
+ source = f"""class Before {{}}
+class Service {{
+ string Text = {literal};
+ void Visible() {{}}
+}}
+class After {{}}
+"""
+
+ preview = generate_preview(tmp_path, "Service.cs", source)
+
+ assert preview.splitlines() == [
+ "class Before",
+ "class Service",
+ "method Service.Visible",
+ "class After",
+ ]
+
+
def test_malformed_python_uses_sampled_source_fallback(tmp_path: Path) -> None:
source = """import package
broken = (