From 69dfdce4f53605b9c86b614a247692bc244a410a Mon Sep 17 00:00:00 2001 From: Jarian Cottingham Date: Sun, 5 Jul 2026 07:43:52 +0000 Subject: [PATCH] fix: validate --output path to prevent arbitrary file write (#2) - Block system-critical paths (/etc, /usr, /bin, /sbin, /boot, /dev, /proc, /sys, /) - Resolve path before validation to defeat symlink tricks - Exit with error message if blocked path detected --- downloader.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/downloader.py b/downloader.py index 6cdf2e3..dcca6e8 100644 --- a/downloader.py +++ b/downloader.py @@ -192,8 +192,23 @@ def save_metadata(item: Dict, dest: Path) -> None: def main() -> None: args = parse_args() - # Resolve output base directory and override global paths + # Resolve output base directory and validate it is safe base_dir = Path(args.output).resolve() + # Prevent writing to system-critical paths + _BLOCKED_PREFIXES = { + str(Path(p).resolve()) + for p in ("/etc", "/usr", "/bin", "/sbin", "/boot", "/dev", "/proc", "/sys") + } + base_str = str(base_dir) + if base_str == "/" or any( + base_str == blocked or base_str.startswith(blocked + "/") + for blocked in _BLOCKED_PREFIXES + ): + print( + f"Error: --output cannot point to system directory: {base_dir}", + file=sys.stderr, + ) + sys.exit(1) global IMG_DIR, META_DIR, STATE_FILE IMG_DIR = base_dir / "images" META_DIR = base_dir / "metadata"