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>
146 lines
4.2 KiB
PHP
146 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Update;
|
||
|
||
use RuntimeException;
|
||
use ZipArchive;
|
||
|
||
/**
|
||
* Entpackt ein Update-Archiv ins Staging und entfernt dabei den gemeinsamen
|
||
* Top-Level-Ordner („repo-<sha>/“).
|
||
*
|
||
* ZIP-Slip-Schutz in zwei Stufen:
|
||
* a) Eintragsnamen vorab ablehnen: absolute Pfade, Laufwerksbuchstaben,
|
||
* „..“-Segmente, Null-Bytes.
|
||
* b) Nach dem Schreiben den aufgelösten Pfad prüfen – liegt er wirklich im
|
||
* Staging? Sonst sofort wieder löschen. Fängt ab, was (a) übersieht.
|
||
*/
|
||
class ZipExtractor
|
||
{
|
||
/**
|
||
* @return int Anzahl entpackter Dateien
|
||
*/
|
||
public function extract(string $zipFile, string $targetDir): int
|
||
{
|
||
$zip = new ZipArchive;
|
||
|
||
if ($zip->open($zipFile) !== true) {
|
||
throw new RuntimeException('Das heruntergeladene Archiv kann nicht geöffnet werden.');
|
||
}
|
||
|
||
try {
|
||
$this->recreate($targetDir);
|
||
$root = realpath($targetDir);
|
||
$prefix = $this->commonPrefix($zip);
|
||
$count = 0;
|
||
|
||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||
$name = (string) $zip->getNameIndex($i);
|
||
|
||
if (! self::isSafeEntryName($name)) {
|
||
throw new RuntimeException("Unsicherer Pfad im Archiv abgelehnt: {$name}");
|
||
}
|
||
|
||
$relative = substr($name, strlen($prefix));
|
||
|
||
if ($relative === '' || $relative === false) {
|
||
continue;
|
||
}
|
||
|
||
$destination = $root.DIRECTORY_SEPARATOR.$relative;
|
||
|
||
if (str_ends_with($name, '/')) {
|
||
$this->makeDirectory($destination);
|
||
|
||
continue;
|
||
}
|
||
|
||
$this->makeDirectory(dirname($destination));
|
||
$stream = $zip->getStream($name);
|
||
|
||
if ($stream === false || @file_put_contents($destination, $stream) === false) {
|
||
throw new RuntimeException("Datei {$relative} kann nicht entpackt werden. Bitte Schreibrechte für storage/ prüfen.");
|
||
}
|
||
|
||
fclose($stream);
|
||
|
||
// Stufe b: aufgelösten Pfad prüfen (Symlinks, ungewöhnliche Kodierungen).
|
||
$resolved = realpath($destination);
|
||
|
||
if ($resolved === false || ! str_starts_with($resolved, $root.DIRECTORY_SEPARATOR)) {
|
||
@unlink($destination);
|
||
|
||
throw new RuntimeException("Datei außerhalb des Entpackverzeichnisses abgelehnt: {$name}");
|
||
}
|
||
|
||
$count++;
|
||
}
|
||
|
||
return $count;
|
||
} finally {
|
||
$zip->close();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Stufe a: Eintragsnamen vor dem Schreiben prüfen.
|
||
*/
|
||
public static function isSafeEntryName(string $name): bool
|
||
{
|
||
if ($name === '' || str_contains($name, "\0")) {
|
||
return false;
|
||
}
|
||
|
||
$normalized = str_replace('\\', '/', $name);
|
||
|
||
if (str_starts_with($normalized, '/') || preg_match('#^[a-zA-Z]:#', $normalized)) {
|
||
return false;
|
||
}
|
||
|
||
foreach (explode('/', $normalized) as $segment) {
|
||
if ($segment === '..') {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* GitHub-Stil: genau EIN gemeinsamer Top-Level-Ordner, der entfernt wird.
|
||
*/
|
||
private function commonPrefix(ZipArchive $zip): string
|
||
{
|
||
$prefix = null;
|
||
|
||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||
$name = str_replace('\\', '/', (string) $zip->getNameIndex($i));
|
||
$first = explode('/', $name, 2)[0].'/';
|
||
|
||
if (! str_contains($name, '/')) {
|
||
return '';
|
||
}
|
||
|
||
if ($prefix === null) {
|
||
$prefix = $first;
|
||
} elseif ($prefix !== $first) {
|
||
return '';
|
||
}
|
||
}
|
||
|
||
return $prefix ?? '';
|
||
}
|
||
|
||
private function recreate(string $dir): void
|
||
{
|
||
Filesystem::deleteDirectory($dir);
|
||
$this->makeDirectory($dir);
|
||
}
|
||
|
||
private function makeDirectory(string $dir): void
|
||
{
|
||
if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) {
|
||
throw new RuntimeException("Verzeichnis {$dir} kann nicht angelegt werden. Bitte Schreibrechte prüfen.");
|
||
}
|
||
}
|
||
}
|