Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(mcp): allow X-Gbrain-Client in both HTTP transports' CORS preflight
garrytan284b0621b5fd
diff --git a/src/commands/serve-http-oauth.ts b/src/commands/serve-http-oauth.tsindex 1de7c17673..ef67274ad5 100644--- a/src/commands/serve-http-oauth.ts+++ b/src/commands/serve-http-oauth.ts@@ -18,6 +18,7 @@ import { GRANT_PROFILES } from '../core/grants/model.ts';import { publicHarnessMetadata } from '../core/harness/registry.ts';import { mcpAdministrationGuidance } from '../mcp/capabilities.ts';import { AGENT_LABEL_HEADER } from '../core/persistence/attribution.ts';+import { GBRAIN_CLIENT_HEADER } from '../mcp/result-rows.ts';import { VERSION } from '../version.ts';import type { ServeHttpContext } from './serve-http.ts';@@ -130,7 +131,7 @@ export function mountOAuth(app: Express, ctx: ServeHttpContext): void {origin: resolveCorsOrigin(corsAllowlistOAuth),credentials: false,methods: ['GET', 'POST', 'OPTIONS'],- allowedHeaders: ['Content-Type', 'Authorization', 'Accept', AGENT_LABEL_HEADER],+ allowedHeaders: ['Content-Type', 'Authorization', 'Accept', AGENT_LABEL_HEADER, GBRAIN_CLIENT_HEADER],};app.use('/mcp', cors(corsOAuthOptions));app.use('/authorize', cors(corsOAuthOptions));diff --git a/src/mcp/http-transport.ts b/src/mcp/http-transport.tsindex 07498d9428..b43ff18c32 100644--- a/src/mcp/http-transport.ts+++ b/src/mcp/http-transport.ts@@ -279,7 +279,7 @@ export async function startHttpTransport(opts: HttpTransportOptions) {headers['Vary'] = 'Origin';if (preflight) {headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS';- headers['Access-Control-Allow-Headers'] = `Content-Type, Authorization, Accept, ${AGENT_LABEL_HEADER}`;+ headers['Access-Control-Allow-Headers'] = `Content-Type, Authorization, Accept, ${AGENT_LABEL_HEADER}, ${GBRAIN_CLIENT_HEADER}`;}}return headers;diff --git a/test/mcp-cors-client-header.test.ts b/test/mcp-cors-client-header.test.tsnew file mode 100644index 0000000000..b566b54a3b--- /dev/null+++ b/test/mcp-cors-client-header.test.ts@@ -0,0 +1,65 @@+/**+ * #8683: a browser MCP client on an allowlisted origin may send X-Gbrain-Client.+ *+ * Protects: both HTTP transports (the OAuth serve --http mount and the legacy bearer transport) list the header in+ * their CORS preflight's Access-Control-Allow-Headers, from one shared list, so a browser client that names itself+ * is not blocked before its request is sent. The header selects result-row shape only, never authority.+ * Fails when: either transport's allow-list drops the header, or the two lists drift apart.+ * Seams: the mounted SDK transport and the legacy Bun transport over real sockets. PGLite only.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import type { Server } from 'node:http';+import type { AddressInfo } from 'node:net';+import express from 'express';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { startHttpTransport } from '../src/mcp/http-transport.ts';+import { RateLimiter } from '../src/mcp/rate-limit.ts';+import { GBRAIN_CLIENT_HEADER } from '../src/mcp/result-rows.ts';+import { buildServeHttpApp } from '../src/commands/serve-http.ts';+import { withEnv } from './helpers/with-env.ts';++describe('#8683 CORS preflight allows X-Gbrain-Client on both HTTP transports', () => {+ const ORIGIN = 'https://allowed.example';+ let engine: BrainEngine;+ let http: Server;+ let legacy: { port: number; stop: (force?: boolean) => unknown };++ beforeAll(async () => {+ const db = new PGLiteEngine();+ await db.connect({});+ await db.initSchema();+ engine = db;+ await withEnv({ GBRAIN_HTTP_CORS_ORIGIN: ORIGIN }, async () => {+ const app = express();+ http = await new Promise(resolve => { const listener = app.listen(0, '127.0.0.1', () => resolve(listener)); });+ const port = (http.address() as AddressInfo).port;+ await buildServeHttpApp(app, engine, { port, tokenTtl: 3600, enableDcr: false, publicUrl: 'http://127.0.0.1:' + port });+ legacy = await startHttpTransport({ port: 0, engine, surface: 'full', limiters: {+ ip: new RateLimiter({ limit: 10_000, windowMs: 60_000, lruCap: 100 }),+ token: new RateLimiter({ limit: 10_000, windowMs: 60_000, lruCap: 100 }),+ } }) as unknown as { port: number; stop: (force?: boolean) => unknown };+ });+ }, 120_000);++ afterAll(async () => {+ await legacy?.stop(true);+ if (http) await new Promise<void>(resolve => { http.closeAllConnections?.(); http.close(() => resolve()); });+ await engine.disconnect();+ });++ const allowed = async (port: number) => {+ const response = await fetch('http://127.0.0.1:' + port + '/mcp', { method: 'OPTIONS', headers: {+ Origin: ORIGIN, 'Access-Control-Request-Method': 'POST', 'Access-Control-Request-Headers': 'authorization, content-type, x-gbrain-client' } });+ expect(response.headers.get('access-control-allow-origin')).toBe(ORIGIN);+ return (response.headers.get('access-control-allow-headers') ?? '').toLowerCase().split(/\s*,\s*/);+ };++ test('the OAuth serve --http mount and the legacy transport allow the same headers, X-Gbrain-Client included', async () => {+ const oauth = await allowed((http.address() as AddressInfo).port);+ const bearer = await allowed(legacy.port);+ expect(oauth).toContain(GBRAIN_CLIENT_HEADER.toLowerCase());+ expect(bearer).toContain(GBRAIN_CLIENT_HEADER.toLowerCase());+ expect([...bearer].sort()).toEqual([...oauth].sort());+ });+});