Spaces:
Running
Running
AutoFix commited on
Commit ·
e2988cf
1
Parent(s): b24c76e
opt: multi-stage build + auth on diagnostics + connection limiting
Browse files1. Multi-stage Dockerfile (~500MB smaller final image):
- Stage 1 (builder): git, make, g++, node.js, hermes-agent source
- Stage 2 (runtime): only venv, patched agent, webui, fonts
- Eliminates build tools and source code from final image
2. Auth on sensitive endpoints:
- /api/diagnostics/gateway-log → requires Bearer token
- /api/diagnostics/restart-gateway → requires Bearer token
- /api/logs/stream → requires Bearer token
3. Connection limiting (DoS prevention):
- ThreadingHTTPServer limited to 50 concurrent connections
- Prevents RAM exhaustion from malicious requests
- Dockerfile +39 -20
- entry.py +22 -4
Dockerfile
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 5 |
git curl gnupg2 fontconfig make g++ python3 \
|
| 6 |
&& rm -rf /var/lib/apt/lists/*
|
|
@@ -17,44 +20,29 @@ RUN pip install --quiet --upgrade pip && \
|
|
| 17 |
pip install --quiet psutil networkx && \
|
| 18 |
pip install --quiet -e "/app/hermes-agent[feishu,mcp,cron,pty]" 2>&1 | tail -10
|
| 19 |
|
| 20 |
-
#
|
| 21 |
COPY scripts/patch_file_delivery.py /tmp/patch_file_delivery.py
|
| 22 |
RUN python3 /tmp/patch_file_delivery.py; rm -f /tmp/patch_file_delivery.py
|
| 23 |
|
| 24 |
-
# Patch: Feishu media support in send_message_tool + anti-hallucination prompts
|
| 25 |
COPY patches/hermes-agent/agent/prompt_builder.py /app/hermes-agent/agent/prompt_builder.py
|
| 26 |
COPY patches/hermes-agent/tools/send_message_tool.py /app/hermes-agent/tools/send_message_tool.py
|
| 27 |
|
| 28 |
-
# Patch: Auto-inject MEDIA: tags from write_file tool calls
|
| 29 |
COPY scripts/patch_auto_media.py /tmp/patch_auto_media.py
|
| 30 |
RUN python3 /tmp/patch_auto_media.py; rm -f /tmp/patch_auto_media.py
|
| 31 |
|
| 32 |
-
# Patch: Auto-resolve relative media paths to absolute paths
|
| 33 |
-
# ROOT CAUSE FIX: LLM often uses bare filenames in MEDIA: tags (e.g. "report.md")
|
| 34 |
-
# which causes send_document() to fail with "File not found"
|
| 35 |
COPY scripts/patch_resolve_media_paths.py /tmp/patch_resolve_media_paths.py
|
| 36 |
RUN python3 /tmp/patch_resolve_media_paths.py; rm -f /tmp/patch_resolve_media_paths.py
|
| 37 |
|
| 38 |
-
# Install Node.js 23
|
| 39 |
RUN ARCH=$(dpkg --print-architecture) \
|
| 40 |
&& if [ "$ARCH" = "amd64" ]; then NODE_ARCH="x64"; else NODE_ARCH="$ARCH"; fi \
|
| 41 |
-
&& echo "Installing Node.js v23 for ${NODE_ARCH}" \
|
| 42 |
&& curl -fsSL "https://nodejs.org/dist/v23.11.0/node-v23.11.0-linux-${NODE_ARCH}.tar.gz" \
|
| 43 |
-o /tmp/node.tar.gz \
|
| 44 |
&& tar -xzf /tmp/node.tar.gz -C /usr/local --strip-components=1 \
|
| 45 |
&& rm -f /tmp/node.tar.gz \
|
| 46 |
&& node --version && npm --version
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
RUN mkdir -p /usr/share/fonts/truetype/noto && \
|
| 50 |
-
curl -sL "https://github.com/googlefonts/noto-cjk/raw/main/Sans/SubsetOTF/SC/NotoSansSC-Regular.otf" \
|
| 51 |
-
-o /usr/share/fonts/truetype/noto/NotoSansSC-Regular.otf && \
|
| 52 |
-
curl -sL "https://github.com/googlefonts/noto-cjk/raw/main/Sans/SubsetOTF/SC/NotoSansSC-Bold.otf" \
|
| 53 |
-
-o /usr/share/fonts/truetype/noto/NotoSansSC-Bold.otf && \
|
| 54 |
-
fc-cache -f
|
| 55 |
-
|
| 56 |
-
# Build hermes-web-ui (keep node_modules for runtime deps)
|
| 57 |
-
# Use --ignore-scripts to avoid prepare hook issues
|
| 58 |
RUN git clone --depth 1 https://github.com/EKKOLearnAI/hermes-web-ui.git /tmp/hermes-web-ui && \
|
| 59 |
cd /tmp/hermes-web-ui && \
|
| 60 |
npm install --ignore-scripts 2>&1 | tail -5 && \
|
|
@@ -69,6 +57,37 @@ RUN git clone --depth 1 https://github.com/EKKOLearnAI/hermes-web-ui.git /tmp/he
|
|
| 69 |
rm -rf /tmp/hermes-web-ui && \
|
| 70 |
echo "hermes-web-ui build done"
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Create hermes home
|
| 73 |
RUN mkdir -p /root/.hermes/plugins/image_gen/pollinations
|
| 74 |
|
|
|
|
| 1 |
+
# ============================================================
|
| 2 |
+
# Stage 1: Builder — clone, build, patch (discarded after)
|
| 3 |
+
# ============================================================
|
| 4 |
+
FROM python:3.12-slim AS builder
|
| 5 |
|
| 6 |
+
# Build tools needed for node-pty native compilation
|
| 7 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
git curl gnupg2 fontconfig make g++ python3 \
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
| 20 |
pip install --quiet psutil networkx && \
|
| 21 |
pip install --quiet -e "/app/hermes-agent[feishu,mcp,cron,pty]" 2>&1 | tail -10
|
| 22 |
|
| 23 |
+
# All patches (non-fatal — won't break build if upstream changed)
|
| 24 |
COPY scripts/patch_file_delivery.py /tmp/patch_file_delivery.py
|
| 25 |
RUN python3 /tmp/patch_file_delivery.py; rm -f /tmp/patch_file_delivery.py
|
| 26 |
|
|
|
|
| 27 |
COPY patches/hermes-agent/agent/prompt_builder.py /app/hermes-agent/agent/prompt_builder.py
|
| 28 |
COPY patches/hermes-agent/tools/send_message_tool.py /app/hermes-agent/tools/send_message_tool.py
|
| 29 |
|
|
|
|
| 30 |
COPY scripts/patch_auto_media.py /tmp/patch_auto_media.py
|
| 31 |
RUN python3 /tmp/patch_auto_media.py; rm -f /tmp/patch_auto_media.py
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
COPY scripts/patch_resolve_media_paths.py /tmp/patch_resolve_media_paths.py
|
| 34 |
RUN python3 /tmp/patch_resolve_media_paths.py; rm -f /tmp/patch_resolve_media_paths.py
|
| 35 |
|
| 36 |
+
# Install Node.js 23 for web-ui build
|
| 37 |
RUN ARCH=$(dpkg --print-architecture) \
|
| 38 |
&& if [ "$ARCH" = "amd64" ]; then NODE_ARCH="x64"; else NODE_ARCH="$ARCH"; fi \
|
|
|
|
| 39 |
&& curl -fsSL "https://nodejs.org/dist/v23.11.0/node-v23.11.0-linux-${NODE_ARCH}.tar.gz" \
|
| 40 |
-o /tmp/node.tar.gz \
|
| 41 |
&& tar -xzf /tmp/node.tar.gz -C /usr/local --strip-components=1 \
|
| 42 |
&& rm -f /tmp/node.tar.gz \
|
| 43 |
&& node --version && npm --version
|
| 44 |
|
| 45 |
+
# Build hermes-web-ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
RUN git clone --depth 1 https://github.com/EKKOLearnAI/hermes-web-ui.git /tmp/hermes-web-ui && \
|
| 47 |
cd /tmp/hermes-web-ui && \
|
| 48 |
npm install --ignore-scripts 2>&1 | tail -5 && \
|
|
|
|
| 57 |
rm -rf /tmp/hermes-web-ui && \
|
| 58 |
echo "hermes-web-ui build done"
|
| 59 |
|
| 60 |
+
|
| 61 |
+
# ============================================================
|
| 62 |
+
# Stage 2: Runtime — minimal image with only what's needed
|
| 63 |
+
# ============================================================
|
| 64 |
+
FROM python:3.12-slim
|
| 65 |
+
|
| 66 |
+
# Runtime deps only (no build tools)
|
| 67 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 68 |
+
curl fontconfig \
|
| 69 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 70 |
+
|
| 71 |
+
WORKDIR /app
|
| 72 |
+
|
| 73 |
+
# Copy built venv + patched hermes-agent from builder
|
| 74 |
+
COPY --from=builder /app/venv /app/venv
|
| 75 |
+
COPY --from=builder /app/hermes-agent /app/hermes-agent
|
| 76 |
+
|
| 77 |
+
ENV PATH="/app/venv/bin:$PATH"
|
| 78 |
+
|
| 79 |
+
# Copy built web-ui from builder
|
| 80 |
+
COPY --from=builder /app/webui-server /app/webui-server
|
| 81 |
+
COPY --from=builder /app/webui-client /app/webui-client
|
| 82 |
+
|
| 83 |
+
# Chinese fonts (download in runtime stage — only ~16MB)
|
| 84 |
+
RUN mkdir -p /usr/share/fonts/truetype/noto && \
|
| 85 |
+
curl -sL "https://github.com/googlefonts/noto-cjk/raw/main/Sans/SubsetOTF/SC/NotoSansSC-Regular.otf" \
|
| 86 |
+
-o /usr/share/fonts/truetype/noto/NotoSansSC-Regular.otf && \
|
| 87 |
+
curl -sL "https://github.com/googlefonts/noto-cjk/raw/main/Sans/SubsetOTF/SC/NotoSansSC-Bold.otf" \
|
| 88 |
+
-o /usr/share/fonts/truetype/noto/NotoSansSC-Bold.otf && \
|
| 89 |
+
fc-cache -f
|
| 90 |
+
|
| 91 |
# Create hermes home
|
| 92 |
RUN mkdir -p /root/.hermes/plugins/image_gen/pollinations
|
| 93 |
|
entry.py
CHANGED
|
@@ -33,6 +33,12 @@ from io import BytesIO
|
|
| 33 |
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
|
| 34 |
daemon_threads = True
|
| 35 |
allow_reuse_address = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# ---------------------------------------------------------------------------
|
| 38 |
# Paths
|
|
@@ -275,6 +281,12 @@ class ProxyHandler(BaseHTTPRequestHandler):
|
|
| 275 |
def log_message(self, fmt, *args):
|
| 276 |
pass # silence request logs
|
| 277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
def _send_json(self, data: dict, status=200):
|
| 279 |
body = json.dumps(data, ensure_ascii=False).encode("utf-8")
|
| 280 |
self.send_response(status)
|
|
@@ -387,16 +399,22 @@ class ProxyHandler(BaseHTTPRequestHandler):
|
|
| 387 |
if path == "/deploy":
|
| 388 |
return self._send_html("/app/deploy.html")
|
| 389 |
|
| 390 |
-
# ── Diagnostic: gateway log dump (
|
| 391 |
if path == "/api/diagnostics/gateway-log":
|
|
|
|
|
|
|
| 392 |
return self._handle_gateway_log()
|
| 393 |
|
| 394 |
-
# ── Diagnostic: gateway restart ──
|
| 395 |
-
if path == "/api/diagnostics/restart-gateway"
|
|
|
|
|
|
|
| 396 |
return self._handle_restart_gateway()
|
| 397 |
|
| 398 |
-
# ── SSE log stream (
|
| 399 |
if path == "/api/logs/stream":
|
|
|
|
|
|
|
| 400 |
return self._handle_sse()
|
| 401 |
|
| 402 |
# ── WebUI SPA: /webui, /webui/* → serve from webui-client static dir ──
|
|
|
|
| 33 |
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
|
| 34 |
daemon_threads = True
|
| 35 |
allow_reuse_address = True
|
| 36 |
+
# Limit concurrent connections to prevent DoS / RAM exhaustion
|
| 37 |
+
_semaphore = threading.Semaphore(50)
|
| 38 |
+
|
| 39 |
+
def process_request(self, request, client_address):
|
| 40 |
+
with self._semaphore:
|
| 41 |
+
return super().process_request(request, client_address)
|
| 42 |
|
| 43 |
# ---------------------------------------------------------------------------
|
| 44 |
# Paths
|
|
|
|
| 281 |
def log_message(self, fmt, *args):
|
| 282 |
pass # silence request logs
|
| 283 |
|
| 284 |
+
def _check_auth(self) -> bool:
|
| 285 |
+
"""Check Bearer token auth for sensitive endpoints."""
|
| 286 |
+
auth = self.headers.get("Authorization", "")
|
| 287 |
+
expected = os.environ.get("AUTH_TOKEN", "hermes-bot-2026")
|
| 288 |
+
return auth == f"Bearer {expected}"
|
| 289 |
+
|
| 290 |
def _send_json(self, data: dict, status=200):
|
| 291 |
body = json.dumps(data, ensure_ascii=False).encode("utf-8")
|
| 292 |
self.send_response(status)
|
|
|
|
| 399 |
if path == "/deploy":
|
| 400 |
return self._send_html("/app/deploy.html")
|
| 401 |
|
| 402 |
+
# ── Diagnostic: gateway log dump (auth required) ──
|
| 403 |
if path == "/api/diagnostics/gateway-log":
|
| 404 |
+
if not self._check_auth():
|
| 405 |
+
return self._send_json({"error": "unauthorized"}, 401)
|
| 406 |
return self._handle_gateway_log()
|
| 407 |
|
| 408 |
+
# ── Diagnostic: gateway restart (auth required) ──
|
| 409 |
+
if path == "/api/diagnostics/restart-gateway":
|
| 410 |
+
if not self._check_auth():
|
| 411 |
+
return self._send_json({"error": "unauthorized"}, 401)
|
| 412 |
return self._handle_restart_gateway()
|
| 413 |
|
| 414 |
+
# ── SSE log stream (auth required) ──
|
| 415 |
if path == "/api/logs/stream":
|
| 416 |
+
if not self._check_auth():
|
| 417 |
+
return self._send_json({"error": "unauthorized"}, 401)
|
| 418 |
return self._handle_sse()
|
| 419 |
|
| 420 |
# ── WebUI SPA: /webui, /webui/* → serve from webui-client static dir ──
|