Skip to content

Commit 9bfa416

Browse files
fix: prevent sending raw errors in responses (calcom#24282)
* fix: prevent sending raw errors in responses * correct already existing typo * style: fix import ordering per biome lint Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 8a96a45 commit 9bfa416

14 files changed

Lines changed: 54 additions & 70 deletions

File tree

apps/web/pages/api/integrations/[...args].ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwI
55
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
66
import { deriveAppDictKeyFromType } from "@calcom/lib/deriveAppDictKeyFromType";
77
import { HttpError } from "@calcom/lib/http-error";
8+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
89
import prisma from "@calcom/prisma";
910
import type { AppDeclarativeHandler, AppHandler } from "@calcom/types/AppHandler";
1011

@@ -80,13 +81,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
8081
return;
8182
} catch (error) {
8283
console.error(error);
83-
if (error instanceof HttpError) {
84-
return res.status(error.statusCode).json({ message: error.message });
85-
}
86-
if (error instanceof Error) {
87-
return res.status(400).json({ message: error.message });
88-
}
89-
return res.status(404).json({ message: `API handler not found` });
84+
const httpError = getServerErrorFromUnknown(error);
85+
return res.status(httpError.statusCode).json({ message: httpError.message });
9086
}
9187
};
9288

packages/app-store/alby/api/add.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

3+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
34
import prisma from "@calcom/prisma";
45

56
import config from "../config.json";
@@ -32,10 +33,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
3233
throw new Error("Unable to create user credential for Alby");
3334
}
3435
} catch (error: unknown) {
35-
if (error instanceof Error) {
36-
return res.status(500).json({ message: error.message });
37-
}
38-
return res.status(500);
36+
const httpError = getServerErrorFromUnknown(error);
37+
return res.status(httpError.statusCode).json({ message: httpError.message });
3938
}
4039

4140
return res.status(200).json({ url: "/apps/alby/setup" });

packages/app-store/giphy/api/add.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

3+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
34
import prisma from "@calcom/prisma";
45

56
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
@@ -44,10 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
4445
throw new Error("Unable to create user credential for giphy");
4546
}
4647
} catch (error: unknown) {
47-
if (error instanceof Error) {
48-
return res.status(500).json({ message: error.message });
49-
}
50-
return res.status(500);
48+
const httpError = getServerErrorFromUnknown(error);
49+
return res.status(httpError.statusCode).json({ message: httpError.message });
5150
}
5251

5352
return res.status(200).json({ url: getInstalledAppPath({ variant: "other", slug: "giphy" }) });

packages/app-store/giphy/api/get.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22
import { z, ZodError } from "zod";
33

4+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
5+
46
import { GiphyManager } from "../lib";
57

68
const giphyUrlRegexp = new RegExp(
@@ -35,11 +37,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
3537
const gifImageUrl = await GiphyManager.getGiphyById(giphyId);
3638
return res.status(200).json({ image: gifImageUrl });
3739
} catch (error: unknown) {
38-
console.error({ error });
39-
if (error instanceof Error) {
40-
return res.status(500).json({ message: error.message });
41-
}
42-
return res.status(500);
40+
const httpError = getServerErrorFromUnknown(error);
41+
return res.status(httpError.statusCode).json({ message: httpError.message });
4342
}
4443
}
4544

packages/app-store/giphy/api/search.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22
import { z, ZodError } from "zod";
33

4+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
45
import prisma from "@calcom/prisma";
56

67
import { GiphyManager } from "../lib";
@@ -39,10 +40,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
3940
nextOffset: total === offset + 1 ? 0 : offset + 1,
4041
});
4142
} catch (error: unknown) {
42-
if (error instanceof Error) {
43-
return res.status(500).json({ message: error.message });
44-
}
45-
return res.status(500);
43+
const httpError = getServerErrorFromUnknown(error);
44+
return res.status(httpError.statusCode).json({ message: httpError.message });
4645
}
4746
}
4847

packages/app-store/googlevideo/api/_getAdd.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

3+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
34
import prisma from "@calcom/prisma";
45

56
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
@@ -32,10 +33,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
3233
throw new Error("Unable to create user credential for google_video");
3334
}
3435
} catch (error: unknown) {
35-
if (error instanceof Error) {
36-
return res.status(500).json({ message: error.message });
37-
}
38-
return res.status(500);
36+
const httpError = getServerErrorFromUnknown(error);
37+
return res.status(httpError.statusCode).json({ message: httpError.message });
3938
}
4039
return res
4140
.status(200)

packages/app-store/hitpay/api/add.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { NextApiRequest, NextApiResponse } from "next";
2-
31
import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam";
2+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
43
import prisma from "@calcom/prisma";
5-
4+
import type { NextApiRequest, NextApiResponse } from "next";
65
import config from "../config.json";
76

87
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -37,13 +36,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
3736
});
3837

3938
if (!installation) {
40-
throw new Error("Unable to create user credential for HitPay");
39+
throw new Error("Unable to create user credential for hitpay");
4140
}
4241
} catch (error: unknown) {
43-
const message =
44-
error instanceof Error ? error.message : typeof error === "string" ? error : JSON.stringify(error);
45-
46-
return res.status(500).json({ message });
42+
const httpError = getServerErrorFromUnknown(error);
43+
return res.status(httpError.statusCode).json({ message: httpError.message });
4744
}
4845

4946
return res.status(200).json({ url: `/apps/hitpay/setup${teamIdNumber ? `?teamId=${teamIdNumber}` : ""}` });

packages/app-store/jitsivideo/api/add.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

33
import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam";
4+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
45
import prisma from "@calcom/prisma";
56

67
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
@@ -16,7 +17,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
1617
}
1718
const { teamId, returnTo } = req.query;
1819

19-
await throwIfNotHaveAdminAccessToTeam({ teamId: Number(teamId) ?? null, userId: req.session.user.id });
20+
await throwIfNotHaveAdminAccessToTeam({
21+
teamId: teamId ? Number(teamId) : null,
22+
userId: req.session.user.id,
23+
});
2024

2125
const installForObject = teamId ? { teamId: Number(teamId) } : { userId: req.session.user.id };
2226
const appType = "jitsi_video";
@@ -42,10 +46,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
4246
throw new Error("Unable to create user credential for jitsivideo");
4347
}
4448
} catch (error: unknown) {
45-
if (error instanceof Error) {
46-
return res.status(500).json({ message: error.message });
47-
}
48-
return res.status(500);
49+
const httpError = getServerErrorFromUnknown(error);
50+
return res.status(httpError.statusCode).json({ message: httpError.message });
4951
}
5052
return res
5153
.status(200)

packages/app-store/paypal/api/add.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

33
import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam";
4+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
45
import prisma from "@calcom/prisma";
56

67
import config from "../config.json";
@@ -12,7 +13,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
1213

1314
const { teamId } = req.query;
1415

15-
await throwIfNotHaveAdminAccessToTeam({ teamId: Number(teamId) ?? null, userId: req.session.user.id });
16+
await throwIfNotHaveAdminAccessToTeam({
17+
teamId: teamId ? Number(teamId) : null,
18+
userId: req.session.user.id,
19+
});
20+
1621
const installForObject = teamId ? { teamId: Number(teamId) } : { userId: req.session.user.id };
1722

1823
const appType = config.type;
@@ -39,10 +44,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
3944
throw new Error("Unable to create user credential for Paypal");
4045
}
4146
} catch (error: unknown) {
42-
if (error instanceof Error) {
43-
return res.status(500).json({ message: error.message });
44-
}
45-
return res.status(500);
47+
const httpError = getServerErrorFromUnknown(error);
48+
return res.status(httpError.statusCode).json({ message: httpError.message });
4649
}
4750

4851
return res.status(200).json({ url: "/apps/paypal/setup" });

packages/app-store/sylapsvideo/api/add.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22

3+
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
34
import prisma from "@calcom/prisma";
45

56
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
@@ -38,10 +39,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
3839
throw new Error("Unable to create user credential for sylaps");
3940
}
4041
} catch (error: unknown) {
41-
if (error instanceof Error) {
42-
return res.status(500).json({ message: error.message });
43-
}
44-
return res.status(500);
42+
const httpError = getServerErrorFromUnknown(error);
43+
return res.status(httpError.statusCode).json({ message: httpError.message });
4544
}
4645
return res
4746
.status(200)

0 commit comments

Comments
 (0)