Check for new files when validating size

Created on 30 August 2022, about 2 years ago
Updated 22 May 2024, 6 months ago

Problem/Motivation

  • If the allowed file size of a field setting is reduced, the entity of that field can no longer be saved.
  • If a file over the limit is imported using the migrate module or other means, the entity for that field can not be edited. Imagine importing media but then the user wishes to set the focal point of an image.

Steps to reproduce

  1. Add an image or file field to any entity type (media perhaps)
  2. set the allowed file size to something large (20MB)
  3. create an entity of that type and upload a fair sized file
  4. Edit the field settings, reducing the allowed file size below that of which you uploaded (10 bytes even)
  5. Attempt to just edit the previous entity
  6. Observe the error.

Proposed resolution

Check if the file is new before validating the size.

Remaining tasks

  • Patch
  • Tests?

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
File systemΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States pookmish

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Could you provide additional steps maybe? Tried replicating in D10 but was unable to generate the error.

  • πŸ‡ΊπŸ‡ΈUnited States pookmish
    1. Install Drupal 10 using the "Standard" profile
    2. Create an "Article" content and upload an image to the image field via '/node/add/article' page
    3. Observe the image file size you've uploaded
    4. Edit the image field settings to be lower than your image size via '/admin/structure/types/manage/article/fields/node.article.field_image' page (even 1KB would work but I suggest 100KB lower than your uploaded image size)
    5. Go back and edit the article from earlier
    6. attempt to save with no changes on the form
    7. Observe the error won't allow saving

    This works for any entity type in my findings: Node, Media, Paragraph, Taxonomy, User, etc.

  • πŸ‡ΈπŸ‡°Slovakia poker10

    I tested this and was able to replicate it using the steps from #14.

    diff --git a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php
    -    $this->assertCount(2, $result);
    +    $this->assertCount(1, $result);
     
         $this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath());
    -    $this->assertEquals('The file is <em class="placeholder">2.93 KB</em> exceeding the maximum file size of <em class="placeholder">2 KB</em>.', (string) $result->get(0)->getMessage());
    -    $this->assertEquals('field_test_file.0', $result->get(1)->getPropertyPath());
    -    $this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(1)->getMessage());
    +    $this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(0)->getMessage());
    

    Initially I was not sure why we need to lower the error count here, because the code is run on EntityTest::create(), but it seems like we are referencing an existing file, so this could be a reason why it should skip the size validation.

    I think this also means that we should add an explicit test for this bug, because we are not testing if the files size validation is/is not applied for new/existing files.

  • πŸ‡ΊπŸ‡ΈUnited States pookmish

    Looks like the test changes from #16 were actually implemented in another change.

    Rerolling the patch with just the .module change for D10.2.0+

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States capysara

    capysara β†’ changed the visibility of the branch 10.1.x to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States capysara

    capysara β†’ changed the visibility of the branch 11.x to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States capysara

    capysara β†’ changed the visibility of the branch ddev1223-drupal-11 to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States capysara

    capysara β†’ changed the visibility of the branch 3306916-check-for-new to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States capysara

    I was able to replicate the issue in D11 using the steps in #14.
    When I tried to edit and save my existing node I got an error message:

    I'm moving this to a merge request instead of patches. Note: the function file_validate_size is deprecated in 11, so this change was made in the validate method of FileSizeLimitConstraintValidator.php instead.

    I updated FileItemValidationTest, repeating the work from #16.

    After the update, a user can save a node even if they previously added a file that exceeds the new limit.

    After the update, the file limit validation still applies correctly when a user attempts to upload a files that exceeds the limit. The same behavior applies if you remove the image and attempt to re-add it.

    Tests are still needed as per #17.

  • πŸ‡―πŸ‡΄Jordan abu-zakham

    Patch #18 created in the core directory, re-roll the patch from the root directory.

  • πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia

    file_validate_size() is deprecated in 10.2.0 and removed in 11.0.0. See https://www.drupal.org/node/3363700 β†’

Production build 0.71.5 2024