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(deep-scan): reject directory trees emptied before dispatch (#2181)

Dipesh Babuc078ae5c5bbe
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_empty_target.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_empty_target.ts
index b80583cd3..2f436cce7 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_empty_target.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_empty_target.ts
@@ -32,6 +32,45 @@ function inspect(run: DeepScanRunState) {
after(() => temporaryDirectories.cleanup());
+test("a directory tree emptied after registration dispatches no workers", async () => {
+ const fixture = await fixtureRun();
+ await mkdir(join(fixture.run.targetPath, "first", "nested"), {
+ recursive: true,
+ });
+ await mkdir(join(fixture.run.targetPath, "second"));
+ const source = join(fixture.run.targetPath, "first", "nested", "source.py");
+ await writeFile(source, "");
+ // A registration-time inventory count remains positive after its file is removed.
+ Object.assign(fixture.run, { scopeFileCount: 1 });
+ await rm(source);
+ const executor = new FakeExecutor({ blockDiscoveryAfterCalls: 0 });
+ const coordinator = createCoordinator(
+ fixture,
+ new FakeStore(fixture.run),
+ executor,
+ {
+ checkFreshTarget: inspect,
+ },
+ );
+ coordinator.start();
+ try {
+ const outcome = await Promise.race([
+ coordinator.settled(),
+ executor.discoveryStarted.promise.then(() =>
+ assert.fail("Empty directory tree dispatched a worker"),
+ ),
+ ]);
+ assert.equal(outcome.status, "failed", outcome.error);
+ assert.match(outcome.error ?? "", /target is empty.*no security audit/iu);
+ assert.equal(executor.discoveryCalls, 0);
+ await writeFile(source, "");
+ await inspect(fixture.run);
+ } finally {
+ coordinator.cancel("fixture cleanup");
+ await coordinator.settled();
+ }
+});
+
test("only registered paths determine whether the target is empty", async () => {
const { run } = await fixtureRun();
run.includePaths = ["left", "right"];
@@ -61,7 +100,7 @@ test("only registered paths determine whether the target is empty", async () =>
);
await assert.rejects(inspect(run), /target is empty.*no security audit/iu);
- // Any entry counts, even if normal source inventory would ignore it.
+ // A file counts even when normal source inventory would ignore it.
await writeFile(join(right, ".gitignore"), "*");
await inspect(run);
run.includePaths.reverse();
@@ -165,14 +204,23 @@ test(
},
);
-for (const stop of ["cancellation", "deadline"]) {
+for (const [stop, nested] of [
+ ["cancellation", false],
+ ["deadline", false],
+ ["cancellation", true],
+ ["deadline", true],
+] as const) {
test(
- `a blocked directory read cannot delay ${stop}`,
+ `a blocked ${nested ? "nested " : ""}directory read cannot delay ${stop}`,
{ timeout: 15_000 },
async (t) => {
const fixture = await fixtureRun();
const { run } = fixture;
await mkdir(run.targetPath);
+ const blockedPath = nested
+ ? join(run.targetPath, "nested")
+ : run.targetPath;
+ if (nested) await mkdir(blockedPath);
const scripts = join(fixture.pluginRoot, "scripts");
await mkdir(scripts, { recursive: true });
const markerDir = join(dirname(run.targetPath), "ready");
@@ -187,7 +235,7 @@ import os, runpy, sys, time
from pathlib import Path
real_scan = os.scandir
def blocked(path):
- if os.fsdecode(path) == ${JSON.stringify(run.targetPath)}:
+ if os.fsdecode(path) == ${JSON.stringify(blockedPath)}:
Path(${JSON.stringify(markerDir)}, str(os.getpid())).touch()
time.sleep(30)
return real_scan(path)
diff --git a/plugins/codex-security/scripts/check_deep_scan_target.py b/plugins/codex-security/scripts/check_deep_scan_target.py
index ba4b58457..a49ce6e6e 100644
--- a/plugins/codex-security/scripts/check_deep_scan_target.py
+++ b/plugins/codex-security/scripts/check_deep_scan_target.py
@@ -1,4 +1,4 @@
-"""Check the physical contents of the targets registered for a fresh Deep Scan."""
+"""Reject registered Deep Scan targets containing only empty directories."""
from __future__ import annotations
@@ -45,16 +45,38 @@ def check_selected_targets(root: Path, selections: list[str], file_paths: list[s
for selection in selections:
check_path(selection)
+ def has_content(selection: str) -> bool:
+ pending = [selection]
+ failure = None
+ while pending:
+ current = pending.pop()
+ try:
+ target, regular = check_path(current)
+ if regular:
+ return True
+ with os.scandir(target) as entries:
+ for entry in entries:
+ if not entry.is_dir(follow_symlinks=False):
+ return True
+ # Preserve link entries as content without traversing their targets.
+ if (
+ getattr(entry.stat(follow_symlinks=False), "st_reparse_tag", 0)
+ & 0x20000000
+ ):
+ return True
+ pending.append(str(Path(entry.path).relative_to(root)))
+ except OSError as exc:
+ if failure is None:
+ failure = exc
+ if failure is not None:
+ raise failure
+ return False
+
failure = None
for selection in selections:
try:
- # A previously validated directory may have changed during another probe.
- target, regular = check_path(selection)
- if regular:
+ if has_content(selection):
return
- with os.scandir(target) as entries:
- if next(entries, None) is not None:
- return
except OSError as exc:
if selection in files:
raise
diff --git a/plugins/codex-security/tests/test_deep_scan_target.py b/plugins/codex-security/tests/test_deep_scan_target.py
new file mode 100644
index 000000000..6048ea77c
--- /dev/null
+++ b/plugins/codex-security/tests/test_deep_scan_target.py
@@ -0,0 +1,56 @@
+"""Exercise filesystem failures encountered below a selected Deep Scan directory."""
+
+from __future__ import annotations
+
+import errno
+import os
+import runpy
+from pathlib import Path
+
+import pytest
+
+
+def check_targets():
+ script = Path(__file__).resolve().parents[1] / "scripts" / "check_deep_scan_target.py"
+ return runpy.run_path(str(script))["check_selected_targets"]
+
+
+def test_nested_read_failure_is_preserved_unless_another_selection_has_content(
+ tmp_path, monkeypatch, check_targets
+):
+ root = tmp_path / "target"
+ denied = root / "empty" / "nested"
+ denied.mkdir(parents=True)
+ failure = PermissionError(errno.EACCES, "Synthetic denied directory", str(denied))
+ original = os.scandir
+
+ def scandir(path):
+ if Path(path) == denied:
+ raise failure
+ return original(path)
+
+ monkeypatch.setattr(os, "scandir", scandir)
+ with pytest.raises(PermissionError) as caught:
+ check_targets(root, ["empty"], [])
+ assert caught.value is failure
+ (root / "sibling.py").write_text("")
+ check_targets(root, ["empty", "sibling.py"], ["sibling.py"])
+
+
[email protected](os.name == "nt", reason="POSIX directory symlink fixture")
+def test_nested_directory_links_are_not_followed(tmp_path, monkeypatch, check_targets):
+ root = tmp_path / "target"
+ root.mkdir()
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (root / "link").symlink_to(outside, target_is_directory=True)
+ original = os.scandir
+
+ def scandir(path):
+ assert Path(path) != outside
+ return original(path)
+
+ monkeypatch.setattr(os, "scandir", scandir)
+ check_targets(root, ["."], [])
diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.py
index c0cf7123e..76c959597 100644
--- a/plugins/codex-security/tests/test_workbench_deep_scan.py
+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py
@@ -99,17 +99,21 @@ def start_empty_target_scan(tmp_path: Path, *, config: str | None = None) -> Dee
@pytest.mark.cross_platform
-def test_target_admission_reads_one_entry_without_looking_up_its_type(
+def test_target_admission_stops_at_the_first_non_directory_entry(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
script = Path(__file__).resolve().parents[1] / "scripts" / "check_deep_scan_target.py"
check_selected_targets = runpy.run_path(str(script))["check_selected_targets"]
events = []
+ class FileEntry:
+ def is_dir(self, *, follow_symlinks):
+ assert not follow_symlinks
+ return False
+
def entries():
events.append("read")
- # A filesystem can report DT_UNKNOWN. Any entry-type lookup here fails.
- yield object()
+ yield FileEntry()
pytest.fail("admission read a second directory entry")
@contextmanager