Skip to content

[5.x] Ensure Glide treats asset urls starting with the app url as internal assets #11839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Assets/AssetContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Statamic\Facades\Stache;
use Statamic\Facades\URL;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;

class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract, Augmentable
Expand Down Expand Up @@ -139,9 +138,7 @@ public function url()
return null;
}

$url = (string) Str::of($this->disk()->url('/'))
->rtrim('/')
->after(config('app.url'));
$url = rtrim($this->disk()->url('/'), '/');

return ($url === '') ? '/' : $url;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Tags/Glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ private function normalizeItem($item)
return $item;
}

if (Str::startsWith($item, config('app.url'))) {
$item = Str::after($item, config('app.url'));
}

// External URLs are already fine as-is.
if (Str::startsWith($item, ['http://', 'https://'])) {
return $item;
Expand Down
15 changes: 0 additions & 15 deletions tests/Assets/AssetContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,6 @@ public function it_gets_the_url_from_the_disk_config_when_its_relative()
$this->assertEquals('http://localhost/container', $container->absoluteUrl());
}

#[Test]
public function it_gets_the_url_from_the_disk_config_when_its_app_url()
{
config(['filesystems.disks.test' => [
'driver' => 'local',
'root' => __DIR__.'/__fixtures__/container',
'url' => 'http://localhost/container',
]]);

$container = (new AssetContainer)->disk('test');

$this->assertEquals('/container', $container->url());
$this->assertEquals('http://localhost/container', $container->absoluteUrl());
}

#[Test]
public function its_private_if_the_disk_has_no_url()
{
Expand Down
23 changes: 21 additions & 2 deletions tests/Tags/GlideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public function it_outputs_a_data_url()
$this->assertStringStartsWith('data:image/jpeg;base64', (string) Parse::template($tag, ['foo' => 'bar.jpg']));
}

#[Test]
#[DefineEnvironment('absoluteHttpRouteUrlWithoutCache')]
/**
* https://github.com/statamic/cms/pull/11839
*/
public function it_treats_assets_urls_starting_with_the_app_url_as_internal_assets()
{
$this->createImageInPublicDirectory();

$result = (string) Parse::template('{{ glide:foo width="100" }}', ['foo' => 'http://localhost/glide/bar.jpg']);

$this->assertStringStartsWith('/img/glide/bar.jpg', $result);
}

public function relativeRouteUrl($app)
{
$this->configureGlideCacheDiskWithUrl($app, '/glide');
Expand All @@ -77,20 +91,25 @@ public function absoluteHttpRouteUrl($app)
$this->configureGlideCacheDiskWithUrl($app, 'http://localhost/glide');
}

public function absoluteHttpRouteUrlWithoutCache($app)
{
$this->configureGlideCacheDiskWithUrl($app, 'http://localhost/glide', false);
}

public function absoluteHttpsRouteUrl($app)
{
$this->configureGlideCacheDiskWithUrl($app, 'https://localhost/glide');
}

private function configureGlideCacheDiskWithUrl($app, $url)
private function configureGlideCacheDiskWithUrl($app, $url, $cache = 'glide')
{
$app['config']->set('filesystems.disks.glide', [
'driver' => 'local',
'root' => public_path('glide'),
'url' => $url,
'visibility' => 'public',
]);
$app['config']->set('statamic.assets.image_manipulation.cache', 'glide');
$app['config']->set('statamic.assets.image_manipulation.cache', $cache);
}

private function createImageInPublicDirectory()
Expand Down
Loading