fix: payload cleanup (#13552)

* fix: payload cleanup

* Update eventType.ts

* Send whitelisted fields everywhere

* Removed a user select

* Removed extra usage of users

* Put back user field with fewer selects

* Added user pick

* Removed more users selects

* Another select removal

---------

Co-authored-by: Hariom <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-02-06 11:20:08 -03:00
1 parent bba68e09a9
commit 66b5dfd796
19 files changed
+230 -48

No files matched your search

@@ -51,7 +51,6 @@ export async function getHandler(req: NextApiRequest) {
include: { include: {
customInputs: true, customInputs: true,
team: { select: { slug: true } }, team: { select: { slug: true } },
users: true,
hosts: { select: { userId: true, isFixed: true } }, hosts: { select: { userId: true, isFixed: true } },
owner: { select: { username: true, id: true } }, owner: { select: { username: true, id: true } },
children: { select: { id: true, userId: true } }, children: { select: { id: true, userId: true } },
-1
View File
@@ -56,7 +56,6 @@ async function getHandler(req: NextApiRequest) {
include: { include: {
customInputs: true, customInputs: true,
team: { select: { slug: true } }, team: { select: { slug: true } },
users: true,
hosts: { select: { userId: true, isFixed: true } }, hosts: { select: { userId: true, isFixed: true } },
owner: { select: { username: true, id: true } }, owner: { select: { username: true, id: true } },
children: { select: { id: true, userId: true } }, children: { select: { id: true, userId: true } },
@@ -59,7 +59,6 @@ async function getHandler(req: NextApiRequest) {
include: { include: {
customInputs: true, customInputs: true,
team: { select: { slug: true } }, team: { select: { slug: true } },
users: true,
hosts: { select: { userId: true, isFixed: true } }, hosts: { select: { userId: true, isFixed: true } },
owner: { select: { username: true, id: true } }, owner: { select: { username: true, id: true } },
children: { select: { id: true, userId: true } }, children: { select: { id: true, userId: true } },
@@ -24,7 +24,12 @@ export const responseHandler = async ({ ctx, input }: ResponseHandlerOptions) =>
id: formId, id: formId,
}, },
include: { include: {
user: true, user: {
select: {
id: true,
email: true,
},
},
}, },
}); });
if (!form) { if (!form) {
@@ -10,7 +10,7 @@ import type { OrderedResponses } from "../types/types";
import type { Response, SerializableForm } from "../types/types"; import type { Response, SerializableForm } from "../types/types";
export async function onFormSubmission( export async function onFormSubmission(
form: Ensure<SerializableForm<App_RoutingForms_Form> & { user: User }, "fields">, form: Ensure<SerializableForm<App_RoutingForms_Form> & { user: Pick<User, "id" | "email"> }, "fields">,
response: Response response: Response
) { ) {
const fieldResponsesByName: Record< const fieldResponsesByName: Record<
@@ -57,8 +57,6 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
const event = await prisma.eventType.findFirstOrThrow({ const event = await prisma.eventType.findFirstOrThrow({
select: { select: {
title: true, title: true,
users: true,
schedulingType: true,
}, },
where: { where: {
id: bookingToReschedule.eventTypeId, id: bookingToReschedule.eventTypeId,
@@ -57,7 +57,6 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
const event = await prisma.eventType.findFirstOrThrow({ const event = await prisma.eventType.findFirstOrThrow({
select: { select: {
title: true, title: true,
users: true,
schedulingType: true, schedulingType: true,
}, },
where: { where: {
@@ -47,7 +47,7 @@ const loginWithTotp = async (user: { email: string }) =>
type UserTeams = { type UserTeams = {
teams: (Membership & { teams: (Membership & {
team: Team; team: Pick<Team, "metadata">;
})[]; })[];
}; };
@@ -106,7 +106,9 @@ const providers: Provider[] = [
throw new Error(ErrorCode.InternalServerError); throw new Error(ErrorCode.InternalServerError);
} }
const user = await UserRepository.findByEmailAndIncludeProfiles({ email: credentials.email }); const user = await UserRepository.findByEmailAndIncludeProfilesAndPassword({
email: credentials.email,
});
// Don't leak information about it being username or password that is invalid // Don't leak information about it being username or password that is invalid
if (!user) { if (!user) {
throw new Error(ErrorCode.IncorrectEmailPassword); throw new Error(ErrorCode.IncorrectEmailPassword);
@@ -273,7 +275,9 @@ if (isSAMLLoginEnabled) {
email?: string; email?: string;
locale?: string; locale?: string;
}) => { }) => {
const user = await UserRepository.findByEmailAndIncludeProfiles({ email: profile.email || "" }); const user = await UserRepository.findByEmailAndIncludeProfilesAndPassword({
email: profile.email || "",
});
if (!user) { if (!user) {
throw new Error(ErrorCode.UserNotFound); throw new Error(ErrorCode.UserNotFound);
} }
@@ -339,7 +343,7 @@ if (isSAMLLoginEnabled) {
} }
const { id, firstName, lastName, email } = userInfo; const { id, firstName, lastName, email } = userInfo;
const user = await UserRepository.findByEmailAndIncludeProfiles({ email }); const user = await UserRepository.findByEmailAndIncludeProfilesAndPassword({ email });
if (!user) { if (!user) {
throw new Error(ErrorCode.UserNotFound); throw new Error(ErrorCode.UserNotFound);
} }
+20 -5
View File
@@ -1,4 +1,5 @@
import type { Prisma, EventType as PrismaEventType } from "@prisma/client"; import type { EventType as PrismaEventType } from "@prisma/client";
import { Prisma } from "@prisma/client";
import logger from "@calcom/lib/logger"; import logger from "@calcom/lib/logger";
import { prisma } from "@calcom/prisma"; import { prisma } from "@calcom/prisma";
@@ -22,6 +23,16 @@ type IEventType = Ensure<
"title" | "slug" | "length" "title" | "slug" | "length"
>; >;
const userSelect = Prisma.validator<Prisma.UserSelect>()({
name: true,
avatarUrl: true,
username: true,
id: true,
email: true,
locale: true,
defaultScheduleId: true,
});
export class EventTypeRepository { export class EventTypeRepository {
static async create(data: IEventType) { static async create(data: IEventType) {
const { const {
@@ -92,19 +103,23 @@ export class EventTypeRepository {
// TODO: As required by getByViewHandler - Make it configurable // TODO: As required by getByViewHandler - Make it configurable
team: { team: {
include: { include: {
eventTypes: true, eventTypes: {
include: {
users: { select: userSelect },
},
},
}, },
}, },
hashedLink: true, hashedLink: true,
users: true, users: { select: userSelect },
children: { children: {
include: { include: {
users: true, users: { select: userSelect },
}, },
}, },
hosts: { hosts: {
include: { include: {
user: true, user: { select: userSelect },
}, },
}, },
}; };
+47 -11
View File
@@ -1,5 +1,6 @@
import { prisma } from "@calcom/prisma"; import { prisma } from "@calcom/prisma";
import type { Prisma, MembershipRole } from "@calcom/prisma/client"; import type { MembershipRole } from "@calcom/prisma/client";
import { Prisma } from "@calcom/prisma/client";
import logger from "../../logger"; import logger from "../../logger";
import { safeStringify } from "../../safeStringify"; import { safeStringify } from "../../safeStringify";
@@ -14,6 +15,33 @@ type IMembership = {
role: MembershipRole; role: MembershipRole;
}; };
const membershipSelect = Prisma.validator<Prisma.MembershipSelect>()({
id: true,
teamId: true,
userId: true,
accepted: true,
role: true,
disableImpersonation: true,
});
const teamParentSelect = Prisma.validator<Prisma.TeamSelect>()({
id: true,
name: true,
slug: true,
logoUrl: true,
parentId: true,
});
const userSelect = Prisma.validator<Prisma.UserSelect>()({
name: true,
avatarUrl: true,
username: true,
id: true,
email: true,
locale: true,
defaultScheduleId: true,
});
export class MembershipRepository { export class MembershipRepository {
static async create(data: IMembership) { static async create(data: IMembership) {
return await prisma.membership.create({ return await prisma.membership.create({
@@ -68,25 +96,33 @@ export class MembershipRepository {
include: { include: {
team: { team: {
include: { include: {
members: true, members: {
parent: true, select: membershipSelect,
},
parent: {
select: teamParentSelect,
},
eventTypes: { eventTypes: {
include: { include: {
team: { team: {
include: { include: {
eventTypes: true, eventTypes: {
include: {
users: { select: userSelect },
},
},
}, },
}, },
hashedLink: true, hashedLink: true,
users: true, users: { select: userSelect },
hosts: {
include: {
user: true,
},
},
children: { children: {
include: { include: {
users: true, users: { select: userSelect },
},
},
hosts: {
include: {
user: { select: userSelect },
}, },
}, },
}, },
+49 -9
View File
@@ -4,6 +4,7 @@ import { v4 as uuidv4 } from "uuid";
import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains"; import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains";
import { safeStringify } from "@calcom/lib/safeStringify"; import { safeStringify } from "@calcom/lib/safeStringify";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import { Prisma } from "@calcom/prisma/client";
import type { Team } from "@calcom/prisma/client"; import type { Team } from "@calcom/prisma/client";
import type { UpId, UserAsPersonalProfile, UserProfile } from "@calcom/types/UserProfile"; import type { UpId, UserAsPersonalProfile, UserProfile } from "@calcom/types/UserProfile";
@@ -11,6 +12,28 @@ import logger from "../../logger";
import { getParsedTeam } from "./teamUtils"; import { getParsedTeam } from "./teamUtils";
import { UserRepository } from "./user"; import { UserRepository } from "./user";
const userSelect = Prisma.validator<Prisma.UserSelect>()({
name: true,
avatarUrl: true,
username: true,
id: true,
email: true,
locale: true,
defaultScheduleId: true,
startTime: true,
endTime: true,
bufferTime: true,
});
const membershipSelect = Prisma.validator<Prisma.MembershipSelect>()({
id: true,
teamId: true,
userId: true,
accepted: true,
role: true,
disableImpersonation: true,
});
const log = logger.getSubLogger({ prefix: ["repository/profile"] }); const log = logger.getSubLogger({ prefix: ["repository/profile"] });
const organizationSelect = { const organizationSelect = {
id: true, id: true,
@@ -30,14 +53,17 @@ export class ProfileRepository {
return uuidv4(); return uuidv4();
} }
private static getInheritedDataFromUser({ user }: { user: PrismaUser }) { private static getInheritedDataFromUser({
user,
}: {
user: Pick<PrismaUser, "name" | "avatarUrl" | "startTime" | "endTime" | "bufferTime">;
}) {
return { return {
name: user.name, name: user.name,
avatarUrl: user.avatarUrl, avatarUrl: user.avatarUrl,
startTime: user.startTime, startTime: user.startTime,
endTime: user.endTime, endTime: user.endTime,
bufferTime: user.bufferTime, bufferTime: user.bufferTime,
avatar: user.avatar,
}; };
} }
@@ -233,7 +259,9 @@ export class ProfileRepository {
organization: { organization: {
select: organizationSelect, select: organizationSelect,
}, },
user: true, user: {
select: userSelect,
},
}, },
}); });
@@ -268,7 +296,9 @@ export class ProfileRepository {
organization: { organization: {
select: organizationSelect, select: organizationSelect,
}, },
user: true, user: {
select: userSelect,
},
}, },
}); });
return profile; return profile;
@@ -313,11 +343,15 @@ export class ProfileRepository {
id, id,
}, },
include: { include: {
user: true, user: {
select: userSelect,
},
movedFromUser: true, movedFromUser: true,
organization: { organization: {
include: { include: {
members: true, members: {
select: membershipSelect,
},
}, },
}, },
}, },
@@ -346,7 +380,9 @@ export class ProfileRepository {
organization: whereClauseForOrgWithSlugOrRequestedSlug(orgSlug), organization: whereClauseForOrgWithSlugOrRequestedSlug(orgSlug),
}, },
include: { include: {
user: true, user: {
select: userSelect,
},
organization: { organization: {
select: organizationSelect, select: organizationSelect,
}, },
@@ -416,7 +452,9 @@ export class ProfileRepository {
organizationId, organizationId,
}, },
include: { include: {
user: true, user: {
select: userSelect,
},
organization: { organization: {
select: organizationSelect, select: organizationSelect,
}, },
@@ -434,7 +472,9 @@ export class ProfileRepository {
organization: { organization: {
select: organizationSelect, select: organizationSelect,
}, },
user: true, user: {
select: userSelect,
},
}, },
}); });
if (!profile) { if (!profile) {
+12 -1
View File
@@ -1,4 +1,4 @@
import type { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import type { z } from "zod"; import type { z } from "zod";
import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains"; import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains";
@@ -148,12 +148,23 @@ export async function getOrg<TeamSelect extends Prisma.TeamSelect>({
teamSelect, teamSelect,
}); });
} }
const teamSelect = Prisma.validator<Prisma.TeamSelect>()({
id: true,
name: true,
slug: true,
logoUrl: true,
parentId: true,
metadata: true,
});
export class TeamRepository { export class TeamRepository {
static async findById({ id }: { id: number }) { static async findById({ id }: { id: number }) {
const team = await prisma.team.findUnique({ const team = await prisma.team.findUnique({
where: { where: {
id, id,
}, },
select: teamSelect,
}); });
if (!team) { if (!team) {
return null; return null;
+59 -4
View File
@@ -1,6 +1,7 @@
import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains"; import { whereClauseForOrgWithSlugOrRequestedSlug } from "@calcom/ee/organizations/lib/orgDomains";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import type { User as UserType, Prisma } from "@calcom/prisma/client"; import { Prisma } from "@calcom/prisma/client";
import type { User as UserType } from "@calcom/prisma/client";
import type { UpId, UserProfile } from "@calcom/types/UserProfile"; import type { UpId, UserProfile } from "@calcom/types/UserProfile";
import { isOrganization } from "../../entityPermissionUtils"; import { isOrganization } from "../../entityPermissionUtils";
@@ -12,6 +13,54 @@ import { getParsedTeam } from "./teamUtils";
const log = logger.getSubLogger({ prefix: ["[repository/user]"] }); const log = logger.getSubLogger({ prefix: ["[repository/user]"] });
export const ORGANIZATION_ID_UNKNOWN = "ORGANIZATION_ID_UNKNOWN"; export const ORGANIZATION_ID_UNKNOWN = "ORGANIZATION_ID_UNKNOWN";
const teamSelect = Prisma.validator<Prisma.TeamSelect>()({
id: true,
name: true,
slug: true,
metadata: true,
logoUrl: true,
});
const userSelect = Prisma.validator<Prisma.UserSelect>()({
id: true,
username: true,
name: true,
email: true,
emailVerified: true,
bio: true,
avatar: true,
avatarUrl: true,
timeZone: true,
startTime: true,
endTime: true,
weekStart: true,
bufferTime: true,
hideBranding: true,
theme: true,
createdDate: true,
trialEndsAt: true,
completedOnboarding: true,
locale: true,
timeFormat: true,
twoFactorSecret: true,
twoFactorEnabled: true,
backupCodes: true,
identityProviderId: true,
invitedTo: true,
brandColor: true,
darkBrandColor: true,
away: true,
allowDynamicBooking: true,
allowSEOIndexing: true,
receiveMonthlyDigestEmail: true,
verified: true,
disableImpersonation: true,
locked: true,
movedToProfileId: true,
metadata: true,
});
export class UserRepository { export class UserRepository {
static async findTeamsByUserId({ userId }: { userId: UserType["id"] }) { static async findTeamsByUserId({ userId }: { userId: UserType["id"] }) {
const teamMemberships = await prisma.membership.findMany({ const teamMemberships = await prisma.membership.findMany({
@@ -19,7 +68,9 @@ export class UserRepository {
userId: userId, userId: userId,
}, },
include: { include: {
team: true, team: {
select: teamSelect,
},
}, },
}); });
@@ -69,6 +120,7 @@ export class UserRepository {
return ( return (
await prisma.user.findMany({ await prisma.user.findMany({
select: userSelect,
where, where,
}) })
).map((user) => { ).map((user) => {
@@ -136,7 +188,7 @@ export class UserRepository {
return { where, profiles }; return { where, profiles };
} }
static async findByEmailAndIncludeProfiles({ email }: { email: string }) { static async findByEmailAndIncludeProfilesAndPassword({ email }: { email: string }) {
const user = await prisma.user.findUnique({ const user = await prisma.user.findUnique({
where: { where: {
email: email.toLowerCase(), email: email.toLowerCase(),
@@ -157,7 +209,9 @@ export class UserRepository {
locale: true, locale: true,
teams: { teams: {
include: { include: {
team: true, team: {
select: teamSelect,
},
}, },
}, },
}, },
@@ -179,6 +233,7 @@ export class UserRepository {
where: { where: {
id, id,
}, },
select: userSelect,
}); });
if (!user) { if (!user) {
@@ -100,7 +100,11 @@ export const confirmHandler = async ({ ctx, input }: ConfirmOptions) => {
schedulingType: SchedulingType.COLLECTIVE, schedulingType: SchedulingType.COLLECTIVE,
}, },
select: { select: {
users: true, users: {
select: {
id: true,
},
},
}, },
}); });
@@ -127,7 +127,6 @@ export const requestRescheduleHandler = async ({ ctx, input }: RequestReschedule
event = await prisma.eventType.findFirstOrThrow({ event = await prisma.eventType.findFirstOrThrow({
select: { select: {
title: true, title: true,
users: true,
schedulingType: true, schedulingType: true,
recurringEvent: true, recurringEvent: true,
}, },
@@ -32,7 +32,11 @@ export const duplicateHandler = async ({ ctx, input }: DuplicateOptions) => {
include: { include: {
customInputs: true, customInputs: true,
schedule: true, schedule: true,
users: true, users: {
select: {
id: true,
},
},
team: true, team: true,
workflows: true, workflows: true,
webhooks: true, webhooks: true,
@@ -20,7 +20,11 @@ export const eventOwnerProcedure = authedProcedure
const event = await ctx.prisma.eventType.findUnique({ const event = await ctx.prisma.eventType.findUnique({
where: { id: input.id }, where: { id: input.id },
include: { include: {
users: true, users: {
select: {
id: true,
},
},
team: { team: {
select: { select: {
members: { members: {
@@ -29,7 +29,14 @@ export const paymentsRouter = router({
}, },
include: { include: {
payment: true, payment: true,
user: true, user: {
select: {
email: true,
locale: true,
name: true,
timeZone: true,
},
},
attendees: true, attendees: true,
eventType: true, eventType: true,
}, },
@@ -147,7 +147,11 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
id: newEventTypeId, id: newEventTypeId,
}, },
include: { include: {
users: true, users: {
select: {
id: true,
},
},
team: { team: {
include: { include: {
members: true, members: true,