fix(webui): upload TUI and WebUI attachments over binary HTTP (#5980)

* feat(webui): add bounded binary attachment staging

* feat(webui): bridge binary HTTP bodies alongside websocket listener

* fix(webui): deliver attachments over authenticated binary HTTP

* fix(webui): bound incomplete upload headers and preserve staging markers

* test: reflect aiohttp runtime dependency for binary uploads

* fix(webui): relay binary uploads and retain rejected drafts

* refactor(webui): remove obsolete attachment transport errors

* refactor(websocket): own binary attachment transport

* fix(websocket): allow bounded uploads over slow connections

* test(webui): restore upload timeout before retry

* docs(websocket): focus attachment guide on client usage
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-08 11:55:13 +08:00
1 parent 5be1e589f2
commit 6646f5fbee
53 files changed
+1972 -847

No files matched your search

-117
View File
@@ -1,117 +0,0 @@
from __future__ import annotations
import base64
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock
import pytest
from nanobot.webui.attachment_ingress import (
extract_data_url_mime,
store_inbound_attachments,
)
from nanobot.webui.ingress_policy import AttachmentIngressLimits
def _data_url(mime: str, payload: bytes) -> str:
encoded = base64.b64encode(payload).decode()
return f"data:{mime};base64,{encoded}"
@pytest.mark.parametrize(
("url", "expected"),
[
("data:image/png;base64,AAAA", "image/png"),
("data:IMAGE/JPEG;charset=utf-8;base64,AAAA", "image/jpeg"),
("data:video/webm;codecs=vp9;base64,AAAA", "video/webm"),
("data:text/plain;base64,AAAA", "text/plain"),
("data:image/svg+xml;base64,AAAA", "image/svg+xml"),
("data:image/png,AAAA", None),
("data:;base64,AAAA", None),
("https://example.invalid/image.png", None),
("", None),
(None, None),
],
)
def test_extract_data_url_mime_normalizes_only_base64_data_urls(
url: Any,
expected: str | None,
) -> None:
assert extract_data_url_mime(url) == expected
def test_store_inbound_document_preserves_safe_name(tmp_path: Path) -> None:
paths, rejection = store_inbound_attachments(
[
{
"data_url": _data_url("text/csv", b"name,value\nnanobot,1"),
"name": "report.csv",
},
],
media_dir=tmp_path,
logger=MagicMock(),
)
assert rejection is None
assert len(paths) == 1
saved = Path(paths[0])
assert saved.parent == tmp_path
assert saved.name.endswith("_report.csv")
assert saved.read_bytes() == b"name,value\nnanobot,1"
def test_invalid_batch_removes_files_already_persisted(tmp_path: Path) -> None:
paths, rejection = store_inbound_attachments(
[
{"data_url": _data_url("image/png", b"valid-first-item")},
{"data_url": _data_url("image/svg+xml", b"<svg/>")},
],
media_dir=tmp_path,
logger=MagicMock(),
)
assert paths == []
assert rejection == "mime"
assert list(tmp_path.iterdir()) == []
def test_invalid_base64_cannot_create_an_empty_attachment(tmp_path: Path) -> None:
paths, rejection = store_inbound_attachments(
[{"data_url": "data:text/plain;base64,@@@@", "name": "empty.txt"}],
media_dir=tmp_path,
logger=MagicMock(),
)
assert paths == []
assert rejection == "decode"
assert list(tmp_path.iterdir()) == []
def test_single_file_limit_is_attachment_policy_not_transport(tmp_path: Path) -> None:
paths, rejection = store_inbound_attachments(
[{"data_url": _data_url("text/plain", b"12345"), "name": "large.txt"}],
media_dir=tmp_path,
logger=MagicMock(),
limits=AttachmentIngressLimits(max_file_bytes=4, max_total_bytes=20),
)
assert paths == []
assert rejection == "size"
assert list(tmp_path.iterdir()) == []
def test_total_attachment_policy_rolls_back_the_batch(tmp_path: Path) -> None:
paths, rejection = store_inbound_attachments(
[
{"data_url": _data_url("text/plain", b"1234"), "name": "one.txt"},
{"data_url": _data_url("text/plain", b"5678"), "name": "two.txt"},
],
media_dir=tmp_path,
logger=MagicMock(),
limits=AttachmentIngressLimits(max_file_bytes=4, max_total_bytes=6),
)
assert paths == []
assert rejection == "total_size"
assert list(tmp_path.iterdir()) == []
+1
View File
@@ -21,6 +21,7 @@ def test_current_producer_remains_acceptable_to_initial_client():
contract = webui_contract()
assert contract["min_protocol"] <= 1 <= contract["max_protocol"]
assert "webui.core.v1" in contract["capabilities"]
assert "webui.attachments.binary.v1" in contract["capabilities"]
@pytest.mark.parametrize("version", ["0.0.1", "9.99.0", "0.3.5.dev17", "custom-build"])
+94 -5
View File
@@ -7,6 +7,7 @@ import socket
import sys
import time
from dataclasses import replace
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock
from urllib.parse import parse_qs, urlsplit
@@ -19,6 +20,7 @@ from websockets.datastructures import Headers
from websockets.exceptions import InvalidStatus
from websockets.http11 import Request
from nanobot.channels.websocket.binary_http import BinaryHTTPBridge
from nanobot.channels.websocket.runtime import WebSocketConfig
from nanobot.webui import remote_ssh
from nanobot.webui.client_contract import webui_contract
@@ -33,6 +35,7 @@ MEDIA = "/api/media/AAAAAAAAAAAAAAAAAAAAAA/remoteFilePayload"
@pytest.fixture
async def remote(tmp_path, monkeypatch):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
assets = tmp_path / "local-dist"
assets.mkdir()
(assets / "index.html").write_text("<!doctype html><title>Local nanobot</title>", encoding="utf-8")
@@ -50,6 +53,7 @@ async def remote(tmp_path, monkeypatch):
seen, children = [], []
# Frozen initial wire declaration: current client vs an independently defined host.
state = SimpleNamespace(block=asyncio.Event(), entered=asyncio.Event(), ws_redirect=False,
binary_upload=False,
contract={"version": "0.3.5", "min_protocol": 1, "max_protocol": 1,
"capabilities": ["webui.core.v1"]})
@@ -98,14 +102,21 @@ async def remote(tmp_path, monkeypatch):
async def messages(ws):
assert ws in services.endpoint.webui_connections
assert ws.request.headers.get("X-Nanobot-Auth") is None
await ws.send(json.dumps({"ready": True, "media": MEDIA,
"query": parse_qs(urlsplit(ws.request.path).query).get("client_id")}))
async for message in ws:
await ws.send(message)
try:
await ws.send(json.dumps({"ready": True, "media": MEDIA,
"query": parse_qs(urlsplit(ws.request.path).query).get("client_id"),
**({"event": "ready", "upload": services.uploads.issue(ws)}
if state.binary_upload else {})}))
async for message in ws:
await ws.send(message)
finally:
services.uploads.revoke(ws)
real_spawn = asyncio.create_subprocess_exec
monkeypatch.setattr(remote_ssh.shutil, "which", lambda _: "fixture-ssh")
async with serve(messages, "127.0.0.1", 0, process_request=process, max_size=64 * 1024 * 1024) as server:
bridge = BinaryHTTPBridge(services.uploads.handle)
async with serve(messages, "127.0.0.1", 0, process_request=process,
create_connection=bridge.connection_factory, max_size=64 * 1024 * 1024) as server:
port = server.sockets[0].getsockname()[1]
pipe = f"""
import os, socket, threading
@@ -135,6 +146,8 @@ while data := upstream.recv(65536):
finally:
state.block.set()
await proxy.close()
await bridge.shutdown()
services.uploads.store.clear()
assert all(child.returncode is not None for child in children)
@@ -280,6 +293,82 @@ async def test_custom_ws_path_multiple_tabs_and_binary_upload(remote):
assert json.loads(await other.recv())["ready"] is True
async def test_binary_http_upload_uses_a_live_local_capability(remote):
remote.state.binary_upload = True
issued = await bootstrap(remote)
raw = b"x" * 1_453_245
url = issued["ws_url"] + "?token=" + issued["token"]
async with connect(url, proxy=None, origin=remote.proxy.origin) as ws:
capability = json.loads(await ws.recv())["upload"]
headers = {"Authorization": "Bearer " + capability["token"],
"Content-Type": "image/png", "X-Attachment-Name": "clipboard.png"}
response = await remote.client.post(remote.proxy.origin + capability["path"],
content=raw, headers=headers)
assert response.status_code == 201, response.text
assert response.headers["cache-control"] == "no-store"
assert capability["token"] not in remote.services.uploads._tokens
owner = next(iter(remote.services.uploads._tokens))
paths = remote.services.uploads.store.resolve([response.json()["reference"]], owner=owner)
assert Path(paths[0]).read_bytes() == raw
assert (await remote.client.post(remote.proxy.origin + "/api/attachments", content=raw,
headers={**headers, "Authorization": "Bearer " + issued["api_token"]})).status_code == 401
assert (await remote.client.post(remote.proxy.origin + "/api/fixture", content=b"write",
headers={"Authorization": "Bearer " + issued["api_token"]})).status_code == 405
assert (await remote.client.post(remote.proxy.origin + capability["path"], content=raw,
headers={**headers, "Origin": "https://foreign.test"})).status_code == 403
async with asyncio.timeout(5):
while remote.services.uploads._tokens:
await asyncio.sleep(.01)
assert (await remote.client.post(remote.proxy.origin + capability["path"],
content=raw, headers=headers)).status_code == 401
assert not remote.services.uploads.store._entries
@pytest.mark.parametrize("encoded", [False, True], ids=["chunked", "compressed"])
async def test_binary_upload_rejects_changed_body_framing(remote, encoded):
remote.state.binary_upload = True
issued = await bootstrap(remote)
async def chunks():
yield b"bytes"
async with connect(issued["ws_url"] + "?token=" + issued["token"], proxy=None) as ws:
capability = json.loads(await ws.recv())["upload"]
response = await remote.client.post(remote.proxy.origin + capability["path"],
content=b"bytes" if encoded else chunks(), headers={
"Authorization": "Bearer " + capability["token"], "Content-Type": "image/png",
**({"Content-Encoding": "gzip"} if encoded else {}),
})
assert response.status_code == 400
assert not remote.services.uploads.store._entries
async def test_stalled_binary_upload_is_bounded_and_retryable(remote, monkeypatch):
remote.state.binary_upload = True
issued = await bootstrap(remote)
async with connect(issued["ws_url"] + "?token=" + issued["token"], proxy=None) as ws:
capability = json.loads(await ws.recv())["upload"]
with monkeypatch.context() as stalled_timeout:
stalled_timeout.setattr("nanobot.webui.remote_proxy.UPLOAD_IDLE_TIMEOUT_SECONDS", .1)
reader, writer = await asyncio.open_connection("127.0.0.1", remote.proxy.port)
try:
writer.write((
f"POST /api/attachments HTTP/1.1\r\nHost: 127.0.0.1:{remote.proxy.port}\r\n"
f"Authorization: Bearer {capability['token']}\r\nContent-Type: image/png\r\n"
"Content-Length: 2\r\nConnection: close\r\n\r\nx"
).encode())
await writer.drain()
response = await asyncio.wait_for(reader.readuntil(b"\r\n\r\n"), 2)
assert b"502 Bad Gateway" in response
finally:
writer.close()
await writer.wait_closed()
response = await remote.client.post(remote.proxy.origin + capability["path"],
content=b"xx", headers={"Authorization": "Bearer " + capability["token"],
"Content-Type": "image/png"})
assert response.status_code == 201, response.text
async def test_signed_media_wrapped_over_http_and_websocket_and_range_preserved(remote):
issued = await bootstrap(remote)
response = await remote.client.get(remote.proxy.origin + "/api/fixture", headers={