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 legacy document filename spelling (#1283)

mldangelo-oai42a0a6bf17dd
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index 574bd129c..51259b0a4 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -267,7 +267,13 @@ def _read_saved_threat_model(
path = scan_dir / filename
if not path.exists() and not path.is_symlink():
continue
- descriptor = open_scan_local_file_descriptor(scan_dir, filename, "Saved threat model")
+ descriptor, filename = _open_scan_local_file_with_path(
+ scan_dir,
+ filename,
+ "Saved threat model",
+ resolve_spelling=sealed_artifact_paths is None,
+ )
+ path = scan_dir / filename
with os.fdopen(descriptor, "rb") as handle:
contents = handle.read()
try:
@@ -584,13 +590,22 @@ def _open_scan_local_directory(root_fd: int, parts: tuple[str, ...], *, create:
def open_scan_local_file_descriptor(scan_dir: Path, relative_path: str, context: str) -> int:
+ return _open_scan_local_file_with_path(scan_dir, relative_path, context)[0]
+
+
+def _open_scan_local_file_with_path(
+ scan_dir: Path, relative_path: str, context: str, *, resolve_spelling: bool = False
+) -> tuple[int, str]:
scan_dir = _require_scan_directory(scan_dir)
relative_path = _require_portable_relative_path(relative_path, context)
if not (os.open in os.supports_dir_fd and hasattr(os, "O_NOFOLLOW")):
if not _is_windows():
raise ContractError("scan-local input requires descriptor-relative file operations")
try:
- return _windows_scan_local_files().open_read_fd(scan_dir, relative_path, context)
+ backend = _windows_scan_local_files()
+ if resolve_spelling:
+ return backend.open_read_fd_with_path(scan_dir, relative_path, context)
+ return backend.open_read_fd(scan_dir, relative_path, context), relative_path
except OSError as exc:
raise ContractError(str(exc)) from exc
root_fd: int | None = None
@@ -622,9 +637,22 @@ def open_scan_local_file_descriptor(scan_dir: Path, relative_path: str, context:
metadata = os.fstat(descriptor)
if not stat.S_ISREG(metadata.st_mode):
raise ContractError(f"{context}: expected a regular non-symlink file")
+ if resolve_spelling:
+ names = os.listdir(parent_fd)
+ if parts[-1] not in names:
+ for name in names:
+ if name.casefold() != parts[-1].casefold():
+ continue
+ try:
+ entry = os.stat(name, dir_fd=parent_fd, follow_symlinks=False)
+ except FileNotFoundError:
+ continue
+ if os.path.samestat(metadata, entry):
+ relative_path = PurePosixPath(relative_path).with_name(name).as_posix()
+ break
result = descriptor
descriptor = None
- return result
+ return result, relative_path
finally:
if descriptor is not None:
os.close(descriptor)
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.py
index 3375178ef..6ffac25d3 100644
--- a/plugins/codex-security/scripts/windows_scan_local_files.py
+++ b/plugins/codex-security/scripts/windows_scan_local_files.py
@@ -453,6 +453,12 @@ def scan_root_identity(scan_dir: Path) -> tuple[Path, tuple[int, int]]:
def open_read_fd(scan_dir: Path, relative_path: str, context: str) -> int:
"""Open a verified regular file and return an owned binary read descriptor."""
+ return open_read_fd_with_path(scan_dir, relative_path, context)[0]
+
+
+def open_read_fd_with_path(scan_dir: Path, relative_path: str, context: str) -> tuple[int, str]:
+ """Return the descriptor and its filename while the verified parents are held."""
+
try:
with _locked_parent(scan_dir, relative_path, create=False) as (parent_path, leaf_name):
path = parent_path / leaf_name
@@ -467,10 +473,13 @@ def open_read_fd(scan_dir: Path, relative_path: str, context: str) -> int:
assert handle is not None and handle.value is not None
with handle:
_verify_regular_file(handle.value, path)
+ filename = Path(_final_path(handle.value)).name
+ resolved_path = PurePosixPath(relative_path).with_name(filename).as_posix()
raw_handle = handle.detach()
try:
assert _msvcrt is not None
- return _msvcrt.open_osfhandle(raw_handle, os.O_RDONLY | os.O_BINARY)
+ descriptor = _msvcrt.open_osfhandle(raw_handle, os.O_RDONLY | os.O_BINARY)
+ return descriptor, resolved_path
except BaseException:
_close_handle(raw_handle)
raise
diff --git a/plugins/codex-security/tests/test_threat_model_projection.py b/plugins/codex-security/tests/test_threat_model_projection.py
index 0b2a9ae6a..d30428018 100644
--- a/plugins/codex-security/tests/test_threat_model_projection.py
+++ b/plugins/codex-security/tests/test_threat_model_projection.py
@@ -442,7 +442,52 @@ class ThreatModelProjectionTest(unittest.TestCase):
path.write_text(body)
self.assertEqual(FINALIZER.build_threat_model_export(source), body.encode())
self.assertEqual(path.read_text(), body)
- self.assertEqual(FINALIZER.describe_threat_model(source)["path"], str(path))
+ description = FINALIZER.describe_threat_model(source)
+ self.assertEqual(description["path"], str(path))
+ self.assertEqual(description["provenance"]["source"], filename)
+
+ @unittest.skipUnless(
+ FINALIZER._descriptor_relative_writes_available(), "requires descriptor-relative reads"
+ )
+ def test_legacy_document_spelling_stays_on_open_parent_after_swap(self) -> None:
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory).resolve()
+ source = root / "scan"
+ parent = source / "artifacts" / "01_context"
+ parent.mkdir(parents=True)
+ body = "# Original model\n"
+ (parent / "threat_model.md").write_text(body)
+ outside = root / "outside"
+ outside.mkdir()
+ (outside / "threat_model.md").write_text("# Replacement model\n")
+ open_directory = FINALIZER._open_scan_local_directory
+ iterdir = Path.iterdir
+ outside_lookups = []
+
+ def open_then_swap(root_fd, parts, *, create):
+ descriptor = open_directory(root_fd, parts, create=create)
+ parent.rename(parent.with_name("original"))
+ parent.symlink_to(outside, target_is_directory=True)
+ return descriptor
+
+ def record_listing(path):
+ if path.resolve() == outside:
+ outside_lookups.append(path)
+ return iterdir(path)
+
+ with (
+ unittest.mock.patch.object(
+ FINALIZER, "_open_scan_local_directory", side_effect=open_then_swap
+ ),
+ unittest.mock.patch.object(Path, "iterdir", record_listing),
+ ):
+ description = FINALIZER.describe_threat_model(source)
+
+ self.assertEqual(description["threatModel"]["content"], body)
+ self.assertEqual(
+ description["provenance"]["source"], "artifacts/01_context/threat_model.md"
+ )
+ self.assertEqual(outside_lookups, [])
def test_malformed_canonical_model_does_not_export_a_stale_legacy_document(self) -> None:
body = "# Earlier model\n\nEarlier service boundaries.\n"
diff --git a/plugins/codex-security/tests/test_windows_scan_local_files.py b/plugins/codex-security/tests/test_windows_scan_local_files.py
index e52ee9257..7fde5aa2b 100644
--- a/plugins/codex-security/tests/test_windows_scan_local_files.py
+++ b/plugins/codex-security/tests/test_windows_scan_local_files.py
@@ -46,6 +46,13 @@ def test_native_windows_backend_writes_reads_replaces_and_deletes(tmp_path: Path
with os.fdopen(descriptor, "rb") as handle:
assert handle.read() == b"first"
+ descriptor, filename = WINDOWS_FILES.open_read_fd_with_path(
+ scan_dir, "exports/RESULTS.sarif", "native Windows spelling test"
+ )
+ with os.fdopen(descriptor, "rb") as handle:
+ assert handle.read() == b"first"
+ assert filename == "exports/results.sarif"
+
WINDOWS_FILES.atomic_write(scan_dir, "exports/results.sarif", b"replacement")
assert (scan_dir / "exports" / "results.sarif").read_bytes() == b"replacement"