Skip to content

Commit 1c6d333

Browse files
committed
fix: parse deploy webhook bodies in the handler so the size limit can be configured
1 parent e8be269 commit 1c6d333

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎apps/dokploy/__test__/deploy/github-webhook-handler.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ vi.mock("@dokploy/server/db", () => ({
6262
},
6363
}));
6464

65+
vi.mock("next/dist/server/api-utils/node/parse-body", () => ({
66+
parseBody: async (req: { body: unknown }) => req.body,
67+
}));
68+
6569
vi.mock("@dokploy/server", () => ({
6670
DEPLOY_WEBHOOK_MAX_BODY_SIZE: 25 * 1024 * 1024,
6771
IS_CLOUD: false,

‎apps/dokploy/pages/api/deploy/[refreshToken].ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { db } from "@dokploy/server/db";
99
import { eq } from "drizzle-orm";
1010
import type { NextApiRequest, NextApiResponse } from "next";
11+
import { parseBody } from "next/dist/server/api-utils/node/parse-body";
1112
import { applications } from "@/server/db/schema";
1213
import type { DeploymentJob } from "@/server/queues/queue-types";
1314
import { myQueue } from "@/server/queues/queueSetup";
@@ -35,16 +36,19 @@ const getPackageVersion = (headers: any, body: any) => {
3536

3637
export const config = {
3738
api: {
38-
bodyParser: {
39-
sizeLimit: DEPLOY_WEBHOOK_MAX_BODY_SIZE,
40-
},
39+
bodyParser: false,
4140
},
4241
};
4342

4443
export default async function handler(
4544
req: NextApiRequest,
4645
res: NextApiResponse,
4746
) {
47+
if (Number(req.headers["content-length"]) > DEPLOY_WEBHOOK_MAX_BODY_SIZE) {
48+
res.status(413).json({ message: "Payload too large" });
49+
return;
50+
}
51+
req.body = await parseBody(req, DEPLOY_WEBHOOK_MAX_BODY_SIZE);
4852
const { refreshToken } = req.query;
4953
try {
5054
if (req.headers["x-github-event"] === "ping") {

‎apps/dokploy/pages/api/deploy/compose/[refreshToken].ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { db } from "@dokploy/server/db";
77
import { eq } from "drizzle-orm";
88
import type { NextApiRequest, NextApiResponse } from "next";
9+
import { parseBody } from "next/dist/server/api-utils/node/parse-body";
910
import { compose } from "@/server/db/schema";
1011
import type { DeploymentJob } from "@/server/queues/queue-types";
1112
import { myQueue } from "@/server/queues/queueSetup";
@@ -21,16 +22,19 @@ import {
2122

2223
export const config = {
2324
api: {
24-
bodyParser: {
25-
sizeLimit: DEPLOY_WEBHOOK_MAX_BODY_SIZE,
26-
},
25+
bodyParser: false,
2726
},
2827
};
2928

3029
export default async function handler(
3130
req: NextApiRequest,
3231
res: NextApiResponse,
3332
) {
33+
if (Number(req.headers["content-length"]) > DEPLOY_WEBHOOK_MAX_BODY_SIZE) {
34+
res.status(413).json({ message: "Payload too large" });
35+
return;
36+
}
37+
req.body = await parseBody(req, DEPLOY_WEBHOOK_MAX_BODY_SIZE);
3438
const { refreshToken } = req.query;
3539
try {
3640
if (req.headers["x-github-event"] === "ping") {

‎apps/dokploy/pages/api/deploy/github.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { db } from "@dokploy/server/db";
1414
import { Webhooks } from "@octokit/webhooks";
1515
import { and, eq } from "drizzle-orm";
1616
import type { NextApiRequest, NextApiResponse } from "next";
17+
import { parseBody } from "next/dist/server/api-utils/node/parse-body";
1718
import { applications, compose, github } from "@/server/db/schema";
1819
import type { DeploymentJob } from "@/server/queues/queue-types";
1920
import { myQueue } from "@/server/queues/queueSetup";
@@ -29,16 +30,19 @@ const getGithubRepositoryOwner = (githubBody: any) =>
2930

3031
export const config = {
3132
api: {
32-
bodyParser: {
33-
sizeLimit: DEPLOY_WEBHOOK_MAX_BODY_SIZE,
34-
},
33+
bodyParser: false,
3534
},
3635
};
3736

3837
export default async function handler(
3938
req: NextApiRequest,
4039
res: NextApiResponse,
4140
) {
41+
if (Number(req.headers["content-length"]) > DEPLOY_WEBHOOK_MAX_BODY_SIZE) {
42+
res.status(413).json({ message: "Payload too large" });
43+
return;
44+
}
45+
req.body = await parseBody(req, DEPLOY_WEBHOOK_MAX_BODY_SIZE);
4246
const signature = req.headers["x-hub-signature-256"];
4347
if (!signature) {
4448
res.status(401).json({ message: "Missing signature header" });

0 commit comments

Comments
 (0)