Loheide-eu-mitarbeiterplatt.../tests/Feature/Update/ArchiveTest.php
Friederich Loheide 43244d751a Self-Updater und Lizenzprüfung
Umsetzung von docs/specs/self-updater.md für Laravel:
- Installierter Stand als SHA in storage/.version, Fortschritt in storage/.update-progress,
  selbstheilender Wartungsmodus in storage/.maintenance
- Update-Client je Kanal (stable/development), nur HTTPS, begrenzte Redirects,
  ZIP-Download per Stream mit PK- und SHA-256-Prüfung
- Entpacken mit zweistufigem ZIP-Slip-Schutz, Übernahme mit geschützten Pfaden
  (atomar je Datei, nie löschen), Migrationen, Cache- und OPcache-Reset
- Lizenzspeicher außerhalb der DB, Geräte-ID als Zufallszahl, rotierendes Gerätetoken
  wird bei jeder Antwort (auch Fehler und Downloads) sofort gespeichert
- Lizenzfehler mit verständlichen Meldungen – sie sperren nie das Portal
- Seite „Aktualisierung“ mit Kanal, Suche, Changelog-Vorschau, Fortschritt,
  Migrationen, Änderungsprotokoll, Lizenz und Datenschutzhinweis
- Wartungs-Middleware (503 + Retry-After) und Auto-Migration per Fingerabdruck
- Audit-Log für Updates, Kanalwechsel und Lizenzänderungen
- Version 0.2.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 06:02:19 +00:00

148 lines
5.2 KiB
PHP

<?php
namespace Tests\Feature\Update;
use App\Support\Update\Filesystem;
use App\Support\Update\ProtectedPaths;
use App\Support\Update\StagingApplier;
use App\Support\Update\ZipExtractor;
use RuntimeException;
use Tests\TestCase;
use ZipArchive;
class ArchiveTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
parent::setUp();
$this->dir = sys_get_temp_dir().'/inhaus-zip-'.bin2hex(random_bytes(4));
mkdir($this->dir);
}
protected function tearDown(): void
{
Filesystem::deleteDirectory($this->dir);
parent::tearDown();
}
/**
* @param array<string, string> $files
*/
private function zip(array $files): string
{
$path = $this->dir.'/archiv.zip';
$zip = new ZipArchive;
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
foreach ($files as $name => $contents) {
$zip->addFromString($name, $contents);
}
$zip->close();
return $path;
}
public function test_common_top_level_folder_is_stripped(): void
{
$zip = $this->zip([
'repo-abc/app/Version.php' => '<?php // neu',
'repo-abc/README.md' => 'Hallo',
]);
$count = (new ZipExtractor)->extract($zip, $this->dir.'/staging');
$this->assertSame(2, $count);
$this->assertFileExists($this->dir.'/staging/app/Version.php');
$this->assertFileExists($this->dir.'/staging/README.md');
}
public function test_zip_slip_creates_nothing_outside_staging(): void
{
$zip = $this->zip([
'repo-abc/ok.txt' => 'ok',
'repo-abc/../../evil.php' => '<?php echo "böse";',
]);
try {
(new ZipExtractor)->extract($zip, $this->dir.'/staging');
$this->fail('Das Archiv hätte abgelehnt werden müssen.');
} catch (RuntimeException $e) {
$this->assertStringContainsString('Unsicherer Pfad', $e->getMessage());
}
$this->assertFileDoesNotExist($this->dir.'/evil.php');
$this->assertFileDoesNotExist(dirname($this->dir).'/evil.php');
$this->assertFileDoesNotExist($this->dir.'/staging/../evil.php');
}
public function test_dangerous_entry_names_are_rejected(): void
{
foreach (['../evil', 'a/../../evil', '/etc/passwd', 'C:\\Windows\\evil.dll', "a\0b", '..\\evil'] as $name) {
$this->assertFalse(ZipExtractor::isSafeEntryName($name), $name);
}
foreach (['app/Version.php', 'repo-abc/public/.htaccess', 'a/..b/c'] as $name) {
$this->assertTrue(ZipExtractor::isSafeEntryName($name), $name);
}
}
public function test_protected_paths_are_skipped_and_nothing_is_deleted(): void
{
$staging = $this->dir.'/staging';
$target = $this->dir.'/target';
foreach ([
'app/Neu.php' => 'neu',
'app/Version.php' => 'neue Version',
'.env' => 'APP_KEY=aus-dem-archiv',
'storage/app/license.json' => '{}',
'public/uploads/bild.jpg' => 'fremd',
'plugins/kunde/plugin.json' => '{}',
'vendor/autoload.php' => 'neu',
] as $file => $contents) {
@mkdir(dirname("{$staging}/{$file}"), 0777, true);
file_put_contents("{$staging}/{$file}", $contents);
}
foreach ([
'app/Version.php' => 'alte Version',
'app/Alt.php' => 'bleibt',
'.env' => 'APP_KEY=geheim',
'storage/app/license.json' => '{"email":"lizenz@example.com"}',
] as $file => $contents) {
@mkdir(dirname("{$target}/{$file}"), 0777, true);
file_put_contents("{$target}/{$file}", $contents);
}
$result = (new StagingApplier(new ProtectedPaths(config('updater.protected_paths'))))->apply($staging, $target);
$this->assertSame(['copied' => 3, 'skipped' => 4], $result);
$this->assertSame('neue Version', file_get_contents("{$target}/app/Version.php"));
$this->assertSame('neu', file_get_contents("{$target}/app/Neu.php"));
$this->assertSame('neu', file_get_contents("{$target}/vendor/autoload.php"));
$this->assertSame('bleibt', file_get_contents("{$target}/app/Alt.php"));
$this->assertSame('APP_KEY=geheim', file_get_contents("{$target}/.env"));
$this->assertSame('{"email":"lizenz@example.com"}', file_get_contents("{$target}/storage/app/license.json"));
$this->assertFileDoesNotExist("{$target}/public/uploads/bild.jpg");
$this->assertFileDoesNotExist("{$target}/plugins/kunde/plugin.json");
$this->assertEmpty(glob("{$target}/app/*.update-*"));
}
public function test_protected_path_matching(): void
{
$paths = new ProtectedPaths(['.env', 'storage/', 'config/local.php']);
$this->assertTrue($paths->isProtected('.env'));
$this->assertTrue($paths->isProtected('storage'));
$this->assertTrue($paths->isProtected('storage/logs/x.log'));
$this->assertTrue($paths->isProtected('config/local.php'));
$this->assertFalse($paths->isProtected('.env.example'));
$this->assertFalse($paths->isProtected('storagebox/a'));
$this->assertFalse($paths->isProtected('config/app.php'));
}
}