Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
178 lines
5.4 KiB
PHP
178 lines
5.4 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 {
|
||
self::checkLimits($zip);
|
||
$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.");
|
||
}
|
||
}
|
||
|
||
/** Grenzen gegen „ZIP-Bomben“ (ein Inhaus-Update entpackt heute ~17.000 Dateien, ~70 MB) */
|
||
public const MAX_ENTRIES = 60_000;
|
||
|
||
public const MAX_TOTAL_BYTES = 800 * 1024 * 1024;
|
||
|
||
public const MAX_RATIO = 200;
|
||
|
||
private static function checkLimits(ZipArchive $zip): void
|
||
{
|
||
if ($zip->numFiles > self::MAX_ENTRIES) {
|
||
throw new RuntimeException('Das Archiv enthält zu viele Dateien.');
|
||
}
|
||
|
||
$total = 0;
|
||
|
||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||
$stat = $zip->statIndex($i);
|
||
$size = (int) ($stat['size'] ?? 0);
|
||
$total += $size;
|
||
|
||
// Stark komprimierte große Einträge sind verdächtig
|
||
if ($size > 10 * 1024 * 1024 && $size > max(1, (int) ($stat['comp_size'] ?? 0)) * self::MAX_RATIO) {
|
||
throw new RuntimeException('Das Archiv enthält eine ungewöhnlich stark komprimierte Datei und wurde abgelehnt.');
|
||
}
|
||
|
||
if ($total > self::MAX_TOTAL_BYTES) {
|
||
throw new RuntimeException('Das Archiv ist entpackt zu groß.');
|
||
}
|
||
}
|
||
}
|
||
}
|