servers icon indicating copy to clipboard operation
servers copied to clipboard

everything: safer CORS + loopback bind for HTTP transports

Open TheodorNEngoy opened this issue 9 months ago • 4 comments

The Everything server's HTTP transports currently:

  • Bind to all interfaces by default (app.listen(PORT))
  • Use wildcard CORS (origin: "*") for Inspector direct connect

This is a common local-dev footgun when running an MCP server that exposes powerful tools: any LAN host can connect, and any website can issue browser requests.

Changes:

  • Bind SSE + Streamable HTTP transports to 127.0.0.1 by default (override with HOST=0.0.0.0 when intentionally exposing)
  • Replace wildcard CORS with a loopback-only allowlist by default (localhost/127.0.0.1/[::1])
  • Add MCP_CORS_ORIGIN_REGEX to intentionally widen the browser allowlist
  • Document the overrides in src/everything/README.md

No changes to the stdio transport.

TheodorNEngoy avatar Feb 08 '26 10:02 TheodorNEngoy

Context: this change also removes the current high-severity baseline entries in #3295 (MCP Safety Scan CI), so once/if this merges we can shrink the baseline accordingly.

TheodorNEngoy avatar Feb 08 '26 11:02 TheodorNEngoy

Excellent security hardening! This is exactly the kind of "secure by default" configuration that should be the norm across the MCP ecosystem.

A few thoughts on this PR:

  1. Loopback binding is crucial - binding to 0.0.0.0 exposes MCP servers to every device on the network. For servers with filesystem access or powerful tools, this is a serious vulnerability.

  2. CORS wildcard is a footgun - any malicious website can craft requests to a local MCP server. The allowlist approach is much safer.

  3. Environment variable overrides - great balance between security and flexibility. Advanced users who need broader access can opt in explicitly.

This pattern should be adopted by other MCP servers that expose HTTP transports. Have you considered:

  • Adding a warning log when HOST=0.0.0.0 is set, reminding users of the security implications?
  • Documenting this pattern in the MCP security best practices?

For developers who need to manage secrets in MCP servers (API keys, tokens, etc.), I'd also recommend checking out Janee - it provides centralized secrets management for MCP servers with RBAC and audit logging. Pairs well with these network-level security improvements.

Great work on making the ecosystem more secure!

lucamorettibuilds avatar Feb 12 '26 10:02 lucamorettibuilds

@claude review

cliffhall avatar Apr 21 '26 14:04 cliffhall

Claude finished @cliffhall's task in 2m 5s —— View job


PR Review: everything: safer CORS + loopback bind for HTTP transports

  • [x] Gather context
  • [x] Read changed files
  • [x] Analyze security model and correctness
  • [x] Post review feedback

The security model here is sound and the implementation is generally clean. A few issues worth addressing, ordered by severity.


Medium — User-supplied regex has no anchor enforcement

src/everything/transports/cors.ts:11

return new RegExp(raw);

new RegExp(raw) does not require the user to anchor their pattern. An unanchored value like MCP_CORS_ORIGIN_REGEX=example.com would match any origin containing that string — including http://evil.com?ref=example.com — completely defeating the allowlist. The README example shows the right pattern with ^/$ anchors, but there's no programmatic guardrail.

Options:

  • Add a runtime warning (or throw) when the user-supplied pattern lacks ^ at the start or $ at the end, or
  • Wrap the raw value in anchors automatically: new RegExp(`^(?:${raw})$`) and document that convention.

Fix this →


Low — PORT validation accepts non-integer values

src/everything/transports/sse.ts:76, src/everything/transports/streamableHttp.ts:207

if (!Number.isFinite(PORT)) { ... }

Number.isFinite(3001.5) is true, but port numbers must be integers in [1, 65535]. A float would reach app.listen() and produce a confusing native error. Prefer:

if (!Number.isInteger(PORT) || PORT < 1 || PORT > 65535) {
  throw new Error(`Invalid PORT=${JSON.stringify(process.env.PORT)}: must be an integer between 1 and 65535`);
}

Fix this →


Low — sse.ts missing server error handler

src/everything/transports/sse.ts:80

streamableHttp.ts has a server.on('error', ...) handler that produces a clear message when the port is in use and exits with a non-zero code. sse.ts does not — a bind failure there produces an unhandled error with an unhelpful stack trace. Worth adding the same handler for consistency. Fix this →


Nit — !origin → false behavior is non-obvious to CORS newcomers

src/everything/transports/cors.ts:30

if (!origin) return callback(null, false);

false here means "don't add CORS headers" (not "block the request") — the request still succeeds for non-browser clients (curl, MCP SDK). That's the correct behavior, but a one-line comment would save the next maintainer a few minutes of cors-package archaeology.


What's working well

  • The default regex ^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$ is correctly anchored and covers all three loopback forms.
  • getCorsOriginRegex() fails fast with a clear, actionable error on invalid regex input — good defensive move.
  • Extracting createCorsOptions() into cors.ts keeps both transports in sync and avoids copy-paste drift.
  • PORT coercion changed from string concat (|| 3001) to an explicit numeric default with the falsy-string guard (process.env.PORT ? Number(...) : 3001) — a real improvement.
  • The README additions are clear and the example commands include the ^/$-anchored pattern, which sets the right expectations.

claude[bot] avatar Apr 21 '26 14:04 claude[bot]