Skip to content

Commit 7eed1b2

Browse files
Prevent unauthorized event type access (#694)
Co-authored-by: Bailey Pumfleet <[email protected]>
1 parent be15868 commit 7eed1b2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pages/api/availability/eventtype.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
1010
return;
1111
}
1212

13+
if (!session.user?.id) {
14+
console.error("Session is missing a user id");
15+
return res.status(500).json({ message: "Something went wrong" });
16+
}
17+
18+
if (req.method !== "POST") {
19+
const event = await prisma.eventType.findUnique({
20+
where: { id: req.body.id },
21+
include: {
22+
users: true,
23+
},
24+
});
25+
26+
if (!event) {
27+
return res.status(404).json({ message: "No event exists matching that id." });
28+
}
29+
30+
const isAuthorized =
31+
event.userId === session.user.id ||
32+
event.users.find((user) => {
33+
return user.id === session.user?.id;
34+
});
35+
36+
if (!isAuthorized) {
37+
console.warn(`User ${session.user.id} attempted to an access an event ${event.id} they do not own.`);
38+
return res.status(404).json({ message: "No event exists matching that id." });
39+
}
40+
}
41+
1342
if (req.method == "PATCH" || req.method == "POST") {
1443
const data = {
1544
title: req.body.title,

0 commit comments

Comments
 (0)