Skip to content

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

TypeScript

fix(mcp-host): reject an invalid sessions cursor in the session list handler

Alfredo Lopez7e56e5fc7895
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 7cacf157e..5778fe896 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.475",
+ "version": "0.1.476",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.475",
+ "version": "0.1.476",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 7265be5e5..ece16c828 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.475",
+ "version": "0.1.476",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/server/__tests__/sessionRouteHandlers.test.ts b/mcp-host/src/server/__tests__/sessionRouteHandlers.test.ts
index e71a4777d..7cccb357e 100644
--- a/mcp-host/src/server/__tests__/sessionRouteHandlers.test.ts
+++ b/mcp-host/src/server/__tests__/sessionRouteHandlers.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from 'vitest'
+import { describe, expect, it, vi } from 'vitest'
import { ConversationManager } from '../../core/conversation/conversation'
import { buildConnectRequiredApproval } from '../../core/extensions/mcpApprovalGateController'
import { BasicSafety } from '../../core/safety/safety'
@@ -112,6 +112,54 @@ describe('createSessionRouteHandlers — handleSessionsList (R1-L2)', () => {
})
})
+describe('createSessionRouteHandlers — handleSessionsList cursor validation', () => {
+ it('serves the next page for a valid cursor with exactly one store read (control)', async () => {
+ const { convManager, handleSessionsList } = makeHandlersUnderTest()
+ await seed(convManager, 'user-A:rpc:agent-x:chat-1')
+ await seed(convManager, 'user-A:rpc:agent-x:chat-2')
+ const first = await handleSessionsList('user-A', { agent: 'agent-x', limit: 1 })
+ expect(first.items).toHaveLength(1)
+ expect(first.nextCursor).toEqual(expect.any(String))
+
+ const listSpy = vi.spyOn(convManager, 'listSessionSummariesForUserAsync')
+ const second = await handleSessionsList('user-A', {
+ agent: 'agent-x',
+ limit: 1,
+ cursor: first.nextCursor,
+ })
+
+ expect(listSpy).toHaveBeenCalledTimes(1)
+ expect(second.items).toHaveLength(1)
+ expect(second.items[0]?.chatId).not.toBe(first.items[0]?.chatId)
+ })
+
+ it.each([
+ ['a malformed cursor', () => 'not-a-cursor'],
+ ['a cursor minted for another agent scope', (otherScopeCursor: string) => otherScopeCursor],
+ ])(
+ 'rejects %s instead of silently serving page 1, without reading the store',
+ async (_label, pick) => {
+ const { convManager, handleSessionsList } = makeHandlersUnderTest()
+ await seed(convManager, 'user-A:rpc:agent-x:chat-1')
+ await seed(convManager, 'user-A:rpc:agent-x:chat-2')
+ await seed(convManager, 'user-A:rpc:agent-y:chat-3')
+ await seed(convManager, 'user-A:rpc:agent-y:chat-4')
+ const otherScope = await handleSessionsList('user-A', { agent: 'agent-y', limit: 1 })
+ expect(otherScope.nextCursor).toEqual(expect.any(String))
+
+ const listSpy = vi.spyOn(convManager, 'listSessionSummariesForUserAsync')
+ await expect(
+ handleSessionsList('user-A', {
+ agent: 'agent-x',
+ limit: 1,
+ cursor: pick(otherScope.nextCursor as string),
+ })
+ ).rejects.toThrow('Invalid sessions cursor')
+ expect(listSpy).toHaveBeenCalledTimes(0)
+ }
+ )
+})
+
describe('createSessionRouteHandlers — handleSessionsList title projection (spec 15 A12)', () => {
async function seedWithTitle(
convManager: ConversationManager,
diff --git a/mcp-host/src/server/sessionRouteHandlers.ts b/mcp-host/src/server/sessionRouteHandlers.ts
index 65c44cc50..90dd2646e 100644
--- a/mcp-host/src/server/sessionRouteHandlers.ts
+++ b/mcp-host/src/server/sessionRouteHandlers.ts
@@ -91,6 +91,12 @@ export function createSessionRouteHandlers(deps: SessionRouteHandlerDeps) {
const keyPrefix = agent ? `${userSub}:rpc:${agent}:` : `${userSub}:rpc:`
const cursorScope = sessionsCursorScope(userSub, agent)
const cursor = decodeSessionsCursor(query.cursor, cursorScope)
+ // A cursor that does not decode for this scope is a caller error. Treating
+ // it as "no cursor" would silently restart the listing at page 1. The HTTP
+ // route rejects it with 400 before reaching here; this guards other callers.
+ if (query.cursor !== undefined && cursor === null) {
+ throw new Error('Invalid sessions cursor')
+ }
const entries = await convManager.listSessionSummariesForUserAsync(keyPrefix, {
limit: query.limit === undefined ? undefined : query.limit + 1,
cursor: cursor