T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/backup.py:58
- Finding
- Unsafe Tar Archive Extraction Allows Writes Outside the Restore Destination<![CDATA[ ## Vulnerability Details **File Location**: `scripts/backup.py`, lines 58–78 **Vulnerability Type**: Unsafe archive extraction **Risk Level**: High ### Vulnerable Code ```python def restore_backup(backup_file, destination, verbose=False): """Restore from a backup archive.""" dest_path = Path(destination) backup_path = Path(backup_file) if not backup_path.exists(): print(f"Error: {backup_file} does not exist") return 1 print(f"Restoring from: {backup_file}") print(f"Destination: {dest_path}") try: dest_path.mkdir(parents=True, exist_ok=True) with tarfile.open(backup_path, 'r:*') as tar: tar.extractall(dest_path) print(f"Restore complete: {destination}") return 0 except Exception as e: print(f"Error restoring backup: {e}") return 1 ``` ### Technical Analysis The restore operation passes every archive member directly to `tar.extractall()` without explicitly selecting a safe extraction filter or independently validating member paths and link targets. On Python versions where restrictive filtering is not the default, a malicious tar archive can contain absolute paths, `..` path traversal components, symbolic links, hard links, or special entries. Such members may escape the intended destination and cause files to be created or overwritten elsewhere. Checking that the archive itself exists does not establish that its contents are trustworthy. Creating the destination directory also does not constrain archive members to that directory. ### Attack Path 1. An attacker creates a tar archive containing a member such as `../../.config/application/startup.conf`, an absolute path, or a link that redirects a subsequent extraction outside the destination. 2. The attacker supplies the archive to a user or places it where the user will restore it. 3. The user runs the documented restore operation with `--restore` and `--des ...[truncated 981 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Use the safest supported `tarfile` extraction filter explicitly rather than depending on runtime defaults. 2. Before extraction, resolve and validate every archive member path and ensure it remains beneath the resolved destination directory. 3. Reject absolute paths, parent-directory traversal, device entries, FIFOs, and other special file types that are unnecessary for backups. 4. Validate symbolic-link and hard-link targets, or reject links entirely unless they are required. 5. Avoid restoring untrusted archives with elevated privileges. 6. Add regression tests containing absolute paths, `../` traversal, symbolic-link escapes, hard-link escapes, and special entries. 7. Consider extracting into a newly created restricted staging directory and moving validated content to the final destination afterward. ]]>
