From 97a4b464473d247ce4e4c32e38c6db6dbe7bdf00 Mon Sep 17 00:00:00 2001 From: devinfoley <139239+devinfoley@users.noreply.github.com> Date: Tue, 1 Dec 2026 06:12:21 +0000 Subject: [PATCH] fix(tool-access): keep OAuth redirect-policy refusals out of Sentry An authorization server that answers dynamic client registration with 400 invalid_redirect_uri is applying its own redirect policy. Paperclip only sends callbacks built from trusted origins and already returns a 502 with remediation, so stop reporting this one case as a crash. Other registration 400s, unknown codes and non-400 rejections stay reported. Co-Authored-By: Claude Opus 5.5 --- .../oauth-dcr-failure-reporting.test.ts | 25 +++++++++++++++++++ server/src/middleware/error-handler.ts | 11 +++++++- .../oauth/provider-failure-diagnostics.ts | 16 ++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/server/src/__tests__/oauth-dcr-failure-reporting.test.ts b/server/src/__tests__/oauth-dcr-failure-reporting.test.ts index e6b9c445de4..5c8dfe66148 100644 --- a/server/src/__tests__/oauth-dcr-failure-reporting.test.ts +++ b/server/src/__tests__/oauth-dcr-failure-reporting.test.ts @@ -75,6 +75,31 @@ describe("OAuth dynamic client registration failure reporting", () => { expect(JSON.stringify(oauthProviderFailureDiagnosticsFor(reported))).not.toContain(PROVIDER_CANARY); }); + it("answers a provider's 400 invalid_redirect_uri with its remediation and no crash report", async () => { + const res = await request(createApp(400, { error: "invalid_redirect_uri", error_description: PROVIDER_CANARY })) + .post("/register"); + + expect(res.status).toBe(502); + expect(res.body).toMatchObject({ + error: "The authorization server rejected Paperclip's callback URL.", + code: "oauth_dynamic_client_registration_failed", + details: { providerError: "invalid_redirect_uri", status: 400 }, + }); + expect(JSON.stringify(res.body)).not.toContain(PROVIDER_CANARY); + expect(captureExceptionMock).not.toHaveBeenCalled(); + }); + + it.each([ + ["a 422 invalid_redirect_uri", 422, { error: "invalid_redirect_uri" }], + ["a 400 with an unrecognized error code", 400, { error: "redirect_rejected_by_policy" }], + ["a 400 with no error code", 400, {}], + ])("still reports %s", async (_label, status, body) => { + const res = await request(createApp(status, body)).post("/register"); + + expect(res.status).toBe(502); + expect(captureExceptionMock).toHaveBeenCalledTimes(1); + }); + it("still reports a successful response with unusable client metadata", async () => { const res = await request(createApp(201, { redirect_uris: [] })).post("/register"); diff --git a/server/src/middleware/error-handler.ts b/server/src/middleware/error-handler.ts index a58bd7eba1e..80c40c7e1cc 100644 --- a/server/src/middleware/error-handler.ts +++ b/server/src/middleware/error-handler.ts @@ -20,6 +20,7 @@ import { import { recordResponsibleUserDenialOnActiveRun } from "../services/responsible-user-denial-run-outcomes.js"; import { isObjectJournalBusyError } from "../services/object-byte-journal.js"; import { isExpectedMcpConnectionFailure } from "../services/mcp-connection-failure.js"; +import { isExpectedOAuthProviderFailure } from "../services/tool-access/oauth/provider-failure-diagnostics.js"; import { isOpenRouterCatalogUnavailable } from "../services/openrouter-models.js"; import { ENVIRONMENT_PROVIDER_UNAVAILABLE_CODE, readTransientSandboxCreationRefusal } from "../services/environment-provider-unavailable-error.js"; import { isEndedDbPoolError } from "../database-retry.js"; @@ -219,7 +220,15 @@ export function errorHandler( ); // An unreachable public provider catalog still answers 503 with its // retry guidance, but it is the provider's outage, not a server fault. - reportCrash(reportableError, req, !isExpectedMcpConnectionFailure(err) && !isOpenRouterCatalogUnavailable(err)); + // An OAuth provider refusing Paperclip's callback URL is its redirect + // policy; the person still gets the 502 and its remediation. + reportCrash( + reportableError, + req, + !isExpectedMcpConnectionFailure(err) + && !isOpenRouterCatalogUnavailable(err) + && !isExpectedOAuthProviderFailure(err), + ); } const secretSensitiveServerError = err.status >= 500 && diff --git a/server/src/services/tool-access/oauth/provider-failure-diagnostics.ts b/server/src/services/tool-access/oauth/provider-failure-diagnostics.ts index 89ecac8f7af..10af1064c29 100644 --- a/server/src/services/tool-access/oauth/provider-failure-diagnostics.ts +++ b/server/src/services/tool-access/oauth/provider-failure-diagnostics.ts @@ -16,3 +16,19 @@ export function recordOAuthProviderFailure(error: T, diagnostic export function oauthProviderFailureDiagnosticsFor(error: unknown): Readonly | null { return error instanceof Error ? recorded.get(error) ?? null : null; } + +/** + * A registration endpoint that refuses Paperclip's callback URL with 400 + * `invalid_redirect_uri` is applying its own redirect policy, such as an + * allowlist of approved clients or an HTTPS rule. Paperclip only sends a + * callback it built from a trusted origin, so it cannot change that answer; + * the person already gets remediation copy. Other registration 400s, such as + * `invalid_client_metadata` or an unrecognized code, can mean Paperclip sent + * bad metadata and stay reportable. + */ +export function isExpectedOAuthProviderFailure(error: unknown): boolean { + const diagnostics = oauthProviderFailureDiagnosticsFor(error); + return diagnostics?.operation === "dynamic_client_registration" + && diagnostics.httpStatus === 400 + && diagnostics.providerError === "invalid_redirect_uri"; +}