Giter Site home page Giter Site logo

Comments (2)

joshuaellis avatar joshuaellis commented on June 2, 2024

Can you please feel out reproduction steps.

from strapi.

dekhanov avatar dekhanov commented on June 2, 2024

To reproduce the bug you can extend @strapi/plugin-upload. The bug is in the version 4.24.2 of the plugin.

src/extensions/upload/overrides.ts

/**
 * Overriding enhanceAndValidateFile method to validate available file extensions
 * node_modules/@strapi/plugin-upload/server/services/upload.js
 */
import { errors } from '@strapi/utils';
import fs from 'fs';

const { ApplicationError } = errors;
const allowedExtensions = [
  '.jpg',
  '.jpeg',
  '.png',
  '.webp',
  '.svg',
  '.pdf',
  '.mp4',
];

const isFileExtensionAllowed = (fileExtension) => {
  return allowedExtensions.includes(fileExtension);
};

export async function enhanceAndValidateFile(file, fileInfo = {}, metas = {}) {
  // @ts-expect-error this will be available on override
  const currentFile = await this.formatFileInfo(
    {
      filename: file.name,
      type: file.type,
      size: file.size,
    },
    fileInfo,
    {
      ...metas,
      tmpWorkingDirectory: file.tmpWorkingDirectory,
    }
  );
  if (!isFileExtensionAllowed(currentFile?.ext)) {
    throw new ApplicationError(
      `File type is not allowed. Allowed extentions: ${allowedExtensions.join(
        ','
      )}`
    );
  }

  currentFile.getStream = () => fs.createReadStream(file.path);

  const { optimize, isImage, isFaultyImage, isOptimizableImage } = strapi
    .plugin('upload')
    .service('image-manipulation');

  if (await isImage(currentFile)) {
    if (await isFaultyImage(currentFile)) {
      throw new ApplicationError('File is not a valid image');
    }
    if (await isOptimizableImage(currentFile)) {
      return optimize(currentFile);
    }
  }
  return currentFile;
}

src/index.ts

import { enhanceAndValidateFile } from './extensions/upload/overrides';

export default {
  register({ strapi }) {
    /**
     * Overriding enhanceAndValidateFile function to validate available file extensions
     */
    strapi.plugin('upload').service('upload').enhanceAndValidateFile =
      enhanceAndValidateFile;
  },
};

With this configuration, I get an error `throw new ApplicationError('File is not a valid image') ' when trying to upload an image to the media library. ' Everything worked with the plugin's 4.24.1 version.

from strapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.