Compare commits

...

12 Commits

Author SHA1 Message Date
Benjamin DALSASS
9898ba4d91 N°9556 - backup restore improvement
- CI fix odain
2026-11-02 10:20:22 +01:00
Benjamin DALSASS
b8c113378a N°9556 - backup restore improvement
- CI fix odain
2026-11-02 10:08:52 +01:00
Benjamin DALSASS
89fc137432 N°9556 - backup restore improvement
- CI fix
2026-10-30 11:55:24 +01:00
Benjamin DALSASS
04049d8e1b N°9556 - backup restore improvement
- CI fix
2026-10-30 11:34:39 +01:00
Benjamin DALSASS
1bf1510bdb 9436 - sanitize a href
- debug test
2026-10-30 11:22:02 +01:00
Benjamin DALSASS
144a5e7d8c 9436 - sanitize a href
- php cs fixer
2026-10-30 11:19:20 +01:00
Benjamin DALSASS
7aad5e71ee N°9556 - backup restore improvement
- eric feedback
2026-10-30 08:12:24 +01:00
Benjamin DALSASS
1365cdb598 N°9556 - backup restore improvement
- eric feedback
2026-10-29 09:16:39 +01:00
Benjamin DALSASS
1190b0dd12 N°9556 - backup restore improvement
- greptile feedback
2026-10-29 08:08:26 +01:00
Benjamin DALSASS
05ca95a2d8 N°9556 - backup restore improvement
- cs fixer
2026-10-28 16:25:02 +01:00
Benjamin DALSASS
e1eaf3b795 N°9556 - backup restore improvement
- add php unit tests
2026-10-28 15:47:06 +01:00
Benjamin DALSASS
554d813050 N°9556 - backup restore improvement 2026-10-28 09:57:53 +01:00
3 changed files with 261 additions and 66 deletions

View File

@@ -29,6 +29,24 @@ require_once(APPROOT.'/application/application.inc.php');
require_once(APPROOT.'core/mutex.class.inc.php');
const BACKUP_DIR = APPROOT.'data/backups/';
class AjaxBackupException extends CoreException
{
public function __construct(array $aContextData = null, Exception $oPreviousException = null)
{
parent::__construct('Backup/Restore exception occurred', $aContextData, oPrevious: $oPreviousException);
}
}
class AjaxBackupInvalidFileException extends AjaxBackupException
{
public function __construct(array $aContextData = null, Exception $oPreviousException = null)
{
parent::__construct($aContextData, oPreviousException: $oPreviousException);
}
}
/**
* @param WebPage $oPage
* @param string $sHtmlErrorMessage the whole HTML error, cinluding div/p/...
@@ -51,73 +69,79 @@ $sOperation = utils::ReadParam('operation', '');
$oPage = new AjaxPage('');
$oPage->SetContentType('text/html');
/**
* Check security
*/
switch ($sOperation) {
/**
* Can't use normal check methods (DoLogin for ex) as the datamodel can't be loaded here
* So we're only using a token generated in the restore_token operation
*/
case 'restore_exec':
IssueLog::Enable(APPROOT.'log/error.log');
if (utils::GetConfig()->Get('demo_mode')) {
DisplayErrorAndDie(
$oPage,
'<div data-error-stimulus="Error">Sorry, '.ITOP_APPLICATION_SHORT.' is in <b>demonstration mode</b>: the feature is disabled.</div>'
);
}
$sToken = utils::ReadParam('token', '', false, 'raw_data');
$sBasePath = APPROOT.'/data/';
$sTokenFile = $sBasePath.'restore.'.$sToken.'.tok';
$tokenRealPath = utils::RealPath($sTokenFile, $sBasePath);
if (($tokenRealPath === false) || (!is_file($tokenRealPath))) {
IssueLog::Error("ajax.backup.php operation=$sOperation ERROR = inexisting token $sToken");
$sEscapedToken = utils::HtmlEntities($sToken);
DisplayErrorAndDie($oPage, "<p>Error: missing token file: '$sEscapedToken'</p>");
}
break;
default:
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin();
$sTransactionId = utils::ReadParam('transaction_id', '', true, 'transaction_id');
// the consumer page is not reloaded after download, we need to keep the transaction_id
$bRemoveTransactionId = ($sOperation !== 'download');
if (!utils::IsTransactionValid($sTransactionId, $bRemoveTransactionId)) {
$sEscapedOperation = utils::HtmlEntities($sOperation);
DisplayErrorAndDie($oPage, "<div data-error-stimulus=\"Error\">Error: invalid Transaction ID. The operation '$sEscapedOperation' was NOT performed!</div>");
}
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
if (utils::GetConfig()->Get('demo_mode')) {
DisplayErrorAndDie($oPage, '<div data-error-stimulus="Error">Sorry, '.ITOP_APPLICATION_SHORT.' is in <b>demonstration mode</b>: the feature is disabled.</div>');
}
break;
}
/**
* Backup from an interactive session
*/
try {
/**
* Check security
*/
switch ($sOperation) {
/**
* Can't use normal check methods (DoLogin for ex) as the datamodel can't be loaded here
* So we're only using a token generated in the restore_token operation
*/
case 'restore_exec':
IssueLog::Enable(APPROOT.'log/error.log');
if (utils::GetConfig()->Get('demo_mode')) {
DisplayErrorAndDie(
$oPage,
'<div data-error-stimulus="Error">Sorry, '.ITOP_APPLICATION_SHORT.' is in <b>demonstration mode</b>: the feature is disabled.</div>'
);
}
$sToken = utils::ReadParam('token', '', false, 'raw_data');
$sBasePath = APPROOT.'/data/';
$sTokenFile = $sBasePath.'restore.'.$sToken.'.tok';
$tokenRealPath = utils::RealPath($sTokenFile, $sBasePath);
if (($tokenRealPath === false) || (!is_file($tokenRealPath))) {
throw new AjaxBackupInvalidFileException([
'backup_operation' => $sOperation,
'message' => 'Could not restore backup, invalid token file.',
'token' => $sToken,
]);
}
break;
default:
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin();
$sTransactionId = utils::ReadParam('transaction_id', '', true, 'transaction_id');
// the consumer page is not reloaded after download, we need to keep the transaction_id
$bRemoveTransactionId = ($sOperation !== 'download');
if (!utils::IsTransactionValid($sTransactionId, $bRemoveTransactionId)) {
throw new AjaxBackupException([
'backup_operation' => $sOperation,
'message' => 'Invalid transaction id',
'transaction_id' => $sTransactionId,
]);
}
ApplicationMenu::CheckMenuIdEnabled('BackupStatus');
if (utils::GetConfig()->Get('demo_mode')) {
DisplayErrorAndDie($oPage, '<div data-error-stimulus="Error">Sorry, '.ITOP_APPLICATION_SHORT.' is in <b>demonstration mode</b>: the feature is disabled.</div>');
}
break;
}
/**
* Backup from an interactive session
*/
switch ($sOperation) {
case 'backup':
try {
set_time_limit(0);
$oBB = new BackupExec(APPROOT.'data/backups/manual/', 0 /*iRetentionCount*/);
$sRes = $oBB->Process(time() + 36000); // 10 hours to complete should be sufficient!
$oPage->output();
} catch (Exception $e) {
$oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage());
throw new AjaxBackupException([
'backup_operation' => $sOperation,
'message' => $e->getMessage(),
], $e);
}
$oPage->output();
break;
/*
@@ -138,6 +162,15 @@ try {
}
$sFile = utils::ReadParam('file', '', false, 'raw_data');
$sBackupFilePath = utils::RealPath(BACKUP_DIR.$sFile, BACKUP_DIR);
if ($sBackupFilePath === false) {
throw new AjaxBackupInvalidFileException([
'backup_operation' => $sOperation,
'message' => 'Could not restore backup, invalid file given.',
'backup_file' => $sFile,
]);
}
$sToken = str_replace(' ', '', (string)microtime()).$sTransactionId;
$sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok';
file_put_contents($sTokenFile, $sFile);
@@ -171,14 +204,15 @@ try {
$oDBRS = new DBRestore($oItopConfig);
$oDBRS->SetMySQLBinDir($sMySQLBinDir);
$sBackupDir = APPROOT.'data/backups/';
$sBackupFile = $sBackupDir.$sFile;
$sBackupFile = BACKUP_DIR.$sFile;
$sRes = $oDBRS->RestoreFromCompressedBackup($sBackupFile, $sEnvironment);
IssueLog::Info('Backup Restore - Done, releasing the LOCK');
} catch (Exception $e) {
$oPage->p('Error: '.$e->getMessage());
IssueLog::Error($sOperation.' - '.$e->getMessage());
throw new AjaxBackupException([
'backup_operation' => $sOperation,
'message' => $e->getMessage(),
], $e);
} finally {
unlink($tokenRealPath);
}
@@ -192,14 +226,33 @@ try {
}
$sFile = utils::ReadParam('file', '', false, 'raw_data');
$oBackup = new DBBackupScheduled();
$sBackupDir = APPROOT.'data/backups/';
$sBackupFilePath = utils::RealPath($sBackupDir.$sFile, $sBackupDir);
$sBackupFilePath = utils::RealPath(BACKUP_DIR.$sFile, BACKUP_DIR);
if ($sBackupFilePath === false) {
throw new CoreUnexpectedValue('Invalid file path');
throw new AjaxBackupInvalidFileException([
'backup_operation' => $sOperation,
'message' => 'Could not download backup, invalid file given.',
'backup_file' => $sFile,
]);
}
$oBackup->DownloadBackup($sBackupFilePath);
break;
}
} catch (AjaxBackupInvalidFileException $e) {
IssueLog::Warning('Unable to perform backup `'.$e->getContextData()['backup_operation'].'` : '.$e->getContextData()['message'], LogChannels::SECURITY, [
'backup_operation' => $e->getContextData()['backup_operation'] ?? null,
'backup_file' => $e->getContextData()['backup_file'] ?? null,
]);
http_response_code(422);
die($e->getContextData()['message']);
} catch (CoreException $e) {
IssueLog::Error('Unable to perform backup `'.$e->getContextData()['backup_operation'].'` : '.$e->getContextData()['message'], LogChannels::BACKUP, [
'backup_operation' => $e->getContextData()['backup_operation'] ?? null,
'backup_file' => $e->getContextData()['backup_file'] ?? null,
]);
http_response_code(500);
die(Dict::S('UI:PageTitle:FatalError'));
} catch (Exception $e) {
IssueLog::Error($sOperation.' - '.$e->getMessage());
IssueLog::Error($e->getMessage());
http_response_code(500);
die(Dict::S('UI:PageTitle:FatalError'));
}

View File

@@ -443,6 +443,13 @@ function LaunchBackupNow()
window.location.reload();
}
oModal.dialog('close');
}).fail(function(xhr){
var sErrorMessage = xhr.status === 500
? (xhr.responseText || 'Erreur serveur (HTTP 500).')
: (xhr.responseText || 'Erreur HTTP ' + xhr.status + '.');
$('#backup_errors').text(sErrorMessage);
$('#backup_errors').removeClass('ibo-is-hidden');
oModal.dialog('close');
});
}
}
@@ -487,11 +494,25 @@ function LaunchRestoreNow(sBackupFile, sConfirmationMessage)
$('#backup_success').removeClass('ibo-is-hidden');
}
oModal.dialog('close');
}).fail(function(xhr){
var sErrorMessage = xhr.status === 500
? (xhr.responseText || 'Erreur serveur (HTTP 500).')
: (xhr.responseText || 'Erreur HTTP ' + xhr.status + '.');
$('#backup_errors').text(sErrorMessage);
$('#backup_errors').removeClass('ibo-is-hidden');
oModal.dialog('close');
});
} else {
$('button.restore').prop('disabled', true);
oModal.dialog('close');
}
}).fail(function(xhr){
var sErrorMessage = xhr.status === 500
? (xhr.responseText || 'Erreur serveur (HTTP 500).')
: (xhr.responseText || 'Erreur HTTP ' + xhr.status + '.');
$('#backup_errors').text(sErrorMessage);
$('#backup_errors').removeClass('ibo-is-hidden');
oModal.dialog('close');
});
}
JS

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace Combodo\iTop\Test\UnitTest\Module\ItopBackup;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use MetaModel;
use UserRights;
use utils;
class BackupAjaxEndpointTest extends ItopDataTestCase
{
public const USE_TRANSACTION = false;
private const AUTHENTICATION_PASSWORD = 'tagada-Secret,007';
protected function setUp(): void
{
parent::setUp();
$this->BackupConfiguration();
$this->AddLoginModeAndSaveConfiguration('url');
}
public function testDownloadInvalidDownloadPathReturnsHttp500(): void
{
$sLogin = 'test-backup-endpoint-'.uniqid();
$this->CreateUser($sLogin, self::$aURP_Profiles['Administrator'], self::AUTHENTICATION_PASSWORD);
UserRights::Login($sLogin);
$sData = $this->CallItopUri(
'env-production/itop-backup/ajax.backup.php?'.http_build_query([
'operation' => 'download',
'file' => 'missing-'.uniqid().'.zip',
'auth_user' => $sLogin,
'auth_pwd' => self::AUTHENTICATION_PASSWORD,
'transaction_id' => utils::GetNewTransactionId(),
]),
[],
[
CURLOPT_HTTPHEADER => ['X-Combodo-Ajax:1'],
CURLOPT_POST => 0,
],
true
);
$this->assertSame(422, $this->aLastCurlGetInfo['http_code'], __METHOD__.':'.$sData);
}
public function testRestoreGetTokenInvalidTransactionIdReturnsHttp500(): void
{
$sLogin = 'test-backup-endpoint-'.uniqid();
$this->CreateUser($sLogin, self::$aURP_Profiles['Administrator'], self::AUTHENTICATION_PASSWORD);
UserRights::Login($sLogin);
$sData = $this->CallItopUri(
'env-production/itop-backup/ajax.backup.php?'.http_build_query([
'operation' => 'restore_get_token',
'auth_user' => $sLogin,
'auth_pwd' => self::AUTHENTICATION_PASSWORD,
'transaction_id' => 'unknown-transaction-id-'.uniqid(),
]),
[],
[
CURLOPT_HTTPHEADER => ['X-Combodo-Ajax:1'],
CURLOPT_POST => 0,
],
true
);
$this->assertSame(500, $this->aLastCurlGetInfo['http_code'], __METHOD__.':'.$sData);
}
public function testRestoreGetToken(): void
{
$sLogin = 'test-backup-endpoint-'.uniqid();
$this->CreateUser($sLogin, self::$aURP_Profiles['Administrator'], self::AUTHENTICATION_PASSWORD);
UserRights::Login($sLogin);
$sBackupDirectory = APPROOT.'data/backups/';
if (!is_dir($sBackupDirectory)) {
$this->assertTrue(mkdir($sBackupDirectory, 0770, true) || is_dir($sBackupDirectory));
}
$sBackupFile = 'backup-'.uniqid().'.zip';
$sBackupPath = $sBackupDirectory.$sBackupFile;
$oArchive = new \ZipArchive();
$bArchiveOpen = $oArchive->open($sBackupPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$this->assertTrue($bArchiveOpen);
$this->assertTrue($oArchive->addFromString('itop-dump.sql', '-- Test backup'));
$sConfigContents = file_get_contents(MetaModel::GetConfig()->GetLoadedFile());
$this->assertNotFalse($sConfigContents);
$this->assertTrue($oArchive->addFromString('config-itop.php', $sConfigContents));
$this->assertTrue($oArchive->close());
$this->aFileToClean[] = $sBackupPath;
$sData = $this->CallItopUri(
'env-production/itop-backup/ajax.backup.php?'.http_build_query([
'operation' => 'restore_get_token',
'auth_user' => $sLogin,
'auth_pwd' => self::AUTHENTICATION_PASSWORD,
'file' => $sBackupFile,
'transaction_id' => utils::GetNewTransactionId(),
]),
[],
[
CURLOPT_HTTPHEADER => ['X-Combodo-Ajax:1'],
CURLOPT_POST => 0,
],
true
);
$aData = json_decode($sData, true);
$this->assertSame(200, $this->aLastCurlGetInfo['http_code'], __METHOD__.':'.$sData);
$this->assertArrayHasKey('token', $aData);
}
}