Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: clean up revoked agent credentials

Richie McIlroyd66351e83550
diff --git a/apps/web/__tests__/e2e/agent-local-e2e.test.ts b/apps/web/__tests__/e2e/agent-local-e2e.test.ts
index 6681b8461..216f21324 100644
--- a/apps/web/__tests__/e2e/agent-local-e2e.test.ts
+++ b/apps/web/__tests__/e2e/agent-local-e2e.test.ts
@@ -1677,10 +1677,12 @@ agentE2e("Cap agent local Docker E2E", () => {
);
await connection.execute(
`INSERT INTO agent_api_keys
- (id, userId, tokenHash, name, scopes, expiresAt)
+ (id, userId, tokenHash, name, scopes, expiresAt, revokedAt)
VALUES
- ('key_e2e_old', ?, ?, 'Expired cleanup key', ?, DATE_SUB(NOW(), INTERVAL 31 DAY)),
- ('key_e2e_live2', ?, ?, 'Live cleanup key', ?, DATE_ADD(NOW(), INTERVAL 1 DAY))`,
+ ('key_e2e_old', ?, ?, 'Expired cleanup key', ?, DATE_SUB(NOW(), INTERVAL 31 DAY), NULL),
+ ('key_e2e_live2', ?, ?, 'Live cleanup key', ?, DATE_ADD(NOW(), INTERVAL 1 DAY), NULL),
+ ('key_e2e_rvold', ?, ?, 'Old revoked cleanup key', ?, DATE_ADD(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 31 DAY)),
+ ('key_e2e_rvnew', ?, ?, 'Recent revoked cleanup key', ?, DATE_ADD(NOW(), INTERVAL 1 DAY), NOW())`,
[
userId,
"5".repeat(64),
@@ -1688,6 +1690,12 @@ agentE2e("Cap agent local Docker E2E", () => {
userId,
"6".repeat(64),
JSON.stringify(["caps:read"]),
+ userId,
+ "7".repeat(64),
+ JSON.stringify(["caps:read"]),
+ userId,
+ "8".repeat(64),
+ JSON.stringify(["caps:read"]),
],
);
process.env.CRON_SECRET = "synthetic-agent-cleanup-secret";
@@ -1711,22 +1719,26 @@ agentE2e("Cap agent local Docker E2E", () => {
const deleted = asObject(cleanupBody.deleted);
expect(Number(deleted.authorizationCodes)).toBeGreaterThanOrEqual(1);
expect(Number(deleted.idempotencyRecords)).toBeGreaterThanOrEqual(1);
- expect(Number(deleted.accessTokens)).toBeGreaterThanOrEqual(1);
+ expect(Number(deleted.accessTokens)).toBeGreaterThanOrEqual(2);
const [remaining] = await connection.execute<RowDataPacket[]>(
`SELECT
(SELECT COUNT(*) FROM agent_api_authorization_codes WHERE id = 'cod_e2e_live') AS liveCodes,
(SELECT COUNT(*) FROM agent_api_idempotency WHERE id = 'ide_e2e_live') AS liveIdempotency,
(SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_live2') AS liveKeys,
+ (SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_rvnew') AS recentRevokedKeys,
(SELECT COUNT(*) FROM agent_api_authorization_codes WHERE id = 'cod_e2e_expired') AS expiredCodes,
(SELECT COUNT(*) FROM agent_api_idempotency WHERE id = 'ide_e2e_expired') AS expiredIdempotency,
- (SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_old') AS expiredKeys`,
+ (SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_old') AS expiredKeys,
+ (SELECT COUNT(*) FROM agent_api_keys WHERE id = 'key_e2e_rvold') AS oldRevokedKeys`,
);
expect(Number(remaining[0]?.liveCodes)).toBe(1);
expect(Number(remaining[0]?.liveIdempotency)).toBe(1);
expect(Number(remaining[0]?.liveKeys)).toBe(1);
+ expect(Number(remaining[0]?.recentRevokedKeys)).toBe(1);
expect(Number(remaining[0]?.expiredCodes)).toBe(0);
expect(Number(remaining[0]?.expiredIdempotency)).toBe(0);
expect(Number(remaining[0]?.expiredKeys)).toBe(0);
+ expect(Number(remaining[0]?.oldRevokedKeys)).toBe(0);
});
it("revokes every session only as the final destructive account action", async () => {
diff --git a/apps/web/lib/agent-api-cleanup.ts b/apps/web/lib/agent-api-cleanup.ts
index 37f705d5a..907526e0a 100644
--- a/apps/web/lib/agent-api-cleanup.ts
+++ b/apps/web/lib/agent-api-cleanup.ts
@@ -6,7 +6,7 @@ import {
agentApiIdempotency,
agentApiKeys,
} from "@cap/database/schema";
-import { lt } from "drizzle-orm";
+import { lt, or } from "drizzle-orm";
const affectedRows = (result: unknown) =>
Array.isArray(result)
@@ -40,7 +40,7 @@ export const cleanupExpiredAgentApiRecords = async (input?: {
input?.keyRetentionMs ?? 30 * 24 * 60 * 60 * 1_000,
0,
);
- const expiredKeyCutoff = new Date(now.getTime() - keyRetentionMs);
+ const keyRetentionCutoff = new Date(now.getTime() - keyRetentionMs);
const database = db();
const authorizationCodes = await deleteInBatches(
@@ -65,7 +65,12 @@ export const cleanupExpiredAgentApiRecords = async (input?: {
() =>
database
.delete(agentApiKeys)
- .where(lt(agentApiKeys.expiresAt, expiredKeyCutoff))
+ .where(
+ or(
+ lt(agentApiKeys.expiresAt, keyRetentionCutoff),
+ lt(agentApiKeys.revokedAt, keyRetentionCutoff),
+ ),
+ )
.limit(batchSize),
batchSize,
maxBatches,