Some checks failed
CI / PHP Lint-1 (push) Successful in 43s
CI / PHP Lint (push) Successful in 45s
CI / PHP Lint (pull_request) Successful in 38s
CI / Unit Tests & Static Analysis (push) Failing after 43s
CI / PHP Lint-1 (pull_request) Successful in 40s
CI / Unit Tests & Static Analysis (pull_request) Failing after 41s
Aktualisierung per Klick im Administrationsbereich, ohne Zugang zum Server. Der bisherige Updater lud ein ZIP und entpackte es ungeprüft über die Produktion; das ist vollständig ersetzt. Neu in updater/: - Version: Release-Version als Konstante, installierter Commit-SHA in storage/.version (Datei, nicht Datenbank - der Updater muss den Stand auch bei hängender Datenbank lesen können) - Progress: Fortschrittsdatei mit eigenem Fehlerzustand (pct = -1) und Verfallszeit, damit ein abgestürzter Lauf den nächsten nicht blockiert - Maintenance: Wartungsmarkierung mit Ablauf, die sich beim Prüfen selbst aufräumt - ProxyClient: nur HTTPS, Zertifikatsprüfung an, begrenzte Weiterleitungen, 503 mirror_pending/not_released als Bitte um Geduld - Updater: Prüfsumme gegen X-Content-Sha256, zweistufiger ZIP-Slip-Schutz, geschützte Pfade, atomares Schreiben je Datei, nie löschen - AutoMigrate: zieht Migrationen beim ersten Seitenaufruf nach, nur bei geändertem Fingerabdruck und mit Sperre gegen Parallellauf - guard.php: Wartungs-Wächter für die Einstiegsseiten, JSON für die Schnittstellen; ausgenommen bleiben Update-Seite, Anmeldung und Health-Check - Changelog: zerlegt CHANGELOG.md und rendert Markdown ohne unsichere Links Oberfläche: Seite "Aktualisierung" mit Stand, Kanal, Suche, Fortschritt, Migrationsliste und Versionsgeschichte. Der Datenschutzhinweis steht dauerhaft neben der Schaltfläche, nicht in einem Dialog. Fehlt changelog_preview in der Antwort, bleibt es bei "neue Fassung verfügbar" - kein leerer Kasten, kein erfundener Ersatztext. Commit-Texte werden nirgends angezeigt. Außerdem (Punkt 2 der Liste): - cron_test.php entfernt: ohne jede Anmeldung erreichbar, meldete Datenbankfehler samt Meldung und die Spalten der vouchers-Tabelle - test.php verweigert ohne config.php die Auskunft; bisher lief die Diagnose auf einer frisch entpackten Installation ohne Anmeldung PHPStan prüft jetzt den gesamten updater/-Ordner. 18 neue Tests (93 Tests, 243 Assertions), Ablauf gegen den echten Update-Dienst durchgespielt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
302 lines
12 KiB
PHP
302 lines
12 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace Tests;
|
||
|
||
use PHPUnit\Framework\TestCase;
|
||
use Updater\Changelog;
|
||
use Updater\Maintenance;
|
||
use Updater\MigrationRunner;
|
||
use Updater\Progress;
|
||
use Updater\Updater;
|
||
use Updater\Version;
|
||
|
||
require_once __DIR__ . '/../updater/bootstrap.php';
|
||
|
||
/**
|
||
* Der Updater schreibt in die laufende Installation - die Tests fassen
|
||
* deshalb ausschliesslich temporaere Verzeichnisse an. Version::useStorageDir()
|
||
* lenkt Version, Fortschritt und Wartungsmarkierung dorthin um.
|
||
*/
|
||
class UpdaterTest extends TestCase
|
||
{
|
||
/** @var string */
|
||
private $tmp;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
$this->tmp = sys_get_temp_dir() . '/uvt-updater-' . bin2hex(random_bytes(6));
|
||
mkdir($this->tmp, 0775, true);
|
||
Version::useStorageDir($this->tmp . '/storage');
|
||
mkdir($this->tmp . '/storage', 0775, true);
|
||
}
|
||
|
||
protected function tearDown(): void
|
||
{
|
||
Version::useStorageDir(null);
|
||
$this->removeTree($this->tmp);
|
||
}
|
||
|
||
// ------------------------------------------------------------- Version
|
||
|
||
public function testVersionIsStoredAndReadBack(): void
|
||
{
|
||
$sha = str_repeat('a1b2c3d4', 5);
|
||
|
||
$this->assertNull(Version::current(), 'Frische Installation kennt noch keinen Stand');
|
||
$this->assertTrue(Version::save($sha));
|
||
$this->assertSame($sha, Version::current());
|
||
$this->assertSame(substr($sha, 0, 7), Version::short());
|
||
}
|
||
|
||
/**
|
||
* Eine halbe Proxy-Antwort darf sich nicht als Version einzementieren -
|
||
* sonst meldet die Installation dauerhaft einen Stand, den es nicht gibt.
|
||
*/
|
||
public function testInvalidRevisionIsRejected(): void
|
||
{
|
||
foreach (['', 'nope', str_repeat('z', 40), str_repeat('ab', 10)] as $bad) {
|
||
$this->assertFalse(Version::save($bad), 'Abgelehnt werden muss: ' . var_export($bad, true));
|
||
}
|
||
$this->assertNull(Version::current());
|
||
}
|
||
|
||
public function testReleaseConstantMatchesVersionFile(): void
|
||
{
|
||
$file = trim((string)file_get_contents(dirname(__DIR__) . '/VERSION'));
|
||
|
||
$this->assertSame($file, Version::RELEASE, 'VERSION-Datei und Konstante duerfen nicht auseinanderlaufen');
|
||
}
|
||
|
||
// --------------------------------------------------------- Wartungsmodus
|
||
|
||
public function testMaintenanceCanBeSwitchedOnAndOff(): void
|
||
{
|
||
$this->assertFalse(Maintenance::isActive());
|
||
Maintenance::activate();
|
||
$this->assertTrue(Maintenance::isActive());
|
||
Maintenance::deactivate();
|
||
$this->assertFalse(Maintenance::isActive());
|
||
}
|
||
|
||
/**
|
||
* Ohne Selbstheilung legt ein Absturz mitten im Update die Installation
|
||
* dauerhaft lahm - und niemand kaeme mehr an die Oberflaeche, um den
|
||
* Modus abzuschalten.
|
||
*/
|
||
public function testMaintenanceHealsItselfAfterExpiry(): void
|
||
{
|
||
file_put_contents(
|
||
$this->tmp . '/storage/.maintenance',
|
||
json_encode(['since' => time() - 7200, 'until' => time() - 60])
|
||
);
|
||
|
||
$this->assertFalse(Maintenance::isActive(), 'Abgelaufene Wartung darf nicht mehr greifen');
|
||
$this->assertFileDoesNotExist($this->tmp . '/storage/.maintenance', 'und wird dabei aufgeraeumt');
|
||
}
|
||
|
||
// -------------------------------------------------------------- Fortschritt
|
||
|
||
public function testProgressIsWrittenAndRead(): void
|
||
{
|
||
Progress::set(42, 'download', 'Update wird geladen …');
|
||
$state = Progress::get();
|
||
|
||
$this->assertSame(42, $state['pct']);
|
||
$this->assertSame('download', $state['step']);
|
||
$this->assertSame('Update wird geladen …', $state['text']);
|
||
$this->assertTrue(Progress::isRunning());
|
||
}
|
||
|
||
/** pct = -1 trennt "fehlgeschlagen" eindeutig von "0 % erledigt". */
|
||
public function testProgressErrorStateIsDistinguishable(): void
|
||
{
|
||
Progress::error('Der Update-Dienst ist nicht erreichbar.');
|
||
$state = Progress::get();
|
||
|
||
$this->assertSame(-1, $state['pct']);
|
||
$this->assertSame('error', $state['step']);
|
||
$this->assertFalse(Progress::isRunning());
|
||
}
|
||
|
||
/** Ein abgestuerzter Lauf darf den naechsten nicht dauerhaft blockieren. */
|
||
public function testStaleProgressDoesNotCountAsRunning(): void
|
||
{
|
||
file_put_contents($this->tmp . '/storage/.update-progress', json_encode([
|
||
'pct' => 50,
|
||
'step' => 'extract',
|
||
'text' => 'haengt seit gestern',
|
||
'ts' => time() - (Progress::STALE_AFTER + 60),
|
||
]));
|
||
|
||
$this->assertFalse(Progress::isRunning());
|
||
}
|
||
|
||
// ------------------------------------------------------------- ZIP-Slip
|
||
|
||
public function testEntryNamesOutsideTheArchiveAreRejected(): void
|
||
{
|
||
foreach (['../evil.php', 'a/../../evil.php', '/etc/passwd', 'C:\\windows\\evil', "ok\0.php"] as $bad) {
|
||
$this->assertFalse(Updater::isSafeEntryName($bad), 'Abgelehnt werden muss: ' . $bad);
|
||
}
|
||
foreach (['index.php', 'admin/index.php', 'a/b/c.txt'] as $good) {
|
||
$this->assertTrue(Updater::isSafeEntryName($good));
|
||
}
|
||
}
|
||
|
||
public function testZipSlipEntryNeverLandsOutsideTheStagingArea(): void
|
||
{
|
||
$zipPath = $this->tmp . '/slip.zip';
|
||
$zip = new \ZipArchive();
|
||
$this->assertTrue($zip->open($zipPath, \ZipArchive::CREATE) === true);
|
||
$zip->addFromString('harmlos.txt', 'ok');
|
||
$zip->addFromString('../evil.txt', 'boesartig');
|
||
$zip->addFromString('a/../../auch-boese.txt', 'boesartig');
|
||
$zip->close();
|
||
|
||
$staging = $this->tmp . '/staging';
|
||
$updater = new Updater(null, null, 'stable', null, $this->tmp . '/root');
|
||
$written = $updater->extractZip($zipPath, $staging);
|
||
|
||
$this->assertSame(1, $written, 'Nur der harmlose Eintrag darf ankommen');
|
||
$this->assertFileExists($staging . '/harmlos.txt');
|
||
$this->assertFileDoesNotExist($this->tmp . '/evil.txt');
|
||
$this->assertFileDoesNotExist($this->tmp . '/auch-boese.txt');
|
||
}
|
||
|
||
// ------------------------------------------------------- Geschuetzte Pfade
|
||
|
||
public function testProtectedPathsAreRecognised(): void
|
||
{
|
||
$this->assertTrue(Updater::isProtected('config.php'));
|
||
$this->assertTrue(Updater::isProtected('uploads/logo.png'));
|
||
$this->assertTrue(Updater::isProtected('updater/storage/.version'));
|
||
$this->assertFalse(Updater::isProtected('index.php'));
|
||
$this->assertFalse(Updater::isProtected('updater/Updater.php'));
|
||
$this->assertFalse(Updater::isProtected('uploads.php'), 'Der Ordnername darf nicht auf Dateien durchschlagen');
|
||
}
|
||
|
||
public function testApplyStagingKeepsProtectedFilesAndUpdatesTheRest(): void
|
||
{
|
||
$root = $this->tmp . '/root';
|
||
mkdir($root . '/uploads', 0775, true);
|
||
file_put_contents($root . '/config.php', '<?php // Zugangsdaten dieser Installation');
|
||
file_put_contents($root . '/uploads/logo.png', 'altes Logo');
|
||
file_put_contents($root . '/index.php', 'alt');
|
||
|
||
$staging = $this->tmp . '/staging-apply';
|
||
mkdir($staging . '/uploads', 0775, true);
|
||
file_put_contents($staging . '/config.php', '<?php // Beispielkonfiguration aus dem Paket');
|
||
file_put_contents($staging . '/uploads/logo.png', 'Logo aus dem Paket');
|
||
file_put_contents($staging . '/index.php', 'neu');
|
||
file_put_contents($staging . '/neu.php', 'neue Datei');
|
||
|
||
$updater = new Updater(null, null, 'stable', null, $root);
|
||
$applied = $updater->applyStaging($staging);
|
||
|
||
$this->assertSame('<?php // Zugangsdaten dieser Installation', file_get_contents($root . '/config.php'));
|
||
$this->assertSame('altes Logo', file_get_contents($root . '/uploads/logo.png'));
|
||
$this->assertSame('neu', file_get_contents($root . '/index.php'));
|
||
$this->assertSame('neue Datei', file_get_contents($root . '/neu.php'));
|
||
$this->assertSame(2, $applied, 'Nur die ungeschuetzten Dateien zaehlen');
|
||
}
|
||
|
||
public function testApplyStagingLeavesNoHalfWrittenFiles(): void
|
||
{
|
||
$root = $this->tmp . '/root2';
|
||
mkdir($root, 0775, true);
|
||
$staging = $this->tmp . '/staging2';
|
||
mkdir($staging, 0775, true);
|
||
file_put_contents($staging . '/index.php', '<?php echo "neu";');
|
||
|
||
(new Updater(null, null, 'stable', null, $root))->applyStaging($staging);
|
||
|
||
$this->assertFileExists($root . '/index.php');
|
||
$this->assertFileDoesNotExist($root . '/index.php.new', 'Die Zwischendatei muss verschwinden');
|
||
}
|
||
|
||
// ------------------------------------------------------------ Migrationen
|
||
|
||
public function testMigrationRunnerCountsOnlyPendingFiles(): void
|
||
{
|
||
$dir = $this->tmp . '/migrations';
|
||
mkdir($dir, 0775, true);
|
||
file_put_contents($dir . '/0001_erste.sql', 'CREATE TABLE test_a (id INTEGER PRIMARY KEY);');
|
||
file_put_contents($dir . '/0002_zweite.sql', 'CREATE TABLE test_b (id INTEGER PRIMARY KEY);');
|
||
|
||
$pdo = new \PDO('sqlite::memory:');
|
||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||
$runner = new MigrationRunner($pdo, $dir, $this->tmp . '/storage');
|
||
|
||
$this->assertCount(2, $runner->runPending(true));
|
||
$this->assertCount(0, $runner->runPending(true), 'Ein zweiter Lauf hat nichts mehr zu tun');
|
||
|
||
$status = $runner->status();
|
||
$this->assertCount(2, $status);
|
||
$this->assertTrue($status[0]['applied']);
|
||
}
|
||
|
||
/** Der Fingerabdruck entscheidet, ob ueberhaupt migriert wird. */
|
||
public function testMigrationFingerprintChangesWithTheFiles(): void
|
||
{
|
||
$dir = $this->tmp . '/migrations-fp';
|
||
mkdir($dir, 0775, true);
|
||
file_put_contents($dir . '/0001_a.sql', 'SELECT 1;');
|
||
$before = \Updater\AutoMigrate::fingerprint($dir);
|
||
|
||
file_put_contents($dir . '/0002_b.sql', 'SELECT 1;');
|
||
$after = \Updater\AutoMigrate::fingerprint($dir);
|
||
|
||
$this->assertNotSame($before, $after);
|
||
$this->assertSame($after, \Updater\AutoMigrate::fingerprint($dir), 'Ohne Aenderung bleibt er gleich');
|
||
}
|
||
|
||
// -------------------------------------------------------------- Changelog
|
||
|
||
public function testChangelogIsSplitIntoVersions(): void
|
||
{
|
||
$markdown = "# Änderungsprotokoll\n\n## 3.0.0 – 2026-09-25\n\n- Selbst-Updater\n- Wartungsmodus\n\n## 2.9.0 – 2026-09-24\n\n- Display-Seiten\n";
|
||
$sections = Changelog::sections($markdown);
|
||
|
||
$this->assertCount(2, $sections);
|
||
$this->assertSame('3.0.0', $sections[0]['version']);
|
||
$this->assertStringContainsString('Selbst-Updater', $sections[0]['text']);
|
||
$this->assertSame('2.9.0', $sections[1]['version']);
|
||
}
|
||
|
||
public function testChangelogRendersListsAndKeepsUnsafeLinksOut(): void
|
||
{
|
||
$html = Changelog::toHtml("- Erster Punkt mit **Nachdruck**\n- [Doku](https://example.com/doku)\n- [Böse](javascript:alert(1))\n");
|
||
|
||
$this->assertStringContainsString('<ul><li>Erster Punkt mit <strong>Nachdruck</strong></li>', $html);
|
||
$this->assertStringContainsString('<a href="https://example.com/doku"', $html);
|
||
$this->assertStringNotContainsString('javascript:', $html);
|
||
$this->assertStringNotContainsString('<script', $html);
|
||
}
|
||
|
||
public function testChangelogEscapesHtmlFromTheSource(): void
|
||
{
|
||
$html = Changelog::toHtml('- <img src=x onerror=alert(1)>');
|
||
|
||
$this->assertStringNotContainsString('<img', $html);
|
||
$this->assertStringContainsString('<img', $html);
|
||
}
|
||
|
||
// ------------------------------------------------------------------ Helfer
|
||
|
||
private function removeTree(string $dir): void
|
||
{
|
||
if (!is_dir($dir)) {
|
||
return;
|
||
}
|
||
$it = new \RecursiveIteratorIterator(
|
||
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
|
||
\RecursiveIteratorIterator::CHILD_FIRST
|
||
);
|
||
foreach ($it as $item) {
|
||
$item->isDir() ? @rmdir($item->getPathname()) : @unlink($item->getPathname());
|
||
}
|
||
@rmdir($dir);
|
||
}
|
||
}
|