Generated with the help of an LLM.
Problem/Motivation
Discovered on Drupal core 11.4.5
PathProcessorImageStyles::processInbound() builds the prefix it looks for by concatenating a leading slash with the public stream wrapper's directory path:
$directory_path = $this->streamWrapperManager->getViaScheme('public')->getDirectoryPath(); if (str_starts_with($path, '/' . $directory_path . '/styles/')) { $path_prefix = '/' . $directory_path . '/styles/'; }
StreamWrapperInterface::getDirectoryPath() may legitimately return an empty string. Core's own PublicStream::getDirectoryPath() never does, but the method is part of the public stream wrapper API and contrib modules that replace the public:// scheme with remote storage (Flysystem, S3FS and similar) return '' when the configured public base URL is a bare host with no path component (a CDN or bucket domain such as https://cdn.example.com).
When that happens the prefix becomes '//styles/', which never matches the incoming path /styles/... that the site actually generates, so processInbound() does not rewrite the path and the image style route is never matched. The result is a 404 for every image style derivative that has not been generated yet.
The path processor already tolerates the analogous private-files case by searching for /system/files/styles/ inside the path instead of anchoring it, so the inbound processing is inconsistent between the two cases.
Steps to reproduce
- Install a module that replaces the
public://stream wrapper with one whosegetDirectoryPath()returns''(any remote-storage module configured with a bare domain as public base URL). Minimal reproduction without contrib: register a stream wrapper forpublicwhosegetDirectoryPath()returns''. - Request an image style derivative that has not been generated yet, e.g.
/styles/thumbnail/public/example.jpg. - The request 404s:
processInbound()compared the path against'//styles/'.
Proposed resolution
Collapse repeated leading slashes when building the prefix, so an empty directory path yields /styles/ — which is exactly the URL Drupal generates in that configuration:
$public_styles_prefix = preg_replace('#^/+#', '/', '/' . $directory_path . '/styles/'); if (str_starts_with($path, $public_styles_prefix)) { $path_prefix = $public_styles_prefix; }
Remaining tasks
User interface changes
NA
Introduced terminology
NA
API changes
NA
Data model changes
NA
Release notes snippet
NA
| Comment | File | Size | Author |
|---|---|---|---|
| core-02-image-style-path-processor-empty-directory-path.patch | 1.37 KB | luigisa |
Issue fork drupal-3616475
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
smustgrave commentedFixes need to be in MRs and as a bug test coverage
Comment #3
luigisa commentedComment #5
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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 #6
luigisa commentedComment #7
luigisa commentedComment #8
smustgrave commentedCan we extend an existing test vs adding a new one. Also can the AI verbosness be cleaned up and written in your own wording?
Comment #9
smustgrave commentedTweaked the IS slightly with the new policy template.
Also un-assigned per the issue policy.
I was wondering if this could be a unit test but see we don't have one that fits and not sure worth adding a new file for it as kernel should be innocent enough
Shows the coverage
But looking at the fix instead of doing a preg_replace could we do something like
$directory_path ? '/' . $directory_path : '' . '/styles/and avoid that call all together?