Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes security issue with Avatar Edit Modal #8917

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/components/Common/AvatarEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const VideoConstraints = {
},
} as const;

const isImageFile = (file?: File) => file?.type.split("/")[0] === "image";

type IVideoConstraint =
(typeof VideoConstraints)[keyof typeof VideoConstraints];

Expand Down Expand Up @@ -87,19 +89,20 @@ const AvatarEditModal = ({
};

useEffect(() => {
if (selectedFile) {
const objectUrl = URL.createObjectURL(selectedFile);
setPreview(objectUrl);
return () => URL.revokeObjectURL(objectUrl);
if (!isImageFile(selectedFile)) {
return;
}
const objectUrl = URL.createObjectURL(selectedFile!);
setPreview(objectUrl);
return () => URL.revokeObjectURL(objectUrl);
}, [selectedFile]);

const onSelectFile: ChangeEventHandler<HTMLInputElement> = (e) => {
if (!e.target.files || e.target.files.length === 0) {
setSelectedFile(undefined);
return;
}
if (e.target.files[0]?.type.split("/")[0] !== "image") {
if (!isImageFile(e.target.files[0])) {
Warn({ msg: "Please upload an image file!" });
return;
}
Expand Down Expand Up @@ -134,7 +137,7 @@ const AvatarEditModal = ({
dragProps.setDragOver(false);
setIsDragging(false);
const droppedFile = e?.dataTransfer?.files[0];
if (droppedFile.type.split("/")[0] !== "image")
if (!isImageFile(droppedFile))
return dragProps.setFileDropError("Please drop an image file to upload!");
setSelectedFile(droppedFile);
};
Expand Down
Loading