Wrong evaluation of an uploaded file size in drupal 10.1

Created on 29 June 2023, over 1 year ago
Updated 5 October 2023, about 1 year ago

Problem/Motivation

Media Bulk Upload 3.0.2 no matter if dropzonejs enabled or not cannot properly get the maximum allowed file size for uploads, so it's impossible to upload.

Steps to reproduce

Drupal 10.1.0, install and enable MBU, configure media image entity with some upload size limit.

Proposed resolution

Looks like the problem is with
public function getTargetFieldMaxSize(MediaTypeInterface $mediaType) inside web/modules/contrib/media_bulk_upload/src/MediaTypeManager.php

It returns allowed max size not in bytes, but in localized form, in my case it's "100 ΠœΠ‘" (100 MB in English). So when validation happens in web/modules/contrib/media_bulk_upload/src/Form/MediaBulkUploadForm.php in protected function validateFileSize(MediaTypeInterface $mediaType, FileInterface $file) it happens to compare 100 with file size in bytes. As a result you can only upload files that are less than 100 bytes in size.

πŸ› Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

πŸ‡·πŸ‡ΊRussia selpi

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

Comments & Activities

  • Issue created by @selpi
  • πŸ‡ΊπŸ‡ΈUnited States TolstoyDotCom L.A.

    MediaTypeManager::getTargetFieldMaxSize can call format_size(), which outputs something like '2 MB'. Then MediaBulkUploadForm::isMaxFileSizeLarger calls Drupal\Component\Utility\Bytes::toNumber() to convert '2 MB' back to 2097152 so it can compare with the other value.

    It seems like the module should instead pass around max filesize as an int (unless that would run into PHP_INT_MAX issues). In any case, it shouldn't deal with formatted sizes. The max filesize should only be formatted when shown to the user.

    Are there issues with doing it like that instead?

  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States TolstoyDotCom L.A.
  • πŸ‡©πŸ‡ͺGermany thomaswalther Rhein-Main Area

    A format_size is also in media for the image type (/admin/structure/media/manage/image/fields/media.image.field_media_image):

    For this feature, an author does not know the large number and can enter the short version. I think this is only an author friendly plain textfield for technical numbers.

    I think the problem would be also on these settings, if the localized ending "ΠœΠ‘" will NOT match for "MB".

    Maybe it is a core problem for this field/setting, if there is a wrong match, no errors will be shown.

Production build 0.71.5 2024