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.
This commit is contained in:
27
server.js
27
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',
|
||||
|
||||
Reference in New Issue
Block a user