No error handling for filesize() in Drupal\Core\Image\Image

Created on 29 November 2016, over 8 years ago
Updated 27 March 2024, about 1 year ago

In Drupal\Core\Image\Image::construct(), filesize() is being used to determine the size of file but no exception/error/warning handling if unable to get filesize.

Use case :- If files are on s3 and trying to read files from there (Creating file entity in drupal), then this filesize() throws warning and no way to suppress it.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Image system 

Last updated 6 days ago

Created by

🇮🇳India joshi.rohit100 Delhi, India

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia kim.pepper 🏄‍♂️🇦🇺Sydney, Australia

    kim.pepper made their first commit to this issue’s fork.

  • Merge request !7217#2831320 Suppress the filesize() warning → (Open) created by kim.pepper
  • Status changed to Needs review about 1 year ago
  • 🇦🇺Australia kim.pepper 🏄‍♂️🇦🇺Sydney, Australia

    We should suppress the warning, and check if @filesize() returns FALSE. Also \Drupal\Core\Image\ImageInterface::getFileSize() expects this to be NULL not FALSE if the file is invalid, so we should handle that too.

  • 🇦🇺Australia kim.pepper 🏄‍♂️🇦🇺Sydney, Australia
  • Pipeline finished with Success
    about 1 year ago
    Total: 557s
    #130975
  • 🇦🇺Australia pameeela

    Issue summary updated. I can't really provide steps to reproduce as it requires a custom architecture to host files on s3.

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Possible to add test coverage for when it is NULL?

  • First commit to issue fork.
  • Pipeline finished with Failed
    4 months ago
    Total: 609s
    #368969
  • 🇪🇸Spain vidorado Logroño (La Rioja)

    I've added a couple of tests and attempted to simulate an S3 filesystem where filesize() returns FALSE, even though the toolkit returns TRUE in parseFile()

  • Pipeline finished with Failed
    4 months ago
    Total: 911s
    #368974
  • 🇦🇺Australia kim.pepper 🏄‍♂️🇦🇺Sydney, Australia

    Good test coverage. I ran the test-only pipeline and it fails as expected. The current docblock already says it should return int|null so there is no API breaks afaik, and we don't need a change record for a bug fix. I think we are safe to RTBC.

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Thanks for the review @kim.pepper!

    Mind the "null" in the @return of the docblock was added in this issue, it wasn´t already there: https://git.drupalcode.org/project/drupal/-/merge_requests/7217/diffs?co...

    I believe that in the cases where a false was taken into account, it would have been with a simple truthy evaluation if($value). I think it's rare that anyone could have used if(FALSE === $value), so the NULL is probably not harming.

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    We need to add a comment about why we're suppressing errors here.

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Done @alexpott! :)

  • Pipeline finished with Success
    4 months ago
    Total: 390s
    #369741
  • 🇦🇺Australia kim.pepper 🏄‍♂️🇦🇺Sydney, Australia

    Feedback resolved. Back to RTBC

  • 🇮🇹Italy mondrake 🇮🇹

    Looking at the motivation, I am not sure about this. The file must have a size, but we cannot get it so we hide the problem. BTW possibly the image cannot be loaded by GD either. IMHO the right solution here would be to download the file from S3 to local storage, get its stats and use it for manipulation. IIRC the ImageMagick toolkit does something similar since it does not support stream wrappers.

  • 🇺🇸United States mfb San Francisco

    filesize() can return zero for empty files; in this case, zero should be returned, not NULL

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    I agree with you @mfb, and I took that into account, but it's an imposible case, as the toolkit will never validate an image as valid if the file is zero size.

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Sorry, I didn't see @mondrake's comment. I agree that returning NULL is hiding the problem in the case of a VALID image. However, I stand by saying that zero should never be returned by getFileSize(), since its docblock states this:

    /**
       * Returns the size of the image file.
       *
       * @return int|null
       *   The size of the file in bytes, or NULL if the image is invalid.
       */
      public function getFileSize();
    

    A zero size image would never be valid, so a NULL must be returned. But in case of a valid image that we can't retrieve easily with filesize()... returning NULL is wrong, that seems clear.

    I will take a look at the ImageMagick toolkit to get ideas about how to download and parse the file from S3.

    Thanks all for the fedback! :)

  • 🇺🇸United States mfb San Francisco

    Ok, sounds reasonable to not worry about the zero size case then (would only happen in edge cases like buggy toolkit, file getting truncated by another process etc.)

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    I've installed an AWS S3 mock with Localstack and amazon-ec2-metadata-mock.

    Then, I've been able to check that filesize() works well with a s3:// stream.

    Perhaps this is not longer an issue? I don't have a real S3 bucket, maybe someone could do a real test.

  • 🇪🇸Spain vidorado Logroño (La Rioja)
  • 🇺🇸United States smustgrave

    Believe feedback has been addressed for this one.

  • 🇬🇧United Kingdom catch

    It looks like #31 needs to be answered here and the issue summary updated based on the results.

    If the file is actually invalid, then I don't think we need to suppress the error in that case - it's correctly informing people about the invalid file. We could convert the FALSE to NULL though but that's not the only thing the MR does.

Production build 0.71.5 2024