HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
gateway: an upload is served to its owner only; an unknown file_id is a 404 (#222)
github-actions[bot]f5f123fa09a1
diff --git a/gateway/app.py b/gateway/app.pyindex 61d0bfb3..b9c4b098 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -7593,23 +7593,8 @@ async def create_response(body: CreateResponseBody, request: Request):prompt, files_in, input_items = _parse_input(body.input)user_text = prompt # the raw user message (before instructions / file-note prepends) for the trace- # resolve file_id-referenced inputs to inline base64 (uploaded via POST /v1/files);- # the original filename comes from the uploads/{fid}.meta blob when the input- # block didn't inline one (mirrors the containers/*.meta read in the files lister)- for f in files_in:- if f.get("content_b64") is None and f.get("file_id"):- data = await _blob_get(f"uploads/{f['file_id']}", kb=RESP_BLOB_KB)- if data is not None:- f["content_b64"] = base64.b64encode(data).decode()- if not f.get("filename"):- fmeta = await _blob_get(f"uploads/{f['file_id']}.meta", kb=RESP_BLOB_KB)- if fmeta is not None:- try:- f["filename"] = (json.loads(fmeta.decode()) or {}).get("filename") or ""- except Exception:- f["filename"] = ""- if not f.get("filename"):- f["filename"] = f["file_id"] # last resort: readable, unique+ # uploads referenced by file_id (POST /v1/files) become inline bytes, this org's only+ await _resolve_uploads(files_in, org)files_in = [{"filename": f["filename"], "content_b64": f["content_b64"]}for f in files_in if f.get("content_b64") and f.get("filename")]if body.instructions:@@ -8512,7 +8497,7 @@ async def upload_file(request: Request, purpose: str = Form("user_data"), file:disk; we relay it in chunks to the blob plane, whose staged-block commit-at-end makes atruncated stream harmless). Capped: uploads are inlined base64 into the runner's turn body,so the cap bounds turn-payload RAM too — matching the engine's 25 MiB attachment cap."""- await _principal(request)+ principal = await _principal(request)if BACKING.mode == "vg" and not VG_GATEWAY_URL:raise HTTPException(503, "file storage is not configured")# Fail fast on the declared size when present (well-behaved clients send Content-Length);@@ -8555,13 +8540,46 @@ async def upload_file(request: Request, purpose: str = Form("user_data"), file:buf += chunkif not await BACKING.blob.put(RESP_BLOB_KB, f"uploads/{fid}", bytes(buf)):raise HTTPException(502, "durable upload failed")+ # THE OWNER IS RECORDED WITH THE UPLOAD. Files §5: a file_id from another principal must+ # answer 404, never the bytes. The sidecar carried only the name and media type, so a task+ # by any org that named a file_id was handed the file (found 2026-09-20 while answering+ # #198); _resolve_uploads reads the org back and refuses a mismatch.await _blob_put(f"uploads/{fid}.meta", json.dumps(- {"filename": file.filename, "media_type": file.content_type or "application/octet-stream"}).encode(),+ {"filename": file.filename, "media_type": file.content_type or "application/octet-stream",+ "org": str(principal.get("org") or "")}).encode(),kb=RESP_BLOB_KB)return {"id": fid, "object": "file", "bytes": nbytes, "created_at": int(time.time()),"filename": file.filename, "purpose": purpose}+async def _resolve_uploads(files_in: list[dict], org: str) -> None:+ """Inline the uploads a task references by file_id, for THIS org only.++ Files §5: a file_id from another principal answers 404, never the bytes. The owner is read+ from the upload's sidecar; an upload made before the owner was recorded has none and is+ served, since nothing can say whose it is. An unknown id is a 404 too: it used to be dropped+ in silence and the task ran without its file, which is the confident, wrong answer §1.2 warns+ about. The name comes from the sidecar when the input block did not inline one."""+ for f in files_in:+ if f.get("content_b64") is not None or not f.get("file_id"):+ continue+ fid = str(f["file_id"])+ meta: dict = {}+ raw = await _blob_get(f"uploads/{fid}.meta", kb=RESP_BLOB_KB)+ if raw is not None:+ try:+ meta = json.loads(raw.decode()) or {}+ except Exception: # noqa: BLE001+ meta = {}+ owner = str(meta.get("org") or "")+ data = None if (owner and owner != org) else await _blob_get(f"uploads/{fid}", kb=RESP_BLOB_KB)+ if data is None:+ raise uhp_error(404, "file_not_found", f"No file with id {fid}.", "input")+ f["content_b64"] = base64.b64encode(data).decode()+ if not f.get("filename"):+ f["filename"] = str(meta.get("filename") or "") or fid # last resort: readable, unique++# ── session workspace files (list + download-by-id) ─────────────────────────────# The agent's workspace persists between turns as a checkpoint tarball. These helpers expose it# read-only: list every user-visible file, and serve any of them through the SAME download endpointdiff --git a/gateway/tests/test_files_upload.py b/gateway/tests/test_files_upload.pyindex a26899cd..8af5db01 100644--- a/gateway/tests/test_files_upload.py+++ b/gateway/tests/test_files_upload.py@@ -35,7 +35,41 @@ def test_an_upload_lands_in_the_local_store_and_answers_the_file_object():import asyncioassert asyncio.run(gw._blob_get(f"uploads/{f['id']}", kb=gw.RESP_BLOB_KB)) == b"The secret token is uhp-198.\n"meta = json.loads(asyncio.run(gw._blob_get(f"uploads/{f['id']}.meta", kb=gw.RESP_BLOB_KB)))- assert meta == {"filename": "brief.txt", "media_type": "text/plain"}+ assert meta == {"filename": "brief.txt", "media_type": "text/plain", "org": "local"}+++def test_an_upload_is_served_to_its_owner_only_and_an_unknown_id_is_a_404():+ """Files §5: a file_id from another principal answers 404, never the bytes. The sidecar carried+ no owner, so a task by any org that named a file_id was handed the file; and an unknown id+ was dropped in silence, the task running without its file."""+ import asyncio+ import base64+ from fastapi import HTTPException+ c = TestClient(gw.app)+ f = _upload(c, "brief.txt", b"The secret token is uhp-198.\n").json()+ mine = [{"file_id": f["id"], "content_b64": None, "filename": ""}]+ asyncio.run(gw._resolve_uploads(mine, "local"))+ assert mine[0]["filename"] == "brief.txt"+ assert mine[0]["content_b64"] == base64.b64encode(b"The secret token is uhp-198.\n").decode()+ theirs = [{"file_id": f["id"], "content_b64": None, "filename": ""}]+ try:+ asyncio.run(gw._resolve_uploads(theirs, "another-org"))+ except HTTPException as e:+ assert e.status_code == 404 and e.detail["code"] == "file_not_found"+ assert theirs[0]["content_b64"] is None # never the bytes+ else:+ raise AssertionError("another org was handed the upload")+ try:+ asyncio.run(gw._resolve_uploads([{"file_id": "file_doesnotexist", "content_b64": None, "filename": ""}], "local"))+ except HTTPException as e:+ assert e.status_code == 404 and e.detail["code"] == "file_not_found"+ else:+ raise AssertionError("an unknown file_id was dropped in silence")+ # an upload made before the owner was recorded has no org in its sidecar and is served+ asyncio.run(gw._blob_put(f"uploads/{f['id']}.meta", json.dumps({"filename": "brief.txt", "media_type": "text/plain"}).encode(), kb=gw.RESP_BLOB_KB))+ legacy = [{"file_id": f["id"], "content_b64": None, "filename": ""}]+ asyncio.run(gw._resolve_uploads(legacy, "another-org"))+ assert legacy[0]["content_b64"]def test_an_oversized_upload_is_refused_as_file_too_large_and_stores_nothing(monkeypatch):