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

Created on 29 November 2016, about 8 years ago
Updated 27 March 2024, 10 months 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 1 day 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 10 months 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
    10 months 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 10 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Possible to add test coverage for when it is NULL?

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 2 months ago
    Total: 609s
    #368969
  • ๐Ÿ‡ช๐Ÿ‡ธSpain vidorado Pamplona (Navarra)

    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
    about 2 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 Pamplona (Navarra)

    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 Pamplona (Navarra)

    Done @alexpott! :)

  • Pipeline finished with Success
    about 2 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 Pamplona (Navarra)

    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 Pamplona (Navarra)

    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 Pamplona (Navarra)

    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 Pamplona (Navarra)
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Believe feedback has been addressed for this one.

Production build 0.71.5 2024