From 3f9e81cf63bc4f46558c2717e58df7280912e152 Mon Sep 17 00:00:00 2001 From: iclukas Date: Fri, 27 May 2022 17:32:32 +0200 Subject: [PATCH] =?UTF-8?q?work=20around=20SQLite=20PDO=E2=80=99s=20inabil?= =?UTF-8?q?ity=20to=20enforce=20permissions=20for=20data=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Stash/Driver/Sub/SqlitePdo.php | 12 +++++++++-- .../Test/Driver/SqlitePdoSqlite3Test.php | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/Stash/Driver/Sub/SqlitePdo.php mode change 100644 => 100755 tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php diff --git a/src/Stash/Driver/Sub/SqlitePdo.php b/src/Stash/Driver/Sub/SqlitePdo.php old mode 100644 new mode 100755 index a05cede7..ee2d83f8 --- a/src/Stash/Driver/Sub/SqlitePdo.php +++ b/src/Stash/Driver/Sub/SqlitePdo.php @@ -11,6 +11,8 @@ namespace Stash\Driver\Sub; +use Stash\Exception\RuntimeException; + /** * Class SqlitePDO * @@ -243,8 +245,14 @@ protected function getDriver() $dir = substr($this->path, 0, $pos); } - if (!is_dir($dir)) { - mkdir($dir, $this->dirPermissions, true); + if (!is_dir($dir) && !mkdir($dir, $this->dirPermissions, true) && !is_dir($dir)) { + throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); + } + if (file_put_contents($this->path, '') === false) { + throw new RuntimeException(sprintf('Cache file "%s" was not created', basename($this->path))); + } + if (!chmod($this->path, $this->filePermissions)) { + throw new RuntimeException(sprintf('Cache file permissions for "%s" could not be set', basename($this->path))); } $runInstall = true; } else { diff --git a/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php b/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php old mode 100644 new mode 100755 index 66a87e58..52b3d357 --- a/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php +++ b/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php @@ -11,6 +11,8 @@ namespace Stash\Test\Driver; +use Stash\Utilities; + /** * @package Stash * @author Robert Hafner @@ -35,6 +37,25 @@ protected function setUp() : void parent::setUp(); } + public function testFilePermissions() + { + $key = array('apple', 'sauce'); + + $driverClass = '\\' . $this->driverClass; + foreach (array(0666, 0622, 0604) as $perms) { + $driver = new $driverClass(array('filePermissions' => $perms, 'dirPermissions' => 0777)); // constructor first + $filename = rtrim(Utilities::getBaseDirectory($driver), '\\/') . DIRECTORY_SEPARATOR . 'cache.sqlite'; + if (file_exists($filename)) { + unlink($filename); + } + $this->assertTrue($driver->storeData($key, 'test', time() + 30)); + $this->assertFileExists($filename); + $result = fileperms($filename) & 0777; // only care for rwx + $this->assertSame($perms, $result, sprintf('Able to set file permissions to 0%o.', $perms)); + } + @unlink($filename); + } + public function getOptions() { $options = parent::getOptions();