OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
test(plugin): split finalizer contracts below source size limit (#1761)
mluck-openai7d5d82f7d56d
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.pyindex 4874ac41a..f6af46085 100644--- a/plugins/codex-security/tests/test_finalize_scan_contract.py+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py@@ -3233,80 +3233,5 @@ The extraction root is not enforced.self.assertEqual(line_hashes.call_count, 1)[email protected](("expected", "value"), [("integer", 1), ("number", 1.5)])-def test_schema_numeric_types_do_not_accept_booleans(expected, value) -> None:- schema = {"type": expected}- FINALIZER._validate_schema_node(value, schema, "value")- with pytest.raises(FINALIZER.ContractError, match="expected schema type"):- FINALIZER._validate_schema_node(True, schema, "value")--[email protected](os.name == "nt", reason="POSIX symlinks and file permissions")- "kind", ["unsafe", "symlink", "dangling-symlink", "parent-symlink", "directory", "unreadable"]-)-def test_missing_writeup_recovery_rejects_other_file_errors(tmp_path, kind):- scan_dir = tmp_path / "scan"- scan_dir.mkdir()- report = scan_dir / "findings" / "report.md"- report.parent.mkdir()- outside = tmp_path / "outside.md"- outside.write_text("# Outside the scan\n")- report_path = "findings/report.md"- if kind == "unsafe":- report_path = "../outside.md"- elif kind == "symlink":- report.symlink_to(outside)- elif kind == "dangling-symlink":- report.symlink_to(report.parent / "missing.md")- elif kind == "parent-symlink":- report.parent.rmdir()- report.parent.symlink_to(tmp_path, target_is_directory=True)- elif kind == "directory":- report.mkdir()- else:- if os.geteuid() == 0:- pytest.skip("root can read files without permission bits")- report.write_text("# Unreadable writeup\n")- report.chmod(0)- findings = {"findings": [{"writeup": {"reportPath": report_path}}]}- original = copy.deepcopy(findings)- with pytest.raises(FINALIZER.ContractError):- FINALIZER._prepare_derived_writeups(scan_dir, findings, allow_missing=True)- assert findings == original--[email protected]_platform[email protected]("error_code", [2, 3, 5, 22, 32, 33])-def test_windows_writeup_recovery_only_omits_missing_files(tmp_path, monkeypatch, error_code):- backend = windows_file_backend()- windows_error = load_script("windows_scan_local_files").WindowsScanLocalFileError- backend.open_read_fd.side_effect = windows_error(error_code, "Synthetic Windows file error")- monkeypatch.setattr(FINALIZER.os, "supports_dir_fd", set())- monkeypatch.setattr(FINALIZER, "_is_windows", lambda: True)- monkeypatch.setattr(FINALIZER, "_windows_scan_local_files", lambda: backend)- findings = {"findings": [{"writeup": {"reportPath": "findings/report.md"}}]}- if error_code in {2, 3}:- warnings = FINALIZER._prepare_derived_writeups(tmp_path, findings, allow_missing=True)- assert findings == {"findings": [{}]}- assert len(warnings) == 1- else:- with pytest.raises(FINALIZER.ContractError):- FINALIZER._prepare_derived_writeups(tmp_path, findings, allow_missing=True)- assert "writeup" in findings["findings"][0]---def test_schema_references_preserve_constraints_siblings_and_cycle_errors() -> None:- root = {"$defs": {"text": {"type": "string", "minLength": 1}}}- reference = {"$ref": "#/$defs/text"}- FINALIZER._validate_schema_node("yes", reference, "value", root)- with pytest.raises(FINALIZER.ContractError, match="string is too short"):- FINALIZER._validate_schema_node("", reference, "value", root)- with pytest.raises(FINALIZER.ContractError, match="string does not match schema pattern"):- FINALIZER._validate_schema_node("no", {**reference, "pattern": "^yes$"}, "value", root)- with pytest.raises(RecursionError):- FINALIZER._validate_schema_node("yes", {"$ref": "#"}, "value")--if __name__ == "__main__":main()diff --git a/plugins/codex-security/tests/test_finalize_schema_validation.py b/plugins/codex-security/tests/test_finalize_schema_validation.pynew file mode 100644index 000000000..f1fe9c2a9--- /dev/null+++ b/plugins/codex-security/tests/test_finalize_schema_validation.py@@ -0,0 +1,26 @@+from __future__ import annotations++import pytest+from workbench_test_support import load_script++FINALIZER = load_script("finalize_scan_contract")++[email protected](("expected", "value"), [("integer", 1), ("number", 1.5)])+def test_schema_numeric_types_do_not_accept_booleans(expected, value) -> None:+ schema = {"type": expected}+ FINALIZER._validate_schema_node(value, schema, "value")+ with pytest.raises(FINALIZER.ContractError, match="expected schema type"):+ FINALIZER._validate_schema_node(True, schema, "value")+++def test_schema_references_preserve_constraints_siblings_and_cycle_errors() -> None:+ root = {"$defs": {"text": {"type": "string", "minLength": 1}}}+ reference = {"$ref": "#/$defs/text"}+ FINALIZER._validate_schema_node("yes", reference, "value", root)+ with pytest.raises(FINALIZER.ContractError, match="string is too short"):+ FINALIZER._validate_schema_node("", reference, "value", root)+ with pytest.raises(FINALIZER.ContractError, match="string does not match schema pattern"):+ FINALIZER._validate_schema_node("no", {**reference, "pattern": "^yes$"}, "value", root)+ with pytest.raises(RecursionError):+ FINALIZER._validate_schema_node("yes", {"$ref": "#"}, "value")diff --git a/plugins/codex-security/tests/test_finalize_writeup_recovery.py b/plugins/codex-security/tests/test_finalize_writeup_recovery.pynew file mode 100644index 000000000..a75f1f59c--- /dev/null+++ b/plugins/codex-security/tests/test_finalize_writeup_recovery.py@@ -0,0 +1,64 @@+from __future__ import annotations++import copy+import os++import pytest+from workbench_test_support import load_script, windows_file_backend++FINALIZER = load_script("finalize_scan_contract")++[email protected](os.name == "nt", reason="POSIX symlinks and file permissions")+ "kind", ["unsafe", "symlink", "dangling-symlink", "parent-symlink", "directory", "unreadable"]+)+def test_missing_writeup_recovery_rejects_other_file_errors(tmp_path, kind):+ scan_dir = tmp_path / "scan"+ scan_dir.mkdir()+ report = scan_dir / "findings" / "report.md"+ report.parent.mkdir()+ outside = tmp_path / "outside.md"+ outside.write_text("# Outside the scan\n")+ report_path = "findings/report.md"+ if kind == "unsafe":+ report_path = "../outside.md"+ elif kind == "symlink":+ report.symlink_to(outside)+ elif kind == "dangling-symlink":+ report.symlink_to(report.parent / "missing.md")+ elif kind == "parent-symlink":+ report.parent.rmdir()+ report.parent.symlink_to(tmp_path, target_is_directory=True)+ elif kind == "directory":+ report.mkdir()+ else:+ if os.geteuid() == 0:+ pytest.skip("root can read files without permission bits")+ report.write_text("# Unreadable writeup\n")+ report.chmod(0)+ findings = {"findings": [{"writeup": {"reportPath": report_path}}]}+ original = copy.deepcopy(findings)+ with pytest.raises(FINALIZER.ContractError):+ FINALIZER._prepare_derived_writeups(scan_dir, findings, allow_missing=True)+ assert findings == original++[email protected]_platform[email protected]("error_code", [2, 3, 5, 22, 32, 33])+def test_windows_writeup_recovery_only_omits_missing_files(tmp_path, monkeypatch, error_code):+ backend = windows_file_backend()+ windows_error = load_script("windows_scan_local_files").WindowsScanLocalFileError+ backend.open_read_fd.side_effect = windows_error(error_code, "Synthetic Windows file error")+ monkeypatch.setattr(FINALIZER.os, "supports_dir_fd", set())+ monkeypatch.setattr(FINALIZER, "_is_windows", lambda: True)+ monkeypatch.setattr(FINALIZER, "_windows_scan_local_files", lambda: backend)+ findings = {"findings": [{"writeup": {"reportPath": "findings/report.md"}}]}+ if error_code in {2, 3}:+ warnings = FINALIZER._prepare_derived_writeups(tmp_path, findings, allow_missing=True)+ assert findings == {"findings": [{}]}+ assert len(warnings) == 1+ else:+ with pytest.raises(FINALIZER.ContractError):+ FINALIZER._prepare_derived_writeups(tmp_path, findings, allow_missing=True)+ assert "writeup" in findings["findings"][0]