Skip to content

Commit

Permalink
work around SQLite PDO’s inability to enforce permissions for data files
Browse files Browse the repository at this point in the history
  • Loading branch information
iclukas committed May 27, 2022
1 parent feae77d commit 3f9e81c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Stash/Driver/Sub/SqlitePdo.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Stash\Driver\Sub;

use Stash\Exception\RuntimeException;

/**
* Class SqlitePDO
*
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Stash\Test\Driver;

use Stash\Utilities;

/**
* @package Stash
* @author Robert Hafner <[email protected]>
Expand All @@ -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();
Expand Down

0 comments on commit 3f9e81c

Please sign in to comment.