Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(mcp): cap pending OAuth authorization requests

Andrej3da2ac5f31bf
diff --git a/apps/api/src/mcp/oauth-store.ts b/apps/api/src/mcp/oauth-store.ts
index da6615f7e..46b2a1435 100644
--- a/apps/api/src/mcp/oauth-store.ts
+++ b/apps/api/src/mcp/oauth-store.ts
@@ -1,4 +1,4 @@
-import { and, eq, lt } from "drizzle-orm";
+import { and, count, desc, eq, lt, notInArray } from "drizzle-orm";
import db from "../database";
import { mcpOauthStateTable } from "../database/schema";
@@ -52,3 +52,32 @@ export async function deleteExpiredStates(): Promise<void> {
.delete(mcpOauthStateTable)
.where(lt(mcpOauthStateTable.expiresAt, new Date()));
}
+
+// Evicts oldest-expiring rows so an insert after this call stays within maxRows.
+export async function enforceStateCap(
+ kind: OauthStateKind,
+ maxRows: number,
+): Promise<void> {
+ const [countRow] = await db
+ .select({ pending: count() })
+ .from(mcpOauthStateTable)
+ .where(eq(mcpOauthStateTable.kind, kind));
+
+ if ((countRow?.pending ?? 0) < maxRows) return;
+
+ const newest = db
+ .select({ key: mcpOauthStateTable.key })
+ .from(mcpOauthStateTable)
+ .where(eq(mcpOauthStateTable.kind, kind))
+ .orderBy(desc(mcpOauthStateTable.expiresAt))
+ .limit(maxRows - 1);
+
+ await db
+ .delete(mcpOauthStateTable)
+ .where(
+ and(
+ eq(mcpOauthStateTable.kind, kind),
+ notInArray(mcpOauthStateTable.key, newest),
+ ),
+ );
+}
diff --git a/apps/api/src/mcp/oauth.ts b/apps/api/src/mcp/oauth.ts
index 9e586cafb..5d4b2491e 100644
--- a/apps/api/src/mcp/oauth.ts
+++ b/apps/api/src/mcp/oauth.ts
@@ -5,6 +5,7 @@ import { sessionTable } from "../database/schema";
import {
consumeState,
deleteExpiredStates,
+ enforceStateCap,
getState,
putState,
} from "./oauth-store";
@@ -34,6 +35,8 @@ export type AuthorizationRequest = {
const clientTtlMs = 30 * 24 * 60 * 60 * 1000;
const codeTtlMs = 5 * 60 * 1000;
const requestTtlMs = 10 * 60 * 1000;
+// Same bound the in-memory store enforced; authorize is reachable without a session.
+const maxAuthorizationRequests = 10_000;
export async function getClient(
clientId: string,
@@ -71,6 +74,7 @@ export async function createAuthorizationRequest(
params: AuthorizationRequest,
): Promise<string> {
await deleteExpiredStates();
+ await enforceStateCap("request", maxAuthorizationRequests);
const requestId = randomUUID();
await putState(
"request",
diff --git a/tests/api-integration/mcp-oauth-store.test.ts b/tests/api-integration/mcp-oauth-store.test.ts
index dc28434f6..1029399e1 100644
--- a/tests/api-integration/mcp-oauth-store.test.ts
+++ b/tests/api-integration/mcp-oauth-store.test.ts
@@ -2,9 +2,11 @@ import { describe, expect, it } from "vitest";
import {
consumeState,
deleteExpiredStates,
+ enforceStateCap,
getState,
putState,
} from "../../apps/api/src/mcp/oauth-store";
+import { resetTestDatabase } from "./helpers/database";
describe("mcp oauth store", () => {
it("stores and returns state by kind and key", async () => {
@@ -42,4 +44,23 @@ describe("mcp oauth store", () => {
await deleteExpiredStates();
await expect(getState("request", `${key}-expired`)).resolves.toBeNull();
});
+
+ it("evicts the oldest rows of a kind once the cap is reached", async () => {
+ await resetTestDatabase();
+ const base = Date.now();
+ const payload = { clientId: "c" };
+ await putState("request", "cap-old", payload, new Date(base + 10_000));
+ await putState("request", "cap-mid", payload, new Date(base + 20_000));
+ await putState("request", "cap-new", payload, new Date(base + 30_000));
+ await putState("client", "cap-client", payload, new Date(base + 30_000));
+
+ await enforceStateCap("request", 100);
+ await expect(getState("request", "cap-old")).resolves.toEqual(payload);
+
+ await enforceStateCap("request", 3);
+ await expect(getState("request", "cap-old")).resolves.toBeNull();
+ await expect(getState("request", "cap-mid")).resolves.toEqual(payload);
+ await expect(getState("request", "cap-new")).resolves.toEqual(payload);
+ await expect(getState("client", "cap-client")).resolves.toEqual(payload);
+ });
});
diff --git a/tests/api/mcp-oauth-security.test.ts b/tests/api/mcp-oauth-security.test.ts
index 2c984f801..2a7210ade 100644
--- a/tests/api/mcp-oauth-security.test.ts
+++ b/tests/api/mcp-oauth-security.test.ts
@@ -59,6 +59,7 @@ vi.mock("../../apps/api/src/mcp/oauth-store", () => {
if (row.expiresAt.getTime() < Date.now()) return null;
return row.payload;
},
+ enforceStateCap: async () => {},
deleteExpiredStates: async () => {
const now = Date.now();
for (const [key, row] of rows) {