From cf2ffd75b05e3e02a9d637db91878d338e356845 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 9 Sep 2021 09:31:16 +0200 Subject: [PATCH] Reject null byte in path to SQLite database file --- res/sqlite-worker.php | 10 ++++++++++ tests/FunctionalDatabaseTest.php | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/res/sqlite-worker.php b/res/sqlite-worker.php index 30d43df..a010a32 100644 --- a/res/sqlite-worker.php +++ b/res/sqlite-worker.php @@ -90,6 +90,11 @@ 'id' => $data->id, 'error' => array('message' => $e->getMessage()) )); + } catch (Error $e) { + $out->write(array( + 'id' => $data->id, + 'error' => array('message' => $e->getMessage()) + )); } } elseif ($data->method === 'open' && \count($data->params) === 2 && \is_string($data->params[0]) && \is_int($data->params[1])) { // open database with two parameters: $filename, $flags @@ -108,6 +113,11 @@ 'id' => $data->id, 'error' => array('message' => $e->getMessage()) )); + } catch (Error $e) { + $out->write(array( + 'id' => $data->id, + 'error' => array('message' => $e->getMessage()) + )); } } elseif ($data->method === 'exec' && $db !== null && \count($data->params) === 1 && \is_string($data->params[0])) { // execute statement and suppress PHP warnings diff --git a/tests/FunctionalDatabaseTest.php b/tests/FunctionalDatabaseTest.php index ba89b1b..adb1e66 100644 --- a/tests/FunctionalDatabaseTest.php +++ b/tests/FunctionalDatabaseTest.php @@ -134,6 +134,29 @@ public function testOpenInvalidPathRejects($flag) $loop->run(); } + /** + * @dataProvider provideSocketFlags + * @param bool $flag + */ + public function testOpenInvalidPathWithNullByteRejects($flag) + { + $loop = \React\EventLoop\Factory::create(); + $factory = new Factory($loop); + + $ref = new \ReflectionProperty($factory, 'useSocket'); + $ref->setAccessible(true); + $ref->setValue($factory, $flag); + + $promise = $factory->open("test\0.db"); + + $promise->then( + null, + $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')) + ); + + $loop->run(); + } + /** * @dataProvider provideSocketFlags * @param bool $flag