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

despite failing file type validation, file is still uploaded to cloudinary #40

Open
ryan-k8 opened this issue Jan 27, 2022 · 0 comments

Comments

@ryan-k8
Copy link

ryan-k8 commented Jan 27, 2022

code that still allowed false file to be uploaded to cloudinary

const storage = new CloudinaryStorage({
  cloudinary,
  params: async (req, file) => {
    return {
      folder: process.env.CLOUDINARY_FOLDER_NAME,
      allowedFormats: ['png','jpeg','jpg'],
      public_id: crypto.randomBytes(16).toString("hex"),
    };
  },
});

const upload = multer({
  storage: storage,
  limits: { fileSize: MAXFILESIZE },
  fileFilter: (req, file, cb) => {
    if (!allowedFormats.includes(file.mimetype)) {
      cb(new UploadError("only jpeg/png/jpg images allowed!"));
    }

    cb(null, true);
  },
});

this still allowed file to be uploaded to cloudinary. so i had to throw an err in instantiation of CloudinaryStorage object to prevent that.

const allowedFormats = ["image/jpeg", "image/jpg", "image/png"];

const storage = new CloudinaryStorage({
  cloudinary,
  params: async (req, file) => {
    if (!allowedFormats.includes(file.mimetype)) {
      throw new UploadError(
        `only files of mimetype ${allowedFormats.join(",")} are allowed`
      );
    }

    return {
      folder: process.env.CLOUDINARY_FOLDER_NAME,
      public_id: crypto.randomBytes(16).toString("hex"),
    };
  },
});

i think something to handle this situation should be added to this package for improvement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant