From 77203955a56fb260e791c1594442fa99449551e0 Mon Sep 17 00:00:00 2001 From: Matt Mabrey Date: Tue, 29 Sep 2026 18:16:35 -0700 Subject: [PATCH] Restrict outbound network to api.anthropic.com only - New egress-proxy/ container (tinyproxy) that only allows CONNECT to api.anthropic.com, refuses everything else. - docker-compose.yml: sde-meeting-toolkit is no longer on 'default' at all. Its only networks are 'proxy' (NPM, inbound) and a new private 'egress' network shared only with egress-proxy. egress-proxy is the only container with a real internet route. - server.js: route the one outbound Anthropic call through EGRESS_PROXY_URL when set, using undici's own fetch + ProxyAgent together (mixing them with Node's global fetch throws a version mismatch error - caught this locally before it hit a real deploy). - Add undici as a real dependency (package.json, package-lock.json), npm ci step in Dockerfile. - Add smoke-test-egress.sh + Step 9b in DOCKER_TESTING.md to verify this whole chain with real Docker. - Update CHANGELOG. --- .env.example | 8 +++ CHANGELOG.md | 8 +++ DOCKER_TESTING.md | 17 +++++++ Dockerfile | 7 ++- docker-compose.yml | 50 +++++++++++++------ egress-proxy/Dockerfile | 6 +++ egress-proxy/filter | 1 + egress-proxy/tinyproxy.conf | 28 +++++++++++ package-lock.json | 24 +++++++++ package.json | 3 ++ server.js | 27 ++++++++++- smoke-test-egress.sh | 97 +++++++++++++++++++++++++++++++++++++ 12 files changed, 258 insertions(+), 18 deletions(-) create mode 100644 egress-proxy/Dockerfile create mode 100644 egress-proxy/filter create mode 100644 egress-proxy/tinyproxy.conf create mode 100644 package-lock.json create mode 100644 smoke-test-egress.sh diff --git a/.env.example b/.env.example index 1a50edb..8bae0df 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,14 @@ MOCK_AI=false # Required for the AI draft button, unless MOCK_AI=true. ANTHROPIC_API_KEY=your-anthropic-api-key-here +# Only meaningful in the Docker deployment (docker-compose.yml sets this +# automatically to http://egress-proxy:8888). When set, the one outbound +# call this app makes (to api.anthropic.com) is routed through that proxy +# instead of going out directly, and the container itself has no other +# route to the internet at all. Leave unset for local, non-Docker runs - +# the AI draft button then calls Anthropic directly, same as always. +EGRESS_PROXY_URL= + # Named users. Comma-separated user:password pairs. Each name doubles as the # identity used for the per-user token quota below. # Example: APP_USERS=alice:pass1,bob:pass2,carol:pass3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 7506785..96f9826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project are logged here, newest first. This file starts from the point the toolkit was handed off and set up in this repo. +## 2026-09-29 (3) + +- Added: outbound network restriction. `sde-meeting-toolkit` no longer has any direct route to the internet. Its one outbound call (the AI draft button, to `api.anthropic.com`) is routed through a new `egress-proxy` container (`egress-proxy/`, tinyproxy) that only allows that one destination and refuses everything else, even from inside a compromised app container. +- Added: one real npm dependency, `undici` (`package.json`, `package-lock.json`), and an `npm ci` step in `Dockerfile`. Used only to route the Anthropic call through `egress-proxy` via `EGRESS_PROXY_URL`. Local, non-Docker runs are unaffected; the app calls Anthropic directly when this isn't set, same as before. +- Fixed (caught before deploy): Node's global `fetch()` and an externally-installed `undici`'s `ProxyAgent` don't mix — throws `invalid onRequestStart method`, since Node's internal `undici` and the npm package aren't guaranteed to match versions. `server.js` now uses `undici`'s own `fetch` together with its own `ProxyAgent` for the proxied call, avoiding the mismatch. Confirmed via a local CONNECT-tunnel test. +- Added: `smoke-test-egress.sh`, a script that builds the full stack and checks the app container can't reach anything but Anthropic's API, `egress-proxy` correctly refuses an unrelated domain, and the AI draft button still works end to end. Documented as Step 9b in `DOCKER_TESTING.md`. +- Network layout note: `sde-meeting-toolkit` is on `proxy` (NPM, inbound) and a new private `egress` network (to `egress-proxy` only). `egress-proxy` is on `egress` and `default` (its own internet route). Neither container is on both `proxy` and `default`. + ## 2026-09-29 (2) - Added: shared login turned on now that the toolkit is reachable at `MVP.controls.dev`. Username `PrimeMVP`, password generated and set in `.env`/Portainer (not committed; `.env` is gitignored). This is a stopgap ahead of real per-user `APP_USERS` accounts. diff --git a/DOCKER_TESTING.md b/DOCKER_TESTING.md index 839920e..24340a3 100644 --- a/DOCKER_TESTING.md +++ b/DOCKER_TESTING.md @@ -121,6 +121,23 @@ Skip this step for a normal test run. The quota ships off by default; this is on 8. Run `docker compose restart`, then confirm `tester` is still blocked. The quota survives a restart because `data` is a mounted volume. 9. Remove `TOKEN_LIMIT_PER_USER` from `.env` and restart again to turn it back off. +## Step 9b: Test the egress restriction + +The container has no direct route to the internet at all. Its one outbound call (the AI draft button) is forced through a second container, `egress-proxy`, which only allows that one destination and refuses everything else. This confirms that restriction actually holds, not just that the AI draft button still works. + +1. Run: + ``` + bash smoke-test-egress.sh + ``` +2. This rebuilds and starts the full stack (`sde-meeting-toolkit` and `egress-proxy` together), then checks: + - the landing page still loads normally + - the app container cannot reach an unrelated site (`example.com`) directly + - `egress-proxy` refuses to forward to that same unrelated site + - `egress-proxy` does allow a tunnel to `api.anthropic.com` + - the AI draft endpoint still works end to end through the whole chain +3. A `PASS`/`FAIL` line prints for each check, with a summary count at the end. Any `FAIL` line explains what broke and points at the relevant log command. +4. For the last check to be a full test (not just "the server route responds"), set `MOCK_AI=false` with a real `ANTHROPIC_API_KEY` in `.env` first. + ## Step 10: Stop the container 1. Run this command: diff --git a/Dockerfile b/Dockerfile index c370fc8..ccbe911 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,11 @@ FROM node:22-alpine WORKDIR /app -# No dependencies to install: server.js uses only Node's built-in modules. -COPY package.json ./ +# One dependency: undici, only used to route the outbound Anthropic API +# call through the egress-restricting proxy (see EGRESS_PROXY_URL in +# server.js). Everything else in this app uses Node's built-in modules. +COPY package.json package-lock.json ./ +RUN npm ci --omit=dev COPY server.js ./ COPY tools ./tools COPY skills ./skills diff --git a/docker-compose.yml b/docker-compose.yml index 9ef28e0..8d2cb69 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,17 +1,20 @@ # Standalone compose file for this tool. # -# Joins the "proxy" Docker network so Nginx Proxy Manager can reach this -# container by name (sde-meeting-toolkit) the same way it reaches Work -# Package Suite's own internal nginx container. "proxy" must already exist -# on the host; it is not created here (external: true), since NPM's own -# stack owns it. +# Network layout: +# - "proxy": external, owned by Nginx Proxy Manager's own stack. Lets NPM +# reach sde-meeting-toolkit by container name. No internet route. +# - "egress": private to this compose file. The ONLY thing +# sde-meeting-toolkit can reach besides "proxy". Used to talk to the +# egress-proxy container below. +# - "default": Compose's own auto-created network. Only egress-proxy is on +# it, since it's the only container here that needs real internet access. # -# Also stays on "default" (Compose's own auto-created network for this -# project). "proxy" is scoped to proxy-to-backend traffic only and has no -# route to the public internet, so without "default" too, the AI draft -# button's outbound call to api.anthropic.com fails (502: "Could not reach -# the Anthropic API"). Landing page and tool pages still work without it, -# since those never leave the container, which is why this was easy to miss. +# sde-meeting-toolkit itself is NOT on "default" and has no direct route to +# the internet at all. Its one outbound call (the AI draft button, to +# api.anthropic.com) is routed through egress-proxy via EGRESS_PROXY_URL, +# which only forwards that one destination and refuses everything else +# (see egress-proxy/tinyproxy.conf and egress-proxy/filter). A compromised +# app container still can't reach anything but Anthropic's API. services: sde-meeting-toolkit: build: . @@ -22,6 +25,7 @@ services: environment: - MOCK_AI=${MOCK_AI:-false} - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-} + - EGRESS_PROXY_URL=${EGRESS_PROXY_URL:-http://egress-proxy:8888} - APP_USERS=${APP_USERS:-} - APP_USERNAME=${APP_USERNAME:-} - APP_PASSWORD=${APP_PASSWORD:-} @@ -40,11 +44,25 @@ services: - ./data:/app/data networks: - proxy - - default + - egress + depends_on: + - egress-proxy # Hardening for a container that's reachable from the public internet - # (through Nginx Proxy Manager) and can itself reach the public internet - # (api.anthropic.com). Neither of these needs any Linux capability the - # app doesn't already have, and the app never needs to escalate privilege. + # (through Nginx Proxy Manager). Neither of these needs any Linux + # capability the app doesn't already have, and the app never needs to + # escalate privilege. + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + + egress-proxy: + build: ./egress-proxy + container_name: sde-meeting-toolkit-egress-proxy + restart: unless-stopped + networks: + - egress + - default cap_drop: - ALL security_opt: @@ -53,3 +71,5 @@ services: networks: proxy: external: true + egress: + driver: bridge diff --git a/egress-proxy/Dockerfile b/egress-proxy/Dockerfile new file mode 100644 index 0000000..d87b4ca --- /dev/null +++ b/egress-proxy/Dockerfile @@ -0,0 +1,6 @@ +FROM alpine:3.20 +RUN apk add --no-cache tinyproxy +COPY tinyproxy.conf /etc/tinyproxy/tinyproxy.conf +COPY filter /etc/tinyproxy/filter +EXPOSE 8888 +CMD ["tinyproxy", "-d", "-c", "/etc/tinyproxy/tinyproxy.conf"] diff --git a/egress-proxy/filter b/egress-proxy/filter new file mode 100644 index 0000000..42c40a5 --- /dev/null +++ b/egress-proxy/filter @@ -0,0 +1 @@ +^api\.anthropic\.com$ diff --git a/egress-proxy/tinyproxy.conf b/egress-proxy/tinyproxy.conf new file mode 100644 index 0000000..9a27280 --- /dev/null +++ b/egress-proxy/tinyproxy.conf @@ -0,0 +1,28 @@ +# Minimal tinyproxy config for the SDE Meeting Toolkit's outbound restriction. +# +# The app container has no direct route to the internet. This is the only +# way out, and it only allows one destination: api.anthropic.com. Every +# other outbound request from the app is refused here, not just unrouted, +# so a compromised app container still can't reach anything else. +User nobody +Group nogroup + +Port 8888 + +# Only the app container can reach this proxy at all (it's the only other +# thing on the "egress" network in docker-compose.yml), so this is a second +# layer, not the primary control. The primary control is the Filter below. +Allow 0.0.0.0/0 + +# Domain allow-list. FilterDefaultDeny means anything not matched here is +# refused. Matched against the CONNECT target for HTTPS traffic, which is +# all this app ever sends (fetch() to https://api.anthropic.com/...). +Filter "/etc/tinyproxy/filter" +FilterDefaultDeny Yes +FilterExtended Yes +FilterCaseSensitive No + +MaxClients 20 +Timeout 60 + +LogLevel Warning diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f3d386a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,24 @@ +{ + "name": "sde-meeting-toolkit", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "sde-meeting-toolkit", + "version": "1.0.0", + "dependencies": { + "undici": "^8.11.2" + } + }, + "node_modules/undici": { + "version": "8.11.2", + "resolved": "https://registry.npmjs.org/undici/-/undici-8.11.2.tgz", + "integrity": "sha512-u4UB2/IrKdU6lFxumHmmo1a3fCQO5tzQllRorfoRS63txhrB7xTpSn1PftwC4qEHkOaqP95fCWW4lJzwErwzhQ==", + "license": "MIT", + "engines": { + "node": ">=22.19.0" + } + } + } +} diff --git a/package.json b/package.json index 7a3637b..8ffdb2d 100644 --- a/package.json +++ b/package.json @@ -6,5 +6,8 @@ "scripts": { "serve": "node server.js", "start": "npm run serve" + }, + "dependencies": { + "undici": "^8.11.2" } } diff --git a/server.js b/server.js index e8001e5..b0d5fb8 100644 --- a/server.js +++ b/server.js @@ -13,6 +13,31 @@ const ROOT = __dirname; const PORT = process.env.PORT || 5173; const ANTHROPIC_VERSION = '2023-06-01'; +/* ===== Outbound egress restriction ===== + * When EGRESS_PROXY_URL is set (only in the hardened Docker deployment, + * via docker-compose.yml), the container has no direct route to the + * internet at all. The only outbound call this app makes - to + * api.anthropic.com - is routed through a small egress-proxy container + * that only allows that one destination. Local, non-Docker runs leave + * this unset and call the API directly with Node's built-in fetch, same + * as always. + * + * Important: this deliberately uses undici's OWN fetch() here, not + * Node's global fetch(). Node's global fetch is powered by an internal, + * pinned copy of undici, which is not guaranteed to match the version of + * the standalone 'undici' package installed from npm. Passing that + * package's ProxyAgent as a `dispatcher` to the GLOBAL fetch throws + * ("invalid onRequestStart method") because the two don't speak the same + * internal protocol - confirmed while building this. Using undici's own + * fetch alongside its own ProxyAgent avoids the mismatch entirely. */ +let doFetch = fetch; // Node's built-in global fetch, used when there's no proxy to route through. +if (process.env.EGRESS_PROXY_URL) { + const { fetch: undiciFetch, ProxyAgent } = require('undici'); + const dispatcher = new ProxyAgent(process.env.EGRESS_PROXY_URL); + doFetch = (url, opts) => undiciFetch(url, { ...opts, dispatcher }); + console.log('Outbound traffic restricted: routing through egress proxy at ' + process.env.EGRESS_PROXY_URL); +} + const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', @@ -293,7 +318,7 @@ function proxyToClaude(req, res, user) { req.on('data', (chunk) => { body += chunk; }); req.on('end', async () => { try { - const upstream = await fetch('https://api.anthropic.com/v1/messages', { + const upstream = await doFetch('https://api.anthropic.com/v1/messages', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/smoke-test-egress.sh b/smoke-test-egress.sh new file mode 100644 index 0000000..aa26114 --- /dev/null +++ b/smoke-test-egress.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +# Smoke test for the egress-restricted setup (docker-compose.yml + +# egress-proxy/). Confirms three things: +# 1. The app's landing page still works normally. +# 2. The app container has NO direct route to the internet. +# 3. The app container CAN reach api.anthropic.com, but nothing else, +# through the egress-proxy container. +# +# Run this from the repo root, with Docker running: +# bash smoke-test-egress.sh +# +# Needs a real ANTHROPIC_API_KEY in .env for step 3's second check to be +# meaningful (a bad/missing key still proves the tunnel works, since you'll +# get a real 401 from Anthropic instead of a connection failure - but a +# real key confirms the whole path end to end). + +set -uo pipefail +PASS=0 +FAIL=0 + +pass() { echo " PASS: $1"; PASS=$((PASS+1)); } +fail() { echo " FAIL: $1"; FAIL=$((FAIL+1)); } + +echo "== Building and starting the stack ==" +docker compose up -d --build +echo "Waiting for the app to come up..." +sleep 3 + +echo +echo "== Check 1: landing page loads ==" +CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5173/) +if [ "$CODE" = "200" ]; then + pass "landing page returned 200" +else + fail "landing page returned $CODE (expected 200)" +fi + +echo +echo "== Check 2: app container has no direct internet route ==" +# example.com is not api.anthropic.com and not reachable at all from this +# container now - not even DNS should resolve, since it's off "default". +if docker compose exec -T sde-meeting-toolkit wget -T 5 -qO- http://example.com/ >/dev/null 2>&1; then + fail "app container reached example.com directly - it should have no route out at all" +else + pass "app container could NOT reach example.com directly (expected)" +fi + +echo +echo "== Check 3: egress-proxy blocks an unrelated domain ==" +if docker compose exec -T sde-meeting-toolkit wget -T 5 -qO- \ + -e use_proxy=yes -e https_proxy=http://egress-proxy:8888 \ + https://example.com/ >/dev/null 2>&1; then + fail "egress-proxy allowed a request to example.com - filter is not restricting properly" +else + pass "egress-proxy refused example.com (expected - not on the allow-list)" +fi + +echo +echo "== Check 4: egress-proxy allows api.anthropic.com ==" +# We don't care about the HTTP status Anthropic itself returns here - just +# that the CONNECT tunnel succeeds instead of being refused by the proxy. +# wget still exits non-zero on a 4xx from the far end, so check for the +# specific "403 Forbidden" tinyproxy itself returns when IT refuses, +# versus any other outcome (which means the tunnel to Anthropic opened). +OUT=$(docker compose exec -T sde-meeting-toolkit wget -T 5 -S -O /dev/null \ + -e use_proxy=yes -e https_proxy=http://egress-proxy:8888 \ + https://api.anthropic.com/v1/messages 2>&1) +if echo "$OUT" | grep -q "403 Forbidden"; then + fail "egress-proxy refused api.anthropic.com - filter regex may not match, check egress-proxy/filter" +else + pass "egress-proxy allowed the tunnel to api.anthropic.com (got a response instead of a proxy-level refusal)" +fi + +echo +echo "== Check 5: the AI draft endpoint works end to end through the proxy ==" +# Uses whatever ANTHROPIC_API_KEY and MOCK_AI are set in .env. If MOCK_AI is +# true this only proves the server route works, not the egress path - set +# MOCK_AI=false with a real key for a full test. +RESP=$(curl -s -o /dev/null -w "%{http_code}" -X POST http://localhost:5173/api/claude \ + -H "Content-Type: application/json" \ + -d '{"model":"claude-sonnet-4-6","max_tokens":16,"messages":[{"role":"user","content":"say hi"}]}') +if [ "$RESP" = "200" ]; then + pass "/api/claude returned 200 (full round trip through the egress proxy worked)" +elif [ "$RESP" = "502" ]; then + fail "/api/claude returned 502 - egress path is broken, check EGRESS_PROXY_URL and egress-proxy logs" +else + echo " INFO: /api/claude returned $RESP - check whether this is expected (e.g. 401 for a bad key, 429 for rate limit)" +fi + +echo +echo "== Results: $PASS passed, $FAIL failed ==" +if [ "$FAIL" -gt 0 ]; then + echo "Check container logs for detail:" + echo " docker compose logs sde-meeting-toolkit" + echo " docker compose logs egress-proxy" + exit 1 +fi