Files
firefly-iii/app/Console/Commands/System/VerifySecurityAlerts.php

122 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2021-08-10 19:31:55 +02:00
/*
* VerifySecurityAlerts.php
2023-04-16 07:33:12 +02:00
* Copyright (c) 2023 james@firefly-iii.org
2021-08-10 19:31:55 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-07-03 12:32:02 +02:00
declare(strict_types=1);
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
2023-03-11 15:04:16 +01:00
use Illuminate\Database\QueryException;
2025-02-23 12:47:04 +01:00
use Illuminate\Support\Facades\Storage;
2023-04-16 07:33:12 +02:00
use League\Flysystem\FilesystemException;
class VerifySecurityAlerts extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Verify security alerts';
2023-11-05 09:54:53 +01:00
protected $signature = 'firefly-iii:verify-security-alerts';
/**
* Execute the console command.
*
2022-10-30 12:24:51 +01:00
* @throws FilesystemException
*/
public function handle(): int
{
2023-03-11 15:04:16 +01:00
$this->removeOldAdvisory();
// check for security advisories.
$version = config('firefly.version');
2025-02-23 12:47:04 +01:00
$disk = Storage::disk('resources');
2022-12-31 06:57:05 +01:00
// Next line is ignored because it's a Laravel Facade.
2023-10-30 19:49:40 +01:00
if (!$disk->has('alerts.json')) { // @phpstan-ignore-line
2023-10-29 06:33:43 +01:00
app('log')->debug('No alerts.json file present.');
return 0;
}
$content = $disk->get('alerts.json');
$json = json_decode($content, true, 10);
/** @var array $array */
foreach ($json as $array) {
if ($version === $array['version'] && true === $array['advisory']) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Version %s has an alert!', $array['version']));
// add advisory to configuration.
2023-03-11 15:04:16 +01:00
$this->saveSecurityAdvisory($array);
// depends on level
if ('info' === $array['level']) {
2023-10-29 06:33:43 +01:00
app('log')->debug('INFO level alert');
$this->friendlyInfo($array['message']);
return 0;
}
if ('warning' === $array['level']) {
2023-10-29 06:33:43 +01:00
app('log')->debug('WARNING level alert');
$this->friendlyWarning('------------------------ :o');
$this->friendlyWarning($array['message']);
$this->friendlyWarning('------------------------ :o');
return 0;
}
if ('danger' === $array['level']) {
2023-10-29 06:33:43 +01:00
app('log')->debug('DANGER level alert');
$this->friendlyError('------------------------ :-(');
$this->friendlyError($array['message']);
$this->friendlyError('------------------------ :-(');
return 0;
}
return 0;
}
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('No security alerts for version %s', $version));
$this->friendlyPositive(sprintf('No security alerts for version %s', $version));
2023-12-20 19:35:52 +01:00
return 0;
}
2023-03-11 15:04:16 +01:00
private function removeOldAdvisory(): void
{
try {
app('fireflyconfig')->delete('upgrade_security_message');
app('fireflyconfig')->delete('upgrade_security_level');
} catch (QueryException $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not delete old security advisory, but thats OK: %s', $e->getMessage()));
2023-03-11 15:04:16 +01:00
}
}
private function saveSecurityAdvisory(array $array): void
{
try {
app('fireflyconfig')->set('upgrade_security_message', $array['message']);
app('fireflyconfig')->set('upgrade_security_level', $array['level']);
} catch (QueryException $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not save new security advisory, but thats OK: %s', $e->getMessage()));
2023-03-11 15:04:16 +01:00
}
}
}