T09 · Insecure Skill Coding Practices
- Location
scripts/backup_toolkit.py:360- Finding
Unsafe Archive Extraction Permits Writes Outside the Restore Destination
- Content
View full analysis
Vulnerability Details
File Location:
scripts/backup_toolkit.py, lines 360-363
Vulnerability Type: Path traversal during archive extraction
Risk Level: HighVulnerable Code:
python if backup_path.endswith('.tar.gz'): with tarfile.open(backup_path, "r:gz") as tar: tar.extractall(destination) result['files_restored'] = len(tar.getmembers())Technical Analysis
The restore function extracts every member of a supplied TAR archive without validating member paths or link targets. A malicious archive may contain entries using absolute paths,
../traversal components, or unsafe symbolic or hard links.On Python versions or configurations where a safe extraction filter is not automatically enforced,
tar.extractall(destination)may write such entries outside the requested destination. Merely creating the destination directory does not constrain archive member paths to that directory.Attack Path
- An attacker creates a
.tar.gzarchive containing a member such as../../target-file. - The archive is provided to a user or automation process as a legitimate backup.
- The victim runs:
bash python scripts/backup_toolkit.py restore \ --backup malicious.tar.gz \ --dest /intended/restore tar.extractall()processes the unvalidated member.- The member is written outside
/intended/restoreif the target location is writable by the process.
Impact Assessment
An attacker can create or overwrite files accessible to the account running the restore operation. The scope is limited by that account's operating-system permissions, but it may include application configuration, user startup files, scripts, data files, or other backups. If the tool is run by a privileged account, the impact expands to privileged filesystem locations and may lead to code execution through overwritten executable or configuration files.
- An attacker creates a
- Remediation
View remediation
Remediation Suggestions
- Resolve and normalize every archive member path before extraction.
- Verify that each resolved output path remains within the resolved destination directory.
- Reject absolute member names, traversal components, device files, and unsafe symbolic or hard links.
- On supported Python versions, use the
dataextraction filter, while retaining explicit containment checks for compatibility and defense in depth. - Extract into a newly created staging directory with restrictive permissions before moving validated files to the final destination.
- Add tests using absolute paths,
../entries, symbolic links, hard links, and special-file members.
Example containment logic:
python destination_real = os.path.realpath(destination) for member in tar.getmembers(): target = os.path.realpath(os.path.join(destination_real, member.name)) if os.path.commonpath([destination_real, target]) != destination_real: raise ValueError(f"Unsafe archive member: {member.name}") tar.extractall(destination_real, filter="data")
