Skip to content

bug: File sharing unusable for require_email agents — build_download_url omits session_token #568

Closed

Description

@vybe

Summary

When an agent with require_email: true shares a file, the generated download URL contains only ?sig=<token>. The /api/files/{id} endpoint then gates the download behind _agent_requires_email() and rejects the request with 401 session_token required. Since the agent has no way to know the recipient's session_token at share time, and build_download_url never appends one, file sharing is permanently broken for all require_email agents. Files are successfully stored and logged, but no recipient can ever download them.

Component

Backend / File Sharing (FILES-001) — src/backend/services/agent_shared_files_service.py + src/backend/routers/files.py

Priority

P1 — feature completely broken for agents with require_email: true

Error

GET /api/files/{id}?sig=<192-bit token>
→ HTTP 401
{"detail": "session_token required"}

Root Cause

build_download_url is unaware of the require_email policy.

src/backend/services/agent_shared_files_service.py:251:

path = f"/api/files/{file_id}?sig={download_token}"
return f"{base}{path}" if base else path

src/backend/routers/files.py (inside _validate_download_request):

if _agent_requires_email(agent_name):
    if not session_token:
        raise HTTPException(status_code=401, detail="session_token required")

The endpoint applies the same channel-access policy gate as /api/public/chat, but:

  1. build_download_url never appends session_token — the agent doesn't know it.
  2. Even if it did, session_token is a runtime public-chat credential the agent has no access to.

The sig token is already a 192-bit entropy one-time credential specifically minted for this download. It provides equivalent or stronger auth than a session_token. Applying the require_email gate on top of a signed URL is redundant and makes the feature inoperable.

Reproduction

  1. Configure an agent with require_email: true (via /api/agents/{name}/access-policy)
  2. Enable file sharing for the agent (PUT /api/agents/{name}/file-sharing)
  3. Have the agent share a file via chat — observe backend log: [shared-files] agent=X shared file_id=...
  4. Click the generated link: <PUBLIC_CHAT_URL>/api/files/{id}?sig=<token>
  5. Receive HTTP 401 {"detail": "session_token required"}

Suggested Fix

Remove the _agent_requires_email gate from _validate_download_request in src/backend/routers/files.py. The sig token already serves as the auth credential (constant-time compared against the stored download_token). There is no security benefit in also requiring a session_token — the sig is a 192-bit random token minted at share time and known only to the recipient.

# REMOVE these lines from _validate_download_request (files.py ~line 140):
agent_name = row["agent_name"]
if _agent_requires_email(agent_name):
    if not session_token:
        raise HTTPException(status_code=401, detail="session_token required")
    valid, _email = db.validate_agent_session(agent_name, session_token)
    if not valid:
        raise HTTPException(status_code=401, detail="invalid or expired session_token")

The agent_name local variable and the audit block below it should be kept; only the require_email policy gate should be removed.

Secondary Finding

GET /api/agents/{name} returns "require_email": null for agents whose access policy is require_email: true (confirmed via GET /api/agents/{name}/access-policy). This is a separate serialisation inconsistency but is not the root cause of this bug.

Environment

  • Trinity version: 1ebe473 (dev branch)
  • Introduced in: this commit (FILES-001 feature)

Related

  • src/backend/services/agent_shared_files_service.py — build_download_url() (line ~231)
  • src/backend/routers/files.py — _validate_download_request() (~line 100)
  • src/backend/routers/public.py — _agent_requires_email() (line 179)

Activity

  1. added
    status-in-dev
    on Apr 29, 2026
  2. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

    Shipped in v0.6.0 (released via #998).

  3. added
    complexity-low
    and removed
    status-in-dev
    on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p1type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions