Skip to content

Commit 8144e1a

Browse files
committed
fix(api): validate OpenAPI body size limits against Greptile findings
1 parent d11735d commit 8144e1a

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

‎apps/dokploy/pages/api/[...trpc].ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
1616
return;
1717
}
1818

19-
// getMultipartBody (trpc-openapi) doesn't accept maxBodySize, so multipart
20-
// uploads have no cap unless enforced here before the handler reads the stream.
21-
const contentLength = Number(req.headers["content-length"] ?? 0);
19+
// getMultipartBody doesn't accept maxBodySize, so we cap it here instead.
20+
const contentLength = Number(req.headers["content-length"]);
2221
const isMultipart = req.headers["content-type"]?.startsWith(
2322
"multipart/form-data",
2423
);
24+
25+
if (isMultipart && !Number.isFinite(contentLength)) {
26+
res.status(411).json({ message: "Content-Length required" });
27+
return;
28+
}
29+
2530
const limit = isMultipart
2631
? OPENAPI_MAX_UPLOAD_SIZE
2732
: OPENAPI_MAX_JSON_BODY_SIZE;
2833

29-
if (contentLength > limit) {
34+
if (Number.isFinite(contentLength) && contentLength > limit) {
3035
res.status(413).json({ message: "Payload too large" });
3136
return;
3237
}

‎packages/server/src/constants/index.ts‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@ export const DOKPLOY_DOCKER_PORT = process.env.DOKPLOY_DOCKER_PORT
1414
export const CLEANUP_CRON_JOB = "50 23 * * *";
1515

1616
// Body size limits for the OpenAPI catch-all route (pages/api/[...trpc].ts).
17-
// JSON/urlencoded bodies are capped by trpc-openapi's own default (100kb) unless
18-
// we pass an explicit limit; multipart uploads (e.g. drop-deployment zips) have
19-
// no built-in cap at all, so we enforce one manually via content-length.
20-
export const OPENAPI_MAX_JSON_BODY_SIZE = process.env.OPENAPI_MAX_JSON_BODY_SIZE
21-
? Number.parseInt(process.env.OPENAPI_MAX_JSON_BODY_SIZE, 10)
22-
: 10 * 1024 * 1024; // 10mb
23-
24-
export const OPENAPI_MAX_UPLOAD_SIZE = process.env.OPENAPI_MAX_UPLOAD_SIZE
25-
? Number.parseInt(process.env.OPENAPI_MAX_UPLOAD_SIZE, 10)
26-
: 1024 * 1024 * 1024; // 1gb
17+
const parseByteSize = (envVar: string, fallback: number): number => {
18+
const raw = process.env[envVar];
19+
if (!raw) return fallback;
20+
const parsed = Number.parseInt(raw, 10);
21+
if (!Number.isFinite(parsed) || parsed <= 0) {
22+
console.warn(`Invalid ${envVar}="${raw}", using default ${fallback}`);
23+
return fallback;
24+
}
25+
return parsed;
26+
};
27+
28+
export const OPENAPI_MAX_JSON_BODY_SIZE = parseByteSize(
29+
"OPENAPI_MAX_JSON_BODY_SIZE",
30+
10 * 1024 * 1024, // 10mb
31+
);
32+
33+
export const OPENAPI_MAX_UPLOAD_SIZE = parseByteSize(
34+
"OPENAPI_MAX_UPLOAD_SIZE",
35+
1024 * 1024 * 1024, // 1gb
36+
);
2737

2838
type DockerSocketCandidate = {
2939
label: string;

0 commit comments

Comments
 (0)