Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Answer a refused credential revoke or rotation with 404 or 409, not 500 (#690)
Omkar Chebale6f5d8260dc69
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 318bf25b..45104457 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,13 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.## Unreleased+### Revoking a credential twice says so, instead of answering a server error++Revoking a credential that was already revoked, or that does not exist, now answers 404 with the+reason. Rotating one that is gone answers 404, and rotating one that is revoked or does not match+the key answers 409. Before, each answered a plain-text 500, as if the deployment were broken; a+double click on Revoke was enough to cause it. The refused-rotation audit row is written as before.+### The channel list no longer skips channels made in the same millisecondThe channel list's page cursor kept the last channel's time to the millisecond, while PostgreSQLdiff --git a/server/src/app.ts b/server/src/app.tsindex f5b64522..a3b04854 100644--- a/server/src/app.ts+++ b/server/src/app.ts@@ -61,7 +61,11 @@ import type { PageFrameStore } from "./computer/page-frames";import type { PolicyStore } from "./computer/policy-store";import { createComputerRoutes } from "./computer/routes";import { configuredAuthProviders, type DeploymentConfig } from "./config";-import type { CredentialAdminService, CredentialInput } from "./credentials";+import {+ type CredentialAdminService,+ type CredentialInput,+ CredentialRefusedError,+} from "./credentials";import type { Database } from "./db/client";import { withoutStatement } from "./db/query-failure";import {@@ -1082,12 +1086,19 @@ export function createApp(return context.json({ error: "Credential input is invalid." }, 400);}- return context.json({- credential: await credentialService.rotate({- ...input,- previousCredentialId: context.req.param("credentialId"),- }),- });+ try {+ return context.json({+ credential: await credentialService.rotate({+ ...input,+ previousCredentialId: context.req.param("credentialId"),+ }),+ });+ } catch (error) {+ if (error instanceof CredentialRefusedError) {+ return context.json({ error: error.message }, error.status);+ }+ throw error;+ }},);app.post(@@ -1105,12 +1116,20 @@ export function createApp();}- return context.json({- credential: await credentialService.revoke(- context.req.param("credentialId"),- context.var.actor.id,- ),- });+ try {+ return context.json({+ credential: await credentialService.revoke(+ context.req.param("credentialId"),+ context.var.actor.id,+ ),+ });+ } catch (error) {+ // Not found or already revoked: the second click on Revoke, answered as what it is.+ if (error instanceof CredentialRefusedError) {+ return context.json({ error: error.message }, error.status);+ }+ throw error;+ }},);app.get("/api/admin/package", requireUser, async (context) => {diff --git a/server/src/credentials.ts b/server/src/credentials.tsindex f467fbed..3cd384ec 100644--- a/server/src/credentials.ts+++ b/server/src/credentials.ts@@ -222,6 +222,24 @@ export class CredentialUnusableError extends Error {}}+/**+ * An administrator asked to revoke or rotate a credential that cannot be revoked or rotated as asked.+ *+ * The id names no credential, or one already revoked, or one that is not what the input describes.+ * It carries the status the route answers with. As a plain `Error` it left the route as a bare 500,+ * so a second click on Revoke read as the deployment being broken rather than the credential being+ * gone already.+ */+export class CredentialRefusedError extends Error {+ readonly status: 404 | 409;++ constructor(message: string, status: 404 | 409) {+ super(message);+ this.name = "CredentialRefusedError";+ this.status = status;+ }+}+export async function decryptCredentialForUse(encodedKey: string,reader: CredentialSecretReader,@@ -317,18 +335,25 @@ export function createCredentialStore(.where(eq(credentials.id, input.previousCredentialId)).for("update");if (!previous) {- throw new Error("Previous credential was not found");+ throw new CredentialRefusedError(+ "Previous credential was not found",+ 404,+ );}if (previous.revokedAt) {- throw new Error("Previous credential is already revoked");+ throw new CredentialRefusedError(+ "Previous credential is already revoked",+ 409,+ );}if (previous.kind !== input.kind ||previous.provider !== input.provider ||previous.keyId !== input.keyId) {- throw new Error(+ throw new CredentialRefusedError("Previous credential does not match the input's kind, provider or keyId",+ 409,);}@@ -390,7 +415,10 @@ export function createCredentialStore(.returning({ revokedAt: credentials.revokedAt });if (!credential?.revokedAt) {- throw new Error("Credential was not found or already revoked");+ throw new CredentialRefusedError(+ "Credential was not found or already revoked",+ 404,+ );}return credential.revokedAt;},diff --git a/server/tests/admin-people-credentials-validation.test.ts b/server/tests/admin-people-credentials-validation.test.tsindex f1a825eb..0271ef55 100644--- a/server/tests/admin-people-credentials-validation.test.ts+++ b/server/tests/admin-people-credentials-validation.test.ts@@ -1,6 +1,7 @@import { describe, expect, test } from "bun:test";import { createApp } from "../src/app";import { loadConfig } from "../src/config";+import { CredentialRefusedError } from "../src/credentials";import { decodeCursor } from "../src/people/store";import { testEnvironment } from "./support/environment";@@ -14,6 +15,8 @@ const ADMIN = {function appWith(opts: {peopleList?: (query: unknown) => Promise<unknown>;credentialCreate?: (input: unknown) => Promise<unknown>;+ credentialRevoke?: () => Promise<unknown>;+ credentialRotate?: () => Promise<unknown>;}) {const calls: { people: unknown[]; credentials: unknown[] } = {people: [],@@ -33,6 +36,8 @@ function appWith(opts: {if (opts.credentialCreate) return opts.credentialCreate(input);return { credential: input };},+ revoke: async () => opts.credentialRevoke?.(),+ rotate: async () => opts.credentialRotate?.(),};// createApp takes positional stores; peopleStore is the 18th parameter. Build the argument// list by index so a future appended store cannot silently shift it.@@ -128,6 +133,74 @@ describe("POST /api/admin/credentials input", () => {});});+/**+ * A revoke or rotation the vault refuses is answered as what it is.+ *+ * A second click on Revoke, or a rotation aimed at a gone or mismatched credential, used to leave+ * the route as a plain-text 500, as if the deployment were broken.+ */+describe("POST /api/admin/credentials/:id refusals", () => {+ const rotation = {+ kind: "mcp",+ provider: "acme",+ keyId: "key-1",+ plaintext: "secret",+ metadata: {},+ };+ const post = (app: ReturnType<typeof appWith>["app"], path: string) =>+ app.request(`http://openbot.test/api/admin/credentials/${path}`, {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify(rotation),+ });++ test("revoking a credential already revoked answers 404 with the reason", async () => {+ const { app } = appWith({+ credentialRevoke: async () => {+ throw new CredentialRefusedError(+ "Credential was not found or already revoked",+ 404,+ );+ },+ });++ const response = await post(app, "cred-1/revoke");++ expect(response.status).toBe(404);+ expect(await response.json()).toEqual({+ error: "Credential was not found or already revoked",+ });+ });++ test("rotating a credential already revoked answers 409 with the reason", async () => {+ const { app } = appWith({+ credentialRotate: async () => {+ throw new CredentialRefusedError(+ "Previous credential is already revoked",+ 409,+ );+ },+ });++ const response = await post(app, "cred-1/rotate");++ expect(response.status).toBe(409);+ expect(await response.json()).toEqual({+ error: "Previous credential is already revoked",+ });+ });++ test("any other failure is still a server error, not a refusal", async () => {+ const { app } = appWith({+ credentialRevoke: async () => {+ throw new Error("connection reset");+ },+ });++ expect((await post(app, "cred-1/revoke")).status).toBe(500);+ });+});+/*** A well-formed cursor carrying a non-date `lastSignedInAt` used to reach* `${cursor.lastSignedInAt}::timestamptz` in SQL and answer 500. It now falls back to thediff --git a/server/tests/credentials.test.ts b/server/tests/credentials.test.tsindex 0e278a58..771f4aba 100644--- a/server/tests/credentials.test.ts+++ b/server/tests/credentials.test.ts@@ -509,6 +509,11 @@ describe("credential store rotation", () => {});await expect(store.revoke(id)).rejects.toThrow("already revoked");+ // As a refusal the route can answer with a status, not an error it can only pass on as a 500.+ await expect(store.revoke(id)).rejects.toMatchObject({+ name: "CredentialRefusedError",+ status: 404,+ });});});