Skip to content

feat(app-server): optional bearer token, Origin check and a security banner - #128

Merged
alderg merged 3 commits into
mainfrom
voice/app-server-auth-origin
Nov 23, 2026
Merged

alderg merged 3 commits into
mainfrom
voice/app-server-auth-origin

Conversation

@alderg

@alderg alderg commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Self-hosted app server (Node and the jgraph/drawio-mcp Docker image): optional authentication, an Origin check 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:3001 publishes 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): /mcp then requires Authorization: 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 with WWW-Authenticate, and /health stays 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.

  • Origin validation against DNS rebinding, as the Streamable HTTP transport requires. Requests without an Origin header pass (MCP clients outside a browser). Browser requests are accepted only from http(s)://localhost, 127.0.0.1 and [::1] unless ALLOWED_ORIGINS lists 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.md covers:

    • who can connect
    • Docker bypassing the firewall
    • Compose secrets
    • how each documented client sends the header (VS Code, Claude Code, Copilot CLI, Cursor, OpenCode; Claude.ai can't)
    • the reverse proxy for a team
    • ALLOWED_HOSTS and the Origin check

    The Docker Hub page, the Dockerfile, the root README and the image smoke test now publish on 127.0.0.1.

  • Tests: test/http-security.test.js tests the functions directly and also starts src/index.js for real, without and with a token. It covers:

    • no, wrong and right token
    • the SDK client sending the header
    • allowed and refused Origin
    • the Host check and /health
    • no token in the output
    • an empty AUTH_TOKEN refused

Fixes from Codex's review

Each one was reproduced by a failing test first:

  • CORS for allowed origins: a browser client listed in ALLOWED_ORIGINS still couldn't connect. Its preflight carries no token, so it got a 401, and no response carried Access-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 expose Mcp-Session-Id and WWW-Authenticate. Checked in headless Chrome as well.
  • ALLOWED_HOSTS that names no hostname (only commas or spaces) stops the server instead of silently turning the Host check off.
  • Docs: docker run with a token uses --pull always, and the Compose examples use pull_policy: always, because a cached image from before tokens would ignore AUTH_TOKEN. A curl check 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.
  • The banner now warns about ALLOWED_ORIGINS=* without a token, also when ALLOWED_HOSTS is set: every origin now gets CORS headers.

Testing

npm test in mcp-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 Origin other than localhost now gets a 403 until ALLOWED_ORIGINS lists it.

No version bump and nothing published (Docker Hub, npm, MCP Registry, Cloudflare); that's for the release cycle.

🤖 Generated with Claude Code

…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>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment on lines +222 to +254
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

alderg and others added 2 commits November 23, 2026 22:21
…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>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
  • corsHeaders echoes back only an origin that originAllowed accepts and never sets Allow-Credentials, so ALLOWED_ORIGINS=* does not let pages send credentials, and the banner now warns about that setting.
  • readAuthToken stops the server on a bad setting (both variables set, an empty token, an unreadable file), the token comparison hashes both sides before timingSafeEqual, and parseAllowedHosts stops 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.
@alderg
alderg merged commit 3076cd9 into main Nov 23, 2026
1 check passed
@alderg
alderg deleted the voice/app-server-auth-origin branch November 23, 2026 21:35
alderg added a commit that referenced this pull request Nov 23, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant