Skip to content

Commit bc02da4

Browse files
fix: check the agent upload directory after path resolution (#4323)
## Changes being requested Compare the selected directory with the resolved directory before selecting files. Reject a change of identity and keep support for symlinks in parent paths. This check is limited to path resolution. ## Additional context & links Sync and async regression tests verify that a change during path resolution stops preparation before any upload. Both failed before the change. The existing parent alias tests also passed. The agent streaming suite had 383 passes and 14 skips. Ruff and ty passed for both files.
1 parent dd69803 commit bc02da4

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎src/openai/lib/beta/agents/_files.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from io import IOBase
44
from os import PathLike, fstat
5-
from stat import S_ISREG
5+
from stat import S_ISDIR, S_ISREG
66
from typing import TYPE_CHECKING, Mapping, BinaryIO, Sequence, Generator, cast
7+
from os.path import samestat
78
from pathlib import Path, PurePosixPath
89
from contextlib import ExitStack, contextmanager
910
from dataclasses import field, dataclass
@@ -118,10 +119,16 @@ def directory_files(root: str | PathLike[str], destination: str, include: Sequen
118119
directory = Path(root).absolute()
119120
if isinstance(include, str) or not include:
120121
raise ValueError("Select directory files with explicit include patterns")
121-
if directory.is_symlink() or not directory.is_dir():
122+
try:
123+
root_stat = directory.lstat()
124+
except (FileNotFoundError, NotADirectoryError):
125+
root_stat = None
126+
if root_stat is None or not S_ISDIR(root_stat.st_mode):
122127
raise ValueError("The selected directory must be a directory, not a symlink")
123-
directory = directory.resolve()
124128
# System aliases such as macOS /tmp are allowed above the chosen root.
129+
directory = directory.resolve()
130+
if not samestat(root_stat, directory.lstat()):
131+
raise ValueError("The selected directory changed during path resolution")
125132
_destination(destination + "/a") # Validate with the shortest possible filename.
126133
for pattern in include:
127134
if not pattern or PurePosixPath(pattern).is_absolute() or ".." in PurePosixPath(pattern).parts:

‎tests/lib/streaming/agents/test_files.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,28 @@ def scandir(path: Any) -> Any:
544544
assert server.uploads == 1
545545

546546

547+
async def test_directory_root_changed_during_resolution_prevents_upload(
548+
sdk: OpenAI | AsyncOpenAI, server: FilesServer, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
549+
) -> None:
550+
root = tmp_path / "chosen"
551+
root.mkdir()
552+
outside = tmp_path / "outside"
553+
outside.mkdir()
554+
(outside / "other.txt").write_text("outside")
555+
resolve = Path.resolve
556+
557+
def replace_root(path: Path, strict: bool = False) -> Path:
558+
if path == root:
559+
root.rename(tmp_path / "original")
560+
root.symlink_to(outside, target_is_directory=True)
561+
return resolve(path, strict=strict)
562+
563+
monkeypatch.setattr(Path, "resolve", replace_root)
564+
with pytest.raises(ValueError, match="directory changed"):
565+
await prepare_directory(sdk, root)
566+
assert not server.requests
567+
568+
547569
async def test_selected_directory_cannot_be_replaced_by_symlink(
548570
sdk: OpenAI | AsyncOpenAI, server: FilesServer, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
549571
) -> None:

0 commit comments

Comments
 (0)