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

Improve validation for image uploads. #2394

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions imports/client/components/InsertImageModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ const InsertImageModal = React.forwardRef(
isInvalid={fileInvalid}
required={imageSource === "upload"}
ref={fileRef}
accept=".png,.jpg,.jpeg,.gif"
/>
</Tab>
<Tab eventKey="link" title="Link">
Expand Down
32 changes: 32 additions & 0 deletions imports/server/methods/insertDocumentImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ import Settings from "../../lib/models/Settings";
import insertDocumentImage from "../../methods/insertDocumentImage";
import defineMethod from "./defineMethod";

async function validateImageLink(link: string) {
// Attempt to fetch the response headers for the image to determine the MIME type.
// If the request fails, we continue with the upload - invalid images will still fail, just with
// a more confusing error message. We do this here and not client-side to bypass any CORS
// restrictions. We do this here and not in the App Script to make a HEAD request and avoid
// fetching the image contents.
const resp = await fetch(link, {
method: "HEAD",
});
if (!resp.ok) {
// Couldn't fetch the image for validation. Let the request through.
return;
}
const contentType = resp.headers.get("Content-Type");
if (!contentType) {
// Response didn't include content-type header. Let the request through.
return;
}
if (
contentType !== "image/png" &&
contentType !== "image/jpeg" &&
contentType !== "image/gif"
) {
throw new Meteor.Error(
400,
"Unsupported format. Only PNG/GIF/JPG are supported. Try copying the image and pasting " +
"into the sheet with the keyboard instead.",
);
}
}

defineMethod(insertDocumentImage, {
validate(arg) {
check(arg, {
Expand Down Expand Up @@ -56,6 +87,7 @@ defineMethod(insertDocumentImage, {
};
break;
case "link":
await validateImageLink(image.url);
imageParams = {
link: image.url,
};
Expand Down
Loading