From 5cd5f77c6f3bfce44bfffe53ceb82ec417a4a728 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 7 Nov 2022 09:50:39 +0100 Subject: [PATCH] Better handling of admin token cookie --- .../app/libraries/RainLoop/ActionsAdmin.php | 25 ++++++++----------- .../v/0.0.0/app/libraries/RainLoop/Utils.php | 3 +++ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php index b5df87b4e..91d3393f7 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php @@ -123,8 +123,7 @@ class ActionsAdmin extends Actions throw new ClientException(Notifications::AuthError); } - $sToken = $this->getAdminToken(); - $this->setAdminAuthToken($sToken); + $sToken = $this->setAdminAuthToken($sToken); return $this->DefaultResponse(__FUNCTION__, $sToken ? $this->AppData(true) : false); } @@ -668,22 +667,18 @@ class ActionsAdmin extends Actions return $this->DefaultResponse(__FUNCTION__, $QR->__toString()); } - private function setAdminAuthToken(string $sToken) : void - { - Utils::SetCookie(static::$AUTH_ADMIN_TOKEN_KEY, $sToken); - } - - private function getAdminToken() : string + private function setAdminAuthToken(string $sToken) : string { $sRand = \MailSo\Base\Utils::Sha1Rand(); - if (!$this->Cacher(null, true)->Set(KeyPathHelper::SessionAdminKey($sRand), \time())) - { - $this->oLogger->Write('Cannot store an admin token', - \LOG_WARNING); - return ''; + if (!$this->Cacher(null, true)->Set(KeyPathHelper::SessionAdminKey($sRand), \time())) { + throw new \RuntimeException('Failed to store admin token'); } - - return Utils::EncodeKeyValuesQ(array('token', $sRand)); + $sToken = Utils::EncodeKeyValuesQ(array('token', $sRand)); + if (!$sToken) { + throw new \RuntimeException('Failed to encode admin token'); + } + Utils::SetCookie(static::$AUTH_ADMIN_TOKEN_KEY, $sToken); + return $sToken; } private function pluginEnable(string $sName, bool $bEnable = true) : bool diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Utils.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Utils.php index 1d79c5564..e9df3939c 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Utils.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Utils.php @@ -166,6 +166,9 @@ class Utils throw new \Exception("Cookie '{$sName}' value too long"); } */ + // Clear 4K split cookie first + static::ClearCookie($sName); + // Now set the new 4K split cookie foreach (\str_split($sValue, $iMaxSize) as $i => $sPart) { static::_SetCookie($i ? "{$sName}~{$i}" : $sName, $sPart, $iExpire); }