Image widgets allow to upload unwanted file types (eg PDF, EPS)

Created on 15 July 2024, 9 months ago
Updated 18 July 2024, 9 months ago

Problem/Motivation

In the latest release 3.0.8 my image fields that are configured to accept only specific file types now allow to upload files using extensions that are not in the allowed list.

Steps to reproduce

  1. Create an image field with the following settings:
    • Upload destination: Rokka
    • Allowed file extensions: "jpg jpeg"
  2. Use the entity create form to upload an image to the field.

Expected result: I can only upload images with the extensions jpg or jpeg.
Actual result: I can upload images with extensions jpg, jpeg, pdf, eps, webp, svg, tiff, heic.

Proposed resolution

Don't change the configured file extensions.

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇧🇬Bulgaria pfrenssen Sofia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pfrenssen
  • Status changed to Needs review 9 months ago
  • 🇨🇭Switzerland ayalon

    It was like that before. But for all image fields. Now it is limited to rokka fields.

  • 🇧🇬Bulgaria pfrenssen Sofia

    Standard image field when using Rokka 3.0.6:

    After updating to Rokka 3.0.8:

    The regression has been introduced in commit: 5387a5f.

    The previous implementation was extending the allowed extensions in RokkaImageFactory::getSupportedExtensions() which was OK since the image factory needs to know which extensions it can handle. This does not affect user facing forms in any way.

    In the commit the extensions that are supported by Rokka but not by the default GD2 image library are being injected in user facing forms:

    function rokka_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
      // ...
      if ($uri_schema === 'rokka') {
        $default = explode(' ', $element['#upload_validators']['FileExtension']['extensions']);
        $rokka_specific = ['pdf', 'eps', 'webp', 'svg', 'tiff', 'heic'];
        $extensions = array_unique(array_merge($default, $rokka_specific), SORT_REGULAR);
        $element['#upload_validators']['FileExtension']['extensions'] = implode(' ', $extensions);
      }
    }
    

    There is no need to append the extensions in this way. The File module already allows the site builder to set the allowed extensions using the Field UI. They can just add PDF or other extensions there.

    From a different perspective: the file extensions that can be stored on Rokka should not be controlled on form level, but rather by the image manipulation API. Then they will work correctly also when files are added outside of forms. We should use ImageToolkitInterface::getSupportedExtensions(), as is proposed as part of the fix for 🐛 Fatal error "Stream must be a resource" Needs review .

Production build 0.71.5 2024