From 5ff20fac44684b6d9e62d4319a1211ea70b251cd Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Wed, 18 Nov 2026 05:47:34 +0100 Subject: [PATCH] fix(archive): harden stem traversal, member sanitizer, pre-1980 mtimes and folder extraction - Sanitize archive subfolder stems in _get_archive_stem and enforce strict child-directory confinement in ArchiveExtractDialog (blocks ..tar.gz, ...tar.gz, ...zip, .zip, trailing dots/spaces and Windows device names CON/NUL/COM1..9/LPT1..9). - Harden sanitize_archive_member_path, extract_zip_archive and extract_tar_archive against empty/root members, NTFS ADS colons (including 1-char names), control chars, trailing dots/spaces on all path segments, symlink/junction intermediates and Unix symlink/special entries in ZIP archives. - Exclude destination archive and PID+UUID .tmp_* staging files from self-inclusion in create_zip_archive and create_tar_archive; default single-folder compression in ArchiveCompressDialog to the parent directory; handle Windows read-only file replacement atomically. - Clamp pre-1980 (including negative pre-1970 mtimes on Windows) and post-2107 file timestamps in create_zip_archive via chunked streaming with explicit zinfo.file_size. - Expand directory prefixes during selective member extraction in ZIP and TAR archives and restrict is_archive from falsely matching standalone .gz/.bz2/.xz streams or unsupported .tar.zst files. - Add 8 hermetic regression tests in tests/test_bugsweep_archive_security_and_resilience_20261118.py. --- CHANGELOG.md | 6 + src/core/archive_service.py | 323 +++++++++++-- src/gui/archive_dialog.py | 68 ++- ...rchive_security_and_resilience_20261118.py | 451 ++++++++++++++++++ 4 files changed, 803 insertions(+), 45 deletions(-) create mode 100644 tests/test_bugsweep_archive_security_and_resilience_20261118.py diff --git a/CHANGELOG.md b/CHANGELOG.md index b98ebab..5f017e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Format basiert auf [Keep a Changelog](https://keepachangelog.com/de/1.1.0/). ## [Unreleased] +### Security & Resilience: Archiv-Engine & Archiv-Dialog-Suite (2026-11-18) +- **Unterordner-Traversal- & Gerätenamen-Schutz (`src/gui/archive_dialog.py`)**: `_get_archive_stem` und `ArchiveExtractDialog` bereinigen Punkt-Traversal-Namen (`..tar.gz`, `...zip`, `.zip`), nachgestellte Punkte/Leerzeichen sowie reservierte Windows-Gerätenamen (`CON`, `NUL`, `COM1..9`, `LPT1..9`) und erzwingen ein echtes Kindverzeichnis unterhalb des gewählten Zielordners. +- **Erweiterte Zip-Slip-/Tar-Slip- & Symlink-Abwehr (`src/core/archive_service.py`)**: `sanitize_archive_member_path`, `extract_zip_archive` und `extract_tar_archive` blockieren leere/Root-Member, NTFS Alternate Data Streams (`:`), Steuerzeichen, reservierte Windows-Gerätepfade, Symlink-/Junction-Zwischenpfade im Zielbaum sowie Unix-Symlink-/Spezialdatei-Einträge in ZIP-Archiven (`S_IFLNK`). +- **Selbst-Einschluss-Schutz, Prä-1980-Zeitstempel & Read-Only-Overwrite (`src/core/archive_service.py`, `src/gui/archive_dialog.py`)**: `create_zip_archive` und `create_tar_archive` schließen Zielarchiv und `.tmp_*`-Staging-Dateien (`PID + UUID`) vom Re-Packing aus, klemmen Datei-Zeitstempel außerhalb des DOS-ZIP-Fensters (`< 1980` / `> 2107`) per Chunk-Streaming verlustfrei auf gültige Grenzen und ersetzen schreibgeschützte Zieldateien bei aktivem Überschreiben atomar; `ArchiveCompressDialog` legt Einzelordner-Archive standardmäßig im Elternverzeichnis an. +- **Selektive Ordner-Extraktion & Einzelstream-Abgrenzung (`src/core/archive_service.py`)**: `extract_zip_archive` und `extract_tar_archive` entpacken ausgewählte Ordnerzeilen im `ArchiveViewerDialog` rekursiv samt Inhalt; `is_archive` meldet reine Einzelstream-Dateien (`.gz`, `.bz2`, `.xz` ohne TAR-Header) und nicht unterstütztes `.tar.zst` nicht mehr fälschlich als unterstützte Archive. + ### Universal-Archiv-Engine & Multi-Format-Härtung (2026-10-29, TW-EP-13) - **Universal-Archiv-Core (`src/core/archive_service.py`)**: - Vereinheitlichte Fassade (`is_archive`, `inspect_archive`, `create_archive`, `extract_archive`, `check_archive_integrity`) für alle gängigen Archivformate: ZIP (`.zip`), TAR (`.tar`), TAR.GZ (`.tar.gz`, `.tgz`), TAR.BZ2 (`.tar.bz2`, `.tbz2`) und TAR.XZ (`.tar.xz`, `.txz`). diff --git a/src/core/archive_service.py b/src/core/archive_service.py index 8f4ea08..f13dbb9 100644 --- a/src/core/archive_service.py +++ b/src/core/archive_service.py @@ -19,6 +19,7 @@ import tarfile import time from typing import Callable, List, Optional, Tuple +import uuid import zipfile from PySide6.QtCore import QThread, Signal @@ -95,26 +96,195 @@ def format_bytes(num_bytes: int) -> str: return f"{num_bytes / (1024 * 1024 * 1024):.2f} GB" +_WINDOWS_RESERVED_DEVICE_NAMES = frozenset({ + "CON", "PRN", "AUX", "NUL", + *(f"COM{i}" for i in range(1, 10)), + *(f"LPT{i}" for i in range(1, 10)), + "COM¹", "COM²", "COM³", + "LPT¹", "LPT²", "LPT³", +}) +_INVALID_MEMBER_SEGMENT_CHARS = frozenset('<>:"|?*') +_ZIP_MIN_DATETIME = (1980, 1, 1, 0, 0, 0) +_ZIP_MAX_DATETIME = (2107, 12, 31, 23, 59, 58) + + +def _is_windows_reserved_segment(segment: str) -> bool: + stem = segment.split(".", 1)[0].strip().upper() + return stem in _WINDOWS_RESERVED_DEVICE_NAMES + + +def _is_harmless_root_dir_member(member_name: str) -> bool: + cleaned = member_name.replace("\\", "/").strip() + return cleaned in (".", "./") + + +def _is_zip_special_or_symlink(info: zipfile.ZipInfo) -> bool: + unix_mode = (info.external_attr >> 16) & 0xFFFF + if unix_mode == 0: + return False + return ( + stat.S_ISLNK(unix_mode) + or stat.S_ISFIFO(unix_mode) + or stat.S_ISCHR(unix_mode) + or stat.S_ISBLK(unix_mode) + or stat.S_ISSOCK(unix_mode) + ) + + +def _make_staging_path(dest_path: Path) -> Path: + return dest_path.with_name( + f"{dest_path.name}.tmp_{os.getpid()}_{uuid.uuid4().hex[:8]}" + ) + + +def _atomic_replace(temp_path: Path, dest_path: Path) -> None: + try: + os.replace(temp_path, dest_path) + except PermissionError: + try: + st = dest_path.stat() + is_readonly_file = not stat.S_ISDIR(st.st_mode) and not (st.st_mode & stat.S_IWRITE) + except OSError: + is_readonly_file = False + if not is_readonly_file: + raise + os.chmod(dest_path, st.st_mode | stat.S_IWRITE) + os.replace(temp_path, dest_path) + + +def _is_excluded_output_artifact(candidate: Path, dest_path: Path, temp_path: Path) -> bool: + try: + resolved = candidate.resolve() + except OSError: + resolved = candidate + if resolved == dest_path or resolved == temp_path: + return True + try: + same_parent = candidate.parent.resolve() == dest_path.parent + except OSError: + same_parent = False + return same_parent and candidate.name.startswith(f"{dest_path.name}.tmp_") + + +def _normalize_member_key(name: str) -> str: + key = name.replace("\\", "/").strip() + while key.startswith("./"): + key = key[2:] + return key.lstrip("/") + + +def _select_matching_members( + entries_meta: List[Tuple[str, bool]], + requested: List[str], +) -> List[bool]: + if not requested: + return [False] * len(entries_meta) + + norm_entries: List[Tuple[str, str, bool]] = [] + known_dirs = set() + known_files = set() + for raw_name, is_dir in entries_meta: + norm = _normalize_member_key(raw_name) + stripped = norm.rstrip("/") + norm_entries.append((norm, stripped, is_dir)) + if is_dir and stripped: + known_dirs.add(stripped) + elif stripped: + known_files.add(stripped) + + exact_keys = set() + dir_prefixes = set() + for req in requested: + req_slash = req.replace("\\", "/").strip() + explicit_dir = req_slash.endswith("/") + norm_req = _normalize_member_key(req_slash).rstrip("/") + if not norm_req: + continue + exact_keys.add(norm_req) + if explicit_dir or norm_req in known_dirs: + dir_prefixes.add(norm_req) + elif norm_req not in known_files: + prefix_probe = norm_req + "/" + if any(stripped.startswith(prefix_probe) for _, stripped, _ in norm_entries): + dir_prefixes.add(norm_req) + + selected: List[bool] = [] + for _, stripped, _ in norm_entries: + if not stripped: + selected.append(False) + continue + match = stripped in exact_keys or any( + stripped == pref or stripped.startswith(pref + "/") + for pref in dir_prefixes + ) + selected.append(match) + return selected + + def sanitize_archive_member_path(member_name: str, target_dir: str | Path) -> Path: """ - Sicherheitsprüfung gegen Zip-Slip- und Tar-Slip-Schwachstellen (Directory Traversal). - Bereinigt den Pfad und stellt sicher, dass das Ziel innerhalb von target_dir liegt. + Sicherheitsprüfung gegen Zip-Slip- und Tar-Slip-Schwachstellen (Directory Traversal), + Symlink-Brücken, NTFS-ADS, leere Root-Pfade und reservierte Windows-Gerätenamen. """ + if not isinstance(member_name, str) or any(ord(ch) < 32 for ch in member_name): + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name!r}' enthält Steuerzeichen!" + ) + target_path = Path(target_dir).resolve() + normalized = member_name.replace("\\", "/") - # Vorverarbeitung: Windows-Laufwerksbuchstaben und absolute Pfade neutralisieren - normalized = member_name.replace("\\", "/").strip() - # Entferne führende Slashes oder Laufwerksbezeichner wie "C:" while normalized.startswith("/"): normalized = normalized[1:] - if len(normalized) > 1 and normalized[1] == ":": + if ( + len(normalized) >= 2 + and normalized[0].isalpha() + and normalized[1] == ":" + and (len(normalized) == 2 or normalized[2] == "/") + ): normalized = normalized[2:] while normalized.startswith("/"): normalized = normalized[1:] - # Pfadteile auf relative Segmente auflösen - dest_path = (target_path / normalized).resolve() + clean_parts: List[str] = [] + for part in normalized.split("/"): + if part in ("", "."): + continue + if part == "..": + if not clean_parts: + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' verlässt das Zielverzeichnis!" + ) + clean_parts.pop() + continue + if part.endswith((".", " ")): + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' enthält unsicheres Pfadsegment '{part}'!" + ) + if any(ch in _INVALID_MEMBER_SEGMENT_CHARS for ch in part): + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' enthält unzulässige Zeichen!" + ) + if _is_windows_reserved_segment(part): + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' verwendet reservierten Gerätenamen '{part}'!" + ) + clean_parts.append(part) + + if not clean_parts: + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' verweist auf das Wurzelverzeichnis!" + ) + + probe = target_path + for part in clean_parts: + probe = probe / part + if probe.is_symlink() or (hasattr(probe, "is_junction") and probe.is_junction()): + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' traversiert einen Symlink!" + ) + dest_path = target_path.joinpath(*clean_parts).resolve() try: dest_path.relative_to(target_path) except ValueError: @@ -122,25 +292,43 @@ def sanitize_archive_member_path(member_name: str, target_dir: str | Path) -> Pa f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' verlässt das Zielverzeichnis!" ) + if dest_path == target_path: + raise ZipSlipSecurityError( + f"Sicherheitswarnung: Archiv-Eintrag '{member_name}' kollidiert mit dem Zielverzeichnis!" + ) + return dest_path SUPPORTED_ARCHIVE_EXTENSIONS = ( - ".zip", ".tar", ".gz", ".tgz", ".bz2", ".tbz2", ".xz", ".txz" + ".zip", + ".tar", + ".tar.gz", + ".tgz", + ".tar.bz2", + ".tbz2", + ".tar.xz", + ".txz", ) def is_archive(path: str | Path) -> bool: """Prüft, ob der Pfad ein unterstütztes Archiv (ZIP oder TAR/komprimiert) ist.""" - path_obj = Path(path) - if not path_obj.is_file(): + try: + path_obj = Path(path) + if not path_obj.is_file(): + return False + except OSError: return False + name_lower = path_obj.name.lower() - if name_lower.endswith((".tar.gz", ".tar.bz2", ".tar.xz", ".tar.zst", ".tgz", ".tbz2", ".txz")): + if name_lower.endswith(SUPPORTED_ARCHIVE_EXTENSIONS): return True - if path_obj.suffix.lower() in SUPPORTED_ARCHIVE_EXTENSIONS: - return True - return zipfile.is_zipfile(path_obj) or tarfile.is_tarfile(path_obj) + + try: + return zipfile.is_zipfile(path_obj) or tarfile.is_tarfile(path_obj) + except OSError: + return False def inspect_zip(zip_path: str | Path) -> Tuple[ArchiveSummary, List[ArchiveEntry]]: @@ -382,6 +570,7 @@ def create_zip_archive( dest_path = Path(dest_zip).resolve() dest_path.parent.mkdir(parents=True, exist_ok=True) + temp_zip = _make_staging_path(dest_path) items_to_add: List[Tuple[Path, str]] = [] @@ -391,15 +580,21 @@ def create_zip_archive( continue if src_path.is_file(): - items_to_add.append((src_path, src_path.name)) + if not _is_excluded_output_artifact(src_path, dest_path, temp_zip): + items_to_add.append((src_path, src_path.name)) elif src_path.is_dir(): root_parent = src_path.parent for dirpath, dirnames, filenames in os.walk(src_path): curr_dir = Path(dirpath) - if not dirnames and not filenames: + effective_files = [ + fname + for fname in filenames + if not _is_excluded_output_artifact(curr_dir / fname, dest_path, temp_zip) + ] + if not dirnames and not effective_files: rel_dir = curr_dir.relative_to(root_parent).as_posix() + "/" items_to_add.append((curr_dir, rel_dir)) - for fname in filenames: + for fname in effective_files: fpath = curr_dir / fname rel_f = fpath.relative_to(root_parent).as_posix() items_to_add.append((fpath, rel_f)) @@ -408,7 +603,6 @@ def create_zip_archive( if total_items == 0: raise ValueError("Keine gültigen Dateien in den ausgewählten Pfaden gefunden.") - temp_zip = dest_path.with_name(f"{dest_path.name}.tmp_{int(time.time()*1000)}") added_files = 0 total_bytes = 0 @@ -429,12 +623,49 @@ def create_zip_archive( progress_callback(idx, total_items, arcname) if fpath.is_file(): - zf.write(fpath, arcname) - added_files += 1 + st = fpath.stat() + clamped_dt: Optional[Tuple[int, int, int, int, int, int]] = None try: - total_bytes += fpath.stat().st_size - except OSError: - pass + dt = time.localtime(st.st_mtime)[:6] + if dt < _ZIP_MIN_DATETIME: + clamped_dt = _ZIP_MIN_DATETIME + elif dt > _ZIP_MAX_DATETIME: + clamped_dt = _ZIP_MAX_DATETIME + except (OSError, OverflowError, ValueError): + clamped_dt = ( + _ZIP_MIN_DATETIME + if st.st_mtime < 315532800 + else _ZIP_MAX_DATETIME + ) + + if clamped_dt is None: + zf.write(fpath, arcname) + added_files += 1 + total_bytes += st.st_size + else: + clean_arc = arcname.replace("\\", "/").lstrip("/") + zinfo = zipfile.ZipInfo(filename=clean_arc, date_time=clamped_dt) + zinfo.compress_type = compression + zinfo.file_size = st.st_size + zinfo.external_attr = (st.st_mode & 0xFFFF) << 16 + if compression in (zipfile.ZIP_DEFLATED, zipfile.ZIP_BZIP2): + zinfo._compresslevel = compresslevel + written = 0 + with open(fpath, "rb") as src_f, zf.open(zinfo, "w") as dst_f: + while True: + if is_cancelled and is_cancelled(): + dst_f.close() + zf.close() + if temp_zip.exists(): + temp_zip.unlink(missing_ok=True) + return 0, 0 + chunk = src_f.read(64 * 1024) + if not chunk: + break + dst_f.write(chunk) + written += len(chunk) + added_files += 1 + total_bytes += written elif fpath.is_dir(): zinfo = zipfile.ZipInfo(arcname if arcname.endswith("/") else arcname + "/") zinfo.external_attr = 0o777 << 16 | stat.S_IFDIR @@ -442,7 +673,7 @@ def create_zip_archive( # Atomare Veröffentlichung via os.replace if temp_zip.exists(): - os.replace(temp_zip, dest_path) + _atomic_replace(temp_zip, dest_path) return added_files, total_bytes @@ -469,6 +700,7 @@ def create_tar_archive( dest_path = Path(dest_tar).resolve() dest_path.parent.mkdir(parents=True, exist_ok=True) + temp_tar = _make_staging_path(dest_path) items_to_add: List[Tuple[Path, str]] = [] for src in sources: @@ -477,7 +709,8 @@ def create_tar_archive( continue if src_path.is_file(): - items_to_add.append((src_path, src_path.name)) + if not _is_excluded_output_artifact(src_path, dest_path, temp_tar): + items_to_add.append((src_path, src_path.name)) elif src_path.is_dir(): root_parent = src_path.parent for dirpath, dirnames, filenames in os.walk(src_path): @@ -487,6 +720,8 @@ def create_tar_archive( items_to_add.append((curr_dir, rel_dir)) for fname in filenames: fpath = curr_dir / fname + if _is_excluded_output_artifact(fpath, dest_path, temp_tar): + continue rel_f = fpath.relative_to(root_parent).as_posix() items_to_add.append((fpath, rel_f)) @@ -494,7 +729,6 @@ def create_tar_archive( if total_items == 0: raise ValueError("Keine gültigen Dateien in den ausgewählten Pfaden gefunden.") - temp_tar = dest_path.with_name(f"{dest_path.name}.tmp_{int(time.time()*1000)}") added_files = 0 total_bytes = 0 @@ -519,7 +753,7 @@ def create_tar_archive( pass if temp_tar.exists(): - os.replace(temp_tar, dest_path) + _atomic_replace(temp_tar, dest_path) return added_files, total_bytes except Exception: @@ -573,8 +807,11 @@ def extract_zip_archive( with zipfile.ZipFile(path_obj, "r") as zf: infolist = zf.infolist() if members is not None: - member_set = set(members) - infolist = [info for info in infolist if info.filename in member_set] + mask = _select_matching_members( + [(info.filename, info.is_dir()) for info in infolist], + members, + ) + infolist = [info for info, keep in zip(infolist, mask) if keep] total_members = len(infolist) extracted_files = 0 @@ -587,23 +824,30 @@ def extract_zip_archive( if progress_callback: progress_callback(idx, total_members, info.filename) + if info.is_dir() and _is_harmless_root_dir_member(info.filename): + continue + dest_path = sanitize_archive_member_path(info.filename, target_path) if info.is_dir(): dest_path.mkdir(parents=True, exist_ok=True) continue + if _is_zip_special_or_symlink(info): + continue + dest_path.parent.mkdir(parents=True, exist_ok=True) if dest_path.exists() and not overwrite: continue # Atomar via temporäre Datei schreiben - temp_dest = dest_path.with_name(f"{dest_path.name}.tmp_{int(time.time()*1000)}") + temp_dest = _make_staging_path(dest_path) try: with zf.open(info, "r") as src_f, open(temp_dest, "wb") as dst_f: while True: if is_cancelled and is_cancelled(): + dst_f.close() if temp_dest.exists(): temp_dest.unlink(missing_ok=True) return extracted_files, extracted_bytes @@ -611,7 +855,7 @@ def extract_zip_archive( if not chunk: break dst_f.write(chunk) - os.replace(temp_dest, dest_path) + _atomic_replace(temp_dest, dest_path) except Exception: if temp_dest.exists(): temp_dest.unlink(missing_ok=True) @@ -653,8 +897,11 @@ def extract_tar_archive( with tarfile.open(path_obj, "r:*") as tf: all_members = tf.getmembers() if members is not None: - member_set = set(members) - all_members = [m for m in all_members if m.name in member_set] + mask = _select_matching_members( + [(m.name, m.isdir()) for m in all_members], + members, + ) + all_members = [m for m, keep in zip(all_members, mask) if keep] total_members = len(all_members) extracted_files = 0 @@ -667,6 +914,9 @@ def extract_tar_archive( if progress_callback: progress_callback(idx, total_members, member.name) + if member.isdir() and _is_harmless_root_dir_member(member.name): + continue + dest_path = sanitize_archive_member_path(member.name, target_path) if member.isdir(): @@ -681,7 +931,7 @@ def extract_tar_archive( if dest_path.exists() and not overwrite: continue - temp_dest = dest_path.with_name(f"{dest_path.name}.tmp_{int(time.time()*1000)}") + temp_dest = _make_staging_path(dest_path) f_in = tf.extractfile(member) if f_in is None: continue @@ -690,6 +940,7 @@ def extract_tar_archive( with f_in, open(temp_dest, "wb") as dst_f: while True: if is_cancelled and is_cancelled(): + dst_f.close() if temp_dest.exists(): temp_dest.unlink(missing_ok=True) return extracted_files, extracted_bytes @@ -697,7 +948,7 @@ def extract_tar_archive( if not chunk: break dst_f.write(chunk) - os.replace(temp_dest, dest_path) + _atomic_replace(temp_dest, dest_path) except Exception: if temp_dest.exists(): temp_dest.unlink(missing_ok=True) diff --git a/src/gui/archive_dialog.py b/src/gui/archive_dialog.py index 6ccf0cc..ee26ec6 100644 --- a/src/gui/archive_dialog.py +++ b/src/gui/archive_dialog.py @@ -30,13 +30,47 @@ +_ARCHIVE_COMPOUND_SUFFIXES = ( + ".tar.gz", ".tar.bz2", ".tar.xz", ".tgz", ".tbz2", ".txz", ".zip", ".tar" +) +_WINDOWS_RESERVED_STEMS = frozenset({ + "CON", "PRN", "AUX", "NUL", + *(f"COM{i}" for i in range(1, 10)), + *(f"LPT{i}" for i in range(1, 10)), + "COM¹", "COM²", "COM³", + "LPT¹", "LPT²", "LPT³", +}) +_UNSAFE_STEM_CHARS = frozenset('<>:"/\\|?*') + + def _get_archive_stem(path: str) -> str: - """Ermittelt den Stammnamen ohne Archiv-Endung (unterstützt auch .tar.gz etc.).""" - name = os.path.basename(path) - for sfx in (".tar.gz", ".tar.bz2", ".tar.xz", ".tgz", ".tbz2", ".txz", ".zip", ".tar"): - if name.lower().endswith(sfx): - return name[:-len(sfx)] - return Path(path).stem + """Ermittelt einen sicheren Unterordner-Stammnamen ohne Archiv-Endung (inkl. .tar.gz).""" + normalized_input = str(path).replace("\\", "/").rstrip("/") + name = os.path.basename(normalized_input) + lower_name = name.lower() + + raw_stem = "" + for sfx in _ARCHIVE_COMPOUND_SUFFIXES: + if lower_name.endswith(sfx): + raw_stem = name[: -len(sfx)] + break + else: + raw_stem = Path(name).stem + + cleaned_chars = [ + "_" if (ch in _UNSAFE_STEM_CHARS or ord(ch) < 32) else ch + for ch in raw_stem + ] + stem = "".join(cleaned_chars).strip().rstrip(". ") + + if not stem or stem in (".", ".."): + return "extracted_archive" + + device_key = stem.split(".", 1)[0].strip().upper() + if device_key in _WINDOWS_RESERVED_STEMS: + return f"_{stem}" + + return stem class _ArchiveWorkerShutdownGuard(QObject): @@ -237,16 +271,28 @@ def _setup_ui(self): dest_group = QGroupBox(t("Zielarchiv")) dest_layout = QHBoxLayout(dest_group) + target_base_dir = self.current_dir default_name = "Archiv.zip" if count == 1: - stem = Path(self.sources[0]).stem + src_resolved = Path(self.sources[0]).resolve() + stem = src_resolved.name if src_resolved.is_dir() else Path(self.sources[0]).stem + if not stem: + stem = "Archiv" default_name = f"{stem}.zip" + if src_resolved.is_dir() and self.current_dir: + try: + if Path(self.current_dir).resolve() == src_resolved: + parent_dir = str(src_resolved.parent) + if parent_dir and Path(parent_dir).resolve() != src_resolved: + target_base_dir = parent_dir + except OSError: + pass elif count > 1 and self.current_dir: parent_name = Path(self.current_dir).name if parent_name: default_name = f"{parent_name}.zip" - default_target = os.path.join(self.current_dir, default_name) + default_target = os.path.join(target_base_dir, default_name) self.dest_edit = QLineEdit(default_target) self.dest_edit.setAccessibleName(t("Zielpfad für das ZIP-Archiv")) @@ -543,7 +589,11 @@ def _start_extraction(self): final_target = base_dir if self.subfolder_cb.isChecked(): stem = _get_archive_stem(self.zip_path) - final_target = os.path.join(base_dir, stem) + base_resolved = Path(base_dir).resolve() + candidate = (base_resolved / stem).resolve() + if candidate == base_resolved or not candidate.is_relative_to(base_resolved): + candidate = base_resolved / "extracted_archive" + final_target = str(candidate) self.extracted_target = final_target overwrite = self.overwrite_cb.isChecked() diff --git a/tests/test_bugsweep_archive_security_and_resilience_20261118.py b/tests/test_bugsweep_archive_security_and_resilience_20261118.py new file mode 100644 index 0000000..bb1113a --- /dev/null +++ b/tests/test_bugsweep_archive_security_and_resilience_20261118.py @@ -0,0 +1,451 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +test_bugsweep_archive_security_and_resilience_20261118.py +========================================================= +Hermetische Regressionstests für den Bugsweep-Lauf 2026-11-18: +1. Unterordner-Stammnamen-Traversal & Windows-Gerätenamen in _get_archive_stem +2. Pfad-Sanitisierung gegen leere/Root-Member, Windows-Gerätenamen, NTFS-ADS & Symlinks +3. Selbst-Einschluss-Schutz, Prä-1980-Zeitstempel & Read-Only-Overwrite bei Archiven +4. Selektive Ordner-Extraktion (members=["folder/"]) in ZIP, TAR & ArchiveViewerDialog +5. Präzise Einzelstream-Abgrenzung (.gz/.bz2/.xz/.tar.zst) in is_archive & PreviewPanel +""" + +import bz2 +import gzip +import io +import lzma +import os +from pathlib import Path +import stat +import tarfile +import time +import zipfile + +import pytest +from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox + +from core.archive_service import ( + ZipSlipSecurityError, + check_zip_integrity, + create_tar_archive, + create_zip_archive, + extract_tar_archive, + extract_zip_archive, + inspect_tar, + inspect_zip, + is_archive, + sanitize_archive_member_path, +) +from gui.archive_dialog import ( + ArchiveCompressDialog, + ArchiveExtractDialog, + ArchiveViewerDialog, + _get_archive_stem, +) + + +@pytest.fixture +def qapp_guard(): + app = QApplication.instance() or QApplication([]) + prev = app.quitOnLastWindowClosed() + app.setQuitOnLastWindowClosed(False) + try: + yield app + finally: + app.processEvents() + app.setQuitOnLastWindowClosed(prev) + + +def _wait_until(predicate, timeout_s: float = 5.0) -> None: + app = QApplication.instance() + deadline = time.monotonic() + timeout_s + while time.monotonic() < deadline: + if app is not None: + app.processEvents() + if predicate(): + if app is not None: + app.processEvents() + return + time.sleep(0.02) + assert predicate(), "Timeout while waiting for asynchronous archive operation" + + +def test_get_archive_stem_blocks_traversal_empty_and_windows_devices(tmp_path: Path): + base = tmp_path / "downloads" + base.mkdir() + + for dangerous_name in ("..tar.gz", "...tar.gz", "...zip", "..zip", ".zip", ".tar.gz", "....tar.xz"): + stem = _get_archive_stem(dangerous_name) + assert stem not in ("", ".", ".."), f"Unsafe stem {stem!r} for {dangerous_name!r}" + resolved = (base / stem).resolve() + assert resolved.parent == base.resolve() + + for dev_archive in ("CON.zip", "nul.tar.gz", "COM1.tgz", "lpt9.tar.xz", "prn.tar"): + dev_stem = _get_archive_stem(dev_archive) + assert dev_stem.upper() not in {"CON", "NUL", "COM1", "LPT9", "PRN"} + assert (base / dev_stem).resolve().parent == base.resolve() + + assert not _get_archive_stem("report..zip").endswith(".") + assert _get_archive_stem("my_backup.zip") == "my_backup" + assert _get_archive_stem("archive.tar.gz") == "archive" + assert _get_archive_stem("document.pdf") == "document" + + +def test_extract_dialog_and_browser_keep_crafted_archive_inside_base_dir( + tmp_path: Path, qapp_guard, monkeypatch +): + monkeypatch.setattr( + QMessageBox, + "information", + lambda *args, **kwargs: QMessageBox.StandardButton.Ok, + ) + monkeypatch.setattr( + QMessageBox, + "critical", + lambda *args, **kwargs: QMessageBox.StandardButton.Ok, + ) + + victim = tmp_path / "victim.txt" + victim.write_text("KEEP_ORIGINAL", encoding="utf-8") + + downloads = tmp_path / "downloads" + downloads.mkdir() + crafted_tar = downloads / "...tar.gz" + + payload_src = tmp_path / "payload_src.txt" + payload_src.write_text("OVERWRITTEN_BY_ARCHIVE", encoding="utf-8") + with tarfile.open(crafted_tar, "w:gz") as tf: + tf.add(str(payload_src), arcname="victim.txt") + + dlg = ArchiveExtractDialog(str(crafted_tar), default_target_dir=str(downloads)) + try: + assert dlg.subfolder_cb.isChecked() + dlg._start_extraction() + assert dlg.worker is not None + _wait_until(lambda: not dlg.worker.isRunning()) + + assert Path(dlg.extracted_target).resolve().parent == downloads.resolve() + assert victim.read_text(encoding="utf-8") == "KEEP_ORIGINAL" + assert ( + Path(dlg.extracted_target) / "victim.txt" + ).read_text(encoding="utf-8") == "OVERWRITTEN_BY_ARCHIVE" + finally: + dlg.close() + + from gui.browser.file_browser import FileBrowser + + downloads2 = tmp_path / "downloads2" + downloads2.mkdir() + victim2 = tmp_path / "victim2.txt" + victim2.write_text("SAFE_PARENT", encoding="utf-8") + crafted_zip = downloads2 / "...zip" + with zipfile.ZipFile(crafted_zip, "w") as zf: + zf.writestr("victim2.txt", "EVIL") + + browser = FileBrowser() + try: + browser._current_path = str(downloads2) + browser.refresh = lambda: None + browser._extract_archive_here(str(crafted_zip)) + _wait_until(lambda: not browser._archive_task_dialogs) + finally: + browser.close() + + assert victim2.read_text(encoding="utf-8") == "SAFE_PARENT" + assert (downloads2 / "extracted_archive" / "victim2.txt").read_text(encoding="utf-8") == "EVIL" + + +def test_sanitize_archive_member_path_rejects_empty_root_ads_and_devices(tmp_path: Path): + target = tmp_path / "jail" + target.mkdir() + + invalid_members = [ + "", + ".", + "./", + "/", + "C:/", + " ", + "a:stream", + "doc.txt:evil_stream", + "sub/file.txt:$DATA", + "CON.txt", + "sub/NUL", + "COM1.log", + "LPT2", + "bad\x00name.txt", + "trailing./file.txt", + "folder/bad ", + ] + for bad in invalid_members: + with pytest.raises(ZipSlipSecurityError, match="."): + sanitize_archive_member_path(bad, target) + + good = sanitize_archive_member_path("./folder/sub/doc.txt", target) + assert good == (target / "folder" / "sub" / "doc.txt").resolve() + + +def test_extract_blocks_symlink_escape_and_skips_zip_symlink_entries( + tmp_path: Path, monkeypatch +): + # 1. ZIP mit Unix-Symlink-Eintrag (S_IFLNK) darf keinen Symlink/Payload extrahieren + zip_with_link = tmp_path / "with_symlink.zip" + with zipfile.ZipFile(zip_with_link, "w") as zf: + link_info = zipfile.ZipInfo("passwd_link") + link_info.create_system = 3 # Unix + link_info.external_attr = (stat.S_IFLNK | 0o777) << 16 + zf.writestr(link_info, "/etc/passwd") + zf.writestr("regular.txt", "safe content") + + out_zip_dir = tmp_path / "out_zip" + count, _ = extract_zip_archive(zip_with_link, out_zip_dir) + assert count == 1 + assert (out_zip_dir / "regular.txt").read_text(encoding="utf-8") == "safe content" + assert not (out_zip_dir / "passwd_link").exists() + + # 2. Symlink-Komponente unterhalb von target_dir muss blockiert werden + jail = tmp_path / "symlink_jail" + jail.mkdir() + escape_sub = jail / "linked_sub" + escape_sub.mkdir() + real_is_symlink = Path.is_symlink + monkeypatch.setattr( + Path, + "is_symlink", + lambda self: True if self == escape_sub.resolve() or self.name == "linked_sub" else real_is_symlink(self), + ) + with pytest.raises(ZipSlipSecurityError): + sanitize_archive_member_path("linked_sub/pwned.txt", jail) + monkeypatch.undo() + + # 3. Legitimes tar -C dir . Archiv mit "./" Verzeichnis-Header funktioniert, + # aber reguläre Datei namens "." wird mit ZipSlipSecurityError abgewiesen. + dot_dir_tar = tmp_path / "dot_dir.tar" + with tarfile.open(dot_dir_tar, "w") as tf: + d_info = tarfile.TarInfo(name="./") + d_info.type = tarfile.DIRTYPE + d_info.mtime = time.time() + tf.addfile(d_info) + + payload = b"hello from dot tar" + f_info = tarfile.TarInfo(name="./hello.txt") + f_info.size = len(payload) + f_info.mtime = time.time() + tf.addfile(f_info, io.BytesIO(payload)) + + out_dot = tmp_path / "out_dot" + c_dot, _ = extract_tar_archive(dot_dir_tar, out_dot) + assert c_dot == 1 + assert (out_dot / "hello.txt").read_bytes() == b"hello from dot tar" + + bad_root_file_tar = tmp_path / "bad_root_file.tar" + with tarfile.open(bad_root_file_tar, "w") as tf: + payload = b"overwrite root" + f_info = tarfile.TarInfo(name=".") + f_info.type = tarfile.REGTYPE + f_info.size = len(payload) + f_info.mtime = time.time() + tf.addfile(f_info, io.BytesIO(payload)) + + with pytest.raises(ZipSlipSecurityError): + extract_tar_archive(bad_root_file_tar, tmp_path / "out_bad_root") + + +def test_create_zip_and_tar_exclude_destination_self_inclusion_and_staging( + tmp_path: Path, qapp_guard +): + project_dir = tmp_path / "project" + project_dir.mkdir() + (project_dir / "alpha.txt").write_text("alpha payload", encoding="utf-8") + + in_place_zip = project_dir / "bundle.zip" + c1, _ = create_zip_archive([project_dir], in_place_zip) + assert c1 == 1 + c2, _ = create_zip_archive([project_dir], in_place_zip) + assert c2 == 1 + _, zip_entries = inspect_zip(in_place_zip) + zip_names = [e.filename for e in zip_entries] + assert zip_names == ["project/alpha.txt"] + + in_place_zip.unlink() + + in_place_tar = project_dir / "bundle.tar.gz" + t1, _ = create_tar_archive([project_dir], in_place_tar, mode="w:gz") + assert t1 == 1 + t2, _ = create_tar_archive([project_dir], in_place_tar, mode="w:gz") + assert t2 == 1 + _, tar_entries = inspect_tar(in_place_tar) + tar_file_names = [e.filename for e in tar_entries if not e.is_dir] + assert tar_file_names == ["project/alpha.txt"] + + dlg = ArchiveCompressDialog([str(project_dir)], current_dir=str(project_dir)) + try: + default_dest = Path(dlg.dest_edit.text()).resolve() + assert default_dest.parent == project_dir.parent.resolve() + finally: + dlg.close() + + +def test_create_zip_archive_clamps_pre_1980_timestamps_and_overwrites_readonly_dest( + tmp_path: Path, +): + epoch_file = tmp_path / "epoch_1970.txt" + epoch_file.write_text("UNIX epoch 1970 content", encoding="utf-8") + os.utime(epoch_file, (86400, 86400)) + + pre_1970_file = tmp_path / "pre_1970.txt" + pre_1970_file.write_text("Pre-1970 negative mtime", encoding="utf-8") + try: + os.utime(pre_1970_file, (-157766400, -157766400)) + except (OSError, OverflowError, ValueError): + os.utime(pre_1970_file, (0, 0)) + + future_2150_file = tmp_path / "future_2150.txt" + future_2150_file.write_text("Post-2107 future mtime", encoding="utf-8") + try: + os.utime(future_2150_file, (5680281600, 5680281600)) + has_future = True + except (OSError, OverflowError, ValueError): + has_future = False + + sources = [epoch_file, pre_1970_file] + ([future_2150_file] if has_future else []) + dest_zip = tmp_path / "epoch.zip" + count, total_bytes = create_zip_archive(sources, dest_zip) + assert count == len(sources) + assert total_bytes > 0 + + ok, err = check_zip_integrity(dest_zip) + assert ok is True + assert err is None + + _, entries = inspect_zip(dest_zip) + by_name = {e.filename: e for e in entries} + assert by_name["epoch_1970.txt"].date_time == (1980, 1, 1, 0, 0, 0) + assert by_name["pre_1970.txt"].date_time == (1980, 1, 1, 0, 0, 0) + if has_future: + assert by_name["future_2150.txt"].date_time == (2107, 12, 31, 23, 59, 58) + + os.chmod(dest_zip, stat.S_IREAD) + try: + count2, _ = create_zip_archive([epoch_file], dest_zip) + assert count2 == 1 + + out_dir = tmp_path / "extracted_ro" + extract_zip_archive(dest_zip, out_dir) + extracted_file = out_dir / "epoch_1970.txt" + assert extracted_file.read_text(encoding="utf-8") == "UNIX epoch 1970 content" + + os.chmod(extracted_file, stat.S_IREAD) + count3, _ = extract_zip_archive(dest_zip, out_dir, overwrite=True) + assert count3 == 1 + finally: + if dest_zip.exists(): + os.chmod(dest_zip, stat.S_IWRITE | stat.S_IREAD) + ro_target = tmp_path / "extracted_ro" / "epoch_1970.txt" + if ro_target.exists(): + os.chmod(ro_target, stat.S_IWRITE | stat.S_IREAD) + + +def test_selective_folder_extraction_in_zip_tar_and_viewer_dialog( + tmp_path: Path, qapp_guard, monkeypatch +): + src_root = tmp_path / "workspace" + nested = src_root / "nested" / "sub" + nested.mkdir(parents=True) + (src_root / "nested" / "a.txt").write_text("file A", encoding="utf-8") + (nested / "b.txt").write_text("file B", encoding="utf-8") + other = src_root / "other" + other.mkdir() + (other / "c.txt").write_text("file C", encoding="utf-8") + + zip_path = tmp_path / "folders.zip" + with zipfile.ZipFile(zip_path, "w") as zf: + zf.writestr("nested/", "") + zf.writestr("nested/a.txt", "file A") + zf.writestr("nested/sub/b.txt", "file B") + zf.writestr("nested2/intruder.txt", "do not match prefix neighbor") + zf.writestr("other/", "") + zf.writestr("other/c.txt", "file C") + + out_zip_sel = tmp_path / "out_zip_sel" + c_zip, _ = extract_zip_archive(zip_path, out_zip_sel, members=["nested/"]) + assert c_zip == 2 + assert (out_zip_sel / "nested" / "a.txt").read_text(encoding="utf-8") == "file A" + assert (out_zip_sel / "nested" / "sub" / "b.txt").read_text(encoding="utf-8") == "file B" + assert not (out_zip_sel / "other" / "c.txt").exists() + assert not (out_zip_sel / "nested2" / "intruder.txt").exists() + + tar_path = tmp_path / "folders.tar.gz" + create_tar_archive([src_root / "nested", src_root / "other"], tar_path, mode="w:gz") + + out_tar_sel = tmp_path / "out_tar_sel" + c_tar, _ = extract_tar_archive(tar_path, out_tar_sel, members=["nested"]) + assert c_tar == 2 + assert (out_tar_sel / "nested" / "a.txt").read_text(encoding="utf-8") == "file A" + assert (out_tar_sel / "nested" / "sub" / "b.txt").read_text(encoding="utf-8") == "file B" + assert not (out_tar_sel / "other" / "c.txt").exists() + + viewer_out = tmp_path / "viewer_out" + monkeypatch.setattr( + QFileDialog, + "getExistingDirectory", + lambda *args, **kwargs: str(viewer_out), + ) + monkeypatch.setattr( + QMessageBox, + "information", + lambda *args, **kwargs: QMessageBox.StandardButton.Ok, + ) + + dlg = ArchiveViewerDialog(str(zip_path)) + try: + dlg.table.selectRow(0) + dlg._extract_selected() + assert dlg.extract_worker is not None + _wait_until(lambda: not dlg.extract_worker.isRunning()) + finally: + dlg.close() + + assert (viewer_out / "nested" / "a.txt").is_file() + assert (viewer_out / "nested" / "sub" / "b.txt").is_file() + assert not (viewer_out / "other" / "c.txt").exists() + + +def test_is_archive_rejects_standalone_compressed_streams_and_unsupported_zst( + tmp_path: Path, qapp_guard +): + gz_single = tmp_path / "server.log.gz" + gz_single.write_bytes(gzip.compress(b"2026-11-18 INFO booted\n")) + + bz2_single = tmp_path / "dump.sql.bz2" + bz2_single.write_bytes(bz2.compress(b"SELECT 1;\n")) + + xz_single = tmp_path / "metrics.json.xz" + xz_single.write_bytes(lzma.compress(b'{"ok": true}\n')) + + zst_unsupported = tmp_path / "backup.tar.zst" + zst_unsupported.write_bytes(b"\x28\xb5\x2f\xfd\x00\x58\x01\x00\x00") + + assert is_archive(gz_single) is False + assert is_archive(bz2_single) is False + assert is_archive(xz_single) is False + assert is_archive(zst_unsupported) is False + + disguised_tgz = tmp_path / "tarball_named_only.gz" + src_f = tmp_path / "note.txt" + src_f.write_text("inside tar", encoding="utf-8") + with tarfile.open(disguised_tgz, "w:gz") as tf: + tf.add(str(src_f), arcname="note.txt") + assert is_archive(disguised_tgz) is True + + from gui.preview.preview_panel import PreviewPanel + + panel = PreviewPanel() + try: + panel.show_preview(str(gz_single)) + assert panel.preview_stack.currentIndex() != 7 + panel.show_preview(str(disguised_tgz)) + assert panel.preview_stack.currentIndex() == 7 + finally: + panel.close()