This filter - according to its own description - is supposed to restrict the src attribute of img tags to local images . (period)

However at the end of _filter_html_image_secure_process() the php function getimagesize() decides whether the path being checked is......an image file that getimagesize() happens to support getting the size of, which excludes svg images.

  if (@getimagesize($local_image_path))

Either:

  • the filter's description should be updated to reflect that it only accepts supported image formats
  • or the filter should simply check for the existence of a file: file_exists()

Issue fork drupal-2855653

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Karol Haltenberger created an issue. See original summary.

Neograph734’s picture

I ran into this exact same problem when working on #2842780: Add a token for the site logo.

Since svg's are perfectly valid resources for a src attribute of an img element according to the HTML5 specification paragraph 4.7.1, naturally I'd suppose it should be supported by Drupal as well. (Though there are more issues ragarding svg files.)

I'd like to propose a third option; in addition to getimagesize(), also check if the file is a valid svg file. For instance by checking its MIME-type.

If fact, PHP does not even recommend using getimagesize to detect if a file is a valid image:

Caution
This function expects filename to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsentical values.
Do not use getimagesize() to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead.

So we might want to get rid of that as well?

Neograph734’s picture

Version: 8.2.6 » 8.3.x-dev
Status: Active » Needs review
FileSize
947 bytes

What about replacing

if (@getimagesize($local_image_path)) {
  // The image has the right path. Erroneous images are dealt with below.
  continue;
}

with

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $local_image_path);

if (isset($type) && substr($type, 0, 6 ) === "image/") {
  // The image has the right path. Erroneous images are dealt with below.
  continue;
}

Though this might not accept certain extensions that getimagesize() did accept (swf, jpc, jpf, jb2 and swc), I think the acceptance of svg over those could be a valid trade off. (getimagesize() would accept all IMAGETYPE_XXX constants, where this method would only accept a part of them http://php.net/manual/en/function.image-type-to-mime-type.php)

Status: Needs review » Needs work

The last submitted patch, 3: 2855653-3-FilterHtmlImageSecure_filters_svg.patch, failed testing.

Neograph734’s picture

Status: Needs work » Needs review
FileSize
945 bytes

I forgot the @ in case the file does not exist.

Neograph734’s picture

Status: Needs review » Needs work

Hmm, it appears that some webservers configurations will recognize the files as text/plain or text/html rather than the image/svg+xml I was aiming for. So this does not solve the problem...

We'd probably need some other solution then...

Neograph734’s picture

So I have done some additional searching, this actually has been requested as a PHP feature; Request #71517 Implement SVG support for getimagesize() and friends, but it has not received a lot of attention yet so it probably won't happen soon...

Furthermore my suggestion from #3 to rely on MIME-types is not safe because finfo_open only checks for known patterns in files, which can be deliberately added, making the files appear as another MIME-type. (Perhaps that should be added as a comment above getimagesize()?)

And third, SVG files can contain JavaScript (MDN example). I suppose we'd want to sanitize or block that.

So, the best I could come up with would be something like this:

// Perhaps first check for the .svg extension before validating to not waste valuable processing power?

$dom = new \DOMDocument;
$dom->load($local_image_path);
if ($dom->schemaValidate($custom_svg_schema)) {
    continue; // Valid document.
}

Where $custom_svg_schema could be an adapted version of the svg schema, which does not allow script tags and would cause the validation to fail.

Basically everything described in this StackOverflow post.

But I really have no experience with this...

Karol Haltenberger’s picture

I still think the filter should only check if the file is local and not bother with image formats and sizes.
Ensuring that it is an image should be the responsibility of whatever or whoever put the url there (plugin, widget, the person typing it in etc.).

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.

amaisano’s picture

Any progress on this? Our authors can choose SVG files in the WYSIWYG editor, but when they preview or publish the page, they are seeing a red X icon due to this bug.

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.

zolt_toth’s picture

It is still an issue, with Drupal core 8.7.x.

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.

Neograph734’s picture

There are two problems here.

  • A filter that intends to limit access to local images is also checking that it is in fact an image (which it should or should not do). Then again, you only want to enable this as some safety measure to prevent users from pulling in unknown (untrusted) content. So it does make sense that the filter also checks if the image is valid.
  • As great as SVG images are, they can be terrible dangerous. If you read through https://blobfolio.com/2017/03/when-a-stranger-calls-sanitizing-svgs/ you can see all kinds of exploits people can inject in your website if someone manages to upload a tampered SVG image. So also for SVG images (perhaps specifically for SVG images) it makes sense to check if it is a safe file.

At the bottom of the article is a link to an issue in Wordpress which attempts to solve this by using a huge whitelist: https://core.trac.wordpress.org/attachment/ticket/24251/24251.2.diff

UPDATE, after reading #2868079-14: Sanitize OR add Content-Security-Policy for SVGs I understand that SVG's in HTML img tags should not pose a risk. That is great for this issue because the sanitizing process should not be a blocker then.

Neograph734’s picture

Neograph734’s picture

Status: Needs work » Needs review
FileSize
1.42 KB

I think something like this could work.

Neograph734’s picture

And I think this should suffice as a test

Status: Needs review » Needs work

The last submitted patch, 19: filterhtmlimagesecure_filters_out_svg-2855653-19.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Neograph734’s picture

Had a typo in classy :(

Switched to using the built in mime type guesser and also asserting that external SVG files are invalid.

Status: Needs review » Needs work
Neograph734’s picture

Version: 8.9.x-dev » 9.2.x-dev
Status: Needs work » Needs review

Built on latest dev.

For lower version one can use MimeTypeGuesser::guess() Instead.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work

Patch fails to apply to 9.5

longwave’s picture

Issue tags: +Needs reroll
smustgrave’s picture

Rerolled

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Pierre Paul Lefebvre’s picture

Issue tags: +Needs reroll
smustgrave’s picture

Will also need an issue summary update before review.

rpayanm made their first commit to this issue’s fork.

rpayanm’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

Please review.

smustgrave’s picture

Status: Needs review » Needs work

Thanks!

The issue summary still needs to be updated.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alison’s picture

I think this issue (#2855653) and #2998318: "Restrict images to this site" restricts images that, by definition, *are* on this site. may be duplicates of each other.