Repository navigation
Description
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 pathsrc/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:
build_download_urlnever appendssession_token— the agent doesn't know it.- Even if it did,
session_tokenis 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
- Configure an agent with
require_email: true(via/api/agents/{name}/access-policy) - Enable file sharing for the agent (
PUT /api/agents/{name}/file-sharing) - Have the agent share a file via chat — observe backend log:
[shared-files] agent=X shared file_id=... - Click the generated link:
<PUBLIC_CHAT_URL>/api/files/{id}?sig=<token> - 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-001feature)
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
fix(files): remove redundant session_token gate from /api/files/{id} (#…
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
fix(workspace): the Files tab — uploads at once, an honest Download, …