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

  1. Install a module that replaces the public:// stream wrapper with one whose getDirectoryPath() returns '' (any remote-storage module configured with a bare domain as public base URL). Minimal reproduction without contrib: register a stream wrapper for public whose getDirectoryPath() returns ''.
  2. Request an image style derivative that has not been generated yet, e.g. /styles/thumbnail/public/example.jpg.
  3. 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

Issue fork drupal-3616475

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:

Comments

luigisa created an issue. See original summary.

smustgrave’s picture

Version: 11.x-dev » main
Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs tests

Fixes need to be in MRs and as a bug test coverage

luigisa’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new731 bytes

The 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.

luigisa’s picture

Status: Needs work » Needs review
luigisa’s picture

Issue summary: View changes
smustgrave’s picture

Can we extend an existing test vs adding a new one. Also can the AI verbosness be cleaned up and written in your own wording?

smustgrave’s picture

Assigned: luigisa » Unassigned
Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Tweaked 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

Failed asserting that two strings are identical.
Expected :'/styles/thumbnail/public'
Actual   :'/styles/thumbnail/public/example.jpg'

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?