From d2ed1b00463d4f0679f927d7dad818235204c7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20R=C3=BCsweg?= Date: Thu, 20 Jun 2024 16:43:11 +0200 Subject: [PATCH] [11.x] Fix PHP_MAXPATHLEN check for strings slightly smaller then PHP_MAXPATHLEN (#51850) * Fix `PHP_MAXPATHLEN` test This test was incorrect because it does not take into account the paths that are prefixed in the FileViewFinder. This means that we have so far tested a path that is significantly longer than `PHP_MAXPATHLEN`. As we cannot reliably access the path, we approach `PHP_MAXPATHLEN` and test all file names from `PHP_MAXPATHLEN - 200` to `PHP_MAXPATHLEN + 1`. * Fix `PHP_MAXPATHLEN` check The previous test was wrong. In fact, `PHP_MAXPATHLEN` is not the maximum length of a file, but `PHP_MAXPATHLEN - 1`. This is checked internally in the PHP source code and if the file is PHP_MAXPATHLEN characters long or longer, the error is thrown in Open-Base-Dir [1]. The problem was incompletely fixed here: #50962 [1] https://github.com/php/php-src/blob/7c860628cd2bf11ee867bfb41b3fd0314c5177c5/main/fopen_wrappers.c#L301 --- src/Illuminate/View/FileViewFinder.php | 2 +- tests/Integration/View/BladeTest.php | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/View/FileViewFinder.php b/src/Illuminate/View/FileViewFinder.php index 4b2f32679307..c2f49bd68bde 100755 --- a/src/Illuminate/View/FileViewFinder.php +++ b/src/Illuminate/View/FileViewFinder.php @@ -130,7 +130,7 @@ protected function findInPaths($name, $paths) foreach ($this->getPossibleViewFiles($name) as $file) { $viewPath = $path.'/'.$file; - if (strlen($viewPath) <= PHP_MAXPATHLEN && $this->files->exists($viewPath)) { + if (strlen($viewPath) < (PHP_MAXPATHLEN - 1) && $this->files->exists($viewPath)) { return $viewPath; } } diff --git a/tests/Integration/View/BladeTest.php b/tests/Integration/View/BladeTest.php index cff73e6a7bca..7a08d43fd0b3 100644 --- a/tests/Integration/View/BladeTest.php +++ b/tests/Integration/View/BladeTest.php @@ -43,11 +43,13 @@ public function test_rendering_blade_long_maxpathlen_string_with_exact_length() $this->assertNotFalse($iniSet, 'Could not set config for open_basedir.'); } - $longString = str_repeat('x', PHP_MAXPATHLEN); + for ($i = PHP_MAXPATHLEN - 200; $i <= PHP_MAXPATHLEN + 1; $i++) { + $longString = str_repeat('x', $i); - $result = Blade::render($longString); + $result = Blade::render($longString); - $this->assertSame($longString, $result); + $this->assertSame($longString, $result); + } } public function test_rendering_blade_component_instance()