On the file-settings page of an image field instance (function image_field_settings_form) you can upload a default image. But the file type isn't checked correctly. It is checked by the file module and not by the image module as it should be. So you could upload a .txt-file to be your default image if you wish. No

Looks like it could be a quick fix. I think that image.field.inc just needs a new and common validation-function that can be shared between different functions. Now, only the widget (image_field_widget_form) is being checked for not-images in the extensions.

Comments

August1914’s picture

StatusFileSize
new23.28 KB

The reported behavior can be reproduced under 7.x and 8.x

Steps to reproduce:
go to admin/structure/types/manage/article/fields/field_image
in the Default Image field, select a file with an extension .txt and upload.
Expected behavior:
Upload is denied for non-image file type.
Observed behavior:
text file is uploaded as default image.

The file system gets a crack a upload validation before it goes on to image, and probably that is as it should be, so the comment
"It is checked by the file module and not by the image module as it should be"
is maybe misleading, should be more like
"should be checked by the image module in addition to the file module check"

(The file module will restrict the upload to jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp on a default installation.)

August1914’s picture

It seems like a valid solution to refactor image_field_widget_form (also in image.field.inc) to make the extension validation used there available to function image_field_settings_form.

    // If not using custom extension validation, ensure this is an image.
    $supported_extensions = array('png', 'gif', 'jpg', 'jpeg');
    $extensions = isset($elements[$delta]['#upload_validators']['file_validate_extensions'][0]) ? $elements[$delta]['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
    $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
    $elements[$delta]['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);

Perhaps another direction, there is a discussion over at http://drupal.org/node/959800 demonstrates validation based on mime type rather than extension...

August1914’s picture

Version: 7.14 » 8.x-dev
Assigned: Unassigned » August1914
Status: Active » Needs review
StatusFileSize
new1.2 KB

Turns out that no refactoring is required, just needed to patch up the plumbing: setting #upload_validators and #file_validate_extensions for the default_image form element. (The actual validation takes place in node.js, and that is unchanged.)

After patch
go to admin/structure/types/manage/article/fields/field_image
in the Default Image field, select a file with an extension .txt and upload.
Expected behavior:
Upload is denied for non-image file type.
Observed behavior is as expected.

This patch applies cleanly on 8.x at 5be3d55
As things stand, the 7.x backport would be same except for file structure.

August1914’s picture

Assigned: August1914 » Unassigned
ryan.ryan’s picture

Status: Needs review » Reviewed & tested by the community

I tested the patch before and after and can verify that before I was allowed to set .txt file as default and after I received the expected error message that notified me of the allowed content types. Looks good to me!

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Nice bug fix! Let's get a test for this.

Tor Arne Thune’s picture

+++ b/core/modules/image/image.field.incundefined
@@ -61,8 +61,10 @@ function image_field_settings_form($field, $instance) {
+    '#upload_validators' => array('png', 'gif', 'jpg', 'jpeg'),

Shouldn't this be: '#upload_validators' => array('file_validate_extensions' => array('png', 'gif', 'jpg', 'jpeg')), or file_field_widget_upload_validators($field, $instance),

+++ b/core/modules/image/image.field.incundefined
@@ -61,8 +61,10 @@ function image_field_settings_form($field, $instance) {
+    '#file_validate_extensions'  => file_field_widget_upload_validators($field, $instance),

Also, what's this about? I can't find any mention of #file_validate_extensions in Drupal core.

nmudgal’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB

I guess as stated in #7, no need of mentioning file extensions manually rather than just lettings 'file' module handle it?
I have attached the modified patch. Thought to make my first test patch for this but no luck :(

Thanks

jhood’s picture

StatusFileSize
new78.53 KB

Applied and tested patch #8 and it works.

valthebald’s picture

Status: Needs review » Needs work

While patch does it's job, there's still no test coverage.
Could you add a case for testing this behavior. It should fail without applying patch, and pass with applied patch

tlarrieu’s picture

While trying to see if I could create the requested simpleTest cases, I noticed that with the patch applied, I get the appropriate error message when I load test.txt in the file upload button at:

/admin/structure/types/manage/article/fields/node.article.field_image

But I do not get the error if I do the upload from:

/admin/structure/types/manage/article/fields/node.article.field_image/field

On the other hand, in the second case, once I press "Save field settings", the badly named uploaded file is silently discarded.

claudiu.cristea’s picture

I'm ready to review this. Can you add the test requested in #6? Thanks.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Issue summary: View changes
Status: Needs work » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative

I tested this on Drupal 9.3.x, standard install and when I tried to upload a .pdf as the default image the following error message, which included the correct allowed file extension, was displayed. "The selected file test.pdf cannot be uploaded. Only files with the following extensions are allowed: png, gif, jpg, jpeg."

Therefore, closing as cannot reproduce. If you are experiencing this problem reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

Thanks!