From acad5815b50ec12ec3883be37d2ab5278a26bf42 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 8 May 2022 08:03:33 +0000 Subject: [PATCH] fix functions and realtime --- app/init.php | 3 +-- app/realtime.php | 33 +++++++-------------------------- src/Appwrite/Resque/Worker.php | 34 ++++++++-------------------------- 3 files changed, 16 insertions(+), 54 deletions(-) diff --git a/app/init.php b/app/init.php index 50aec5b64f..f29a2e61c8 100644 --- a/app/init.php +++ b/app/init.php @@ -311,7 +311,7 @@ Database::addFilter('encrypt', 'method' => OpenSSL::CIPHER_AES_128_GCM, 'iv' => \bin2hex($iv), 'tag' => \bin2hex($tag ?? ''), - 'version' => '1', + 'version' => 'v1', ]); }, function($value) { @@ -799,7 +799,6 @@ function decode(mixed $value, array $secrets): mixed { if(is_null($value)) { return null; } - $value = json_decode($value, true); $version = $value['version']; $key = $secrets[$version]; diff --git a/app/realtime.php b/app/realtime.php index 93edc85aa5..283e3176e8 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -517,24 +517,11 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re if(!$project->isEmpty()) { $secrets = $project->getAttribute('databaseSecrets'); - $displacement = $project->getAttribute('databaseSecretsDisplacement', 0); - $version = $displacement + \count($secrets); $filters['encrypt'] = [ - 'encode' => function($value) use($version, $secrets) { - - $key = $secrets[\count($secrets)-1]; - $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); - $tag = null; - $value = json_encode([ - 'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag), - 'method' => OpenSSL::CIPHER_AES_128_GCM, - 'iv' => \bin2hex($iv), - 'tag' => \bin2hex($tag ?? ''), - 'version' => $version, - ]); - - $key = App::getEnv('_APP_OPENSSL_KEY_V1'); + 'encode' => function($value) use($secrets) { + $version = array_key_last($secrets); + $key = $secrets[$version]; $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); $tag = null; return json_encode([ @@ -542,22 +529,16 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re 'method' => OpenSSL::CIPHER_AES_128_GCM, 'iv' => \bin2hex($iv), 'tag' => \bin2hex($tag ?? ''), - 'version' => '1', + 'version' => $version, ]); }, - 'decode' => function($value) use($secrets, $displacement) { + 'decode' => function($value) use($secrets) { if(is_null($value)) { return null; } - $value = json_decode($value, true); - $key = App::getEnv('_APP_OPENSSL_KEY_V'.$value['version']); - - $value = OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag'])); - - $value = json_decode($value, true); - $version = ($value['version'] ?? 1) - $displacement; - $key = $secrets[$version - 1]; + $version = $value['version']; + $key = $secrets[$version]; return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag'])); } diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index 913fce7cf2..a6d77aa54c 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -205,30 +205,17 @@ abstract class Worker $database = new Database(new MariaDB($register->get('db')), $cache); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - if($namespace != "_console" && !empty($projectId)) { + if(!empty($projectId)) { $database->setNamespace("_console"); $project = $database->getDocument('projects', $projectId); if(!$project->isEmpty()) { $secrets = $project->getAttribute('databaseSecrets'); - $displacement = $project->getAttribute('databaseSecretsDisplacement', 0); - $version = $displacement + \count($secrets); $filters['encrypt'] = [ - 'encode' => function($value) use($version, $secrets) { - - $key = $secrets[\count($secrets)-1]; - $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); - $tag = null; - $value = json_encode([ - 'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag), - 'method' => OpenSSL::CIPHER_AES_128_GCM, - 'iv' => \bin2hex($iv), - 'tag' => \bin2hex($tag ?? ''), - 'version' => $version, - ]); - - $key = App::getEnv('_APP_OPENSSL_KEY_V1'); + 'encode' => function($value) use($secrets) { + $version = array_key_last($secrets); + $key = $secrets[$version]; $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); $tag = null; return json_encode([ @@ -236,22 +223,17 @@ abstract class Worker 'method' => OpenSSL::CIPHER_AES_128_GCM, 'iv' => \bin2hex($iv), 'tag' => \bin2hex($tag ?? ''), - 'version' => '1', + 'version' => $version, ]); }, - 'decode' => function($value) use($secrets, $displacement) { + 'decode' => function($value) use($secrets) { if(is_null($value)) { return null; } $value = json_decode($value, true); - $key = App::getEnv('_APP_OPENSSL_KEY_V'.$value['version']); - - $value = OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag'])); - - $value = json_decode($value, true); - $version = ($value['version'] ?? 1) - $displacement; - $key = $secrets[$version - 1]; + $version = $value['version']; + $key = $secrets[$version]; return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag'])); }