Skip to content

Commit f81f0a2

Browse files
fix: Prevent possible reason behind avatar infinite redirect (#12143)
1 parent 9a80bb6 commit f81f0a2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { TRPCError } from "@trpc/server";
2222
import { getDefaultScheduleId } from "../viewer/availability/util";
2323
import { updateUserMetadataAllowedKeys, type TUpdateProfileInputSchema } from "./updateProfile.schema";
2424

25+
const log = logger.getSubLogger({ prefix: ["updateProfile"] });
2526
type UpdateProfileOptions = {
2627
ctx: {
2728
user: NonNullable<TrpcSessionUser>;
@@ -35,6 +36,7 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
3536
const userMetadata = handleUserMetadata({ ctx, input });
3637
const data: Prisma.UserUpdateInput = {
3738
...input,
39+
avatar: await getAvatarToSet(input.avatar),
3840
metadata: userMetadata,
3941
};
4042

@@ -61,12 +63,6 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
6163
}
6264
}
6365
}
64-
if (input.avatar) {
65-
data.avatar = await resizeBase64Image(input.avatar);
66-
}
67-
if (input.avatar === null) {
68-
data.avatar = null;
69-
}
7066

7167
if (isPremiumUsername) {
7268
const stripeCustomerId = userMetadata?.stripeCustomerId;
@@ -234,3 +230,17 @@ const handleUserMetadata = ({ ctx, input }: UpdateProfileOptions) => {
234230
// Required so we don't override and delete saved values
235231
return { ...userMetadata, ...cleanMetadata };
236232
};
233+
234+
async function getAvatarToSet(avatar: string | null | undefined) {
235+
if (avatar === null || avatar === undefined) {
236+
return avatar;
237+
}
238+
239+
if (!avatar.startsWith("data:image")) {
240+
// Non Base64 avatar currently could only be the dynamic avatar URL(i.e. /{USER}/avatar.png). If we allow setting that URL, we would get infinite redirects on /user/avatar.ts endpoint
241+
log.warn("Non Base64 avatar, ignored it", { avatar });
242+
// `undefined` would not ignore the avatar, but `null` would remove it. So, we return `undefined` here.
243+
return undefined;
244+
}
245+
return await resizeBase64Image(avatar);
246+
}

0 commit comments

Comments
 (0)