Repository navigation
feat(app-server): optional bearer token, Origin check and a security banner - #128
Conversation
…banner The Node server and the Docker image listen on loopback, but loopback keeps the server off the network, not off the machine: on a shared dev box every user and process can call the port, and Docker's plain -p 3001:3001 publishes it on every interface past ufw and firewalld. - AUTH_TOKEN / AUTH_TOKEN_FILE (Docker or Compose secret) require Authorization: Bearer <token> on /mcp. Off by default; compared in constant time (SHA-256 + timingSafeEqual), never logged, 401 with WWW-Authenticate; /health stays open. Both variables, an empty token or an unreadable file stop the server instead of running without auth. - The Origin header is validated, as the Streamable HTTP transport requires against DNS rebinding: requests without one pass (MCP clients outside a browser), browser requests only from http(s)://localhost, 127.0.0.1 and [::1] unless ALLOWED_ORIGINS lists others (* for any). 403 otherwise. The checks run before the body is parsed. - The startup banner states the bind address, whether auth is on, the allowed hosts and origins and whether it is safe on a shared machine, and warns (stderr) when it listens beyond loopback without a token, with the -p 127.0.0.1: rule for Docker. - README: a Security section with the threat model, the Docker firewall bypass, Compose secrets, how each documented client sends the header, reverse proxy, ALLOWED_HOSTS and the Origin check; the Docker Hub page, Dockerfile, root README and the image smoke test publish on 127.0.0.1. - test/http-security.test.js: the functions, and src/index.js started for real without and with a token (no/wrong/right token, the SDK client with the header, Origin allowed/refused, Host check, /health, no token in the output, empty AUTH_TOKEN refused). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| export function originAllowed(origin, allowed) | ||
| { | ||
| if (origin == null) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (allowed != null && allowed.any) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| const normalized = normalizeOrigin(origin); | ||
|
|
||
| if (normalized == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (allowed != null) | ||
| { | ||
| return allowed.origins.includes(normalized); | ||
| } | ||
|
|
||
| if (normalized === "null") | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| const url = new URL(normalized); | ||
|
|
||
| return (url.protocol === "http:" || url.protocol === "https:") && | ||
| LOOPBACK_ORIGIN_HOSTS.includes(url.hostname); |
There was a problem hiding this comment.
🟡 (optional) If a cloud host such as Claude.ai or ChatGPT sends an Origin header through a tunnel, its requests to a self-hosted server now get 403. On the base branch they were answered. With ALLOWED_ORIGINS unset, originAllowed accepts only http(s) loopback origins. Any other Origin, even from a server-side caller, is refused. Fix: make sure the documented tunnel setups still work under the default. Either confirm those hosts send no Origin and test that, or say in the 403 message and the "Connecting to Claude.ai" section to set ALLOWED_ORIGINS for them, while still refusing rebinding origins.
Why this was flagged
I could not confirm from this checkout which callers send an Origin header. The README sends users to tunnel the Node server for Claude.ai ("Connecting to Claude.ai", npx cloudflared tunnel) and lists ChatGPT custom MCP servers. Their requests reach app.use(originValidation(allowedOrigins)) at src/index.js:321. That middleware runs on every path before /mcp. When ALLOWED_ORIGINS is unset, parseAllowedOrigins returns null. originAllowed (http-security.js:222-254) then passes a request only if it has no Origin or its hostname is in LOOPBACK_ORIGIN_HOSTS. A host whose backend or proxy adds something like Origin: https://chatgpt.com or https://claude.ai gets 403 Forbidden: Origin ... is not allowed. On the base branch the same request reached the transport and worked. The only safeguard is the README's claim that "the cloud hosts behind a tunnel send none", and no code or test here checks it. The PR's "Open point" covers browser-based clients only, not these server-side hosts.
Verification: src/index.js:321 adds app.use(originValidation(allowedOrigins)) for every path. With ALLOWED_ORIGINS unset, originAllowed accepts only loopback origins, so https://claude.ai or https://chatgpt.com gets a 403. On the base, index.js had no Origin check, so those requests were answered. The new tests cover only loopback/refused origins from a local client.
There was a problem hiding this comment.
Left as is for now: with the defaults, a request with Origin: https://claude.ai or https://chatgpt.com already gets a 403 whose message (and the server log line) says to set ALLOWED_ORIGINS, refusing unknown origins by default is what the Streamable HTTP transport asks for against DNS rebinding, and whether those hosts send an Origin through a tunnel at all can only be checked against a real connector, so this stays open for that check.
…r token docs Fixes from Codex's review of #128, each reproduced by a test first: - CORS: an origin listed in ALLOWED_ORIGINS still couldn't connect. The browser's preflight carries no token, so bearerAuth answered it with 401, and no answer carried Access-Control-Allow-Origin. corsHeaders, after the Origin check and before the token, names the allowed origin, exposes Mcp-Session-Id and WWW-Authenticate and answers the preflight with 204. - ALLOWED_HOSTS that names no hostname (" , ") stops the server: parseList dropped the empty entries and the Host check was silently off. - Docs: docker run with a token uses --pull always and Compose pull_policy: always (a cached image from before tokens ignores AUTH_TOKEN), with a curl check for the 401; Claude Code gets the token from ${DRAWIO_MCP_TOKEN} in .mcp.json instead of a --header argument that other users can read in the process list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… ALLOWED_HOSTS With CORS headers for every origin, any web page can call the server and read its answers, and a page that calls localhost sends a Host that ALLOWED_HOSTS lists, so only the token keeps it out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
I went through the two follow-up commits and found no new bugs. They add authentication and CORS handling to the self-hosted server, so a human should still look before merging.
What was reviewed:
- The order of the middleware in
src/index.js: Host check, then Origin check, then CORS, then the bearer token on/mcp, then the body parser. A preflight gets its 204 only after the Origin check has passed. corsHeadersechoes back only an origin thatoriginAllowedaccepts and never setsAllow-Credentials, soALLOWED_ORIGINS=*does not let pages send credentials, and the banner now warns about that setting.readAuthTokenstops the server on a bad setting (both variables set, an empty token, an unreadable file), the token comparison hashes both sides beforetimingSafeEqual, andparseAllowedHostsstops the server on a value that names no hostname.
Extended reasoning...
The PR adds about 1,300 lines: a new access-control module (http-security.js) with an optional bearer token, an Origin check against DNS rebinding, CORS and a startup banner, plus how it is wired into the Node server, along with tests, Docker files and docs. This is an authentication and access-control surface, which rules out auto-approval. The two follow-up commits added CORS preflight handling ahead of the token check and stricter ALLOWED_HOSTS parsing, and those are new security-relevant code paths. My earlier inline thread about the default Origin policy is still open.
Still open from earlier reviews (1):
- Unresolved: 1 minor or pre-existing.
package.json, package-lock.json and server.json in lockstep; server.json keeps @drawio/mcp 1.13.1 under packages, released in the same cycle. The changelog's unreleased section becomes 1.11.0, with #125 and #128. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CUK3U3orJF9Q5x2VbPi9jX
Self-hosted app server (Node and the
jgraph/drawio-mcpDocker image): optional authentication, anOrigincheck and a startup banner that states the protection. The hosted endpoint and the Cloudflare Worker are unchanged.Loopback keeps the server off the network but not off the machine. On a shared dev box, any user or process can call the port. A plain
-p 3001:3001publishes it on every interface, and Docker's port rules bypass ufw and firewalld.Changes
AUTH_TOKEN/AUTH_TOKEN_FILE(the file works with Docker and Compose secrets):/mcpthen requiresAuthorization: Bearer <token>. Off by default, so existing setups keep working. The token is compared in constant time (SHA-256 +timingSafeEqual) and never logged. Refused requests get a 401 withWWW-Authenticate, and/healthstays open. The server stops at startup instead of running without auth if both variables are set, the token is empty or the file can't be read.Originvalidation against DNS rebinding, as the Streamable HTTP transport requires. Requests without anOriginheader pass (MCP clients outside a browser). Browser requests are accepted only fromhttp(s)://localhost,127.0.0.1and[::1]unlessALLOWED_ORIGINSlists others (*allows any origin). Others get a 403. These checks run before the body is parsed.Startup banner: shows the bind address, whether auth is on, the allowed hosts and origins, and whether the setup is safe on a shared machine. It warns on stderr when the server listens beyond loopback without a token and gives the
-p 127.0.0.1:rule for Docker.Docs: a new Security section in
mcp-app-server/README.mdcovers:ALLOWED_HOSTSand the Origin checkThe Docker Hub page, the
Dockerfile, the root README and the image smoke test now publish on127.0.0.1.Tests:
test/http-security.test.jstests the functions directly and also startssrc/index.jsfor real, without and with a token. It covers:Origin/healthAUTH_TOKENrefusedFixes from Codex's review
Each one was reproduced by a failing test first:
ALLOWED_ORIGINSstill couldn't connect. Its preflight carries no token, so it got a 401, and no response carriedAccess-Control-Allow-Origin. Now the preflight is answered with 204 after the Origin check and before the token check, and responses name the allowed origin and exposeMcp-Session-IdandWWW-Authenticate. Checked in headless Chrome as well.ALLOWED_HOSTSthat names no hostname (only commas or spaces) stops the server instead of silently turning the Host check off.docker runwith a token uses--pull always, and the Compose examples usepull_policy: always, because a cached image from before tokens would ignoreAUTH_TOKEN. Acurlcheck for the 401 follows. Claude Code now reads${DRAWIO_MCP_TOKEN}from.mcp.json, so the token is no longer passed in--header, where other users can see it in the process list.ALLOWED_ORIGINS=*without a token, also whenALLOWED_HOSTSis set: every origin now gets CORS headers.Testing
npm testinmcp-app-server/: 435 tests, 429 passed, 0 failed, 6 skipped. The skips are the usual environment ones (no live Worker URL, no routing drawio-mermaid build, no elkjs).Open point
A browser-based client that sends an
Originother than localhost now gets a 403 untilALLOWED_ORIGINSlists it.No version bump and nothing published (Docker Hub, npm, MCP Registry, Cloudflare); that's for the release cycle.
🤖 Generated with Claude Code