AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/workspace): handle long UTF-8 upload names safely (#15653)
Nicholas Tindle2dedfa53762a
diff --git a/autogpt_platform/backend/backend/api/features/workspace/routes.py b/autogpt_platform/backend/backend/api/features/workspace/routes.pyindex 681b207bc3..c8ebba0263 100644--- a/autogpt_platform/backend/backend/api/features/workspace/routes.py+++ b/autogpt_platform/backend/backend/api/features/workspace/routes.py@@ -389,6 +389,11 @@ async def upload_file(raise fastapi.HTTPException(status_code=400, detail=str(e)) from eexcept VirusScanError as e:raise fastapi.HTTPException(status_code=500, detail=str(e)) from e+ except OSError as e:+ logger.exception("Failed to store uploaded workspace file")+ raise fastapi.HTTPException(+ status_code=500, detail="Unable to store file. Please try again."+ ) from eexcept ValueError as e:# write_file raises ValueError for path-conflict, size-limit, and# storage-quota cases; map each to its correct HTTP status.diff --git a/autogpt_platform/backend/backend/api/features/workspace/upload_filename_test.py b/autogpt_platform/backend/backend/api/features/workspace/upload_filename_test.pynew file mode 100644index 0000000000..10c942465b--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/workspace/upload_filename_test.py@@ -0,0 +1,119 @@+import io+from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock++import fastapi+import fastapi.testclient+import pytest+from autogpt_libs.auth.jwt_utils import get_jwt_payload++from backend.api.features.workspace.routes import router+from backend.data.workspace import Workspace, WorkspaceFile+from backend.util.workspace_storage import LocalWorkspaceStorage+++def client(mock_jwt_user):+ app = fastapi.FastAPI()+ app.include_router(router)+ app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]+ return fastapi.testclient.TestClient(app)+++def workspace(mocker):+ now = datetime.now(timezone.utc)+ workspace = Workspace(+ id="ws-001", user_id="test-user-id", created_at=now, updated_at=now+ )+ mocker.patch(+ "backend.api.features.workspace.routes.get_or_create_workspace",+ return_value=workspace,+ )+ mocker.patch(+ "backend.api.features.workspace.routes.get_workspace_total_size", return_value=0+ )+ mocker.patch(+ "backend.api.features.workspace.routes.get_workspace_storage_limit_bytes",+ return_value=250 * 1024 * 1024,+ )+ return workspace+++def test_upload_multibyte_name_preserves_metadata_and_content(+ client, workspace, mocker, tmp_path+):+ filename = "夜" * 86 + ".txt"+ storage = LocalWorkspaceStorage(base_dir=str(tmp_path))+ mocker.patch("backend.util.workspace.get_workspace_storage", return_value=storage)+ mocker.patch(+ "backend.util.workspace.get_workspace_storage_limit_bytes",+ return_value=250 * 1024 * 1024,+ )+ mocker.patch("backend.util.workspace.scan_content_safe", new=AsyncMock())+ db = MagicMock()+ db.get_workspace_total_size = AsyncMock(return_value=0)+ db.get_workspace_file_by_path = AsyncMock(return_value=None)++ async def create_file(**kwargs):+ return WorkspaceFile(+ id=kwargs["file_id"],+ workspace_id=workspace.id,+ name=kwargs["name"],+ path=kwargs["path"],+ storage_path=kwargs["storage_path"],+ mime_type=kwargs["mime_type"],+ size_bytes=kwargs["size_bytes"],+ checksum=kwargs["checksum"],+ metadata=kwargs["metadata"],+ created_at=workspace.created_at,+ updated_at=workspace.updated_at,+ )++ db.create_workspace_file = AsyncMock(side_effect=create_file)+ mocker.patch("backend.util.workspace.workspace_db", return_value=db)++ response = client.post(+ "/files/upload", files={"file": (filename, io.BytesIO(b"hello"), "text/plain")}+ )++ assert response.status_code == 200, response.text+ assert response.json()["name"] == filename+ assert response.json()["path"] == f"/{filename}"+ stored = db.create_workspace_file.await_args.kwargs["storage_path"]+ assert storage._parse_storage_path(stored).read_bytes() == b"hello"+++def test_upload_storage_error_hides_exception_details(client, workspace, mocker):+ secret = "/data/workspaces/ws-001/f1/secret-name.txt"+ manager = MagicMock()+ manager.write_file = AsyncMock(+ side_effect=OSError(36, "File name too long", secret)+ )+ mocker.patch(+ "backend.api.features.workspace.routes.WorkspaceManager", return_value=manager+ )++ response = client.post(+ "/files/upload", files={"file": ("secret-name.txt", b"x", "text/plain")}+ )++ assert response.status_code == 500+ assert response.json()["detail"] == "Unable to store file. Please try again."+ assert "/data/" not in response.text+ assert "File name too long" not in response.text+++def test_upload_conflict_retains_useful_message(client, workspace, mocker):+ manager = MagicMock()+ manager.write_file = AsyncMock(side_effect=ValueError("File already exists"))+ mocker.patch(+ "backend.api.features.workspace.routes.WorkspaceManager", return_value=manager+ )++ response = client.post(+ "/files/upload", files={"file": ("existing.txt", b"x", "text/plain")}+ )++ assert response.status_code == 409+ assert response.json()["detail"] == "File already exists"diff --git a/autogpt_platform/backend/backend/util/file.py b/autogpt_platform/backend/backend/util/file.pyindex 4c66949840..03b01a7294 100644--- a/autogpt_platform/backend/backend/util/file.py+++ b/autogpt_platform/backend/backend/util/file.py@@ -11,6 +11,9 @@ from urllib.parse import urlparsefrom pydantic import BaseModelfrom backend.util.cloud_storage import get_cloud_storage_handler+from backend.util.filename import MAX_FILENAME_BYTES as MAX_FILENAME_BYTES+from backend.util.filename import MAX_FILENAME_LENGTH as MAX_FILENAME_LENGTH+from backend.util.filename import sanitize_filename as sanitize_filenamefrom backend.util.request import Requestsfrom backend.util.settings import Configfrom backend.util.type import MediaFileType@@ -61,37 +64,6 @@ MediaReturnFormat = Literal[TEMP_DIR = Path(tempfile.gettempdir()).resolve()-# Maximum filename length (conservative limit for most filesystems)-MAX_FILENAME_LENGTH = 200---def sanitize_filename(filename: str) -> str:- """- Sanitize and truncate filename to prevent filesystem errors.- """- # Remove or replace invalid characters- sanitized = re.sub(r'[<>:"/\\|?*\n\r\t]', "_", filename)-- # Truncate if too long- if len(sanitized) > MAX_FILENAME_LENGTH:- # Keep the extension if possible, but only if it's reasonable length- if "." in sanitized:- name, ext = sanitized.rsplit(".", 1)- # If extension is too long, it's likely not a file extension but just text- if len(ext) <= 20:- max_name_length = MAX_FILENAME_LENGTH - len(ext) - 1- sanitized = name[:max_name_length] + "." + ext- else:- sanitized = sanitized[:MAX_FILENAME_LENGTH]- else:- sanitized = sanitized[:MAX_FILENAME_LENGTH]-- # Ensure it's not empty or just dots- if not sanitized or sanitized.strip(".") == "":- sanitized = f"file_{uuid.uuid4().hex[:8]}"-- return sanitized-def get_exec_file_path(graph_exec_id: str, path: str) -> str:"""diff --git a/autogpt_platform/backend/backend/util/filename.py b/autogpt_platform/backend/backend/util/filename.pynew file mode 100644index 0000000000..59674292f7--- /dev/null+++ b/autogpt_platform/backend/backend/util/filename.py@@ -0,0 +1,53 @@+import re+import uuid++# Maximum filename length (conservative limit for most filesystems)+MAX_FILENAME_LENGTH = 200+# Filesystems cap a name at 255 *bytes* (NAME_MAX), not characters: 200 CJK or+# emoji characters can be 600-800 UTF-8 bytes. Leave headroom under 255.+MAX_FILENAME_BYTES = 240+# A longer "extension" is most likely just text after a dot.+_MAX_EXTENSION_LENGTH = 20+++def sanitize_filename(filename: str) -> str:+ """+ Sanitize and truncate filename to prevent filesystem errors.++ The result is at most ``MAX_FILENAME_LENGTH`` characters and+ ``MAX_FILENAME_BYTES`` UTF-8 bytes, keeping a short extension when present.+ """+ # Remove or replace invalid characters+ sanitized = re.sub(r'[<>:"/\\|?*\n\r\t]', "_", filename)++ # Truncate if too long+ if not _fits(sanitized):+ name, dot, ext = sanitized.rpartition(".")+ # Keep the extension if possible, but only if it's reasonable length+ if dot and name and len(ext) <= _MAX_EXTENSION_LENGTH:+ suffix = "." + ext+ max_name_chars = MAX_FILENAME_LENGTH - len(suffix)+ max_name_bytes = MAX_FILENAME_BYTES - len(suffix.encode("utf-8"))+ sanitized = _truncate_utf8(name[:max_name_chars], max_name_bytes) + suffix+ else:+ sanitized = _truncate_utf8(+ sanitized[:MAX_FILENAME_LENGTH], MAX_FILENAME_BYTES+ )++ # Ensure it's not empty or just dots+ if not sanitized or sanitized.strip(".") == "":+ sanitized = f"file_{uuid.uuid4().hex[:8]}"++ return sanitized+++def _truncate_utf8(text: str, max_bytes: int) -> str:+ """Cut ``text`` to at most ``max_bytes`` UTF-8 bytes on a character boundary."""+ return text.encode("utf-8")[:max_bytes].decode("utf-8", "ignore")+++def _fits(text: str) -> bool:+ return (+ len(text) <= MAX_FILENAME_LENGTH+ and len(text.encode("utf-8")) <= MAX_FILENAME_BYTES+ )diff --git a/autogpt_platform/backend/backend/util/filename_test.py b/autogpt_platform/backend/backend/util/filename_test.pynew file mode 100644index 0000000000..038bbc6abd--- /dev/null+++ b/autogpt_platform/backend/backend/util/filename_test.py@@ -0,0 +1,68 @@+import pytest++from backend.util.filename import MAX_FILENAME_BYTES, sanitize_filename+++class TestSanitizeFilenameByteLimit:+ """Filesystems cap a name component at 255 bytes, not characters (#15651)."""++ @staticmethod+ def _assert_fits(result: str) -> None:+ encoded = result.encode("utf-8")+ assert len(encoded) <= MAX_FILENAME_BYTES < 255+ # Round-trips, i.e. no code point was split at the cut.+ assert encoded.decode("utf-8") == result++ @pytest.mark.parametrize(+ "filename",+ [+ "夜" * 84 + ".txt", # 88 chars / 256 bytes: under the old char cap+ "夜" * 300 + ".txt",+ "🌙" * 64 + ".png", # 4-byte code points+ "Nachtschicht " + "🌙" * 190 + ".png",+ "a" + "夜" * 100 + ".txt", # cut lands mid-character without care+ "ab" + "🌙" * 80 + ".md",+ ],+ ids=[+ "cjk-256-bytes",+ "cjk-long",+ "emoji",+ "mixed-emoji",+ "cjk-odd-boundary",+ "emoji-odd-boundary",+ ],+ )+ def test_multibyte_names_fit_and_keep_extension(self, filename: str):+ result = sanitize_filename(filename)++ self._assert_fits(result)+ assert result.endswith(filename.rsplit(".", 1)[1])+ assert result.rsplit(".", 1)[1] == filename.rsplit(".", 1)[1]+ assert filename.startswith(result.rsplit(".", 1)[0])++ def test_long_ascii_name_keeps_extension(self):+ result = sanitize_filename("a" * 300 + ".pdf")++ self._assert_fits(result)+ assert result.endswith(".pdf")+ assert len(result) == 200 # the character cap still applies++ def test_long_multibyte_name_without_extension(self):+ result = sanitize_filename("夜" * 300)++ self._assert_fits(result)+ assert result == "夜" * 80++ def test_long_text_after_dot_is_not_treated_as_extension(self):+ result = sanitize_filename("report." + "夜" * 300)++ self._assert_fits(result)+ assert result.startswith("report.")++ @pytest.mark.parametrize(+ "filename",+ ["夜" * 20 + ".txt", "🌙 moon.png", "a.b"],+ ids=["cjk", "emoji", "ascii"],+ )+ def test_short_names_are_unchanged(self, filename: str):+ assert sanitize_filename(filename) == filenamediff --git a/autogpt_platform/backend/backend/util/workspace_storage.py b/autogpt_platform/backend/backend/util/workspace_storage.pyindex ee3af757b6..67d0ecc40d 100644--- a/autogpt_platform/backend/backend/util/workspace_storage.py+++ b/autogpt_platform/backend/backend/util/workspace_storage.py@@ -8,17 +8,13 @@ for Google Cloud Storage (cloud deployments) and local filesystem (self-hosted dimport asyncioimport hashlibimport logging-from abc import ABC, abstractmethodfrom datetime import datetime, timezone-from pathlib import Pathfrom typing import Optional-import aiofilesimport aiohttpfrom gcloud.aio import storage as async_gcs_storagefrom google.cloud import storage as gcs_storage-from backend.util.data import get_data_pathfrom backend.util.gcs_utils import (download_range,download_with_fresh_session,@@ -27,89 +23,16 @@ from backend.util.gcs_utils import (parse_gcs_path,)from backend.util.settings import Config+from backend.util.workspace_storage_base import (+ WorkspaceStorageBackend as WorkspaceStorageBackend,+)+from backend.util.workspace_storage_local import (+ LocalWorkspaceStorage as LocalWorkspaceStorage,+)logger = logging.getLogger(__name__)-class WorkspaceStorageBackend(ABC):- """Abstract interface for workspace file storage."""-- @abstractmethod- async def store(- self,- workspace_id: str,- file_id: str,- filename: str,- content: bytes,- ) -> str:- """- Store file content, return storage path.-- Args:- workspace_id: The workspace ID- file_id: Unique file ID for storage- filename: Original filename- content: File content as bytes-- Returns:- Storage path string (cloud path or local path)- """- pass-- @abstractmethod- async def retrieve(self, storage_path: str) -> bytes:- """- Retrieve file content from storage.-- Args:- storage_path: The storage path returned from store()-- Returns:- File content as bytes- """- pass-- @abstractmethod- async def retrieve_partial(self, storage_path: str, max_bytes: int) -> bytes:- """- Retrieve up to ``max_bytes`` from the start of the file.-- Used for cheap text previews so large files are not loaded in full.-- Args:- storage_path: The storage path returned from store()- max_bytes: Maximum number of leading bytes to return-- Returns:- At most ``max_bytes`` of file content as bytes- """- pass-- @abstractmethod- async def delete(self, storage_path: str) -> None:- """- Delete file from storage.-- Args:- storage_path: The storage path to delete- """- pass-- @abstractmethod- async def get_download_url(self, storage_path: str, expires_in: int = 3600) -> str:- """- Get URL for downloading the file.-- Args:- storage_path: The storage path- expires_in: URL expiration time in seconds (default 1 hour)-- Returns:- Download URL (signed URL for GCS, direct API path for local)- """- pass--class GCSWorkspaceStorage(WorkspaceStorageBackend):"""Google Cloud Storage implementation for workspace storage.@@ -249,142 +172,6 @@ class GCSWorkspaceStorage(WorkspaceStorageBackend):return blob_name-class LocalWorkspaceStorage(WorkspaceStorageBackend):- """Local filesystem implementation for workspace storage (self-hosted deployments)."""-- def __init__(self, base_dir: Optional[str] = None):- """- Initialize local storage backend.-- Args:- base_dir: Base directory for workspace storage.- If None, defaults to {app_data}/workspaces- """- if base_dir:- self.base_dir = Path(base_dir)- else:- self.base_dir = Path(get_data_path()) / "workspaces"-- # Ensure base directory exists- self.base_dir.mkdir(parents=True, exist_ok=True)-- def _build_file_path(self, workspace_id: str, file_id: str, filename: str) -> Path:- """Build the local file path with path traversal protection."""- # Import here to avoid circular import- # (file.py imports workspace.py which imports workspace_storage.py)- from backend.util.file import sanitize_filename-- # Sanitize filename to prevent path traversal (removes / and \ among others)- safe_filename = sanitize_filename(filename)- file_path = (self.base_dir / workspace_id / file_id / safe_filename).resolve()-- # Verify the resolved path is still under base_dir- if not file_path.is_relative_to(self.base_dir.resolve()):- raise ValueError("Invalid filename: path traversal detected")-- return file_path-- def _parse_storage_path(self, storage_path: str) -> Path:- """Parse local storage path to filesystem path."""- if storage_path.startswith("local://"):- relative_path = storage_path[8:] # Remove "local://"- else:- relative_path = storage_path-- full_path = (self.base_dir / relative_path).resolve()-- # Security check: ensure path is under base_dir- # Use is_relative_to() for robust path containment check- # (handles case-insensitive filesystems and edge cases)- if not full_path.is_relative_to(self.base_dir.resolve()):- raise ValueError("Invalid storage path: path traversal detected")-- return full_path-- async def store(- self,- workspace_id: str,- file_id: str,- filename: str,- content: bytes,- ) -> str:- """Store file locally."""- file_path = self._build_file_path(workspace_id, file_id, filename)-- # Create parent directories- file_path.parent.mkdir(parents=True, exist_ok=True)-- # Write file asynchronously- async with aiofiles.open(file_path, "wb") as f:- await f.write(content)-- # Return relative path as storage path- relative_path = file_path.relative_to(self.base_dir)- return f"local://{relative_path}"-- async def retrieve(self, storage_path: str) -> bytes:- """Retrieve file from local storage."""- file_path = self._parse_storage_path(storage_path)-- if not file_path.exists():- raise FileNotFoundError(f"File not found: {storage_path}")-- async with aiofiles.open(file_path, "rb") as f:- return await f.read()-- async def retrieve_partial(self, storage_path: str, max_bytes: int) -> bytes:- """Retrieve the first ``max_bytes`` of a local file."""- file_path = self._parse_storage_path(storage_path)-- try:- async with aiofiles.open(file_path, "rb") as f:- return await f.read(max_bytes)- except FileNotFoundError as e:- raise FileNotFoundError(f"File not found: {storage_path}") from e-- async def delete(self, storage_path: str) -> None:- """Delete file from local storage."""- file_path = self._parse_storage_path(storage_path)-- if file_path.exists():- # Remove file- file_path.unlink()-- # Clean up empty parent directories- parent = file_path.parent- while parent != self.base_dir:- try:- if parent.exists() and not any(parent.iterdir()):- parent.rmdir()- else:- break- except OSError:- break- parent = parent.parent-- async def get_download_url(self, storage_path: str, expires_in: int = 3600) -> str:- """- Get download URL for local file.-- For local storage, this returns an API endpoint path.- The actual serving is handled by the API layer.- """- # Parse the storage path to get the components- if storage_path.startswith("local://"):- relative_path = storage_path[8:]- else:- relative_path = storage_path-- # Return the API endpoint for downloading- # The file_id is extracted from the path: {workspace_id}/{file_id}/{filename}- parts = relative_path.split("/")- if len(parts) >= 2:- file_id = parts[1] # Second component is file_id- return f"/api/workspace/files/{file_id}/download"- else:- raise ValueError(f"Invalid storage path format: {storage_path}")--# ---------------------------------------------------------------------------# Storage instance management# ---------------------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/util/workspace_storage_base.py b/autogpt_platform/backend/backend/util/workspace_storage_base.pynew file mode 100644index 0000000000..6f30db2c2f--- /dev/null+++ b/autogpt_platform/backend/backend/util/workspace_storage_base.py@@ -0,0 +1,80 @@+from abc import ABC, abstractmethod+++class WorkspaceStorageBackend(ABC):+ """Abstract interface for workspace file storage."""++ @abstractmethod+ async def store(+ self,+ workspace_id: str,+ file_id: str,+ filename: str,+ content: bytes,+ ) -> str:+ """+ Store file content, return storage path.++ Args:+ workspace_id: The workspace ID+ file_id: Unique file ID for storage+ filename: Original filename+ content: File content as bytes++ Returns:+ Storage path string (cloud path or local path)+ """+ pass++ @abstractmethod+ async def retrieve(self, storage_path: str) -> bytes:+ """+ Retrieve file content from storage.++ Args:+ storage_path: The storage path returned from store()++ Returns:+ File content as bytes+ """+ pass++ @abstractmethod+ async def retrieve_partial(self, storage_path: str, max_bytes: int) -> bytes:+ """+ Retrieve up to ``max_bytes`` from the start of the file.++ Used for cheap text previews so large files are not loaded in full.++ Args:+ storage_path: The storage path returned from store()+ max_bytes: Maximum number of leading bytes to return++ Returns:+ At most ``max_bytes`` of file content as bytes+ """+ pass++ @abstractmethod+ async def delete(self, storage_path: str) -> None:+ """+ Delete file from storage.++ Args:+ storage_path: The storage path to delete+ """+ pass++ @abstractmethod+ async def get_download_url(self, storage_path: str, expires_in: int = 3600) -> str:+ """+ Get URL for downloading the file.++ Args:+ storage_path: The storage path+ expires_in: URL expiration time in seconds (default 1 hour)++ Returns:+ Download URL (signed URL for GCS, direct API path for local)+ """+ passdiff --git a/autogpt_platform/backend/backend/util/workspace_storage_local.py b/autogpt_platform/backend/backend/util/workspace_storage_local.pynew file mode 100644index 0000000000..ffc13c8d53--- /dev/null+++ b/autogpt_platform/backend/backend/util/workspace_storage_local.py@@ -0,0 +1,160 @@+import logging+import tempfile+from pathlib import Path+from typing import Optional++import aiofiles++from backend.util.data import get_data_path+from backend.util.filename import sanitize_filename+from backend.util.workspace_storage_base import WorkspaceStorageBackend++logger = logging.getLogger(__name__)+++class LocalWorkspaceStorage(WorkspaceStorageBackend):+ """Local filesystem implementation for workspace storage (self-hosted deployments)."""++ def __init__(self, base_dir: Optional[str] = None):+ """+ Initialize local storage backend.++ Args:+ base_dir: Base directory for workspace storage.+ If None, defaults to {app_data}/workspaces+ """+ if base_dir:+ self.base_dir = Path(base_dir)+ else:+ self.base_dir = Path(get_data_path()) / "workspaces"++ self.base_dir = self.base_dir.resolve()++ # Ensure base directory exists+ self.base_dir.mkdir(parents=True, exist_ok=True)++ def _build_file_path(self, workspace_id: str, file_id: str, filename: str) -> Path:+ """Build the local file path with path traversal protection."""+ # Sanitize filename to prevent path traversal (removes / and \ among others)+ safe_filename = sanitize_filename(filename)+ file_path = (self.base_dir / workspace_id / file_id / safe_filename).resolve()++ # Verify the resolved path is still under base_dir+ if not file_path.is_relative_to(self.base_dir.resolve()):+ raise ValueError("Invalid filename: path traversal detected")++ return file_path++ def _parse_storage_path(self, storage_path: str) -> Path:+ """Parse local storage path to filesystem path."""+ if storage_path.startswith("local://"):+ relative_path = storage_path[8:] # Remove "local://"+ else:+ relative_path = storage_path++ full_path = (self.base_dir / relative_path).resolve()++ # Security check: ensure path is under base_dir+ # Use is_relative_to() for robust path containment check+ # (handles case-insensitive filesystems and edge cases)+ if not full_path.is_relative_to(self.base_dir.resolve()):+ raise ValueError("Invalid storage path: path traversal detected")++ return full_path++ async def store(+ self,+ workspace_id: str,+ file_id: str,+ filename: str,+ content: bytes,+ ) -> str:+ """Store file locally."""+ file_path = self._build_file_path(workspace_id, file_id, filename)++ # Create parent directories+ file_path.parent.mkdir(parents=True, exist_ok=True)++ temporary_path: Path | None = None+ try:+ with tempfile.NamedTemporaryFile(+ dir=file_path.parent, prefix=".upload-", delete=False+ ) as temporary_file:+ temporary_path = Path(temporary_file.name)+ async with aiofiles.open(temporary_path, "wb") as f:+ await f.write(content)+ temporary_path.replace(file_path)+ except BaseException:+ if temporary_path is not None:+ try:+ temporary_path.unlink(missing_ok=True)+ except OSError:+ logger.exception("Failed to remove incomplete workspace upload")+ self._remove_empty_parents(file_path.parent)+ raise++ # Return relative path as storage path+ relative_path = file_path.relative_to(self.base_dir)+ return f"local://{relative_path}"++ async def retrieve(self, storage_path: str) -> bytes:+ """Retrieve file from local storage."""+ file_path = self._parse_storage_path(storage_path)++ if not file_path.exists():+ raise FileNotFoundError(f"File not found: {storage_path}")++ async with aiofiles.open(file_path, "rb") as f:+ return await f.read()++ async def retrieve_partial(self, storage_path: str, max_bytes: int) -> bytes:+ """Retrieve the first ``max_bytes`` of a local file."""+ file_path = self._parse_storage_path(storage_path)++ try:+ async with aiofiles.open(file_path, "rb") as f:+ return await f.read(max_bytes)+ except FileNotFoundError as e:+ raise FileNotFoundError(f"File not found: {storage_path}") from e++ async def delete(self, storage_path: str) -> None:+ """Delete file from local storage."""+ file_path = self._parse_storage_path(storage_path)++ if file_path.exists():+ # Remove file+ file_path.unlink()++ # Clean up empty parent directories+ self._remove_empty_parents(file_path.parent)++ def _remove_empty_parents(self, parent: Path) -> None:+ """Remove ``parent`` and its ancestors while empty, stopping at base_dir."""+ while parent != self.base_dir:+ try:+ parent.rmdir()+ except OSError:+ break+ parent = parent.parent++ async def get_download_url(self, storage_path: str, expires_in: int = 3600) -> str:+ """+ Get download URL for local file.++ For local storage, this returns an API endpoint path.+ The actual serving is handled by the API layer.+ """+ # Parse the storage path to get the components+ if storage_path.startswith("local://"):+ relative_path = storage_path[8:]+ else:+ relative_path = storage_path++ # Return the API endpoint for downloading+ # The file_id is extracted from the path: {workspace_id}/{file_id}/{filename}+ parts = relative_path.split("/")+ if len(parts) >= 2:+ file_id = parts[1] # Second component is file_id+ return f"/api/workspace/files/{file_id}/download"+ else:+ raise ValueError(f"Invalid storage path format: {storage_path}")diff --git a/autogpt_platform/backend/backend/util/workspace_storage_local_test.py b/autogpt_platform/backend/backend/util/workspace_storage_local_test.pynew file mode 100644index 0000000000..9c0f070d85--- /dev/null+++ b/autogpt_platform/backend/backend/util/workspace_storage_local_test.py@@ -0,0 +1,103 @@+import asyncio+from pathlib import Path++import aiofiles.threadpool.binary+import pytest++from backend.util import workspace_storage_local+from backend.util.workspace_storage import LocalWorkspaceStorage+++async def test_failed_open_preserves_existing_transcript(tmp_path, mocker):+ storage = LocalWorkspaceStorage(str(tmp_path))+ path = await storage.store("sessions", "owner", "session.jsonl", b"previous turn")+ mocker.patch.object(+ workspace_storage_local.aiofiles, "open", side_effect=OSError(28, "disk full")+ )++ with pytest.raises(OSError, match="disk full"):+ await storage.store("sessions", "owner", "session.jsonl", b"next turn")++ assert storage._parse_storage_path(path).read_bytes() == b"previous turn"+ assert list(tmp_path.rglob("*.*")) == [storage._parse_storage_path(path)]++[email protected]("existing", [False, True])[email protected]("cancelled", [False, True])+async def test_interrupted_write_preserves_completed_files(+ tmp_path, mocker, existing, cancelled+):+ storage = LocalWorkspaceStorage(str(tmp_path))+ if existing:+ await storage.store("sessions", "owner", "session.jsonl", b"previous turn")+ original_write = aiofiles.threadpool.binary.AsyncBufferedIOBase.write+ error = asyncio.CancelledError() if cancelled else OSError(28, "disk full")++ async def partial_write(handle, content):+ await original_write(handle, content[:3])+ raise error++ mocker.patch.object(+ aiofiles.threadpool.binary.AsyncBufferedIOBase, "write", partial_write+ )++ with pytest.raises(type(error)):+ await storage.store("sessions", "owner", "session.jsonl", b"next turn")++ path = tmp_path / "sessions" / "owner" / "session.jsonl"+ if existing:+ assert path.read_bytes() == b"previous turn"+ assert list(path.parent.iterdir()) == [path]+ else:+ assert list(tmp_path.iterdir()) == []+++async def test_failed_store_stops_cleanup_at_relative_root(+ tmp_path, monkeypatch, mocker+):+ monkeypatch.chdir(tmp_path)+ (tmp_path / "guard.txt").write_text("keep test parent")+ storage = LocalWorkspaceStorage("outer/storage")+ mocker.patch.object(+ workspace_storage_local.aiofiles, "open", side_effect=OSError("failed")+ )++ with pytest.raises(OSError, match="failed"):+ await storage.store("ws", "file", "data.txt", b"content")++ assert (tmp_path / "outer" / "storage").is_dir()+ assert list((tmp_path / "outer" / "storage").iterdir()) == []+++async def test_successful_overwrite_replaces_complete_file(tmp_path):+ storage = LocalWorkspaceStorage(str(tmp_path))+ original_path = await storage.store("ws", "file", "data.txt", b"old")++ new_path = await storage.store("ws", "file", "data.txt", b"complete new content")++ assert new_path == original_path+ assert await storage.retrieve(new_path) == b"complete new content"+ assert list((tmp_path / "ws" / "file").iterdir()) == [+ storage._parse_storage_path(new_path)+ ]+++async def test_failed_replace_preserves_original_and_removes_temporary_file(+ tmp_path, mocker+):+ storage = LocalWorkspaceStorage(str(tmp_path))+ original_path = await storage.store("ws", "file", "data.txt", b"old")+ mocker.patch.object(Path, "replace", side_effect=OSError("replace failed"))++ with pytest.raises(OSError, match="replace failed"):+ await storage.store("ws", "file", "data.txt", b"new")++ assert await storage.retrieve(original_path) == b"old"+ assert list((tmp_path / "ws" / "file").iterdir()) == [+ storage._parse_storage_path(original_path)+ ]diff --git a/autogpt_platform/backend/backend/util/workspace_storage_test.py b/autogpt_platform/backend/backend/util/workspace_storage_test.pyindex 877d3b2fdf..5355adce2d 100644--- a/autogpt_platform/backend/backend/util/workspace_storage_test.py+++ b/autogpt_platform/backend/backend/util/workspace_storage_test.py@@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMockimport aiohttpimport pytest-from backend.util import workspace_storage+from backend.util import workspace_storage, workspace_storage_localfrom backend.util.gcs_utils_test import _URL_CONTAINING_404, _gcs_http_errorfrom backend.util.workspace_storage import GCSWorkspaceStorage, LocalWorkspaceStorage@@ -122,3 +122,49 @@ async def test_gcs_delete_raises_other_errors_when_url_contains_404(mocker, statwith pytest.raises(aiohttp.ClientResponseError):await storage.delete("gcs://my-bucket/path/file.txt")+++async def test_local_store_accepts_name_over_255_utf8_bytes(tmp_path):+ """A name under the char cap but over NAME_MAX bytes stores fine (#15651)."""+ storage = LocalWorkspaceStorage(base_dir=str(tmp_path))+ filename = "夜" * 86 + ".txt" # 90 chars, 262 bytes+ assert len(filename.encode("utf-8")) > 255++ storage_path = await storage.store("ws", "file", filename, b"hello")++ assert storage_path.endswith(".txt")+ assert await storage.retrieve(storage_path) == b"hello"+++async def test_local_store_failure_leaves_no_empty_directory(tmp_path, mocker):+ storage = LocalWorkspaceStorage(base_dir=str(tmp_path))+ mocker.patch.object(+ workspace_storage_local.aiofiles,+ "open",+ side_effect=OSError(28, "No space left on device"),+ )++ with pytest.raises(OSError):+ await storage.store("ws", "file", "data.txt", b"hello")++ assert not (tmp_path / "ws" / "file").exists()+ assert not (tmp_path / "ws").exists()+ assert tmp_path.exists()+++async def test_local_store_failure_keeps_sibling_files(tmp_path, mocker):+ storage = LocalWorkspaceStorage(base_dir=str(tmp_path))+ kept = await storage.store("ws", "other", "keep.txt", b"keep")+ mocker.patch.object(+ workspace_storage_local.aiofiles, "open", side_effect=OSError("boom")+ )++ with pytest.raises(OSError):+ await storage.store("ws", "file", "data.txt", b"hello")++ assert not (tmp_path / "ws" / "file").exists()+ mocker.stopall()+ assert await storage.retrieve(kept) == b"keep"