From 14191760940393a74d679e88136db33455971bfc Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 2 Dec 2021 18:14:43 +0100 Subject: [PATCH] Validate key existence more strictly. --- app/Support/System/OAuthKeys.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/Support/System/OAuthKeys.php b/app/Support/System/OAuthKeys.php index 7817af69e7..646a28e328 100644 --- a/app/Support/System/OAuthKeys.php +++ b/app/Support/System/OAuthKeys.php @@ -26,7 +26,11 @@ namespace FireflyIII\Support\System; use Artisan; use Crypt; +use FireflyIII\Exceptions\FireflyException; use Laravel\Passport\Console\KeysCommand; +use Log; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Class OAuthKeys @@ -62,7 +66,23 @@ class OAuthKeys */ public static function keysInDatabase(): bool { - return app('fireflyconfig')->has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY); + $privateKey = ''; + $publicKey = ''; + // better check if keys are in the database: + if (app('fireflyconfig')->has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY)) { + try { + $privateKey = (string)app('fireflyconfig')->get(self::PRIVATE_KEY)?->data; + $publicKey = (string)app('fireflyconfig')->get(self::PUBLIC_KEY)?->data; + } catch (ContainerExceptionInterface | NotFoundExceptionInterface | FireflyException $e) { + Log::error(sprintf('Could not validate keysInDatabase(): %s', $e->getMessage())); + Log::error($e->getTraceAsString()); + } + } + if ('' !== $privateKey && '' !== $publicKey) { + return true; + } + + return false; } /** @@ -101,8 +121,10 @@ class OAuthKeys */ public static function restoreKeysFromDB(): void { - $privateContent = Crypt::decrypt(app('fireflyconfig')->get(self::PRIVATE_KEY)->data); - $publicContent = Crypt::decrypt(app('fireflyconfig')->get(self::PUBLIC_KEY)->data); + $privateKey = (string)app('fireflyconfig')->get(self::PRIVATE_KEY)?->data; + $publicKey = (string)app('fireflyconfig')->get(self::PUBLIC_KEY)?->data; + $privateContent = Crypt::decrypt($privateKey); + $publicContent = Crypt::decrypt($publicKey); $private = storage_path('oauth-private.key'); $public = storage_path('oauth-public.key'); file_put_contents($private, $privateContent);