diff --git a/Classes/Middleware/FallbackMiddleware.php b/Classes/Middleware/FallbackMiddleware.php index 4ae71417036..f8f3e78ab7b 100644 --- a/Classes/Middleware/FallbackMiddleware.php +++ b/Classes/Middleware/FallbackMiddleware.php @@ -80,7 +80,13 @@ protected function handleViaFallback(ServerRequestInterface $request): ResponseI throw new Exception('The path is not in the cache directory', 348923472); } - return new HtmlResponse(GeneralUtility::getUrl($possibleStaticFile), 200, $headers); + // The file might be removed (e.g. by a cache flush) after the checks above + $content = GeneralUtility::getUrl($possibleStaticFile); + if (false === $content) { + throw new Exception('StaticFileCache file could not be read', 1795530268); + } + + return new HtmlResponse($content, 200, $headers); } /** diff --git a/Tests/Unit/Middleware/FallbackMiddlewareTest.php b/Tests/Unit/Middleware/FallbackMiddlewareTest.php index 826febd4bc2..fc5f2798417 100644 --- a/Tests/Unit/Middleware/FallbackMiddlewareTest.php +++ b/Tests/Unit/Middleware/FallbackMiddlewareTest.php @@ -222,6 +222,22 @@ public function testMissingStaticFileSkipsFallback(): void $this->assertHandledByTypo3($this->getMiddleware()); } + public function testStaticFileRemovedWhileReadingSkipsFallback(): void + { + file_put_contents($this->staticFile, 'cached'); + + // The cache directory is requested after the file checks and before the file is read: + // remove the file at this point, like a concurrent cache flush would do. + $this->cacheService = $this->createStub(CacheService::class); + $this->cacheService->method('getAbsoluteBaseDirectory')->willReturnCallback(function (): string { + unlink($this->staticFile); + + return $this->cacheDirectory; + }); + + $this->assertHandledByTypo3($this->getMiddleware()); + } + public function testFileOutsideOfCacheDirectorySkipsFallback(): void { file_put_contents($this->staticFile, 'cached');