Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix error code on missing grant
Thiago Araujo da Silva93ec240cf00d
diff --git a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.tsindex d1d061d7ad..50f0ce9a3e 100644--- a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts+++ b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts@@ -269,6 +269,19 @@ describe("POST /api/v1/oauth/token, token exchange grant", async () => {expectOauthError(res, 400, "unsupported_grant_type", "grant_type");});+ // RFC 6749 section 5.2 keeps these apart: a parameter the caller forgot is `invalid_request`, and+ // `unsupported_grant_type` is reserved for a grant type we do not implement.+ test("rejects a missing grant type as invalid_request, not unsupported_grant_type", async () => {+ const body = exchangeBody();+ delete (body as Record<string, string>).grant_type;++ expectOauthError(await postToken(body), 400, "invalid_request", "Missing 'grant_type'");+ });++ test("treats an empty grant type as missing", async () => {+ expectOauthError(await postToken(exchangeBody({ grant_type: "" })), 400, "invalid_request", "Missing 'grant_type'");+ });+test("requires a subject token", async () => {const body = exchangeBody();delete (body as Record<string, string>).subject_token;diff --git a/backend/src/server/routes/v1/oauth-router.ts b/backend/src/server/routes/v1/oauth-router.tsindex 9a5e9a353b..c30d49949b 100644--- a/backend/src/server/routes/v1/oauth-router.ts+++ b/backend/src/server/routes/v1/oauth-router.ts@@ -20,6 +20,8 @@ import { OauthTokenError, OauthTokenErrorCode, toOauthTokenError } from "@app/seconst SanitizedOauthClientSchema = OauthClientsSchema.omit({ clientSecretHash: true });+const SUPPORTED_GRANT_TYPES = Object.values(OauthGrantType).join(", ");+const redirectUriSchema = z.string().url()@@ -475,6 +477,13 @@ export const registerOAuthRouter = async (server: FastifyZodProvider) => {if (req.validationError) {const grantType = (req.body as { grant_type?: unknown } | undefined)?.grant_type;+ if (grantType === undefined || grantType === null || grantType === "") {+ throw new OauthTokenError({+ code: OauthTokenErrorCode.InvalidRequest,+ message: `Missing 'grant_type'. Supported values are: ${SUPPORTED_GRANT_TYPES}`+ });+ }+const isKnownGrantType =typeof grantType === "string" && Object.values(OauthGrantType).includes(grantType as OauthGrantType);@@ -482,7 +491,7 @@ export const registerOAuthRouter = async (server: FastifyZodProvider) => {code: isKnownGrantType ? OauthTokenErrorCode.InvalidRequest : OauthTokenErrorCode.UnsupportedGrantType,message: isKnownGrantType? describeValidationError(req.validationError)- : `Unsupported 'grant_type'. Supported values are: ${Object.values(OauthGrantType).join(", ")}`+ : `Unsupported 'grant_type'. Supported values are: ${SUPPORTED_GRANT_TYPES}`});}