Skip to content

Commit

Permalink
pkp#9895 added app key check and add in upgrade process
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed May 3, 2024
1 parent 122bc15 commit 635822e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
17 changes: 17 additions & 0 deletions classes/cliTool/UpgradeTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use APP\core\Application;
use APP\install\Upgrade;
use PKP\core\PKPAppKey;
use PKP\site\VersionCheck;

Application::upgrade();
Expand Down Expand Up @@ -75,6 +76,7 @@ public function execute()
public function check()
{
$this->checkVersion(VersionCheck::getLatestVersion());
$this->checkForAppKey();
}

/**
Expand Down Expand Up @@ -209,6 +211,21 @@ public function checkVersion($versionInfo, $displayInfo = false)
return $compare1;
}

/**
* Check the existence of `app_key` variable in config file and print warning message if not found
*/
public function checkForAppKey(): void
{
// if the app key variable `app_key` set in the config, nothing to do
if (PKPAppKey::hasKeyVariable()) {
return;
}

printf("\n\e[;43mWARNING: It is noticed that there is not `app_key` variable defined in the `general` section of the config file which is necessary to cookie and other encryption purpose.\nWe suggest add the following line in the `general` section of config file.\e[0m\n\n");

printf("\e[;44mapp_key = \e[0m\n");
}

/**
* Prompt user for yes/no input (default no).
*
Expand Down
12 changes: 9 additions & 3 deletions classes/core/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,15 @@ public function initSession(): void
(new \Illuminate\Pipeline\Pipeline(PKPContainer::getInstance()))
->send($illuminateRequest)
->through(
\PKP\middleware\PKPEncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\PKP\middleware\PKPAuthenticateSession::class,
app()->has('encrypter')
? [
\PKP\middleware\PKPEncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\PKP\middleware\PKPAuthenticateSession::class,
] : [
\Illuminate\Session\Middleware\StartSession::class,
\PKP\middleware\PKPAuthenticateSession::class,
]
)
->via('handle')
->then(function (\Illuminate\Http\Request $request) {
Expand Down
5 changes: 5 additions & 0 deletions classes/core/PKPEncryptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ protected function registerEncrypter()
return;
}

// if no app key set, the encrypter can not be registered
if (!PKPAppKey::hasKey()) {
return;
}

parent::registerEncrypter();
}
}
2 changes: 1 addition & 1 deletion classes/core/PKPSessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function updateSessionCookieToResponse(Session $session = null): void
// update response header cookie values in formar [name=value]
$response->headers->set('cookie', $headerCookies);

if ($config['cookie_encryption']) {
if ($config['cookie_encryption'] && app()->has('encrypter')) {
$pkpEncryptCookies = app()->make(\PKP\middleware\PKPEncryptCookies::class); /** @var \PKP\middleware\PKPEncryptCookies $pkpEncryptCookies */
$pkpEncryptCookies->encrypt($response);
}
Expand Down
30 changes: 30 additions & 0 deletions classes/install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PKP\cache\CacheManager;
use PKP\config\Config;
use PKP\core\Core;
use PKP\core\PKPAppKey;
use PKP\core\PKPApplication;
use PKP\core\PKPContainer;
use PKP\db\DAORegistry;
Expand All @@ -41,6 +42,7 @@
use PKP\site\VersionDAO;
use PKP\xml\PKPXMLParser;
use PKP\xml\XMLNode;
use Throwable;

class Installer
{
Expand Down Expand Up @@ -1008,6 +1010,34 @@ public function checkPhpVersion()
$this->setError(self::INSTALLER_ERROR_GENERAL, 'installer.unsupportedPhpError');
return false;
}

/**
* Add the app key if not already set
*
* @return bool Success/failure
*/
public function addAppKey()
{
// if APP KEY already exists, nothing to do
if (PKPAppKey::hasKey()) {
return true;
}

// will set an error if app key variable not set
// but will not halt the process
if (!PKPAppKey::hasKeyVariable()) {
error_log("No key variable named `app_key` defined in the `general` section of config file. Please update the config file's general section and add line `app_key = `");
return true;
}

try {
PKPAppKey::writeToConfig(PKPAppKey::generate());
} catch (Throwable $exception) {
error_log($exception->getMessage());
} finally {
return true;
}
}
}

if (!PKP_STRICT_MODE) {
Expand Down

0 comments on commit 635822e

Please sign in to comment.