T09 · Insecure Skill Coding Practices
- Location
scripts/extract_pkg.py:32- Finding
Arbitrary File Write Through Archive Path Traversal
- Content
View full analysis
Vulnerability Details
File Location:
scripts/extract_pkg.py, lines 32-55
Vulnerability Type: Archive extraction path traversal
Risk Level: HighVulnerable Code
python for i in range(num_entries): name_len = struct.unpack_from('<I', data, pos)[0]; pos += 4 name = data[pos:pos + name_len].decode('ascii', errors='replace'); pos += name_len # NO alignment padding after filename in PKGV0023 offset = struct.unpack_from('<I', data, pos)[0]; pos += 4 size = struct.unpack_from('<I', data, pos)[0]; pos += 4 entries.append((name, offset, size)) print(f' [{i}] {name} offset={offset} size={size} ({size / 1024:.1f} KB)') data_start = pos print(f'\nData section at file offset: {data_start}') # --- Extract all entry files --- os.makedirs(out_dir, exist_ok=True) tex_files = [] for name, offset, size in entries: file_offset = data_start + offset file_data = data[file_offset:file_offset + size] out_path = os.path.join(out_dir, name) os.makedirs(os.path.dirname(out_path), exist_ok=True) with open(out_path, 'wb') as f: f.write(file_data)Technical Analysis
The filename assigned to
nameis read directly from attacker-controlled package metadata. The extractor then passes that value toos.path.join(out_dir, name)and writes package data to the resulting path without validating that its canonical destination remains insideout_dir.os.path.join()does not enforce directory confinement. A filename containing parent-directory components such as../../targetcan escape the extraction directory. An absolute filename may also cause the output directory component to be discarded. Platform-specific absolute paths, drive-qualified paths, and path separators can create equivalent conditions.Because the code also invokes
os.makedirs()for the attacker-selected parent path, it can create missing directories before w ...[truncated 1598 chars]- Remediation
View remediation
Remediation Suggestions
Canonicalize the output directory and each candidate destination before creating directories or writing data. Reject absolute paths, drive-qualified paths, empty filenames, and entries containing
.or..path components. Confirm confinement withos.path.commonpath()rather than relying on string-prefix comparisons.A hardened implementation should follow this pattern:
python base_dir = os.path.realpath(out_dir) os.makedirs(base_dir, exist_ok=True) normalized_name = name.replace('\\', '/') if ( not normalized_name or os.path.isabs(name) or os.path.splitdrive(name)[0] or any(part in ('', '.', '..') for part in normalized_name.split('/')) ): raise ValueError(f'Unsafe package entry path: {name!r}') out_path = os.path.realpath(os.path.join(base_dir, *normalized_name.split('/'))) if os.path.commonpath([base_dir, out_path]) != base_dir: raise ValueError(f'Package entry escapes output directory: {name!r}') parent = os.path.dirname(out_path) os.makedirs(parent, exist_ok=True) # Recheck containment and account for symlink races before writing. if os.path.commonpath([base_dir, os.path.realpath(parent)]) != base_dir: raise ValueError(f'Unsafe package entry parent: {name!r}') with open(out_path, 'xb') as f: f.write(file_data)For stronger protection, extract into a newly created private directory, refuse symbolic links in every path component, avoid overwriting existing files, and use directory-relative file operations with no-follow semantics where the operating system supports them. Add automated tests covering
../, absolute paths, mixed separators, drive-qualified paths, nested traversal, symlink escapes, and ordinary valid nested entries.
