Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
fix(router): isolate forwarded proxy headers from workers
myhloli918454607b81
diff --git a/mineru/kit/router/proxy.py b/mineru/kit/router/proxy.pyindex c92944d5..a5fee2ef 100644--- a/mineru/kit/router/proxy.py+++ b/mineru/kit/router/proxy.py@@ -28,6 +28,7 @@ _HOP_BY_HOP_HEADERS = frozenset("content-length",})+_PROXY_REQUEST_HEADERS = frozenset({"forwarded", "x-real-ip"})_TERMINAL_JOB_STATUSES = frozenset({"completed", "partial", "failed", "canceled"})@@ -56,8 +57,14 @@ def router_error_response(exc: RouterProxyError) -> JSONResponse:def forwarded_headers(request: Request) -> dict[str, str]:- """选择可安全传给 upstream 的入站请求头,包括 Authorization。"""- return {name: value for name, value in request.headers.items() if name.lower() not in _HOP_BY_HOP_HEADERS}+ """保留认证和业务请求头,移除属于客户端到 Router 这一跳的代理来源信息。"""+ return {+ name: value+ for name, value in request.headers.items()+ if name.lower() not in _HOP_BY_HOP_HEADERS+ and name.lower() not in _PROXY_REQUEST_HEADERS+ and not name.lower().startswith("x-forwarded-")+ }def response_headers(headers: Mapping[str, str]) -> dict[str, str]:diff --git a/tests/unittest/test_task_http.py b/tests/unittest/test_task_http.pyindex 3f922925..78297915 100644--- a/tests/unittest/test_task_http.py+++ b/tests/unittest/test_task_http.py@@ -63,6 +63,21 @@ class _UploadCounter:await self.app(scope, receive, send)+class _WorkerRequests:+ """记录真实 HTTP worker 收到的请求头,验证内外两跳的代理来源边界。"""++ def __init__(self, app: ASGIApp, calls: list[tuple[str, str, dict[str, str]]]) -> None:+ """由测试保存观测结果,不改变应用或 Uvicorn 的代理头处理。"""+ self.app = app+ self.calls = calls++ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:+ """原样执行 worker,只读取实际抵达的 HTTP 请求头。"""+ if scope["type"] == "http":+ self.calls.append((scope["method"], scope["path"], dict(Request(scope).headers)))+ await self.app(scope, receive, send)++@dataclassclass _LiveService:"""保留真实网络地址与应用对象,便于核对上传量和唯一任务记录。"""@@ -150,6 +165,75 @@ def _members(data: bytes) -> dict[str, bytes]:return {name: archive.read(name) for name in archive.namelist()}+def test_router_keeps_tls_proxy_headers_on_the_public_hop_only(tmp_path: Path) -> None:+ """TLS 终止代理后的上传、hash 复用及下载保持可用,worker 不继承客户端来源头。"""+ calls: list[tuple[str, str, dict[str, str]]] = []+ worker_app = api_server.create_app(upload_dir=str(tmp_path / "worker"), tier="flash", api_key="test-key")+ worker_app.add_middleware(_WorkerRequests, calls=calls)+ worker = _Server(worker_app)+ servers = [worker]+ proxy_headers = {+ "Forwarded": 'for=203.0.113.10;proto=https;host="mineru.example"',+ "X-Forwarded-Proto": "https",+ "X-Forwarded-For": "203.0.113.10",+ "X-Forwarded-Host": "mineru.example",+ "X-Forwarded-Port": "443",+ "X-Forwarded-Prefix": "/mineru",+ "X-Real-IP": "203.0.113.10",+ }+ try:+ worker.start()+ router = _Server(+ create_router(RouterSettings(upstream_urls=(worker.url,), local_gpus="none", worker_refresh_interval_seconds=0))+ )+ servers.append(router)+ router.start()+ with httpx.Client(+ base_url=router.url,+ trust_env=False,+ timeout=30,+ headers={**proxy_headers, "Authorization": "Bearer test-key", "X-Request-ID": "proxy-regression"},+ ) as client:+ for index in range(2):+ task = _submit(client, [(f"document-{index}.html", HTML)])+ assert urlsplit(task["status_url"]).scheme == "https"+ assert len(task["uploads"]) == (1 if index == 0 else 0)+ for upload in task["uploads"]:+ # 以本地 HTTP 模拟 TLS 终止代理到 Router 的后段连接,保留真实入站代理头。+ response = client.put(urlsplit(upload["upload_url"]).path, content=HTML)+ assert response.status_code == 200, response.text+ deadline = time.monotonic() + 10+ while time.monotonic() < deadline:+ response = client.get(urlsplit(task["status_url"]).path)+ assert response.status_code == 200, response.text+ result = response.json()+ if result["status"] in TERMINAL:+ break+ time.sleep(0.01)+ else:+ pytest.fail("Task did not finish through the TLS proxy hop")+ assert result["status"] == "completed", result+ result_url = result["files"][0]["result_url"]+ assert urlsplit(result_url).scheme == "https"+ response = client.get(urlsplit(result_url).path)+ assert response.status_code == 200, response.text+ assert b"Same source and output" in _members(response.content)["markdown.md"]+ ranged = client.get(urlsplit(result_url).path, headers={"Range": "bytes=0-31"})+ assert ranged.status_code == 206+ assert ranged.content == response.content[:32]+ forwarded_calls = [call for call in calls if call[2].get("x-request-id") == "proxy-regression"]+ assert any(method == "POST" and path == "/v1/tasks" for method, path, _ in forwarded_calls)+ assert any(method == "PUT" for method, _, _ in forwarded_calls)+ assert any(path.endswith("/content") for _, path, _ in forwarded_calls)+ assert any(headers.get("range") == "bytes=0-31" for _, _, headers in forwarded_calls)+ for _, _, headers in forwarded_calls:+ assert headers["authorization"] == "Bearer test-key"+ assert not set(headers).intersection(key.lower() for key in proxy_headers)+ finally:+ for server in reversed(servers):+ server.stop()++def _script_env(tmp_path: Path, base: str, **extra: str) -> dict[str, str]:"""隔离示例脚本环境,只继承执行 Python 与本地字符编码所需变量。"""env = {