The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(server): MCP refuses disallowed browser origins with 403 before auth and CORS (#35810)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 14, 2026 at 20:42 UTC
6c0032d655e2866bc2284c41ee5c260508ed7069
4 files changed
+51
-3
apps/server/src/http.ts
+11
-1
@@ -52,6 +52,7 @@ import {
52
browserApiCorsAllowedHeaders,
53
browserApiCorsAllowedMethods,
54
DESKTOP_RENDERER_ORIGINS,
55
+ MCP_HTTP_PATH,
56
} from "./httpCors.ts";
57
import { requestUrl } from "./requestUrl.ts";
58
@@ -258,7 +259,7 @@ export const layerBrowserApiCors = Layer.unwrap(
259
const config = yield* ServerConfig.ServerConfig;
260
const devOrigin = config.devUrl?.origin;
261
// Credentialed dev requests need explicit Vite/Electron origins; packaged desktop uses the default wildcard.
261
- return HttpRouter.cors({
262
+ const cors = HttpMiddleware.cors({
263
...(devOrigin
264
? {
265
allowedOrigins: [devOrigin, ...DESKTOP_RENDERER_ORIGINS, ...config.devAllowedOrigins],
@@ -269,6 +270,15 @@ export const layerBrowserApiCors = Layer.unwrap(
270
allowedHeaders: browserApiCorsAllowedHeaders,
271
maxAge: 600,
272
});
273
+ return HttpRouter.middleware(
274
+ (httpEffect) => {
275
+ const withCors = cors(httpEffect);
276
+ return Effect.flatMap(HttpServerRequest.HttpServerRequest, (request) =>
277
+ request.url.split("?", 1)[0] === MCP_HTTP_PATH ? httpEffect : withCors,
278
+ );
279
+ },
280
+ { global: true },
281
+ );
282
}),
283
);
284
apps/server/src/httpCors.ts
+3
@@ -11,3 +11,6 @@ export const browserApiCorsAllowedHeaders = [
11
] as const;
12
13
export const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"] as const;
14
+
15
+/** MCP refuses every browser Origin with 403, so the browser CORS layer must not answer for it. */
16
+export const MCP_HTTP_PATH = "/mcp";
apps/server/src/mcp/McpHttpServer.test.ts
+31
@@ -49,6 +49,7 @@ import * as DeviceService from "../device/DeviceService.ts";
49
import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts";
50
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
51
import * as ServerConfig from "../config.ts";
52
+import * as ServerHttp from "../http.ts";
53
import * as ServerSettings from "../serverSettings.ts";
54
import { advertisedJsonSchema } from "./advertisedJsonSchema.ts";
55
import * as McpHttpServer from "./McpHttpServer.ts";
@@ -1257,6 +1258,12 @@ it.effect("admits provider and OAuth client credentials and points only clients
1258
),
1259
}),
1260
),
1261
+ Layer.provide(ServerHttp.layerBrowserApiCors),
1262
+ Layer.provide(
1263
+ ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-origin-test-" }).pipe(
1264
+ Layer.provide(NodeServices.layer),
1265
+ ),
1266
+ ),
1267
);
1268
const traced = recordSpans();
1269
yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe(
@@ -1339,6 +1346,30 @@ it.effect("admits provider and OAuth client credentials and points only clients
1346
(span) => span.name.startsWith("mcp.tool.") || span.name.startsWith("McpServer."),
1347
),
1348
).toEqual([]);
1349
+
1350
+ // A present, disallowed Origin is refused with 403 before credentials or CORS answer.
1351
+ const foreignOrigin = (method: "POST" | "OPTIONS", token: string | undefined) =>
1352
+ httpClient.execute(
1353
+ HttpClientRequest.make(method)("/mcp", {
1354
+ headers: {
1355
+ origin: "https://untrusted.invalid",
1356
+ accept: "application/json, text/event-stream",
1357
+ "access-control-request-method": "POST",
1358
+ ...(token === undefined ? {} : { authorization: `Bearer ${token}` }),
1359
+ },
1360
+ ...(method === "POST"
1361
+ ? {
1362
+ body: HttpBody.text(
1363
+ `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`,
1364
+ "application/json",
1365
+ ),
1366
+ }
1367
+ : {}),
1368
+ }),
1369
+ );
1370
+ expect((yield* foreignOrigin("POST", providerToken)).status).toBe(403);
1371
+ expect((yield* foreignOrigin("POST", undefined)).status).toBe(403);
1372
+ expect((yield* foreignOrigin("OPTIONS", undefined)).status).toBe(403);
1373
}),
1374
).pipe(Effect.provide(NodeHttpServer.layerTest)),
1375
);
apps/server/src/mcp/McpHttpServer.ts
+6
-2
@@ -89,6 +89,7 @@ import * as HtmlHandlers from "./toolkits/html/handlers.ts";
89
import { MessageToolkit } from "./toolkits/message/tools.ts";
90
import * as MessageHandlers from "./toolkits/message/handlers.ts";
91
import { HtmlPreviewTool, HtmlPreviewToolkit, HtmlRenderToolkit } from "./toolkits/html/tools.ts";
92
+import { MCP_HTTP_PATH } from "../httpCors.ts";
93
import { requestUrl } from "../requestUrl.ts";
94
95
const mcpResourceMetadataUrl = (request: HttpServerRequest.HttpServerRequest) =>
@@ -279,7 +280,10 @@ const makeMcpAuthMiddleware = Effect.gen(function* () {
280
});
281
// Untraced: a span here would wrap the tool call; the HTTP server span already covers it.
282
return Effect.fnUntraced(function* (httpEffect) {
282
- const admitted = yield* authenticateRequest(yield* HttpServerRequest.HttpServerRequest);
283
+ const request = yield* HttpServerRequest.HttpServerRequest;
284
+ // The transport allows no browser Origin; refuse it before credentials, as the MCP spec requires.
285
+ if (request.headers.origin !== undefined) return HttpServerResponse.empty({ status: 403 });
286
+ const admitted = yield* authenticateRequest(request);
287
if (HttpServerResponse.isHttpServerResponse(admitted)) return admitted;
288
return yield* httpEffect.pipe(
289
Effect.provideService(McpInvocationContext.McpInvocationContext, admitted),
@@ -1327,7 +1331,7 @@ export const layerMcpTransport = <RA, RE, RR>(registrations: Layer.Layer<RA, RE,
1331
McpServer.layerHttp({
1332
name: "T3 Code",
1333
version: packageJson.version,
1330
- path: "/mcp",
1334
+ path: MCP_HTTP_PATH,
1335
// The first protocol is the fallback for clients that offer an unknown version.
1336
protocols: [McpProtocol.v2025_06_18, McpProtocol.v2026_07_28],
1337
allowSessionTermination: true,