feat: enhance image upload validation across the application (#22766)
* feat: enhance image upload validation across the application
* Patch improvements.
* refactor: streamline avatar upload error handling in updateProfile handler
* refactor: extract image validation logic into a separate module for reuse in BannerUploader and ImageUploader
* fix: reset file input value on validation failure in image uploaders
* fix: update localization key for image file upload error message
* fix: update HTML content validation in image uploader to check for additional byte
* Code Quality improvements
* add : unit tests.
* fix : fixing some more issues.
* fix : some import fixes
* fix : fixing type-check issues
* fix : some more import fixes.
* fix : removing Duplicate max-file-size constant
* refactor: enhance base64 validation with regex pattern
* refactor: centralize MAX_IMAGE_FILE_SIZE and fix SVG checks.
* fix : some toast fixes.
* fixing the size of the banner.
* fix: update accepted image formats in BannerUploader and ImageUploader components
* fix
* coderabbit suggestions addressed.
* addressed coderrabit comment
* refactor: implement generic i18n error messages with interpolation
- Add generic 'unsupported_file_type' translation key with {{type}} interpolation
- Replace hardcoded file type error messages with reusable i18n pattern
- Update imageValidation interfaces to support errorKey and errorParams
- Refactor server-side and client-side validation to use new pattern
- Remove individual translation keys for each file type (PDF, HTML, Script, ZIP, Executable)
- Add new translation keys for other validation errors (SVG, base64, empty data, etc.)
- Type-safe error handling with proper interpolation support
Benefits:
- Single reusable translation pattern reduces duplication
- Easier to maintain and localize
- Consistent error messaging across file types
* feat: add MAX_BANNER_SIZE constant and update file size validation
- Add MAX_BANNER_SIZE constant (5MB) to shared constants file
- Update BannerUploader to use shared constant instead of inline value
- Update ImageUploader to handle new errorKey/errorParams pattern
- Maintain backward compatibility with existing error handling
Note: Pre-existing linting warnings in constants.ts and BannerUploader.tsx
are unrelated to these changes and were present before this refactor.
* adressed volnei's suggestions.
* test: update image validation tests for new errorKey/errorParams pattern
- Update all dangerous file type tests to expect errorKey and errorParams instead of hardcoded error messages
- Add comprehensive tests for new error format validation pattern
- Add tests for backward compatibility with error field
- Add tests for MAX_BANNER_SIZE constant integration
- Verify that unsupported file types now return generic 'unsupported_file_type' key with type interpolation
- Ensure all existing functionality continues to work with enhanced error handling
All 42 tests passing ✅
* test: update server-side image validation tests for new error format
- Update all dangerous file type tests to expect errorKey and errorParams
- Change hardcoded error messages to i18n keys (unsupported_file_type, svg_contains_dangerous_content, etc.)
- Update edge case tests to use new error keys (empty_image_data, invalid_base64_format, unrecognized_image_format)
- Add comprehensive tests for new error format validation pattern
- Add tests for backward compatibility with error field
- Verify that unsupported file types return generic 'unsupported_file_type' key with type interpolation
All 26 server-side tests passing ✅
Complements client-side test updates for complete test coverage
---------
Co-authored-by: Anik Dhabal Babu <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: unknown <[email protected]>
This commit is contained in:
16 files changed
+1128
-64
No files matched your search
@@ -13,6 +13,7 @@ import logger from "@calcom/lib/logger";
|
||||
import { uploadAvatar } from "@calcom/lib/server/avatar";
|
||||
import { checkUsername } from "@calcom/lib/server/checkUsername";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { validateBase64Image } from "@calcom/lib/server/imageValidation";
|
||||
import { updateNewTeamMemberEventTypes } from "@calcom/lib/server/queries/teams";
|
||||
import { resizeBase64Image } from "@calcom/lib/server/resizeBase64Image";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
@@ -151,17 +152,26 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
}
|
||||
}
|
||||
|
||||
// if defined AND a base 64 string, upload and update the avatar URL
|
||||
if (
|
||||
input.avatarUrl &&
|
||||
(input.avatarUrl.startsWith("data:image/png;base64,") ||
|
||||
input.avatarUrl.startsWith("data:image/jpeg;base64,") ||
|
||||
input.avatarUrl.startsWith("data:image/jpg;base64,"))
|
||||
) {
|
||||
data.avatarUrl = await uploadAvatar({
|
||||
avatar: await resizeBase64Image(input.avatarUrl),
|
||||
userId: user.id,
|
||||
});
|
||||
if (input.avatarUrl) {
|
||||
const validation = validateBase64Image(input.avatarUrl);
|
||||
if (!validation.isValid) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Invalid avatar image: ${validation.error}`,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
data.avatarUrl = await uploadAvatar({
|
||||
avatar: await resizeBase64Image(input.avatarUrl),
|
||||
userId: user.id,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: error instanceof Error ? error.message : "Failed to upload avatar",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (input.completedOnboarding) {
|
||||
|
||||
@@ -15,6 +15,8 @@ import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../types";
|
||||
import type { TUpdateInputSchema } from "./update.schema";
|
||||
import { validateBase64Image } from "@calcom/lib/server/imageValidation";
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
|
||||
type UpdateOptions = {
|
||||
ctx: {
|
||||
@@ -186,32 +188,76 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
metadata: mergeMetadata({ ...input.metadata }),
|
||||
};
|
||||
|
||||
if (
|
||||
input.banner &&
|
||||
(input.banner.startsWith("data:image/png;base64,") ||
|
||||
if (input.banner) {
|
||||
const validation = validateBase64Image(input.banner);
|
||||
if (!validation.isValid) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Invalid banner image: ${validation.error}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
input.banner.startsWith("data:image/png;base64,") ||
|
||||
input.banner.startsWith("data:image/jpeg;base64,") ||
|
||||
input.banner.startsWith("data:image/jpg;base64,"))
|
||||
) {
|
||||
const banner = await resizeBase64Image(input.banner, { maxSize: 1500 });
|
||||
data.bannerUrl = await uploadLogo({
|
||||
logo: banner,
|
||||
teamId: currentOrgId,
|
||||
isBanner: true,
|
||||
});
|
||||
input.banner.startsWith("data:image/jpg;base64,") ||
|
||||
input.banner.startsWith("data:image/svg+xml;base64,")
|
||||
) {
|
||||
try {
|
||||
const banner = await resizeBase64Image(input.banner, { maxSize: 1500 });
|
||||
data.bannerUrl = await uploadLogo({
|
||||
logo: banner,
|
||||
teamId: currentOrgId,
|
||||
isBanner: true,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: error instanceof Error ? error.message : "Failed to upload banner",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Unsupported banner format. Please use PNG, JPEG, or SVG.",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
data.bannerUrl = null;
|
||||
}
|
||||
|
||||
if (
|
||||
input.logoUrl &&
|
||||
(input.logoUrl.startsWith("data:image/png;base64,") ||
|
||||
if (input.logoUrl) {
|
||||
const validation = validateBase64Image(input.logoUrl);
|
||||
if (!validation.isValid) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Invalid logo image: ${validation.error}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
input.logoUrl.startsWith("data:image/png;base64,") ||
|
||||
input.logoUrl.startsWith("data:image/jpeg;base64,") ||
|
||||
input.logoUrl.startsWith("data:image/jpg;base64,"))
|
||||
) {
|
||||
data.logoUrl = await uploadLogo({
|
||||
logo: await resizeBase64Image(input.logoUrl),
|
||||
teamId: currentOrgId,
|
||||
});
|
||||
input.logoUrl.startsWith("data:image/jpg;base64,") ||
|
||||
input.logoUrl.startsWith("data:image/svg+xml;base64,")
|
||||
) {
|
||||
try {
|
||||
data.logoUrl = await uploadLogo({
|
||||
logo: await resizeBase64Image(input.logoUrl),
|
||||
teamId: currentOrgId,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: error instanceof Error ? error.message : "Failed to upload logo",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Unsupported logo format. Please use PNG, JPEG, or SVG.",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (input.slug) {
|
||||
@@ -220,12 +266,10 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
/** If the team doesn't have a slug we can assume that it hasn't been published yet. */
|
||||
!prevOrganisation.slug
|
||||
) {
|
||||
// Save it on the metadata so we can use it later
|
||||
data.metadata = mergeMetadata({ requestedSlug: input.slug });
|
||||
} else {
|
||||
data.slug = input.slug;
|
||||
data.metadata = mergeMetadata({
|
||||
// If we save slug, we don't need the requestedSlug anymore
|
||||
requestedSlug: undefined,
|
||||
...input.metadata,
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import { RoleManagementError } from "@calcom/features/pbac/domain/errors/role-ma
|
||||
import { RoleManagementFactory } from "@calcom/features/pbac/services/role-management.factory";
|
||||
import { uploadAvatar } from "@calcom/lib/server/avatar";
|
||||
import { checkRegularUsername } from "@calcom/lib/server/checkRegularUsername";
|
||||
import { validateBase64Image } from "@calcom/lib/server/imageValidation";
|
||||
import { resizeBase64Image } from "@calcom/lib/server/resizeBase64Image";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { MembershipRole } from "@calcom/prisma/enums";
|
||||
@@ -116,20 +117,41 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
|
||||
timeZone: input.timeZone,
|
||||
};
|
||||
|
||||
if (
|
||||
input.avatar &&
|
||||
(input.avatar.startsWith("data:image/png;base64,") ||
|
||||
if (input.avatar) {
|
||||
const validation = validateBase64Image(input.avatar);
|
||||
if (!validation.isValid) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Invalid avatar image: ${validation.error}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
input.avatar.startsWith("data:image/png;base64,") ||
|
||||
input.avatar.startsWith("data:image/jpeg;base64,") ||
|
||||
input.avatar.startsWith("data:image/jpg;base64,"))
|
||||
) {
|
||||
const avatar = await resizeBase64Image(input.avatar);
|
||||
data.avatarUrl = await uploadAvatar({
|
||||
avatar,
|
||||
userId: user.id,
|
||||
});
|
||||
}
|
||||
if (input.avatar === "") {
|
||||
data.avatarUrl = null;
|
||||
input.avatar.startsWith("data:image/jpg;base64,") ||
|
||||
input.avatar.startsWith("data:image/svg+xml;base64,")
|
||||
) {
|
||||
try {
|
||||
const avatar = await resizeBase64Image(input.avatar);
|
||||
data.avatarUrl = await uploadAvatar({
|
||||
avatar,
|
||||
userId: user.id,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: error instanceof Error ? error.message : "Failed to upload avatar",
|
||||
});
|
||||
}
|
||||
} else if (input.avatar === "") {
|
||||
data.avatarUrl = null;
|
||||
} else {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Unsupported image format. Please use PNG, JPEG, or SVG.",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Update user
|
||||
|
||||
Reference in new issue
Block a user