Problem/Motivation
My concern is to use the ability provided by $settings['file_public_path'] and $settings['file_public_base_url'] to
provide a separate webserver dedicated to serve static files and user content. For security, this web server has
deliberately no PHP support.
To facilitate the debug of this issue, I created a simple docker-project hosted on Github:
https://github.com/mickaelperrin/drupal-issues/tree/2754273-image_styles...
Steps to reproduce using the docker container:
1. Launch the docker container with docker-compose up -d`
2. Go to http://your.docker.machine.ip:8888
3. Authenticate with drupal / drupal
4. Go to http://your.docker.machine.ip:8888/node/add/article
5. Click on the "Add image" button
6. Select a file
Exepected result: The thumbnail of the selected image is properly displayed
Result: The thumbnail show a missing image (error 404)`
Proposed resolution
The problem seems to be related with the `PathProcessor`of the module `image`. It appends at the beggining of the
public setting path a slash wether the path is absolute or relative.
In case of absolute path, two slashs begin the path and the method to detect if the path is public or private fails, and
so on the url processing.
The attached add a simple test to check if a slash is needed or not.
You can test the issue with the path by launching the docker containers with the command: `docker-compose -f docker-compose.yml -f docker-compose.patched.yml up -d`
Remaining tasks
As it is my first patch and I am new to Drupal, a review is obviously needed.
| Comment | File | Size | Author |
|---|---|---|---|
| #51 | 2754273-51.patch | 3.37 KB | heatherwoz |
| #48 | 2754273-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #42 | 14808630-37.patch | 3.25 KB | suryabhi |
Issue fork drupal-2754273
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
Comment #2
mickaelperrin commentedComment #3
mickaelperrin commentedComment #7
klausiI think the patch is not fully correct. Instead of using the path from $settings['file_public_path'] we should use the path from $settings['file_public_base_url'].
This is necessary for sites that want to run Drupal in a secure fashion with an isolated web root. They have to set
for example. As this is an important security improvement and this breaks image styles on such sites I think this should be major.
Here is a new patch that uses $settings['file_public_base_url'] if set.
Comment #11
bsztreha commentedI can successfully apply #7 patch to Drupal 8.6.16, and seems working.
Comment #14
twilderanPatch from #7 works for me on Drupal 8.9.6, both Media and regular images.
Comment #16
jibranNow that #3036494: Race condition in ImageStyle::createDerivative() is committed can we try to reproduce this?
Comment #18
richard.walker.ardc commentedAttached re-rolled patch.
Comment #19
richard.walker.ardc commentedA note about configuration that works, and what doesn't work.
What does work #1: where the Drupal base URL doesn't end with a subpath, and
file_public_base_urlends with a subpath. For example, Drupal installed ashttps://myhost, andfile_public_base_urlashttps://myhost/files.What does work #2: where the Drupal base URL ends with a subpath, and
file_public_base_urlends with a different subpath. For example, Drupal installed ashttps://myhost/drupalpath, andfile_public_base_urlashttps://myhost/drupalpathfiles.What doesn't work: where the Drupal base URL ends with a subpath, and
file_public_base_urlends with a subpath of the Drupal base supbath. For example, Drupal installed ashttps://myhost/drupalpath, andfile_public_base_urlashttps://myhost/drupalpath/files. Here's what seems to be the reason: in this case, theimage.style_publicroute is set up with path/drupalpath/files/styles/..., but since REQUEST_URI also begins/drupalpath/files/styles/...and Drupal's base path ends/drupalpath, Symfony strips the leading/drupalpathelement from the REQUEST_URI and looks for a route/files/styles/...instead ... but there isn't one.Comment #20
richard.walker.ardc commentedAnd (of course?) if
file_public_pathpoints to an absolute path that's outside Drupal, that directory needs to have its.htaccessrewrite to Drupal in the case of a miss.The existing, generated
.htaccesshas:I inserted this (copy/pasted from Drupal's own
.htaccess) at the top (where, as per my previous comment, my Drupal is installed at/drupalpath):Any problems with this? (I'm not sure if I need the line for authorization, and I'm reasonably sure I don't need the line for favicon.ico.)
Comment #22
richard.walker.ardc commentedA follow-up to something I wrote earlier:
It turns out that although this example works for thumbnail generation, it breaks file download URLs. In fact, in this example, note that the base URL is not "different" enough, it's now a prefix of the
file_public_base_url. To make file download URLs work, the example must be changed to, e.g.,https://myhost/filesdrupalpath.See
core/lib/Drupal/Component/Utility/UrlHelper.php's functionexternalIsLocal(), as invoked bycore/modules/file/FileUrlGenerator.php's functiongenerate().Comment #25
vladimirausComment #28
vladimirausMoved to merge request.
Comment #30
vladimirausComment #31
fagoThank you, I took a look at the MR and left a couple of comments, see above. Also, the MR should fixed to point against 10.1.x to match the issue version.
Comment #34
vladimirausUse 9.5 MR for Drupal 10.0.
Comment #35
manarth commentedComment #36
brad.bulger commentedIs this testing relative paths that resolve outside of the Drupal webroot? For example, I've been testing a setup where Drupal is installed in a subdirectory on the server, using
$settings['file_public_path'] = '../public';. So a Drupal page would be https://my.server/path/to/drupal/web/node/1 and an uploaded image would be https://my.server/path/to/drupal/public/image01.png. That doesn't work with image styles, so I tried setting file_public_base_url to 'https://my.server/path/to/drupal/public'. That has many of the same symptoms described here.Is adding the clean URL redirect lines to the public file folder .htaccess file going to be a requirement in any case? In order to do things like process the virtual URLs of image styles, for instance.
Comment #38
vladimirausComment #42
suryabhi commentedReroll the patch
Comment #43
smustgrave commentedCan MR be updated to 11.x please
have not reviewed for tests yet
Comment #44
socialnicheguru commentedis there a MR for Drupal 10.3?
Comment #45
vladimiraus@SocialNicheGuru current MR's diff is working for 10.3.
Comment #47
vladimiraus@smustgrave done
Comment #48
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #49
vladimirausShould be good now and pass phpcs
Comment #50
smustgrave commentedThanks! Appears to still need tests though.
Comment #51
heatherwoz commentedPosting the latest diff as a patch.