Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion tests/test_tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,58 @@ def test_download_tool_returns_error_when_destination_is_a_file(tm, tmp_path, mo

download_root = tmp_path / "downloads"
download_root.mkdir()
(download_root / "universal_mail_cleaner").write_text("blocked", encoding="utf-8")
sentinel = download_root / "universal_mail_cleaner"
sentinel.write_text("blocked", encoding="utf-8")
monkeypatch.setattr(tool_manager, "_DOWNLOAD_DIR", download_root)

network_calls = []

def fail_fast_urlopen(*args, **kwargs):
network_calls.append((args, kwargs))
raise AssertionError("unexpected network request for an invalid destination")

monkeypatch.setattr(tool_manager.urllib.request, "urlopen", fail_fast_urlopen)
err = tm.download_tool("universal_mail_cleaner")

assert err is not None
assert err.startswith("Download error: cannot prepare destination:")
assert network_calls == []
assert sentinel.read_text(encoding="utf-8") == "blocked"


def test_download_tool_returns_error_when_destination_stat_fails(tm, tmp_path, monkeypatch):
import tool_manager

download_root = tmp_path / "downloads"
download_root.mkdir()
dest_dir = download_root / "universal_mail_cleaner"
dest_dir.mkdir()
sentinel = dest_dir / "keep.txt"
sentinel.write_text("keep", encoding="utf-8")
monkeypatch.setattr(tool_manager, "_DOWNLOAD_DIR", download_root)

original_stat = Path.stat

def fail_destination_stat(path, *args, **kwargs):
if path == dest_dir:
raise PermissionError("synthetic destination stat failure")
return original_stat(path, *args, **kwargs)

monkeypatch.setattr(Path, "stat", fail_destination_stat)
network_calls = []

def fail_fast_urlopen(*args, **kwargs):
network_calls.append((args, kwargs))
raise AssertionError("unexpected network request after a destination stat failure")

monkeypatch.setattr(tool_manager.urllib.request, "urlopen", fail_fast_urlopen)
err = tm.download_tool("universal_mail_cleaner")

assert err is not None
assert err.startswith("Download error: cannot prepare destination:")
assert "synthetic destination stat failure" in err
assert network_calls == []
assert sentinel.read_text(encoding="utf-8") == "keep"


def test_download_tool_extracts_flat_archive(tm, tmp_path, monkeypatch):
Expand Down
18 changes: 17 additions & 1 deletion tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import re
import shutil
import stat
import subprocess
import sys
import urllib.request
Expand Down Expand Up @@ -302,6 +303,22 @@ def download_tool(self, tool_id: str,
return "No GitHub repo configured for this tool"
repo = meta["github_repo"]

# Validate the local destination before making any network request.
dest_dir = _DOWNLOAD_DIR / tool_id
for candidate in (dest_dir, *dest_dir.parents):
try:
destination_stat = candidate.stat()
except FileNotFoundError:
continue
except OSError as exc:
return f"Download error: cannot prepare destination: {exc}"
if not stat.S_ISDIR(destination_stat.st_mode):
return (
"Download error: cannot prepare destination: "
f"not a directory: {candidate}"
)
break

# Fetch release metadata
api_url = f"https://api.github.com/repos/{repo}/releases/latest"
try:
Expand All @@ -320,7 +337,6 @@ def download_tool(self, tool_id: str,
return "No zipball_url in release info"

# Prepare download directory
dest_dir = _DOWNLOAD_DIR / tool_id
try:
dest_dir.mkdir(parents=True, exist_ok=True)
except OSError as exc:
Expand Down
Loading