Problem/Motivation

file_url_transform_relative($file_url) function might need to perform additional check on a file URL port:

Steps to reproduce

On one of ours development environments we are using s3 storage, that is under the same host with the drupal site, but having different port. (like drupal is accessible at http://example.com and s3 storage under http://example.com:9000)
Since file_url_transform_relative preg match doesn't count in file url ports and all the absolute paths were transformed to relative with port in the beginning ":9000/{{some image path}}" and broken.

Proposed resolution

I've resolved this issue by adding next lines (patch attached):
$file_url_port = parse_url($file_url, PHP_URL_PORT) ?? $port;
if ($file_url_port != $port) {
return $file_url;
}

Maybe someone will find this useful.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

itaran created an issue. See original summary.

Version: 9.0.x-dev » 9.1.x-dev

Drupal 9.0.10 was released on December 3, 2020 and is the final full bugfix release for the Drupal 9.0.x series. Drupal 9.0.x will not receive any further development aside from security fixes. Sites should update to Drupal 9.1.0 to continue receiving regular bugfixes.

Drupal-9-only bug reports should be targeted for the 9.1.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.2.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Kristen Pol’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative, +Needs tests

Thanks for the issue and patch. Moving back to needs work for a test.

Kristen Pol’s picture

Version: 9.1.x-dev » 9.2.x-dev
quietone’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
2.23 KB
2.84 KB

Added a test, thanks to guidance from dww on #bugsmash.

There is no interdiff because this is so small.

The last submitted patch, 5: 3174349-5-fail.patch, failed testing. View results

dww’s picture

Status: Needs review » Needs work

Yay, thanks for working on this. Glad my Slack comments were helpful. ;)

I'm a little reluctant to NW for this review, since it's mostly a reflection of my own confusion. But perhaps we could update the comments to help others make sense of it all?

  1. +++ b/core/includes/file.inc
    @@ -126,6 +126,13 @@ function file_url_transform_relative($file_url) {
    +  // Files may be accessible on a unique port.
    

    See below -- I don't think "unique" clarifies the bug here. I was confused until I read the patch and the surrounding code twice.

  2. +++ b/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php
    @@ -41,41 +41,56 @@ public function testFileUrlTransformRelative($host, $port, $https, $url, $expect
    -    $data[] = [
    -      'example.com',
    -      8080,
    -      '',
    -      'https://example.com:8080/page',
    -      '/page',
    -    ];
    -    $data[] = [
    -      'example.com',
    -      8443,
    -      'on',
    -      'https://example.com:8443/page',
    -      '/page',
    -    ];
    

    That's weird, looks like we already had test coverage that a file URL with an explicit port is properly handled? Nope, I'm just confused about the bug we're fixing. ;)

  3. +++ b/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php
    @@ -41,41 +41,56 @@ public function testFileUrlTransformRelative($host, $port, $https, $url, $expect
    +    $data = [
    +      'http' => [
    ...
    

    +1. It's a bigger diff, but it helps to convert this provider to our usual conventions and to give each case a unique key so it's easier to make sense of failures. I hope no one calls this "out of scope", since I think this is definitely worth keeping.

  4. +++ b/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php
    @@ -41,41 +41,56 @@ public function testFileUrlTransformRelative($host, $port, $https, $url, $expect
    +      'http files on unique port' => [
    +        'example.com',
    +        80,
    +        '',
    +        'http://example.com:9000/page',
    +        'http://example.com:9000/page',
    +      ],
    

    I was going to flag this as "weird, why use 80 as the port for the request, but 9000 for the file?", then I realized that's the bug we're fixing! ;)

    The existing test is a little confusing, since file_url_transform_relative() itself is confusing. But the key point here is that these ports should *not* be the same.

    Maybe a comment to that effect, both in these test cases and with the fix? "Unique" didn't exactly clarify this for me, but maybe that's end-of-day brain talking. Something like "non-standard" or "different port than the web request" or something?

dww’s picture

Title: Additional check for port in file_url_transform_relative() » file_url_transform_relative() cannot handle URLs where the port is different from the site's request port

Initial stab at a more accurate title for the issue. ;) Further refinements welcome!

quietone’s picture

Status: Needs work » Needs review
FileSize
1.61 KB
2.92 KB

@dww, thanks for the review.

Your comment is much better. Added it to file.inc and the array keys for the test. And I noticed that one of the tests should have been using 'https' so changed that.

dww’s picture

Status: Needs review » Reviewed & tested by the community

Excellent, much appreciated, @quietone! Everything now looks really good and clear.

Assuming the bot doesn't contradict me, RTBC. ;)

Thanks,
-Derek

  • larowlan committed b43c49e on 9.2.x
    Issue #3174349 by quietone, itaran, dww, Kristen Pol:...

  • larowlan committed 70813f2 on 9.1.x
    Issue #3174349 by quietone, itaran, dww, Kristen Pol:...

  • larowlan committed 9a4f58d on 9.0.x
    Issue #3174349 by quietone, itaran, dww, Kristen Pol:...

  • larowlan committed 0c7a62a on 9.0.x
    Revert "Issue #3174349 by quietone, itaran, dww, Kristen Pol:...
larowlan’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed b43c49e and pushed to 9.2.x. Thanks!

Backported to 9.1.x

Whilst the changes to the data-provider are borderline out of scope, I think they improve readability, assist debugging fails (named cases are much simpler to debug) and are a low risk of disruption.

Thanks all

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.