Skip to content

The open-source app everyone uses to manage agents at work

fix(tools): treat OAuth sign-in challenges as client errors (#13786)

Devin Foley878734a061c6
diff --git a/doc/observability.md b/doc/observability.md
index 15b0266ef9..3d1732e7ed 100644
--- a/doc/observability.md
+++ b/doc/observability.md
@@ -479,6 +479,10 @@ These fields contain build identifiers; they add no tenant or user identity.
- A Zod validation error, which answers 400.
- Each `HttpError` below status 500, such as 401, 403, 404, 409, and 422.
+- A remote app's recognized OAuth sign-in challenge. Connecting an app or
+ refreshing its catalog returns 422 with `oauth_challenge` and the existing
+ setup/reconnect links. Other upstream failures still return 502 and are
+ reported, including an unexplained upstream HTTP 400.
- A performance trace and a profile, because `tracesSampleRate` is 0.
### Operator responsibilities
diff --git a/server/src/__tests__/tool-access-service.test.ts b/server/src/__tests__/tool-access-service.test.ts
index 0c748429f4..0b63c21ae5 100644
--- a/server/src/__tests__/tool-access-service.test.ts
+++ b/server/src/__tests__/tool-access-service.test.ts
@@ -85,6 +85,7 @@ import {
} from "../services/tool-gateway.js";
import { toolAccessRoutes } from "../routes/tool-access.js";
import { errorHandler } from "../middleware/index.js";
+import * as sentry from "../sentry.js";
import type { VercelConnectClient } from "../services/vercel-connect.js";
import { invalidatePaperclipCloudConnectorCapabilities, type PaperclipCloudConnector } from "../services/paperclip-cloud-connector.js";
@@ -13758,7 +13759,7 @@ describeEmbeddedPostgres("tool access service", () => {
name: "Sign-in app",
});
- expect(res.status).toBe(502);
+ expect(res.status).toBe(422);
expect(res.body).toMatchObject({
error: "This app needs you to sign in.",
details: expect.objectContaining({ code: "oauth_challenge" }),
@@ -13767,6 +13768,77 @@ describeEmbeddedPostgres("tool access service", () => {
await expect(db.select().from(toolConnections)).resolves.toHaveLength(0);
});
+ it.each([
+ ["catalog", 401, 'Bearer realm="app"', 422, false],
+ ["catalog/refresh", 401, 'Bearer realm="app"', 422, false],
+ ["catalog", 400, null, 502, true],
+ ["catalog/refresh", 400, null, 502, true],
+ ["catalog", 503, null, 502, true],
+ ["catalog/refresh", 503, null, 502, true],
+ ] as const)(
+ "classifies %s upstream HTTP %i without hiding provider failures",
+ async (path, upstreamStatus, challenge, expectedStatus, reportable) => {
+ const company = await createCompany(db);
+ const [application] = await db
+ .insert(toolApplications)
+ .values({
+ companyId: company.id,
+ applicationKey: `catalog-status-${randomUUID()}`,
+ name: "Catalog status fixture",
+ type: "mcp_http",
+ status: "active",
+ })
+ .returning();
+ const [connection] = await db
+ .insert(toolConnections)
+ .values({
+ companyId: company.id,
+ applicationId: application!.id,
+ name: "Catalog status fixture",
+ uid: `test/${randomUUID()}`,
+ transport: "mcp_remote",
+ status: "draft",
+ enabled: false,
+ config: { url: "https://catalog-status.example.test/mcp" },
+ transportConfig: { url: "https://catalog-status.example.test/mcp" },
+ credentialSecretRefs: [],
+ })
+ .returning();
+ vi.spyOn(globalThis, "fetch").mockImplementation(async () =>
+ new Response(JSON.stringify({ error: "upstream request rejected" }), {
+ status: upstreamStatus,
+ headers: challenge ? { "www-authenticate": challenge } : {},
+ }),
+ );
+ const capture = vi
+ .spyOn(sentry, "captureException")
+ .mockImplementation(() => {});
+ const app = createRouteApp(db);
+ const url = `/api/tool-connections/${connection!.id}/${path}`;
+ const res = await (path === "catalog"
+ ? request(app).get(url)
+ : request(app).post(url));
+
+ expect(res.status).toBe(expectedStatus);
+ if (challenge) {
+ expect(res.body).toMatchObject({
+ error: "This app needs you to sign in.",
+ code: "oauth_challenge",
+ details: {
+ code: "oauth_challenge",
+ setupUrl: expect.any(String),
+ reconnectUrl: expect.any(String),
+ },
+ });
+ }
+ expect(capture).toHaveBeenCalledTimes(reportable ? 1 : 0);
+ await expect(
+ db.select().from(toolCatalogEntries)
+ .where(eq(toolCatalogEntries.connectionId, connection!.id)),
+ ).resolves.toHaveLength(0);
+ },
+ );
+
it.each([
[
"local_trusted",
@@ -13833,7 +13905,7 @@ describeEmbeddedPostgres("tool access service", () => {
.post(`/api/companies/${company.id}/tools/apps/connect`)
.send({ link: "https://8.8.8.8/mcp", name: "Redirect OAuth MCP" });
- expect(res.status).toBe(502);
+ expect(res.status).toBe(422);
expect(fetchMock).toHaveBeenCalledWith(
"https://8.8.8.8/.well-known/oauth-protected-resource",
expect.objectContaining({ redirect: "manual" }),
diff --git a/server/src/services/tool-access.ts b/server/src/services/tool-access.ts
index 8c5c3bbdd4..7ac8c5f597 100644
--- a/server/src/services/tool-access.ts
+++ b/server/src/services/tool-access.ts
@@ -2803,6 +2803,7 @@ function healthFailureHttpStatus(failure: {
code: string;
}): number {
if (failure.status === "missing_secret") return 422;
+ if (failure.code === "oauth_challenge") return 422;
if (failure.code === "user_authorization_required") return 422;
if (failure.code === "composio_broker_retired") return 422;
if (failure.code === "tool_connection_transport_unsupported") return 422;
@@ -6903,7 +6904,7 @@ export function toolAccessService(
})
.where(eq(toolConnections.id, connection.id));
}
- throw new HttpError(502, "This app needs you to sign in.", {
+ throw unprocessable("This app needs you to sign in.", {
code: "oauth_challenge",
status: response.status,
setupUrl: connectionSetupUrl(connection),