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
rfc compliant error on body parse error
Thiago Araujo da Silvaec10a91fef11
diff --git a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.tsindex 50f0ce9a3e..8d78cb6777 100644--- a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts+++ b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts@@ -282,6 +282,28 @@ describe("POST /api/v1/oauth/token, token exchange grant", async () => {expectOauthError(await postToken(exchangeBody({ grant_type: "" })), 400, "invalid_request", "Missing 'grant_type'");});+ // The content-type parser runs before the handler, so these reach neither its try/catch nor+ // attachValidation. They still have to answer in the RFC shape rather than the house envelope.+ const postRawToken = async (payload: string, contentType: string) =>+ testServer.inject({+ method: "POST",+ url: "/api/v1/oauth/token",+ headers: { "content-type": contentType },+ payload+ });++ test("rejects a body the parser cannot read in the RFC shape", async () => {+ expectOauthError(await postRawToken("{not json", "application/json"), 400, "invalid_request", "could not be read");+ });++ test("rejects an empty json body in the RFC shape", async () => {+ expectOauthError(await postRawToken("", "application/json"), 400, "invalid_request", "could not be read");+ });++ test("rejects an unsupported content type in the RFC shape", async () => {+ expectOauthError(await postRawToken("<a/>", "application/xml"), 400, "invalid_request", "could not be read");+ });+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 c30d49949b..74114b8dbb 100644--- a/backend/src/server/routes/v1/oauth-router.ts+++ b/backend/src/server/routes/v1/oauth-router.ts@@ -22,6 +22,11 @@ const SanitizedOauthClientSchema = OauthClientsSchema.omit({ clientSecretHash: tconst SUPPORTED_GRANT_TYPES = Object.values(OauthGrantType).join(", ");+const isBodyParserError = (error: unknown) => {+ const code = (error as { code?: unknown } | null)?.code;+ return typeof code === "string" && code.startsWith("FST_ERR_CTP_");+};+const redirectUriSchema = z.string().url()@@ -442,6 +447,17 @@ export const registerOAuthRouter = async (server: FastifyZodProvider) => {rateLimit: authRateLimit},attachValidation: true,+ errorHandler: (error) => {+ if (isBodyParserError(error)) {+ throw new OauthTokenError({+ code: OauthTokenErrorCode.InvalidRequest,+ message:+ "The request body could not be read. Send the parameters as 'application/x-www-form-urlencoded' or 'application/json'.",+ error+ });+ }+ throw error;+ },schema: {body: z.object({grant_type: z.nativeEnum(OauthGrantType),