Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix error handling

Thiago Araujo da Silva73b06d3d0f90
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index ac8ddc8efc..28bc901de7 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -244,10 +244,14 @@ derives the status from it (400; 401 for `invalid_client`, 500 for `server_error
handler renders `{ error, error_description }`. When extending it:
- The handler's `try`/`catch` is the endpoint's only exit, and every `return` inside it is **awaited**: a
bare `return promise` would resolve outside the `catch` and escape in the house envelope.
-- `toOauthTokenError` defaults by **error class**, never message text: `UnauthorizedError` takes the
- rejected-grant code for that grant (`invalid_grant` on the redirect and refresh grants,
- `invalid_request` on token exchange, per RFC 8693 §2.2.2), `BadRequestError` takes `invalid_request`,
- and anything else becomes `server_error`, whose response carries no detail and whose original is logged.
+- `toOauthTokenError` defaults by **error class**, never message text: `UnauthorizedError` and
+ `ForbiddenRequestError` take the rejected-grant code for that grant (`invalid_grant` on the redirect and
+ refresh grants, `invalid_request` on token exchange, per RFC 8693 §2.2.2), `BadRequestError` takes
+ `invalid_request`, and anything else becomes `server_error`, whose response carries no detail and whose
+ original is logged. Both refusal classes are listed because the exchange rejects an unusable subject in
+ two vocabularies: `exchangeSubjectToken`'s own membership checks raise `UnauthorizedError`, while
+ `getOrgPermission` raises `ForbiddenRequestError` for the same kind of refusal. A new way to refuse a
+ subject belongs in one of those two classes, or it lands in `server_error` as an unexplained 500.
- Throw `OauthTokenError` directly where that default is wrong: client authentication
(`invalid_client`), a grant the client doesn't hold (`unauthorized_client`), a redirect-URI or PKCE
mismatch (`invalid_grant`), the unsupported `scope` (`invalid_scope`) and `audience`/`resource`
diff --git a/backend/src/services/oauth-client/oauth-token-error.test.ts b/backend/src/services/oauth-client/oauth-token-error.test.ts
index c702d39b02..74edcc52a0 100644
--- a/backend/src/services/oauth-client/oauth-token-error.test.ts
+++ b/backend/src/services/oauth-client/oauth-token-error.test.ts
@@ -1,4 +1,10 @@
-import { BadRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
+import {
+ BadRequestError,
+ ForbiddenRequestError,
+ NotFoundError,
+ PermissionBoundaryError,
+ UnauthorizedError
+} from "@app/lib/errors";
import { OauthGrantType } from "./oauth-client-types";
import { OauthTokenError, OauthTokenErrorCode, toErrorDescription, toOauthTokenError } from "./oauth-token-error";
@@ -46,6 +52,34 @@ describe("toOauthTokenError", () => {
expect(mapped.message).toBe("The subject token has expired.");
});
+ // getOrgPermission refuses a subject with ForbiddenRequestError where the exchange's own membership
+ // checks refuse it with UnauthorizedError. Both are the user being unable to act, not an outage.
+ test("maps a forbidden subject the same way as a rejected one", () => {
+ const forbidden = new ForbiddenRequestError({
+ message: "You are not a member of this organization with ID 8f2c."
+ });
+
+ const onExchange = toOauthTokenError(forbidden, OauthGrantType.TokenExchange);
+ expect(onExchange.oauthErrorCode).toBe(OauthTokenErrorCode.InvalidRequest);
+ expect(onExchange.statusCode).toBe(400);
+ expect(onExchange.message).toBe("You are not a member of this organization with ID 8f2c.");
+
+ expect(toOauthTokenError(forbidden, OauthGrantType.AuthorizationCode).oauthErrorCode).toBe(
+ OauthTokenErrorCode.InvalidGrant
+ );
+ });
+
+ // PermissionBoundaryError extends ForbiddenRequestError, so the one instanceof has to cover it.
+ test("maps a permission boundary failure the same way", () => {
+ const mapped = toOauthTokenError(
+ new PermissionBoundaryError({ message: "Insufficient permissions" }),
+ OauthGrantType.TokenExchange
+ );
+
+ expect(mapped.oauthErrorCode).toBe(OauthTokenErrorCode.InvalidRequest);
+ expect(mapped.statusCode).toBe(400);
+ });
+
test("maps a bad request to invalid_request, keeping its message", () => {
const mapped = toOauthTokenError(new BadRequestError({ message: "Missing 'subject_token'" }));
diff --git a/backend/src/services/oauth-client/oauth-token-error.ts b/backend/src/services/oauth-client/oauth-token-error.ts
index ac6afee8f0..bb1b73511a 100644
--- a/backend/src/services/oauth-client/oauth-token-error.ts
+++ b/backend/src/services/oauth-client/oauth-token-error.ts
@@ -1,4 +1,4 @@
-import { BadRequestError, UnauthorizedError } from "@app/lib/errors";
+import { BadRequestError, ForbiddenRequestError, UnauthorizedError } from "@app/lib/errors";
import { OauthGrantType } from "./oauth-client-types";
@@ -76,15 +76,19 @@ const REJECTED_GRANT_CODE_BY_GRANT_TYPE: Record<OauthGrantType, OauthTokenErrorC
// the message text. Sites where the class default is wrong throw `OauthTokenError` directly and pass
// through untouched.
//
-// `UnauthorizedError` here always means a grant we will not act on (a bad authorization code, refresh
-// token or subject token, or a subject we cannot resolve to a usable account), so it takes whichever code
-// that grant owes. Client authentication throws `invalid_client` explicitly instead, because there it is
-// the client that failed rather than the token. Anything else is a bug or an outage, so it becomes a
-// generic `server_error`: its message was written for a different envelope and could carry internals.
+// `UnauthorizedError` and `ForbiddenRequestError` both mean a grant we will not act on (a bad
+// authorization code, refresh token or subject token, or a subject we cannot resolve to a usable
+// account), so they take whichever code that grant owes. Both are needed because the exchange refuses an
+// unusable subject in two vocabularies: its own membership checks raise the first, while
+// `getOrgPermission` raises the second for the same kind of refusal, and mapping only one would answer
+// half of them with an unexplained 500. Client authentication throws `invalid_client` explicitly instead,
+// because there it is the client that failed rather than the token. Anything else is a bug or an outage,
+// so it becomes a generic `server_error`: its message was written for a different envelope and could
+// carry internals.
export const toOauthTokenError = (error: unknown, grantType?: OauthGrantType): OauthTokenError => {
if (error instanceof OauthTokenError) return error;
- if (error instanceof UnauthorizedError) {
+ if (error instanceof UnauthorizedError || error instanceof ForbiddenRequestError) {
const code = grantType ? REJECTED_GRANT_CODE_BY_GRANT_TYPE[grantType] : OauthTokenErrorCode.InvalidGrant;
return new OauthTokenError({ code, message: error.message, error });
diff --git a/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx b/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx
index a88fd1af09..035cd75102 100644
--- a/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx
+++ b/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx
@@ -106,7 +106,10 @@ const oauthClientFormSchema = z
flow: z.nativeEnum(OauthClientFlow),
redirectUris: z.string().trim(),
requirePkce: z.boolean(),
- accessTokenTtlValue: z.number().int().min(1, "Value must be at least 1"),
+ accessTokenTtlValue: z
+ .number({ invalid_type_error: "A lifetime value is required" })
+ .int()
+ .min(1, "Value must be at least 1"),
accessTokenTtlUnit: z.enum(["m", "h", "d", "w"], {
invalid_type_error: "Please select a valid time unit"
}),