Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

Harden team action auth with composite user-team lookup

Cursor Agent7efd2689d340
diff --git a/app/(ee)/api/ai/store/teams/[teamId]/route.ts b/app/(ee)/api/ai/store/teams/[teamId]/route.ts
index 3f767afe..4b0c0287 100644
--- a/app/(ee)/api/ai/store/teams/[teamId]/route.ts
+++ b/app/(ee)/api/ai/store/teams/[teamId]/route.ts
@@ -27,10 +27,12 @@ export async function GET(
const userId = (session.user as CustomUser).id;
// Verify user is member of team
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- userId,
- teamId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/change-role.ts b/pages/api/teams/[teamId]/change-role.ts
index 48549f02..a2fce9b1 100644
--- a/pages/api/teams/[teamId]/change-role.ts
+++ b/pages/api/teams/[teamId]/change-role.ts
@@ -28,10 +28,12 @@ export default async function handle(
};
try {
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
- userId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/incoming-webhooks/index.ts b/pages/api/teams/[teamId]/incoming-webhooks/index.ts
index a50fa61a..605bdefb 100644
--- a/pages/api/teams/[teamId]/incoming-webhooks/index.ts
+++ b/pages/api/teams/[teamId]/incoming-webhooks/index.ts
@@ -90,10 +90,12 @@ export default async function handle(
try {
// Check if user has access to team
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
- userId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/invitations/accept.ts b/pages/api/teams/[teamId]/invitations/accept.ts
index fe54b3f2..cfebbfec 100644
--- a/pages/api/teams/[teamId]/invitations/accept.ts
+++ b/pages/api/teams/[teamId]/invitations/accept.ts
@@ -44,10 +44,12 @@ export default async function handle(
try {
// Check if user is already in the team
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
- userId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/invitations/index.ts b/pages/api/teams/[teamId]/invitations/index.ts
index 84b92863..8323aa6d 100644
--- a/pages/api/teams/[teamId]/invitations/index.ts
+++ b/pages/api/teams/[teamId]/invitations/index.ts
@@ -22,10 +22,12 @@ export default async function handle(
try {
// check if currentUser is part of the team with the teamId
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
- userId: (session.user as CustomUser).id,
+ userId_teamId: {
+ userId: (session.user as CustomUser).id,
+ teamId,
+ },
},
});
@@ -66,10 +68,12 @@ export default async function handle(
try {
// check if currentUser is part of the team with the teamId
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
- userId: (session.user as CustomUser).id,
+ userId_teamId: {
+ userId: (session.user as CustomUser).id,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/invitations/resend.ts b/pages/api/teams/[teamId]/invitations/resend.ts
index 5071916a..78e3379b 100644
--- a/pages/api/teams/[teamId]/invitations/resend.ts
+++ b/pages/api/teams/[teamId]/invitations/resend.ts
@@ -29,10 +29,12 @@ export default async function handle(
try {
// check if currentUser is part of the team with the teamId
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
- userId: (session.user as CustomUser).id,
+ userId_teamId: {
+ userId: (session.user as CustomUser).id,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/invite.ts b/pages/api/teams/[teamId]/invite.ts
index 488efa21..af7df98b 100644
--- a/pages/api/teams/[teamId]/invite.ts
+++ b/pages/api/teams/[teamId]/invite.ts
@@ -26,6 +26,7 @@ export default async function handle(
}
const { teamId } = req.query as { teamId: string };
+ const userId = (session.user as CustomUser).id;
const { email } = req.body;
@@ -34,6 +35,28 @@ export default async function handle(
}
try {
+ const userTeam = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ select: {
+ role: true,
+ },
+ });
+
+ if (!userTeam) {
+ res.status(403).json("You are not part of this team");
+ return;
+ }
+
+ if (userTeam.role !== "ADMIN") {
+ res.status(403).json("Only admins can send the invitation!");
+ return;
+ }
+
const team = await prisma.team.findUnique({
where: {
id: teamId,
@@ -58,22 +81,12 @@ export default async function handle(
return;
}
- // check that the user is admin of the team, otherwise return 403
const teamUsers = team.users;
- const isUserAdmin = teamUsers.some(
- (user) =>
- user.role === "ADMIN" &&
- user.userId === (session.user as CustomUser).id,
- );
- if (!isUserAdmin) {
- res.status(403).json("Only admins can send the invitation!");
- return;
- }
// Check if the user has reached the limit of users in the team
const limits = await getLimits({
teamId,
- userId: (session.user as CustomUser).id,
+ userId,
});
if (limits && teamUsers.length >= limits.users) {
diff --git a/pages/api/teams/[teamId]/remove-teammate.ts b/pages/api/teams/[teamId]/remove-teammate.ts
index ca15b9f8..85d7e46a 100644
--- a/pages/api/teams/[teamId]/remove-teammate.ts
+++ b/pages/api/teams/[teamId]/remove-teammate.ts
@@ -25,14 +25,17 @@ export default async function handle(
const { userToBeDeleted } = req.body;
try {
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- teamId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
if (!userTeam) {
- return res.status(401).json("The teammate isn't the part of this team");
+ return res.status(401).json("Unauthorized");
}
if (userTeam?.role === "ADMIN" && userTeam.userId === userToBeDeleted) {
diff --git a/pages/api/teams/[teamId]/webhooks/[id]/events.ts b/pages/api/teams/[teamId]/webhooks/[id]/events.ts
index 656915ba..2d70d9a8 100644
--- a/pages/api/teams/[teamId]/webhooks/[id]/events.ts
+++ b/pages/api/teams/[teamId]/webhooks/[id]/events.ts
@@ -19,10 +19,12 @@ export default async function handler(
const { teamId, id: webhookId } = req.query as { teamId: string; id: string };
const userId = (session.user as CustomUser).id;
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- userId,
- teamId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/webhooks/[id]/index.ts b/pages/api/teams/[teamId]/webhooks/[id]/index.ts
index dfd31e12..b650e6f2 100644
--- a/pages/api/teams/[teamId]/webhooks/[id]/index.ts
+++ b/pages/api/teams/[teamId]/webhooks/[id]/index.ts
@@ -19,10 +19,12 @@ export default async function handler(
const { teamId, id } = req.query as { teamId: string; id: string };
const userId = (session.user as CustomUser).id;
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- userId,
- teamId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});
diff --git a/pages/api/teams/[teamId]/webhooks/index.ts b/pages/api/teams/[teamId]/webhooks/index.ts
index 552dac01..165f4f8b 100644
--- a/pages/api/teams/[teamId]/webhooks/index.ts
+++ b/pages/api/teams/[teamId]/webhooks/index.ts
@@ -20,10 +20,12 @@ export default async function handle(
const { teamId } = req.query as { teamId: string };
const userId = (session.user as CustomUser).id;
- const userTeam = await prisma.userTeam.findFirst({
+ const userTeam = await prisma.userTeam.findUnique({
where: {
- userId: userId,
- teamId: teamId,
+ userId_teamId: {
+ userId,
+ teamId,
+ },
},
});