Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(security): accept OAuth tokens on /mcp only with connector sign-in on, and cap the body

spacesdriveac8b948ddee1
diff --git a/backend/controllers/mcpController.ts b/backend/controllers/mcpController.ts
index c5568fb..87fccd7 100644
--- a/backend/controllers/mcpController.ts
+++ b/backend/controllers/mcpController.ts
@@ -3,11 +3,18 @@ import { readConfig } from '../lib/config.ts';
import { RPC_ERROR, errorReply } from '../lib/mcp/protocol.ts';
import type { ContentfulStatusCode } from 'hono/utils/http-status';
+// a JSON-RPC message is a few kilobytes; anything far larger is refused before it is read into memory and parsed
+export const MAX_MCP_BODY_BYTES = 1024 * 1024;
+
// POST /mcp: one JSON-RPC message in, one JSON reply out, for the caller requireMcpAuth verified
export const handleMcpMessage = controller.createHandlers(async c => {
+ const tooLarge = () => c.json(errorReply(null, RPC_ERROR.invalidRequest, 'The request body is too large for one MCP message.', 413).body, 413);
+ if (Number(c.req.header('Content-Length') ?? 0) > MAX_MCP_BODY_BYTES) return tooLarge();
+ const text = await c.req.text();
+ if (new TextEncoder().encode(text).length > MAX_MCP_BODY_BYTES) return tooLarge();
let body: unknown;
try {
- body = await c.req.json();
+ body = JSON.parse(text);
} catch {
const reply = errorReply(null, RPC_ERROR.parse, 'The request body is not valid JSON.', 400);
return c.json(reply.body, 400);
diff --git a/backend/middleware/mcpAuth.ts b/backend/middleware/mcpAuth.ts
index c84abd9..aaf2e66 100644
--- a/backend/middleware/mcpAuth.ts
+++ b/backend/middleware/mcpAuth.ts
@@ -28,6 +28,8 @@ function failure(c: AppContext, status: 401 | 403, message: string): Response {
async function principalFor(c: AppContext, token: string): Promise<McpPrincipal> {
const access = servicesFor(c).mcpAccess;
if (isAccessToken(token)) return access.authenticateToken(token);
+ // without connector sign-in turned on, no Claude app was ever approved here, so only access tokens are accepted
+ if (!readConfig(c.env).mcp.oauthEnabled) throw new AppError('UNAUTHORIZED', 'This server takes access tokens from Settings, Claude. Connector sign-in is not turned on.');
const { data: { user }, error } = await getSupabaseAuth(c.env).auth.getUser(token);
if (error || !user) throw new AppError('UNAUTHORIZED', 'Your Claude connection has expired. Sign in again from Claude.');
// only tokens issued to an OAuth client are accepted, so an app session token copied from a browser cannot be replayed here
diff --git a/backend/tests/routes/mcpRoutes.test.ts b/backend/tests/routes/mcpRoutes.test.ts
index d1678cf..486ebbe 100644
--- a/backend/tests/routes/mcpRoutes.test.ts
+++ b/backend/tests/routes/mcpRoutes.test.ts
@@ -120,3 +120,19 @@ describe('access token management routes', () => {
assert.deepEqual(JSON.parse(info.body), { success: true, serverUrl: 'https://api.example.com/mcp', oauthEnabled: false });
});
});
+
+describe('MCP endpoint limits', () => {
+ test('a body larger than one MCP message is refused with 413 before it is parsed', async () => {
+ const { app } = endpoint([await tokenRow(READ_TOKEN, 'user-1', 'read')]);
+ const big = JSON.stringify({ ...rpc('tools/list'), padding: 'x'.repeat(1024 * 1024) });
+ const response = await app.request('/mcp', { method: 'POST', body: big, headers: { Authorization: `Bearer ${READ_TOKEN}`, 'Content-Type': 'application/json' } }, ENV);
+ assert.equal(response.status, 413);
+ });
+
+ test('a non-access token is refused while connector sign-in is off, without asking Supabase', async () => {
+ const { app } = endpoint([]);
+ const response = await callRoute(app, 'POST', '/mcp', { json: rpc('tools/list'), headers: { Authorization: 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEiLCJjbGllbnRfaWQiOiJjIn0.sig' }, env: ENV });
+ assert.equal(response.status, 401);
+ assert.match(response.body, /Connector sign-in is not turned on/);
+ });
+});