diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 20e3d4e68e..eb325df072 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -1012,11 +1012,6 @@ function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $va } $file->setFilename(file_munge_filename($filename, $extensions)); \Drupal::messenger()->addStatus(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()])); - // The .txt extension may not be in the allowed list of extensions. We - // have to add it here or else the file upload will fail. - if (!empty($validators['file_validate_extensions'][0])) { - $validators['file_validate_extensions'][0] .= ' txt'; - } } } } diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php index 17fee8558f..d709552883 100644 --- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php +++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php @@ -485,7 +485,6 @@ protected function prepareFilename($filename, array &$validators) { // If the file will be rejected anyway due to a disallowed extension, it // should not be renamed; rather, we'll let file_validate_extensions() // reject it below. - $passes_validation = FALSE; if (!empty($validators['file_validate_extensions'][0])) { $file = File::create([]); $file->setFilename($filename); @@ -493,18 +492,17 @@ protected function prepareFilename($filename, array &$validators) { // Only allow upload and rename to .txt if .txt files are allowed. $passes_validation = $passes_validation && preg_match('/(^|\s)txt(\s|$)/', $validators['file_validate_extensions'][0]); } - if (empty($validators['file_validate_extensions'][0]) || $passes_validation) { + else { + // All file extensions are allowed. + $passes_validation = TRUE; + } + + if ($passes_validation) { if ((substr($filename, -4) != '.txt')) { // The destination filename will also later be used to create the URI. $filename .= '.txt'; } $filename = file_munge_filename($filename, $validators['file_validate_extensions'][0] ?? ''); - - // The .txt extension may not be in the allowed list of extensions. We - // have to add it here or else the file upload will fail. - if (!empty($validators['file_validate_extensions'][0])) { - $validators['file_validate_extensions'][0] .= ' txt'; - } } } } diff --git a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php index 5c046b1982..d49cad8e42 100644 --- a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php +++ b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php @@ -403,7 +403,6 @@ protected function prepareFilename($filename, array &$validators) { // If the file will be rejected anyway due to a disallowed extension, it // should not be renamed; rather, we'll let file_validate_extensions() // reject it below. - $passes_validation = FALSE; if (!empty($validators['file_validate_extensions'][0])) { $file = File::create([]); $file->setFilename($filename); @@ -411,18 +410,17 @@ protected function prepareFilename($filename, array &$validators) { // Only allow upload and rename to .txt if .txt files are allowed. $passes_validation = $passes_validation && preg_match('/(^|\s)txt(\s|$)/', $validators['file_validate_extensions'][0]); } - if (empty($validators['file_validate_extensions'][0]) || $passes_validation) { + else { + // All file extensions are allowed. + $passes_validation = TRUE; + } + + if ($passes_validation) { if (substr($filename, -4) != '.txt') { // The destination filename will also later be used to create the URI. $filename .= '.txt'; } $filename = file_munge_filename($filename, $validators['file_validate_extensions'][0] ?? ''); - - // The .txt extension may not be in the allowed list of extensions. We - // have to add it here or else the file upload will fail. - if (!empty($validators['file_validate_extensions'][0])) { - $validators['file_validate_extensions'][0] .= ' txt'; - } } } }