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

ci(plugin): enforce source import compatibility (#722)

soyeon-oai047414649cc1
diff --git a/.github/scripts/check_plugin_source_compatibility.py b/.github/scripts/check_plugin_source_compatibility.py
new file mode 100644
index 000000000..4c4def985
--- /dev/null
+++ b/.github/scripts/check_plugin_source_compatibility.py
@@ -0,0 +1,152 @@
+#!/usr/bin/env python3
+"""Check that tracked plugin source stays portable across repository imports."""
+
+from __future__ import annotations
+
+import argparse
+import re
+import stat
+import subprocess
+import sys
+from pathlib import Path
+
+MAX_SOURCE_FILE_BYTES = 150_000
+MAX_DEPENDENCY_LOCK_BYTES = 2_000_000
+DEPENDENCY_LOCK_NAMES = {
+ "Cargo.lock",
+ "package-lock.json",
+ "pnpm-lock.yaml",
+ "requirements.txt",
+ "uv.lock",
+ "yarn.lock",
+}
+LIST_ITEM = re.compile(r"^\s*(?:[-*+]|\d+[.)])\s+")
+HTML_BLOCK = re.compile(r"^\s*</?[A-Za-z][^>]*>\s*$")
+NATURAL_LINE_ENDINGS = tuple(".?!:;。!?:;)]}'\"`>")
+
+
+def tracked_files(plugin_root: Path) -> list[Path]:
+ result = subprocess.run(
+ ["git", "-C", str(plugin_root), "ls-files", "-z", "--", "."],
+ check=False,
+ capture_output=True,
+ )
+ if result.returncode != 0:
+ detail = result.stderr.decode(errors="replace").strip()
+ raise RuntimeError(detail or "git ls-files failed")
+ return [Path(value.decode()) for value in sorted(result.stdout.split(b"\0")) if value]
+
+
+def is_markdown_structure(line: str) -> bool:
+ stripped = line.strip()
+ return (
+ not stripped
+ or stripped.startswith(("#", ">", "|", "<!--", "::", "```", "~~~"))
+ or stripped in {"---", "***", "___"}
+ or HTML_BLOCK.match(stripped) is not None
+ or line.startswith((" ", "\t"))
+ )
+
+
+def line_ends_naturally(line: str) -> bool:
+ stripped = line.rstrip()
+ return (
+ line.endswith(" ")
+ or stripped.endswith(("\\", *NATURAL_LINE_ENDINGS))
+ or re.search(r"https?://\S+$", stripped) is not None
+ )
+
+
+def hard_wrapped_lines(content: str) -> list[int]:
+ lines = content.splitlines()
+ offenders: list[int] = []
+ in_fence = False
+ in_frontmatter = content.startswith("---\n")
+
+ for line_number, line in enumerate(lines[:-1], start=1):
+ stripped = line.strip()
+ if stripped.startswith(("```", "~~~")):
+ in_fence = not in_fence
+ continue
+ if line_number > 1 and in_frontmatter and stripped == "---":
+ in_frontmatter = False
+ continue
+ if in_fence or in_frontmatter:
+ continue
+
+ following_line = lines[line_number]
+ following_stripped = following_line.strip()
+ if is_markdown_structure(line) or is_markdown_structure(following_line):
+ continue
+ if LIST_ITEM.match(following_line) or line_ends_naturally(line):
+ continue
+ if re.search(r"[A-Za-z0-9`]$", stripped) is None:
+ continue
+ if re.match(r"[A-Za-z0-9`(]", following_stripped) is None:
+ continue
+ offenders.append(line_number)
+
+ return offenders
+
+
+def source_compatibility_errors(plugin_root: Path) -> list[str]:
+ errors: list[str] = []
+ for relative_path in tracked_files(plugin_root):
+ path = plugin_root / relative_path
+ if not stat.S_ISREG(path.lstat().st_mode):
+ continue
+
+ maximum = (
+ MAX_DEPENDENCY_LOCK_BYTES
+ if relative_path.name in DEPENDENCY_LOCK_NAMES
+ else MAX_SOURCE_FILE_BYTES
+ )
+ size = path.stat().st_size
+ if size > maximum:
+ errors.append(
+ f"{relative_path.as_posix()}: file is {size} bytes; maximum is {maximum} bytes"
+ )
+
+ if relative_path.suffix.lower() != ".md":
+ continue
+ for line_number in hard_wrapped_lines(path.read_text(encoding="utf-8")):
+ errors.append(
+ f"{relative_path.as_posix()}:{line_number}: prose is hard-wrapped "
+ "mid-sentence; use a natural Markdown line"
+ )
+
+ return sorted(errors)
+
+
+def parse_args() -> argparse.Namespace:
+ parser = argparse.ArgumentParser(
+ description="Check tracked plugin source for deterministic import compatibility."
+ )
+ parser.add_argument(
+ "--plugin-root",
+ type=Path,
+ default=Path(__file__).resolve().parents[2] / "plugins" / "codex-security",
+ help="plugin source root (default: plugins/codex-security in this repository)",
+ )
+ return parser.parse_args()
+
+
+def main() -> int:
+ args = parse_args()
+ plugin_root = args.plugin_root.resolve()
+ try:
+ errors = source_compatibility_errors(plugin_root)
+ except (OSError, RuntimeError, UnicodeError) as exc:
+ print(f"source compatibility check failed: {exc}", file=sys.stderr)
+ return 2
+
+ if errors:
+ print("\n".join(errors), file=sys.stderr)
+ return 1
+
+ print("Plugin source compatibility checks passed.")
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/.github/scripts/test_check_plugin_source_compatibility.py b/.github/scripts/test_check_plugin_source_compatibility.py
new file mode 100644
index 000000000..b0cd1093c
--- /dev/null
+++ b/.github/scripts/test_check_plugin_source_compatibility.py
@@ -0,0 +1,135 @@
+from __future__ import annotations
+
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+CHECKER = Path(__file__).with_name("check_plugin_source_compatibility.py")
+
+
+def initialize_repository(root: Path) -> None:
+ subprocess.run(["git", "init", "--quiet", str(root)], check=True)
+
+
+def track(root: Path, *paths: str) -> None:
+ subprocess.run(["git", "-C", str(root), "add", "--", *paths], check=True)
+
+
+def run_checker(root: Path, *args: str) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(
+ [sys.executable, str(CHECKER), "--plugin-root", str(root), *args],
+ check=False,
+ capture_output=True,
+ text=True,
+ )
+
+
+def test_reports_tracked_source_violations_in_stable_order(tmp_path: Path) -> None:
+ (tmp_path / "notes.md").write_text(
+ "This prose continues in the middle of a sentence\nonto another source line.\n",
+ encoding="utf-8",
+ )
+ (tmp_path / "oversized.py").write_bytes(b"x" * 150_001)
+ initialize_repository(tmp_path)
+ track(tmp_path, "oversized.py", "notes.md")
+
+ result = run_checker(tmp_path)
+
+ assert result.returncode == 1
+ assert result.stdout == ""
+ assert result.stderr.splitlines() == [
+ "notes.md:1: prose is hard-wrapped mid-sentence; use a natural Markdown line",
+ "oversized.py: file is 150001 bytes; maximum is 150000 bytes",
+ ]
+
+
+def test_accepts_valid_source_and_ignores_untracked_files(tmp_path: Path) -> None:
+ (tmp_path / "README.md").write_text("A complete sentence.\n", encoding="utf-8")
+ (tmp_path / "package-lock.json").write_bytes(b"x" * 150_001)
+ (tmp_path / "untracked.md").write_text(
+ "This untracked prose continues\nonto another source line.\n",
+ encoding="utf-8",
+ )
+ initialize_repository(tmp_path)
+ track(tmp_path, "README.md", "package-lock.json")
+
+ result = run_checker(tmp_path)
+
+ assert result.returncode == 0, result.stderr
+ assert result.stdout == "Plugin source compatibility checks passed.\n"
+ assert result.stderr == ""
+
+
+def test_accepts_prose_after_an_opening_thematic_break(tmp_path: Path) -> None:
+ (tmp_path / "README.md").write_text(
+ """---
+This prose continues
+onto another source line.
+""",
+ encoding="utf-8",
+ )
+ initialize_repository(tmp_path)
+ track(tmp_path, "README.md")
+
+ result = run_checker(tmp_path)
+
+ assert result.returncode == 0, result.stderr
+
+
+ "content",
+ [
+ "First clause,\ncontinues here.\n",
+ "First clause\n**continues** here.\n",
+ ],
+)
+def test_accepts_wraps_adjacent_to_inline_markup(tmp_path: Path, content: str) -> None:
+ (tmp_path / "README.md").write_text(content, encoding="utf-8")
+ initialize_repository(tmp_path)
+ track(tmp_path, "README.md")
+
+ result = run_checker(tmp_path)
+
+ assert result.returncode == 0, result.stderr
+
+
+def test_rejects_dependency_lock_files_above_two_megabytes(tmp_path: Path) -> None:
+ (tmp_path / "pnpm-lock.yaml").write_bytes(b"x" * 2_000_001)
+ initialize_repository(tmp_path)
+ track(tmp_path, "pnpm-lock.yaml")
+
+ result = run_checker(tmp_path)
+
+ assert result.returncode == 1
+ assert result.stderr == ("pnpm-lock.yaml: file is 2000001 bytes; maximum is 2000000 bytes\n")
+
+
[email protected](os.name == "nt", reason="creating symlinks requires elevated Windows access")
+def test_does_not_follow_tracked_symlinks_outside_the_plugin(tmp_path: Path) -> None:
+ outside = tmp_path.parent / f"{tmp_path.name}-outside.md"
+ outside.write_text(
+ "This outside prose continues\nonto another source line.\n",
+ encoding="utf-8",
+ )
+ (tmp_path / "linked.md").symlink_to(outside)
+ initialize_repository(tmp_path)
+ track(tmp_path, "linked.md")
+
+ result = run_checker(tmp_path)
+
+ assert result.returncode == 0, result.stderr
+
+
+def test_help_describes_the_source_contract() -> None:
+ result = subprocess.run(
+ [sys.executable, str(CHECKER), "--help"],
+ check=False,
+ capture_output=True,
+ text=True,
+ )
+
+ assert result.returncode == 0, result.stderr
+ assert "tracked plugin source" in result.stdout
diff --git a/.github/workflows/node-ci.yml b/.github/workflows/node-ci.yml
index 952bc2f27..0bf017df4 100644
--- a/.github/workflows/node-ci.yml
+++ b/.github/workflows/node-ci.yml
@@ -73,6 +73,11 @@ jobs:
exit 1
fi
+ - name: Check plugin source compatibility
+ if: steps.scope.outputs.ci-mode == 'markdown'
+ run: |
+ python .github/scripts/check_plugin_source_compatibility.py
+
- name: Set up pnpm
if: steps.scope.outputs.ci-mode == 'markdown'
uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9
@@ -245,8 +250,14 @@ jobs:
sudo apt-get update
sudo apt-get install --yes ripgrep
+ - name: Check plugin source compatibility
+ run: |
+ python -m ruff check --config plugins/codex-security/pyproject.toml .github/scripts/check_plugin_source_compatibility.py .github/scripts/test_check_plugin_source_compatibility.py plugins/codex-security
+ python -m ruff format --check --config plugins/codex-security/pyproject.toml .github/scripts/check_plugin_source_compatibility.py .github/scripts/test_check_plugin_source_compatibility.py plugins/codex-security
+ python .github/scripts/check_plugin_source_compatibility.py
+
- name: Test Python source contracts
- run: python -m pytest plugins/codex-security/tests -q
+ run: python -m pytest .github/scripts/test_check_plugin_source_compatibility.py plugins/codex-security/tests -q
- name: Set up Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
diff --git a/AGENTS.md b/AGENTS.md
index 8ed99d8c4..a395c4c4a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -14,6 +14,15 @@ Codex Security is a thin wrapper around Codex and its security plugin.
only after checking that it is public. If you cannot confirm its visibility,
leave it out.
+When changing `plugins/codex-security`, run its portable source checks before
+submitting the change:
+
+```bash
+python -m ruff check --config plugins/codex-security/pyproject.toml .github/scripts/check_plugin_source_compatibility.py .github/scripts/test_check_plugin_source_compatibility.py plugins/codex-security
+python -m ruff format --check --config plugins/codex-security/pyproject.toml .github/scripts/check_plugin_source_compatibility.py .github/scripts/test_check_plugin_source_compatibility.py plugins/codex-security
+python .github/scripts/check_plugin_source_compatibility.py
+```
+
## Avoid speculative defenses
- Do not add sanitization, redaction, validation, or fallback logic for
diff --git a/plugins/codex-security/pyproject.toml b/plugins/codex-security/pyproject.toml
index 4b0727716..85f487962 100644
--- a/plugins/codex-security/pyproject.toml
+++ b/plugins/codex-security/pyproject.toml
@@ -9,11 +9,20 @@ test = [
"packaging==26.3",
"pytest==9.0.3",
"PyYAML==6.0.3",
+ "ruff==0.16.1",
]
[tool.pytest.ini_options]
testpaths = ["tests"]
+[tool.ruff]
+line-length = 100
+required-version = "==0.16.1"
+target-version = "py312"
+
+[tool.ruff.lint]
+select = ["I"]
+
[tool.setuptools.packages.find]
include = []
diff --git a/sdk/typescript/tests-ts/release-automation.test.ts b/sdk/typescript/tests-ts/release-automation.test.ts
index b5943716e..012ac4459 100644
--- a/sdk/typescript/tests-ts/release-automation.test.ts
+++ b/sdk/typescript/tests-ts/release-automation.test.ts
@@ -3952,6 +3952,7 @@ describe("GitHub release workflow safeguards", () => {
"Set up Node.js",
"Install dependencies",
"Check Markdown formatting",
+ "Check plugin source compatibility",
]) {
expect(validationSteps.find(({ name }) => name === stepName)?.if).toBe(
"steps.scope.outputs.ci-mode == 'markdown'",
@@ -4091,6 +4092,57 @@ describe("GitHub release workflow safeguards", () => {
},
);
+ test.skipIf(process.platform === "win32")(
+ "rejects oversized plugin Markdown in reduced CI",
+ () => {
+ const workspace = mkdtempSync(
+ join(tmpdir(), "release-ci-plugin-source-"),
+ );
+ const pluginRoot = join(workspace, "plugins", "codex-security");
+ const scripts = join(workspace, ".github", "scripts");
+ mkdirSync(scripts, { recursive: true });
+ mkdirSync(pluginRoot, { recursive: true });
+ writeFileSync(
+ join(scripts, "check_plugin_source_compatibility.py"),
+ readFileSync(
+ new URL(
+ "../../../.github/scripts/check_plugin_source_compatibility.py",
+ import.meta.url,
+ ),
+ ),
+ );
+ writeFileSync(join(pluginRoot, "README.md"), "x".repeat(150_001));
+ spawnSync("git", ["init", "--quiet", workspace]);
+ spawnSync("git", [
+ "-C",
+ workspace,
+ "add",
+ "--",
+ ".github/scripts/check_plugin_source_compatibility.py",
+ "plugins/codex-security/README.md",
+ ]);
+ try {
+ const result = spawnSync(
+ bash,
+ [
+ "-c",
+ workflowStepShell(
+ nodeCiWorkflow,
+ "Check plugin source compatibility",
+ ),
+ ],
+ { cwd: workspace, encoding: "utf8" },
+ );
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain(
+ "README.md: file is 150001 bytes; maximum is 150000 bytes",
+ );
+ } finally {
+ rmSync(workspace, { recursive: true, force: true });
+ }
+ },
+ );
+
test.skipIf(process.platform === "win32")(
"formats every changed regular non-symlink Markdown file",
() => {