Skip to content

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

requested changes

varonix1b522ccec4bc
diff --git a/backend/src/server/plugins/error-handler.ts b/backend/src/server/plugins/error-handler.ts
index 360468c977..242fcb7a61 100644
--- a/backend/src/server/plugins/error-handler.ts
+++ b/backend/src/server/plugins/error-handler.ts
@@ -55,6 +55,7 @@ enum HttpStatusCodes {
}
const BODY_PARSER_ERRORS = [
+ errorCodes.FST_ERR_CTP_EMPTY_JSON_BODY,
errorCodes.FST_ERR_CTP_INVALID_JSON_BODY,
errorCodes.FST_ERR_CTP_INVALID_MEDIA_TYPE,
errorCodes.FST_ERR_CTP_BODY_TOO_LARGE,
diff --git a/backend/src/server/plugins/swagger.ts b/backend/src/server/plugins/swagger.ts
index 32aa05ce6e..b35999ff41 100644
--- a/backend/src/server/plugins/swagger.ts
+++ b/backend/src/server/plugins/swagger.ts
@@ -14,6 +14,7 @@ import { jsonSchemaTransform } from "./fastify-zod";
const DOCS_ROUTE_PREFIX = "/api/docs";
const SPEC_CACHE_MAX_AGE_SECONDS = 600;
+const MAX_TAGGED_SPEC_PAYLOADS = 20;
const gzipAsync = promisify(gzip);
const brotliCompressAsync = promisify(brotliCompress);
@@ -59,9 +60,19 @@ const filterSpecByTag = (spec: OpenApiDocument, tag: ApiDocsTags): OpenApiDocume
return { ...spec, paths };
};
-const buildSpecPayload = async (fastify: FastifyInstance, format: SpecFormat): Promise<SpecPayload> => {
+const buildSpecPayload = async (
+ fastify: FastifyInstance,
+ format: SpecFormat,
+ tag?: ApiDocsTags
+): Promise<SpecPayload> => {
const isYaml = format === "yaml";
- const body = isYaml ? fastify.swagger({ yaml: true }) : JSON.stringify(fastify.swagger());
+ let body: string;
+ if (isYaml) {
+ body = fastify.swagger({ yaml: true });
+ } else {
+ const spec = fastify.swagger() as unknown as OpenApiDocument;
+ body = JSON.stringify(tag ? filterSpecByTag(spec, tag) : spec);
+ }
const contentType = isYaml ? "application/x-yaml" : "application/json; charset=utf-8";
const identity = Buffer.from(body, "utf8");
@@ -79,6 +90,7 @@ const buildSpecPayload = async (fastify: FastifyInstance, format: SpecFormat): P
fastify.log.info(
{
format,
+ tag,
identityBytes: identity.byteLength,
brotliBytes: brotlied.byteLength,
gzipBytes: gzipped.byteLength
@@ -163,29 +175,30 @@ export const fastifySwagger = fp(async (fastify) => {
}
});
- const specPayloads = new Map<SpecFormat, Promise<SpecPayload>>();
+ const specPayloads = new Map<string, Promise<SpecPayload>>();
+ const taggedSpecPayloads = new Map<string, Promise<SpecPayload>>();
+
+ const getSpecPayload = (format: SpecFormat, tag?: ApiDocsTags) => {
+ const cache = tag ? taggedSpecPayloads : specPayloads;
+ const key = tag ?? format;
- const getSpecPayload = (format: SpecFormat) => {
- const cached = specPayloads.get(format);
+ const cached = cache.get(key);
if (cached) return cached;
- const pending = buildSpecPayload(fastify, format).catch((err) => {
- specPayloads.delete(format);
+ const pending = buildSpecPayload(fastify, format, tag).catch((err) => {
+ cache.delete(key);
throw err;
});
- specPayloads.set(format, pending);
+ if (tag && cache.size >= MAX_TAGGED_SPEC_PAYLOADS) cache.delete(cache.keys().next().value as string);
+ cache.set(key, pending);
return pending;
};
const serveSpec = async (req: FastifyRequest, reply: FastifyReply, format: SpecFormat) => {
const { tag } = SpecQuerySchema.parse(req.query);
- if (format === "json" && tag) {
- await reply.send(filterSpecByTag(fastify.swagger() as unknown as OpenApiDocument, tag));
- return;
- }
-
- const payload = await getSpecPayload(format);
+ // Only the JSON spec is filtered by tag.
+ const payload = await getSpecPayload(format, format === "json" ? tag : undefined);
const encoding = pickSpecEncoding(req.headers["accept-encoding"]);
void reply
diff --git a/backend/src/server/routes/v1/oauth-router.ts b/backend/src/server/routes/v1/oauth-router.ts
index 74114b8dbb..92d5c64782 100644
--- a/backend/src/server/routes/v1/oauth-router.ts
+++ b/backend/src/server/routes/v1/oauth-router.ts
@@ -80,6 +80,11 @@ const describeValidationError = (error: Error) => {
};
export const registerOAuthRouter = async (server: FastifyZodProvider) => {
+ // The token endpoint answers an empty JSON body with its RFC 6749 "body could not be read" invalid_request error,
+ // so this scope keeps Fastify's default JSON parser instead of the app-wide one that reads an empty body as {}.
+ server.removeContentTypeParser("application/json");
+ server.addContentTypeParser("application/json", { parseAs: "string" }, server.getDefaultJsonParser("error", "error"));
+
server.route({
method: "POST",
url: "/clients",
diff --git a/backend/src/server/routes/v3/deprecated-secret-router.ts b/backend/src/server/routes/v3/deprecated-secret-router.ts
index b375c69185..8540b214a1 100644
--- a/backend/src/server/routes/v3/deprecated-secret-router.ts
+++ b/backend/src/server/routes/v3/deprecated-secret-router.ts
@@ -61,7 +61,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use POST /v4/secrets/tags/:secretName instead.",
+ description: "Deprecated. Use PATCH /api/v4/secrets/:secretName and set tagIds instead.",
hide: false,
tags: [ApiDocsTags.Secrets],
security: [
@@ -119,7 +119,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use DELETE /v4/secrets/tags/:secretName instead.",
+ description: "Deprecated. Use PATCH /api/v4/secrets/:secretName and set tagIds instead.",
hide: false,
tags: [ApiDocsTags.Secrets],
security: [
@@ -177,7 +177,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use GET /v4/secrets/raw instead.",
+ description: "Deprecated. Use GET /api/v4/secrets instead.",
hide: false,
tags: [ApiDocsTags.Secrets],
security: [
@@ -397,7 +397,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use GET /v4/secrets/raw/id/:secretId instead.",
+ description: "Deprecated. Use GET /api/v4/secrets/id/:secretId instead.",
params: z.object({
secretId: z.string()
}),
@@ -436,7 +436,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use GET /v4/secrets/raw/:secretName instead.",
+ description: "Deprecated. Use GET /api/v4/secrets/:secretName instead.",
security: [
{
bearerAuth: []
@@ -570,7 +570,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use POST /v4/secrets/raw/:secretName instead.",
+ description: "Deprecated. Use POST /api/v4/secrets/:secretName instead.",
security: [
{
bearerAuth: []
@@ -726,7 +726,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use PATCH /v4/secrets/raw/:secretName instead.",
+ description: "Deprecated. Use PATCH /api/v4/secrets/:secretName instead.",
security: [
{
bearerAuth: []
@@ -891,7 +891,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use DELETE /v4/secrets/raw/:secretName instead.",
+ description: "Deprecated. Use DELETE /api/v4/secrets/:secretName instead.",
security: [
{
bearerAuth: []
@@ -1016,7 +1016,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use GET /v4/secrets instead.",
+ description: "Deprecated. Use GET /api/v4/secrets instead.",
querystring: z.object({
workspaceId: z.string().trim(),
environment: z.string().trim(),
@@ -1135,7 +1135,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use GET /v4/secrets/:secretName instead.",
+ description: "Deprecated. Use GET /api/v4/secrets/:secretName instead.",
params: z.object({
secretName: z.string().trim()
}),
@@ -1223,7 +1223,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use POST /v4/secrets/:secretName instead.",
+ description: "Deprecated. Use POST /api/v4/secrets/:secretName instead.",
body: z.object({
workspaceId: z.string().trim(),
environment: z.string().trim(),
@@ -1407,7 +1407,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use PATCH /v4/secrets/:secretName instead.",
+ description: "Deprecated. Use PATCH /api/v4/secrets/:secretName instead.",
params: z.object({
secretName: z.string()
}),
@@ -1609,7 +1609,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use DELETE /v4/secrets/:secretName instead.",
+ description: "Deprecated. Use DELETE /api/v4/secrets/:secretName instead.",
params: z.object({
secretName: z.string()
}),
@@ -1746,7 +1746,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use POST /v4/secrets/move instead.",
+ description: "Deprecated. Use POST /api/v4/secrets/move instead.",
hide: false,
tags: [ApiDocsTags.Secrets],
body: z.object({
@@ -1805,7 +1805,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use POST /v4/secrets/batch instead.",
+ description: "Deprecated. Use POST /api/v4/secrets/batch instead.",
body: z.object({
workspaceId: z.string().trim(),
environment: z.string().trim(),
@@ -1944,7 +1944,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use PATCH /v4/secrets/batch instead.",
+ description: "Deprecated. Use PATCH /api/v4/secrets/batch instead.",
body: z.object({
workspaceId: z.string().trim(),
environment: z.string().trim(),
@@ -2086,7 +2086,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
},
schema: {
deprecated: true,
- description: "Deprecated. Use DELETE /v4/secrets/batch instead.",
+ description: "Deprecated. Use DELETE /api/v4/secrets/batch instead.",
body: z.object({
workspaceId: z.string().trim(),
environment: z.string().trim(),
@@ -2222,7 +2222,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use POST /v4/secrets/batch/raw instead.",
+ description: "Deprecated. Use POST /api/v4/secrets/batch instead.",
security: [
{
bearerAuth: []
@@ -2362,7 +2362,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use PATCH /v4/secrets/batch/raw instead.",
+ description: "Deprecated. Use PATCH /api/v4/secrets/batch instead.",
security: [
{
bearerAuth: []
@@ -2575,7 +2575,7 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
hide: false,
tags: [ApiDocsTags.Secrets],
deprecated: true,
- description: "Deprecated. Use DELETE /v4/secrets/batch/raw instead.",
+ description: "Deprecated. Use DELETE /api/v4/secrets/batch instead.",
security: [
{
bearerAuth: []