fix(api): configurable body size limits for OpenAPI catch-all route

This commit is contained in:
Narciso 2026-08-11 14:24:35 -04:00
parent 870d592a97
commit d11735d4cd
2 changed files with 39 additions and 1 deletions

View File

@ -1,4 +1,8 @@
import { validateRequest } from "@dokploy/server";
import {
OPENAPI_MAX_JSON_BODY_SIZE,
OPENAPI_MAX_UPLOAD_SIZE,
validateRequest,
} from "@dokploy/server";
import { createOpenApiNextHandler } from "@dokploy/trpc-openapi";
import type { NextApiRequest, NextApiResponse } from "next";
import { appRouter } from "@/server/api/root";
@ -12,10 +16,26 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return;
}
// getMultipartBody (trpc-openapi) doesn't accept maxBodySize, so multipart
// uploads have no cap unless enforced here before the handler reads the stream.
const contentLength = Number(req.headers["content-length"] ?? 0);
const isMultipart = req.headers["content-type"]?.startsWith(
"multipart/form-data",
);
const limit = isMultipart
? OPENAPI_MAX_UPLOAD_SIZE
: OPENAPI_MAX_JSON_BODY_SIZE;
if (contentLength > limit) {
res.status(413).json({ message: "Payload too large" });
return;
}
// @ts-ignore
return createOpenApiNextHandler({
router: appRouter,
createContext: createTRPCContext,
maxBodySize: OPENAPI_MAX_JSON_BODY_SIZE,
onError:
process.env.NODE_ENV === "development"
? ({ path, error }: { path: string | undefined; error: Error }) => {
@ -28,3 +48,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
};
export default handler;
export const config = {
api: {
bodyParser: false,
},
};

View File

@ -13,6 +13,18 @@ export const DOKPLOY_DOCKER_PORT = process.env.DOKPLOY_DOCKER_PORT
export const CLEANUP_CRON_JOB = "50 23 * * *";
// Body size limits for the OpenAPI catch-all route (pages/api/[...trpc].ts).
// JSON/urlencoded bodies are capped by trpc-openapi's own default (100kb) unless
// we pass an explicit limit; multipart uploads (e.g. drop-deployment zips) have
// no built-in cap at all, so we enforce one manually via content-length.
export const OPENAPI_MAX_JSON_BODY_SIZE = process.env.OPENAPI_MAX_JSON_BODY_SIZE
? Number.parseInt(process.env.OPENAPI_MAX_JSON_BODY_SIZE, 10)
: 10 * 1024 * 1024; // 10mb
export const OPENAPI_MAX_UPLOAD_SIZE = process.env.OPENAPI_MAX_UPLOAD_SIZE
? Number.parseInt(process.env.OPENAPI_MAX_UPLOAD_SIZE, 10)
: 1024 * 1024 * 1024; // 1gb
type DockerSocketCandidate = {
label: string;
path: string;