Problem/Motivation

Currently, logic for deciding whether to remove extension validation checks:

  if (isset($validators['FileExtension'])) {
      if (!isset($validators['FileExtension']['extensions'])) {
        // If 'FileExtension' is set and the list is empty then the caller wants
        // to allow any extension. In this case we have to remove the validator
        // or else it will reject all extensions.
        unset($validators['FileExtension']);
      }
    }

However, the code comments say:

// If 'FileExtension' is set and the list is empty then the caller wants
// to allow any extension. In this case we have to remove the validator
// or else it will reject all extensions.

Either the code is wrong or the comment is wrong.

There are tests that rely on having an empty extensions list to not trigger the security rename feature later in the code.

We need to evaluate whether the test assumptions are correct.

Steps to reproduce

Proposed resolution

Change the code to check for empty instead:

  if (isset($validators['FileExtension'])) {
      if (empty($validators['FileExtension']['extensions'])) {
        // If 'FileExtension' is set and the list is empty then the caller wants
        // to allow any extension. In this case we have to remove the validator
        // or else it will reject all extensions.
        unset($validators['FileExtension']);
      }
    }

Remaining tasks

Fix test failures.

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3409904

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kim.pepper created an issue. See original summary.

sudishth’s picture

Status: Active » Needs review
kim.pepper’s picture

Status: Needs review » Needs work

\Drupal\Tests\file\Functional\SaveUploadTest::testHandleFileMunge() tests explicitly:

// Ensure that setting $validators['FileExtension'] = ['extensions' = '']
// rejects all files without munging or renaming.

so I guess my assumption that empty string and unset 'extensions' key have the same behaviour was wrong.

kim.pepper’s picture

Status: Needs work » Closed (won't fix)

I think this can be closed now that the logic in #3420802: [regression] file_save_upload does not properly handle extensions has been fixed. We can work on improving the API in #3403253: Simplify how 'allow all extensions' file upload validation works