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
This commit is contained in:
Jarian Cottingham 2026-07-05 07:43:52 +00:00
parent 8e7f5bc920
commit 69dfdce4f5

View File

@ -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"