Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): validate session routes consistently

Josué7d27b70b8b60
diff --git a/mcp-host/src/server/__tests__/sessionsRoutes.test.ts b/mcp-host/src/server/__tests__/sessionsRoutes.test.ts
index 1224c1445..2ecab9005 100644
--- a/mcp-host/src/server/__tests__/sessionsRoutes.test.ts
+++ b/mcp-host/src/server/__tests__/sessionsRoutes.test.ts
@@ -201,6 +201,23 @@ describe('handleSessionsListRoute', () => {
expect(captured.statusCode).toBe(501)
})
+ it('validates a malformed cursor before reporting an unavailable list handler', async () => {
+ const req = {
+ ...makeReqWithAuth('user-1'),
+ query: { cursor: 'not-json' },
+ } as unknown as Request
+ const captured = makeRes()
+
+ await handleSessionsListRoute(
+ req,
+ captured.res,
+ makeHandlers({ sessionsListHandler: null })
+ )
+
+ expect(captured.statusCode).toBe(400)
+ expect(captured.jsonBody).toEqual({ error: 'Invalid sessions cursor' })
+ })
+
it('returns 401 if req.auth is missing', async () => {
const req = {} as Request
const captured = makeRes()
@@ -418,6 +435,23 @@ describe('handleSessionMessagesRoute', () => {
expect(captured.statusCode).toBe(501)
})
+ it('validates malformed pagination before reporting an unavailable messages handler', async () => {
+ const req = {
+ ...makeReqWithParams('user-1', 'chatllm', 'c1'),
+ query: { limit: '1.5' },
+ } as unknown as Request
+ const captured = makeRes()
+
+ await handleSessionMessagesRoute(
+ req,
+ captured.res,
+ makeHandlers({ sessionMessagesHandler: null })
+ )
+
+ expect(captured.statusCode).toBe(400)
+ expect(captured.jsonBody).toEqual({ error: 'limit must be a positive integer' })
+ })
+
it('returns 500 when sessionMessagesHandler throws', async () => {
const sessionMessagesHandler: SessionMessagesHandler = vi
.fn()
@@ -509,6 +543,7 @@ describe('handleContextBreakdownRoute (F1.5)', () => {
makeHandlers({ contextBreakdownHandler: vi.fn() })
)
expect(captured.statusCode).toBe(400)
+ expect(captured.jsonBody).toEqual({ error: 'Invalid agent or chatId' })
})
it('rejects unsafe route segments', async () => {
diff --git a/mcp-host/src/server/routes.ts b/mcp-host/src/server/routes.ts
index e0c69a351..9dd8d4a5f 100644
--- a/mcp-host/src/server/routes.ts
+++ b/mcp-host/src/server/routes.ts
@@ -1117,19 +1117,12 @@ export async function handleSessionsListRoute(
json(res, 401, { error: 'Missing rpc edge caller context' })
return
}
- if (!handlers.sessionsListHandler) {
- json(res, 501, { error: 'Sessions handler not configured' })
- return
- }
if (req.query.agent !== undefined && typeof req.query.agent !== 'string') {
badRequest(res, 'Invalid session agent')
return
}
const agent = typeof req.query.agent === 'string' ? req.query.agent.trim() : undefined
- if (
- agent !== undefined &&
- (!isSafeRouteSegment(agent) || agent.length > 200 || agent.includes(':'))
- ) {
+ if (agent !== undefined && !isSafeAgentRouteSegment(agent)) {
badRequest(res, 'Invalid session agent')
return
}
@@ -1146,6 +1139,10 @@ export async function handleSessionsListRoute(
badRequest(res, 'limit must be a positive integer')
return
}
+ if (!handlers.sessionsListHandler) {
+ json(res, 501, { error: 'Sessions handler not configured' })
+ return
+ }
const limit = rawLimit === undefined ? (cursor ? 50 : undefined) : Math.min(rawLimit, 100)
const result = await handlers.sessionsListHandler(caller.userId, { agent, limit, cursor })
json(res, 200, result)
@@ -1229,10 +1226,6 @@ export async function handleSessionMessagesRoute(
badRequest(res, 'Invalid agent or chatId')
return
}
- if (!handlers.sessionMessagesHandler) {
- json(res, 501, { error: 'Sessions handler not configured' })
- return
- }
const invalidQueryShape = ['limit', 'beforeTurn', 'afterTurn'].some(
key => req.query[key] !== undefined && typeof req.query[key] !== 'string'
)
@@ -1259,6 +1252,10 @@ export async function handleSessionMessagesRoute(
badRequest(res, 'limit must be a positive integer')
return
}
+ if (!handlers.sessionMessagesHandler) {
+ json(res, 501, { error: 'Sessions handler not configured' })
+ return
+ }
const limit =
rawLimit === undefined
? rawBeforeTurn !== undefined || rawAfterTurn !== undefined
@@ -1302,7 +1299,7 @@ export async function handleContextBreakdownRoute(
const agent = String(req.params.agent || '').trim()
const chatId = String(req.params.chatId || '').trim()
if (!isSafeAgentRouteSegment(agent) || !isSafeRouteSegment(chatId)) {
- badRequest(res, 'agent and chatId are required')
+ badRequest(res, 'Invalid agent or chatId')
return
}
if (!handlers.contextBreakdownHandler) {