From 97cf462577a01b6268870203cbd7ca434211ec62 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 15 Oct 2026 20:47:43 +0530 Subject: [PATCH 1/4] Only allow Glide supported image types in "pass through" mode. Add "passThroughAllowedContentTypes" config to allow configuring allowed content types in "pass through" mode. --- README.md | 10 +++++- src/Middleware/GlideMiddleware.php | 17 ++++++++-- .../Middleware/GlideMiddlewareTest.php | 31 +++++++++++++++++++ tests/test_app/webroot/upload/nope.txt | 1 + 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/test_app/webroot/upload/nope.txt diff --git a/README.md b/README.md index 061de1b..4876abf 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,15 @@ $routes->scope('/images', function ($routes) { // Allowed query string params. If for e.g. you are only using glide presets // then you can set allowed params as `['p']` to prevent users from using // any other image manipulation params. - 'allowedParams' => null + 'allowedParams' => null, + + // If the image URL is requested without any transformation params then + // directly return the original image. Default `false`. + 'originalPassThrough' => false, + + // Allowed content types in response when "originalPassThrough" is `true`. + // By default only the image type supported by Glide are allowed. + 'passThroughAllowedContentTypes' => [], ])); $routes->applyMiddleware('glide'); diff --git a/src/Middleware/GlideMiddleware.php b/src/Middleware/GlideMiddleware.php index 1f57c34..9b2959d 100644 --- a/src/Middleware/GlideMiddleware.php +++ b/src/Middleware/GlideMiddleware.php @@ -15,6 +15,7 @@ use Cake\Utility\Security; use Exception; use Laminas\Diactoros\Stream; +use League\Glide\Api\Encoder; use League\Glide\Server; use League\Glide\ServerFactory; use League\Glide\Signatures\SignatureFactory; @@ -56,6 +57,7 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface 'headers' => null, 'allowedParams' => null, 'originalPassThrough' => false, + 'passThroughAllowedContentTypes' => [], ]; /** @@ -263,6 +265,15 @@ protected function _passThrough(ServerRequestInterface $request, Server $server) $stream = new Stream($resource); $contentType = $source->mimeType($path); + $allowedContentTypes = $this->getConfig('passThroughAllowedContentTypes', []); + if ($allowedContentTypes === []) { + $allowedContentTypes = Encoder::supportedFormats(); + } + + if (!in_array($contentType, $allowedContentTypes, true)) { + throw new ResponseException('Content type not supported.'); + } + $contentLength = $source->fileSize($path); /** @psalm-suppress PossiblyFalseArgument */ @@ -307,9 +318,9 @@ protected function _withCacheHeaders( return $response ->withHeader('Cache-Control', 'public,max-age=' . $maxAge) - ->withHeader('Date', gmdate(DATE_RFC7231, time())) - ->withHeader('Last-Modified', gmdate(DATE_RFC7231, (int)$modifiedTime)) - ->withHeader('Expires', gmdate(DATE_RFC7231, $expire)); + ->withHeader('Date', gmdate('D, d M Y H:i:s \G\M\T', time())) + ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', (int)$modifiedTime)) + ->withHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', $expire)); } /** diff --git a/tests/TestCase/Middleware/GlideMiddlewareTest.php b/tests/TestCase/Middleware/GlideMiddlewareTest.php index 97b634b..5ec726f 100644 --- a/tests/TestCase/Middleware/GlideMiddlewareTest.php +++ b/tests/TestCase/Middleware/GlideMiddlewareTest.php @@ -123,6 +123,37 @@ public function testOriginalPassThrough() $this->assertSame($fileSize, (int)$headers['Content-Length'][0]); } + /** + * Test that original pass through fails for unsupported (non-image) content types. + * + * @return void + */ + public function testOriginalPassThroughFail() + { + $this->expectException(ResponseException::class); + + $request = ServerRequestFactory::fromGlobals([ + 'REQUEST_URI' => '/images/nope.txt', + ]); + + (new GlideMiddleware($this->config + ['originalPassThrough' => true])) + ->process($request, $this->handler); + } + + public function testOriginalPassThroughCustomType() + { + $request = ServerRequestFactory::fromGlobals([ + 'REQUEST_URI' => '/images/nope.txt', + ]); + + $config = [ + 'originalPassThrough' => true, + 'passThroughAllowedContentTypes' => ['text/plain'], + ] + $this->config; + $response = (new GlideMiddleware($config))->process($request, $this->handler); + $this->assertSame('text/plain', $response->getHeaderLine('Content-Type')); + } + public function testPathConfig() { $middleware = new GlideMiddleware(['path' => '/img'] + $this->config); diff --git a/tests/test_app/webroot/upload/nope.txt b/tests/test_app/webroot/upload/nope.txt new file mode 100644 index 0000000..1634764 --- /dev/null +++ b/tests/test_app/webroot/upload/nope.txt @@ -0,0 +1 @@ +nope From fcd2421ceb6cc8448c2ee40381f0d7518559b7a9 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 15 Oct 2026 20:49:17 +0530 Subject: [PATCH 2/4] Allow using Glide v4 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0095fb2..4b42375 100644 --- a/composer.json +++ b/composer.json @@ -30,10 +30,10 @@ "require": { "php": ">=8.1", "cakephp/cakephp": "^5.0", - "league/glide": "^3.0" + "league/glide": "^3.0 || ^4.0" }, "require-dev": { - "phpunit/phpunit": "^10.1" + "phpunit/phpunit": "^10.1 || ^11.5.3 || ^12.1.3 || ^13.0" }, "autoload": { "psr-4": { From c8974b5427d2443f48fc39621bdec191fb76e09a Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 15 Oct 2026 20:59:24 +0530 Subject: [PATCH 3/4] Fix CS and stan errors --- phpcs.xml | 8 ++++++++ psalm.xml | 1 + src/Middleware/GlideMiddleware.php | 6 ------ src/View/Helper/GlideHelper.php | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 phpcs.xml diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..2bcdf5b --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,8 @@ + + + src/ + tests/ + + + + diff --git a/psalm.xml b/psalm.xml index d93e5f2..097fbc2 100644 --- a/psalm.xml +++ b/psalm.xml @@ -17,5 +17,6 @@ + diff --git a/src/Middleware/GlideMiddleware.php b/src/Middleware/GlideMiddleware.php index 9b2959d..7c7ead4 100644 --- a/src/Middleware/GlideMiddleware.php +++ b/src/Middleware/GlideMiddleware.php @@ -24,14 +24,8 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -/** - * @implements \Cake\Event\EventDispatcherInterface<\ADmad\Glide\Middleware\GlideMiddleware> - */ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface { - /** - * @use \Cake\Event\EventDispatcherTrait<\ADmad\Glide\Middleware\GlideMiddleware> - */ use EventDispatcherTrait; use InstanceConfigTrait; diff --git a/src/View/Helper/GlideHelper.php b/src/View/Helper/GlideHelper.php index a61737d..5d87517 100644 --- a/src/View/Helper/GlideHelper.php +++ b/src/View/Helper/GlideHelper.php @@ -12,13 +12,14 @@ * GlideHelper * * @property \Cake\View\Helper\HtmlHelper $Html + * @extends \Cake\View\Helper<\Cake\View\View> */ class GlideHelper extends Helper { /** * Helpers used by this helper. * - * @var array + * @var array> */ protected array $helpers = ['Html']; From 0ffbc9fd1a40d0fa92f595a17936303995683f92 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 15 Oct 2026 21:04:33 +0530 Subject: [PATCH 4/4] Update badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4876abf..9a9c3f6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CakePHP Glide -[![Build Status](https://img.shields.io/github/actions/workflow/status/ADmad/cakephp-glide/ci.yml?style=flat-square&branch=master)](https://github.com/ADmad/cakephp-glide/actions?query=workflow%3ACI+branch%3Amaster) +[![Build Status](https://img.shields.io/github/actions/workflow/status/ADmad/cakephp-glide/ci.yml?branch=master&style=flat-square)](https://github.com/ADmad/cakephp-glide/actions/workflows/ci.yml?query=branch%3Amaster) [![Coverage Status](https://img.shields.io/codecov/c/github/ADmad/cakephp-glide.svg?style=flat-square)](https://codecov.io/github/ADmad/cakephp-glide) [![Total Downloads](https://img.shields.io/packagist/dt/ADmad/cakephp-glide.svg?style=flat-square)](https://packagist.org/packages/ADmad/cakephp-glide) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.txt)