From 19ab9df063e5be19b8a05ae0e3945b1082a81243 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 18 May 2021 18:42:42 -0400 Subject: [PATCH] Fix more bugs --- CHANGELOG | 2 ++ lib/Conf.php | 2 +- lib/Db/SQLite3/Driver.php | 6 ++++++ tests/cases/Conf/TestConf.php | 8 ++++++++ tests/cases/Db/SQLite3/TestCreation.php | 13 +++++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 9dfdf8e..b0f89fc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,8 @@ Version 0.9.2 (2021-??-??) Bug fixes: - Do not fail adding users to an empty database (regression since 0.9.0) +- Cleanly ignore unknown configuration properties +- Set access mode to rw-r---- when creating SQLite databases Changes: - Packages for Arch Linux are now available (see manual for details) diff --git a/lib/Conf.php b/lib/Conf.php index dfe35e2..428e87a 100644 --- a/lib/Conf.php +++ b/lib/Conf.php @@ -265,7 +265,7 @@ class Conf { protected function propertyImport(string $key, $value, string $file = "") { $typeName = $this->types[$key]['name'] ?? "mixed"; $typeConst = $this->types[$key]['const'] ?? Value::T_MIXED; - $nullable = (int) (bool) ($this->types[$key]['const'] & Value::M_NULL); + $nullable = (int) (bool) ($typeConst & Value::M_NULL); try { if ($typeName === "\\DateInterval") { // date intervals have special handling: if the existing value (ultimately, the default value) diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index 3445b89..7c5a110 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -31,6 +31,12 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { $dbKey = Arsse::$conf->dbSQLite3Key; $timeout = Arsse::$conf->dbSQLite3Timeout * 1000; try { + // check whether the file exists; if it doesn't create the file and set its mode to rw-r----- + if ($dbFile !== ":memory:" && !file_exists($dbFile)) { + if (@touch($dbFile)) { + chmod($dbFile, 0640); + } + } $this->makeConnection($dbFile, $dbKey); } catch (\Throwable $e) { // if opening the database doesn't work, check various pre-conditions to find out what the problem might be diff --git a/tests/cases/Conf/TestConf.php b/tests/cases/Conf/TestConf.php index 088746b..0e827d4 100644 --- a/tests/cases/Conf/TestConf.php +++ b/tests/cases/Conf/TestConf.php @@ -108,6 +108,14 @@ class TestConf extends \JKingWeb\Arsse\Test\AbstractTest { $conf->import($arr); } + public function testImportCustomProperty(): void { + $arr = [ + 'customProperty' => "I'm special!", + ]; + $conf = new Conf; + $this->assertSame($conf, $conf->import($arr)); + } + public function testImportBogusDriver(): void { $arr = [ 'dbDriver' => "this driver does not exist", diff --git a/tests/cases/Db/SQLite3/TestCreation.php b/tests/cases/Db/SQLite3/TestCreation.php index 3e3b2f2..348e2ae 100644 --- a/tests/cases/Db/SQLite3/TestCreation.php +++ b/tests/cases/Db/SQLite3/TestCreation.php @@ -185,4 +185,17 @@ class TestCreation extends \JKingWeb\Arsse\Test\AbstractTest { $this->assertException("fileCorrupt", "Db"); new Driver; } + + public function testSetFileMode(): void { + $f = tempnam(sys_get_temp_dir(), "arsse"); + Arsse::$conf->dbSQLite3File = $f; + // delete the file PHP just created + unlink($f); + // recreate the file + new Driver; + // check the mode + clearstatcache(); + $mode = base_convert((string) stat($f)['mode'], 10, 8); + $this->assertMatchesRegularExpression("/640$/", $mode); + } }