diff --git a/server/src/__tests__/agent-auth-identity-transfer.test.ts b/server/src/__tests__/agent-auth-identity-transfer.test.ts index 995464f7933..384ca18dd02 100644 --- a/server/src/__tests__/agent-auth-identity-transfer.test.ts +++ b/server/src/__tests__/agent-auth-identity-transfer.test.ts @@ -63,7 +63,7 @@ const support = await getEmbeddedPostgresTestSupport(); } finally { capture.flush(); } const reads = events.filter(event => event.event === "paperclip.test_db_response_capture.entry.v1" && event.table === "heartbeat_runs" && (event.operation === "select" || event.operation === "execute")); - expect(reads.reduce((sum, event) => sum + Number(event.calls), 0)).toBeGreaterThanOrEqual(3); + expect(reads.reduce((sum, event) => sum + Number(event.calls), 0)).toBe(1); expect(reads.reduce((sum, event) => sum + Number(event.responseValueBytes), 0)).toBeLessThan(16_384); expect((await captureRunIdentity(db, fixture)).run).toMatchObject(fixture.payloads); }); diff --git a/server/src/__tests__/agent-jwt-request-authentication.test.ts b/server/src/__tests__/agent-jwt-request-authentication.test.ts new file mode 100644 index 00000000000..88678fba4c4 --- /dev/null +++ b/server/src/__tests__/agent-jwt-request-authentication.test.ts @@ -0,0 +1,221 @@ +import { randomUUID } from "node:crypto"; +import express from "express"; +import request from "supertest"; +import { eq } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { + agents, + authUsers, + companies, + companyMemberships, + createDb, + heartbeatRunEvents, + heartbeatRuns, + issueComments, + issues, + runIdentityContexts, +} from "@paperclipai/db"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; +import { meterDatabaseStatements } from "./helpers/database-statement-meter.js"; +import { createLocalAgentJwt } from "../agent-auth-jwt.js"; +import { actorMiddleware } from "../middleware/auth.js"; +import { errorHandler } from "../middleware/error-handler.js"; +import { acceptSteeredIdentity, initializeRunIdentity, prepareSteeredIdentity, reserveSteeredIdentity } from "../services/run-identity.js"; + +const support = await getEmbeddedPostgresTestSupport(); + +(support.supported ? describe : describe.skip)("agent JWT request authentication", () => { + let database: Awaited>; + let db: ReturnType; + let meter: ReturnType; + + beforeAll(async () => { + vi.stubEnv("PAPERCLIP_AGENT_JWT_SECRET", "request-authentication-test-secret"); + vi.stubEnv("PAPERCLIP_INSTANCE_ID", "default"); + database = await startEmbeddedPostgresTestDatabase("paperclip-agent-jwt-request-auth-"); + db = createDb(database.connectionString); + meter = meterDatabaseStatements(db, database.connectionString); + }, 30_000); + + afterAll(async () => { + meter?.uninstall(); + vi.unstubAllEnvs(); + await db?.$client.end({ timeout: 5 }); + await database?.cleanup(); + }, 60_000); + + async function addUser(companyId: string, userId: string) { + await db.insert(authUsers).values({ id: userId, name: userId, email: `${userId}@example.test`, emailVerified: true, + createdAt: new Date(), updatedAt: new Date() }).onConflictDoNothing(); + await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: userId, + status: "active", membershipRole: "owner" }); + } + + async function seed() { + const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), runId = randomUUID(); + const owner = `owner-${companyId.slice(0, 8)}`, next = `next-${companyId.slice(0, 8)}`; + await db.insert(companies).values({ id: companyId, name: "Request authentication", issuePrefix: companyId.slice(0, 8) }); + await addUser(companyId, owner); + await addUser(companyId, next); + await db.insert(agents).values({ id: agentId, companyId, name: "Worker", role: "engineer", adapterType: "codex_local", + status: "running", adapterConfig: { instructions: "saved ".repeat(20_000) } }); + await db.insert(issues).values({ id: issueId, companyId, title: "Request authentication task", responsibleUserId: owner }); + await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "running", contextSnapshot: { issueId } }); + const identity = await initializeRunIdentity(db, { companyId, runId, issueId, messageIds: [], responsibleUserId: owner, + cause: "instruction" }); + const app = express(); + app.use(actorMiddleware(db, { deploymentMode: "authenticated", resolveSession: async () => null })); + app.get("/actor", (req, res) => res.json(req.actor)); + app.patch("/actor", (req, res) => res.json(req.actor)); + app.use(errorHandler); + const token = createLocalAgentJwt(agentId, companyId, "codex_local", runId, "stale-claim-user")!; + expect(token).toBeTruthy(); + return { companyId, agentId, issueId, runId, owner, next, identity, app, token }; + } + + type Fixture = Awaited>; + const get = (f: Fixture, token = f.token) => request(f.app).get("/actor").set("Authorization", `Bearer ${token}`); + const patch = (f: Fixture, token = f.token) => request(f.app).patch("/actor").set("Authorization", `Bearer ${token}`); + + it("authenticates a running agent with three statements and no transaction", async () => { + const f = await seed(); + const before = meter.read(); + meter.startLog(); + const response = await get(f); + const log = meter.stopLog(); + const after = meter.read(); + expect(response.status).toBe(200); + expect(response.body).toMatchObject({ + type: "agent", source: "agent_jwt", agentId: f.agentId, companyId: f.companyId, runId: f.runId, + identityContextId: f.identity.id, onBehalfOfUserId: f.owner, + onBehalfOfMemberships: [{ companyId: f.companyId, membershipRole: "owner", status: "active" }], + }); + const statements = after.statements - before.statements; + expect({ statements, transactions: after.transactions - before.transactions, waits: after.waits - before.waits }, + log.map(entry => entry.sql).join("\n---\n")).toEqual({ statements: 3, transactions: 0, waits: 3 }); + expect(after.bytesReceived - before.bytesReceived).toBeLessThan(8_192); + }); + + it("refuses the very next write after the run is cancelled, stopped or superseded", async () => { + const cases: Array<{ revoke: Partial; code: string }> = [ + { revoke: { status: "cancelled" }, code: "agent_run_cancelled" }, + { revoke: { resultJson: { executionCancellation: { state: "requested" } } }, code: "agent_run_cancelled" }, + { revoke: { resultJson: { freshContinuationSupersededAt: new Date().toISOString() } }, code: "agent_run_superseded" }, + ]; + for (const { revoke, code } of cases) { + const f = await seed(); + expect((await patch(f)).status).toBe(200); + await db.update(heartbeatRuns).set(revoke).where(eq(heartbeatRuns.id, f.runId)); + const refused = await patch(f); + expect(refused.status).toBe(403); + expect(refused.body.code).toBe(code); + } + }); + + it("uses the new responsible user on the request after steering is accepted", async () => { + const f = await seed(); + expect((await get(f)).body).toMatchObject({ identityContextId: f.identity.id, onBehalfOfUserId: f.owner }); + const messageId = randomUUID(); + await db.insert(issueComments).values({ id: messageId, companyId: f.companyId, issueId: f.issueId, + authorUserId: f.next, body: "Next instruction" }); + const pending = (await reserveSteeredIdentity(db, { ...f, messageId }))!; + await db.transaction(tx => acceptSteeredIdentity(tx as never, pending)); + const second = await get(f); + expect(second.status).toBe(200); + expect(second.body).toMatchObject({ identityContextId: pending.id, onBehalfOfUserId: f.next }); + }); + + function holdOpen(f: Fixture, work: (tx: Parameters[0]>[0]) => Promise) { + let release!: () => void; + const released = new Promise(resolve => { release = resolve; }); + let ready!: () => void; + const locked = new Promise(resolve => { ready = resolve; }); + const done = db.transaction(async tx => { + await tx.select({ id: issues.id }).from(issues).where(eq(issues.id, f.issueId)).for("no key update"); + await tx.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(eq(heartbeatRuns.id, f.runId)).for("no key update"); + await work(tx); + ready(); + await released; + }); + const holding = Promise.race([locked, done.then(() => undefined)]); + return { holding, release, done }; + } + + const settledWithin = (promise: Promise, ms: number) => + Promise.race([promise, new Promise<"pending">(resolve => setTimeout(() => resolve("pending"), ms))]); + + async function acknowledge(f: Fixture, messageId: string) { + await db.insert(heartbeatRunEvents).values({ companyId: f.companyId, agentId: f.agentId, runId: f.runId, + seq: 1, eventType: "item.completed", sourceEventId: randomUUID(), + payload: { prpEvent: { turnId: "auth-turn", itemId: `auth-turn:steer:${messageId}`, + payload: { kind: "steering_acknowledgement" } } } }); + } + + it("does not wait on an uncommitted steering reservation and holds the next request until it is acknowledged", async () => { + const f = await seed(); + const messageId = randomUUID(); + await db.insert(issueComments).values({ id: messageId, companyId: f.companyId, issueId: f.issueId, + authorUserId: f.next, body: "Concurrent instruction" }); + let pendingId = ""; + const writer = holdOpen(f, async tx => { + pendingId = (await prepareSteeredIdentity(tx as never, { ...f, messageId })).id; + }); + await writer.holding; + try { + const during = await settledWithin(get(f), 3_000); + expect(during).not.toBe("pending"); + expect((during as request.Response).status).toBe(200); + expect((during as request.Response).body).toMatchObject({ identityContextId: f.identity.id, onBehalfOfUserId: f.owner }); + } finally { + writer.release(); + await writer.done; + } + expect((await get(f)).status).toBe(409); + await acknowledge(f, messageId); + expect((await get(f)).body).toMatchObject({ identityContextId: pendingId, onBehalfOfUserId: f.next }); + }); + + it("waits for an acceptance in flight and returns the identity it commits", async () => { + const f = await seed(); + const messageId = randomUUID(); + await db.insert(issueComments).values({ id: messageId, companyId: f.companyId, issueId: f.issueId, + authorUserId: f.next, body: "Accepted instruction" }); + const pending = (await reserveSteeredIdentity(db, { ...f, messageId }))!; + const writer = holdOpen(f, tx => acceptSteeredIdentity(tx as never, pending)); + await writer.holding; + const response = get(f).then(result => result); + try { + expect(await settledWithin(response, 500)).toBe("pending"); + } finally { + writer.release(); + await writer.done; + } + expect((await response).body).toMatchObject({ identityContextId: pending.id, onBehalfOfUserId: f.next }); + }); + + it("keeps the identity of the run the token names, in the company and for the agent it names", async () => { + const f = await seed(); + const other = await seed(); + const crossCompany = createLocalAgentJwt(other.agentId, f.companyId, "codex_local", other.runId, f.owner)!; + expect((await get(f, crossCompany)).status).toBe(401); + const foreignRun = createLocalAgentJwt(f.agentId, f.companyId, "codex_local", other.runId, null)!; + const foreign = await get(f, foreignRun); + expect(foreign.status).toBe(200); + expect(foreign.body).toMatchObject({ runId: other.runId, identityContextId: null, onBehalfOfUserId: null, + onBehalfOfMemberships: [] }); + await db.update(heartbeatRuns).set({ activeIdentityContextId: other.identity.id }).where(eq(heartbeatRuns.id, f.runId)); + const mismatched = await get(f); + expect(mismatched.body).toMatchObject({ identityContextId: null, onBehalfOfUserId: "stale-claim-user" }); + const [context] = await db.select().from(runIdentityContexts).where(eq(runIdentityContexts.id, other.identity.id)); + expect(context.runId).toBe(other.runId); + }); + + it("still settles a pending steering identity through the locked capture", async () => { + const f = await seed(); + const messageId = randomUUID(); + await db.insert(issueComments).values({ id: messageId, companyId: f.companyId, issueId: f.issueId, + authorUserId: f.next, body: "Pending instruction" }); + await reserveSteeredIdentity(db, { ...f, messageId }); + expect((await get(f)).status).toBe(409); + }); +}); diff --git a/server/src/middleware/agent-run-authentication.ts b/server/src/middleware/agent-run-authentication.ts index 58634b3702b..74eb4c7f42a 100644 --- a/server/src/middleware/agent-run-authentication.ts +++ b/server/src/middleware/agent-run-authentication.ts @@ -1,5 +1,6 @@ import { and, eq, sql } from "drizzle-orm"; import { heartbeatRuns, type Db } from "@paperclipai/db"; +import { requestRunIdentityFields } from "../services/run-identity.js"; export function loadAgentRunAuthentication(db: Db, input: { runId: string; @@ -19,6 +20,7 @@ export function loadAgentRunAuthentication(db: Db, input: { contextSnapshot: sql>`jsonb_build_object( 'conversationMode', ${heartbeatRuns.contextSnapshot}->'conversationMode' )`, + ...requestRunIdentityFields, }).from(heartbeatRuns).where(and( eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.companyId, input.companyId), diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts index 6f0ccb106fe..b8106d6fdb3 100644 --- a/server/src/middleware/auth.ts +++ b/server/src/middleware/auth.ts @@ -2,7 +2,7 @@ import { notifyMuseContactVerification } from "../modules/external-agents/index. import { idleApiActivity } from "../services/idle-api-activity.js"; import { createHash, timingSafeEqual } from "node:crypto"; import type { Request, RequestHandler } from "express"; -import { and, eq, isNull, sql } from "drizzle-orm"; +import { and, eq, exists, isNull, sql } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; import { activityLog, @@ -29,7 +29,7 @@ import { isUuidLike, normalizeAgentApiKeyScope, type DeploymentMode } from "@pap import type { BetterAuthSessionResult } from "../auth/better-auth.js"; import { logger } from "./logger.js"; import { localAgentProcessBoundary, type LocalAgentProcessBoundary } from "./local-agent-process-boundary.js"; -import { captureRunIdentity } from "../services/run-identity.js"; +import { resolveRequestRunIdentity } from "../services/run-identity.js"; import { boardAuthService } from "../services/board-auth.js"; import { retryIdempotentDatabaseOperation } from "../database-retry.js"; import { beginIdleTrackedWork } from "../services/task-admission.js"; @@ -70,53 +70,33 @@ function invalidAgentTokenMessage(token: string) { return "Agent token did not verify; obtain fresh credentials and retry"; } -async function resolveLegacyRunResponsibleUserId( - db: Db, - input: { companyId: string; agentId: string; runId: string }, -) { - if (!isUuidLike(input.runId)) return null; - const run = await db - .select({ responsibleUserId: heartbeatRuns.responsibleUserId }) - .from(heartbeatRuns) - .where( - and( - eq(heartbeatRuns.id, input.runId), - eq(heartbeatRuns.companyId, input.companyId), - eq(heartbeatRuns.agentId, input.agentId), - ), - ) - .then((rows) => rows[0] ?? null); - return normalizeOptionalString(run?.responsibleUserId); -} +const agentAuthenticationFields = { + companyId: agents.companyId, + status: agents.status, + lifecycleHolds: agents.lifecycleHolds, +}; async function loadResponsibleUserMemberships( db: Db, input: { companyId: string; userId: string | null }, ) { if (!input.userId) return []; - const [user, memberships] = await Promise.all([ - db - .select({ id: authUsers.id }) - .from(authUsers) - .where(eq(authUsers.id, input.userId)) - .then((rows) => rows[0] ?? null), - db - .select({ - companyId: companyMemberships.companyId, - membershipRole: companyMemberships.membershipRole, - status: companyMemberships.status, - }) - .from(companyMemberships) - .where( - and( - eq(companyMemberships.companyId, input.companyId), - eq(companyMemberships.principalType, "user"), - eq(companyMemberships.principalId, input.userId), - eq(companyMemberships.status, "active"), - ), + return db + .select({ + companyId: companyMemberships.companyId, + membershipRole: companyMemberships.membershipRole, + status: companyMemberships.status, + }) + .from(companyMemberships) + .where( + and( + eq(companyMemberships.companyId, input.companyId), + eq(companyMemberships.principalType, "user"), + eq(companyMemberships.principalId, input.userId), + eq(companyMemberships.status, "active"), + exists(db.select({ id: authUsers.id }).from(authUsers).where(eq(authUsers.id, input.userId))), ), - ]); - return user ? memberships : []; + ); } /** @@ -323,7 +303,8 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa return; } - const boardKey = await boardAuth.findBoardApiKeyByToken(token); + const verifiedClaims = verifyLocalAgentJwt(token); + const boardKey = verifiedClaims ? null : await boardAuth.findBoardApiKeyByToken(token); if (boardKey) { const access = await boardAuth.resolveBoardAccess(boardKey.userId); if (access.user) { @@ -348,22 +329,21 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa } } - const tokenHash = hashToken(token); - const key = await db + const key = verifiedClaims ? null : await db .select() .from(agentApiKeys) - .where(and(eq(agentApiKeys.keyHash, tokenHash), isNull(agentApiKeys.revokedAt))) + .where(and(eq(agentApiKeys.keyHash, hashToken(token)), isNull(agentApiKeys.revokedAt))) .then((rows) => rows[0] ?? null); if (!key) { - const claims = verifyLocalAgentJwt(token); + const claims = verifiedClaims; if (!claims) { next(unauthorized(invalidAgentTokenMessage(token))); return; } const agentRecord = await db - .select() + .select(agentAuthenticationFields) .from(agents) .where(eq(agents.id, claims.sub)) .then((rows) => rows[0] ?? null); @@ -422,11 +402,9 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa code: conversation ? "conversation_turn_cancelled" : "agent_run_cancelled" }); return; } - if (identityRun?.activeIdentityContextId && identityRun.status === "running") { - const captured = await captureRunIdentity(db, { companyId: claims.company_id, agentId: claims.sub, runId: claims.run_id }, - { readScope: "request_authentication" }); - identityRun.activeIdentityContextId = captured.context?.id ?? null; - identityRun.responsibleUserId = captured.context?.responsibleUserId ?? null; + if (identityRun) { + Object.assign(identityRun, await resolveRequestRunIdentity(db, + { companyId: claims.company_id, agentId: claims.sub, runId: claims.run_id }, identityRun)); } if (claims.authorizing_user_id) { // This signed projection exists only for internal native tools. Recheck @@ -455,11 +433,9 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa ? identityRun.responsibleUserId : claims.responsible_user_id !== undefined ? normalizeOptionalString(claims.responsible_user_id) - : await resolveLegacyRunResponsibleUserId(db, { - companyId: claims.company_id, - agentId: claims.sub, - runId: claims.run_id, - }); + : isUuidLike(claims.run_id) + ? normalizeOptionalString(identityRun?.responsibleUserId) + : null; const onBehalfOfMemberships = await loadResponsibleUserMemberships(db, { companyId: claims.company_id, userId: onBehalfOfUserId, @@ -488,7 +464,7 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa .where(eq(agentApiKeys.id, key.id)); const agentRecord = await db - .select() + .select(agentAuthenticationFields) .from(agents) .where(eq(agents.id, key.agentId)) .then((rows) => rows[0] ?? null); diff --git a/server/src/services/run-identity.ts b/server/src/services/run-identity.ts index e9ebc3219ed..18edcdfeaa9 100644 --- a/server/src/services/run-identity.ts +++ b/server/src/services/run-identity.ts @@ -76,6 +76,20 @@ export async function explicitOperatorRunIdentity( } export type RunIdentityContext = typeof runIdentityContexts.$inferSelect; + +function assertRunTaskIdentity(run: { + nativeIssueId?: string | null; + issueId?: string | null; + contextSnapshot?: Record | null; +} | undefined) { + const issueId = run?.nativeIssueId ?? run?.issueId ?? run?.contextSnapshot?.issueId ?? run?.contextSnapshot?.taskId; + const legacyIssueId = run?.contextSnapshot?.issueId ?? run?.contextSnapshot?.taskId; + if ([issueId, legacyIssueId].some(id => id !== undefined && id !== null && + (typeof id !== "string" || !isUuidLike(id)))) { + throw forbidden("Run task identity is invalid"); + } + return issueId; +} type Executor = Pick; /** @@ -100,12 +114,7 @@ async function lockIdentityTask( .where( and(eq(heartbeatRuns.id, runId), eq(heartbeatRuns.companyId, companyId)), ); - const issueId = run?.nativeIssueId ?? run?.issueId ?? run?.contextSnapshot?.issueId ?? run?.contextSnapshot?.taskId; - const legacyIssueId = run?.contextSnapshot?.issueId ?? run?.contextSnapshot?.taskId; - if ([issueId, legacyIssueId].some(id => id !== undefined && id !== null && - (typeof id !== "string" || !isUuidLike(id)))) { - throw forbidden("Run task identity is invalid"); - } + const issueId = assertRunTaskIdentity(run); if (typeof issueId === "string") await executor .select({ id: issues.id }) @@ -614,6 +623,43 @@ export async function captureRunIdentity`exists (select 1 from run_identity_contexts pending_context + where pending_context.run_id = "heartbeat_runs"."id" and pending_context.status = 'pending')`, + identityContext: sql<{ id: string; responsibleUserId: string | null } | null>`(select + jsonb_build_object('id', active_context.id, 'responsibleUserId', active_context.responsible_user_id) + from run_identity_contexts active_context + where active_context.id = "heartbeat_runs"."active_identity_context_id" + and active_context.run_id = "heartbeat_runs"."id")`, +}; + +type RequestRunIdentityRow = Pick & { + nativeIssueId?: string | null; + issueId?: string | null; + taskContext?: Record | null; + pendingIdentityContext?: boolean | null; + identityContext?: { id: string; responsibleUserId: string | null } | null; +}; + +export async function resolveRequestRunIdentity( + db: Db, + input: { companyId: string; runId: string; agentId: string }, + run: RequestRunIdentityRow, +): Promise<{ activeIdentityContextId: string | null; responsibleUserId: string | null }> { + if (!run.activeIdentityContextId || run.status !== "running") { + return { activeIdentityContextId: run.activeIdentityContextId, responsibleUserId: run.responsibleUserId }; + } + if (run.pendingIdentityContext) { + const captured = await captureRunIdentity(db, input, { readScope: "request_authentication" }); + return { activeIdentityContextId: captured.context?.id ?? null, responsibleUserId: captured.context?.responsibleUserId ?? null }; + } + assertRunTaskIdentity({ nativeIssueId: run.nativeIssueId, issueId: run.issueId, contextSnapshot: run.taskContext }); + return { activeIdentityContextId: run.identityContext?.id ?? null, responsibleUserId: run.identityContext?.responsibleUserId ?? null }; +} + export async function listRunIdentityContexts( db: Db, companyId: string,