Skip to content

fix: require auth for agent file downloads#3067

Open
wolfkill wants to merge 5 commits into
eosphoros-ai:mainfrom
wolfkill:fix/agent-file-download-auth
Open

fix: require auth for agent file downloads#3067
wolfkill wants to merge 5 commits into
eosphoros-ai:mainfrom
wolfkill:fix/agent-file-download-auth

Conversation

@wolfkill

Copy link
Copy Markdown
Contributor

Summary

  • require the same user dependency used by nearby agent/skill endpoints for /v1/agent/files/download
  • stop allowing downloads from the whole application root; keep /tmp and PILOT_PATH/tmp as the agent-created file directories
  • add route and path-boundary regression tests

Fixes #3018

Tests

  • PYTHONPATH=packages/dbgpt-core/src:packages/dbgpt-app/src:packages/dbgpt-serve/src:packages/dbgpt-ext/src .venv/bin/python -m pytest packages/dbgpt-app/src/dbgpt_app/tests/test_agent_file_download_api.py -q
  • .venv/bin/ruff check packages/dbgpt-app/src/dbgpt_app/openapi/api_v1/agentic_data_api.py packages/dbgpt-app/src/dbgpt_app/tests/test_agent_file_download_api.py
  • .venv/bin/ruff format --check packages/dbgpt-app/src/dbgpt_app/openapi/api_v1/agentic_data_api.py packages/dbgpt-app/src/dbgpt_app/tests/test_agent_file_download_api.py
  • PYTHONPATH=packages/dbgpt-core/src:packages/dbgpt-app/src:packages/dbgpt-serve/src:packages/dbgpt-ext/src .venv/bin/python -m compileall -q packages/dbgpt-app/src/dbgpt_app/openapi/api_v1/agentic_data_api.py packages/dbgpt-app/src/dbgpt_app/tests/test_agent_file_download_api.py
  • git diff --check

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the /v1/agent/files/download endpoint by aligning it with nearby agent/skill endpoints’ authentication dependency and restricting downloads to agent-created temp directories, with regression tests intended to prevent future path-boundary regressions.

Changes:

  • Adds a user dependency (Depends(get_user_from_headers)) to the agent file download route.
  • Removes ROOT_PATH from the allowlist so downloads are restricted to /tmp and PILOT_PATH/tmp.
  • Adds tests intended to validate the auth dependency and allowlist/path-boundary behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/dbgpt-app/src/dbgpt_app/openapi/api_v1/agentic_data_api.py Adds user dependency to the route and tightens allowlist by removing ROOT_PATH.
packages/dbgpt-app/src/dbgpt_app/tests/test_agent_file_download_api.py Adds regression tests for dependency presence and allowed/denied download paths.
Comments suppressed due to low confidence (1)

packages/dbgpt-app/src/dbgpt_app/openapi/api_v1/agentic_data_api.py:3989

  • The file_path parameter description says “Absolute path”, but the handler still accepts relative paths and resolves them against ROOT_PATH. Since ROOT_PATH has been removed from the allowlist, relative paths will now typically resolve into a directory that is denied, which is confusing for API consumers. Either enforce absolute-only input (and drop the ROOT_PATH join) or update the description/logic so relative paths resolve into an allowed base directory.
async def download_agent_file(
    file_path: str = Query(..., description="Absolute path to the file to download"),
    user_token: UserRequest = Depends(get_user_from_headers),
):
    """Download a file created by agent tools (shell_interpreter, code_interpreter).

    Only files under allowed directories (/tmp, PILOT_PATH/tmp/) can be downloaded.
    This prevents arbitrary file access on the server.
    """
    from fastapi import HTTPException
    from fastapi.responses import FileResponse

    from dbgpt.configs.model_config import PILOT_PATH, ROOT_PATH

    # If path is not absolute, resolve relative to ROOT_PATH (sandbox working dir)
    if not os.path.isabs(file_path):
        file_path = os.path.join(ROOT_PATH, file_path)


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +28 to +40
async def test_agent_file_download_rejects_project_root_file(tmp_path, monkeypatch):
from dbgpt.configs import model_config
from dbgpt_app.openapi.api_v1 import agentic_data_api

project_file = tmp_path / "docker-compose.yml"
project_file.write_text("services: {}\n", encoding="utf-8")
monkeypatch.setattr(model_config, "ROOT_PATH", str(tmp_path))
monkeypatch.setattr(model_config, "PILOT_PATH", str(tmp_path / "pilot"))

with pytest.raises(HTTPException) as exc_info:
await agentic_data_api.download_agent_file(str(project_file))

assert exc_info.value.status_code == 403
Comment on lines +44 to +58
async def test_agent_file_download_allows_pilot_tmp_file(tmp_path, monkeypatch):
from dbgpt.configs import model_config
from dbgpt_app.openapi.api_v1 import agentic_data_api

pilot_tmp = tmp_path / "pilot" / "tmp"
pilot_tmp.mkdir(parents=True)
generated_file = pilot_tmp / "result.txt"
generated_file.write_text("ok\n", encoding="utf-8")
monkeypatch.setattr(model_config, "ROOT_PATH", str(tmp_path / "project"))
monkeypatch.setattr(model_config, "PILOT_PATH", str(tmp_path / "pilot"))

response = await agentic_data_api.download_agent_file(str(generated_file))

assert isinstance(response, FileResponse)
assert response.path == str(generated_file)
Comment on lines 3971 to 3975
@router.get("/v1/agent/files/download")
async def download_agent_file(
file_path: str = Query(..., description="Absolute path to the file to download"),
user_token: UserRequest = Depends(get_user_from_headers),
):
@chenliang15405

Copy link
Copy Markdown
Collaborator

Thanks for your fix.

A note on removing ROOT_PATH: This change drops ROOT_PATH from the allowed download directories to prevent access to sensitive files under the project root. However, this might introduce compatibility issues — if any existing code relies on downloading files from ROOT_PATH, it will start getting 403 errors after the upgrade. Worth double-checking if there's any such usage in the current codebase.

@wolfkill wolfkill force-pushed the fix/agent-file-download-auth branch from 10b4568 to 228b143 Compare May 22, 2026 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Web] Unauthenticated Arbitrary File Download via /v1/agent/files/download

3 participants