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): keep OAuth requests valid outside UTC (#1795)

Tin Severc5cb1e864b47
diff --git a/apps/api/src/mcp/oauth-store.ts b/apps/api/src/mcp/oauth-store.ts
index 2be165b6d..487250ce9 100644
--- a/apps/api/src/mcp/oauth-store.ts
+++ b/apps/api/src/mcp/oauth-store.ts
@@ -16,10 +16,11 @@ type Transaction = Parameters<Parameters<typeof db.transaction>[0]>[0];
async function sweepExpired(dbOrTx: typeof db | Transaction) {
// Never issue an unbounded delete or evict a live consent request. Fixed
// rate-counter rows are reused rather than accumulated per time window.
+ // Drizzle stores these timezone-less timestamps as UTC; compare in UTC too.
await dbOrTx.execute(sql`
DELETE FROM ${mcpOauthStateTable} WHERE id IN (
SELECT id FROM ${mcpOauthStateTable}
- WHERE kind <> 'rate' AND expires_at <= now()
+ WHERE kind <> 'rate' AND expires_at <= (now() AT TIME ZONE 'UTC')
ORDER BY expires_at LIMIT ${EXPIRED_STATE_BATCH}
FOR UPDATE SKIP LOCKED
)
diff --git a/tests/api-integration/mcp-oauth-store.test.ts b/tests/api-integration/mcp-oauth-store.test.ts
index 87f9cc990..6dfc96182 100644
--- a/tests/api-integration/mcp-oauth-store.test.ts
+++ b/tests/api-integration/mcp-oauth-store.test.ts
@@ -1,5 +1,5 @@
import { and, count, eq, sql } from "drizzle-orm";
-import { beforeEach, describe, expect, it } from "vitest";
+import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
import db from "../../apps/api/src/database";
import { mcpOauthStateTable } from "../../apps/api/src/database/schema";
import {
@@ -12,6 +12,10 @@ import {
} from "../../apps/api/src/mcp/oauth-store";
import { resetTestDatabase } from "./helpers/database";
+// Set this before the lazy database pool opens any connections, including in CI.
+vi.stubEnv("PGOPTIONS", "-c timezone=Europe/Paris");
+afterAll(() => vi.unstubAllEnvs());
+
beforeEach(async () => {
await resetTestDatabase();
});
@@ -25,7 +29,7 @@ async function seed(
await db.execute(sql`INSERT INTO mcp_oauth_state (id,kind,key,payload,expires_at)
SELECT ${kind} || '-' || i, ${kind}, ${kind} || '-' || i,
jsonb_build_object('clientId', ${clientId}::text),
- CASE WHEN ${expired} THEN now() - interval '1 hour' ELSE now() + interval '1 hour' END
+ CASE WHEN ${expired} THEN (now() AT TIME ZONE 'UTC') - interval '1 hour' ELSE (now() AT TIME ZONE 'UTC') + interval '1 hour' END
FROM generate_series(1, ${total}::integer) AS i`);
}
async function total(kind: string) {
@@ -37,6 +41,32 @@ async function total(kind: string) {
}
describe("bounded shared MCP OAuth store", () => {
+ it("keeps live UTC timestamps through cleanup and consent approval in the configured database timezone", async () => {
+ const timezone = await db.execute<{ timezone: string }>(
+ sql`SELECT current_setting('TimeZone') AS timezone`,
+ );
+ expect(timezone.rows[0].timezone).toBe("Europe/Paris");
+ await putState("request", "live-request", { clientId: "client" }, future());
+ await db.insert(mcpOauthStateTable).values({
+ kind: "request",
+ key: "expired-request",
+ payload: {},
+ expiresAt: new Date(Date.now() - 60_000),
+ });
+ await deleteExpiredStates();
+ expect(await total("request")).toBe(1);
+ await putState(
+ "code",
+ "approved-code",
+ { clientId: "client" },
+ future(),
+ "live-request",
+ );
+ expect(await consumeState("code", "approved-code")).toEqual({
+ clientId: "client",
+ });
+ });
+
it("stores by kind and consumes exactly once", async () => {
const payload = { clientId: "client", userId: "user" };
await putState("code", "code", payload, future());