/“). * * 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ß.'); } } } }