T05 · Unauthorized Access and Privilege Escalation
- Location
scripts/scan.py:86- Finding
Symlink Following Allows Files Outside the Scan Target to Be Uploaded
- Content
View full analysis
Vulnerability Details
File Location:
scripts/scan.py:86-104; resulting archive upload atscripts/scan.py:197-251
Vulnerability Type: Improper symlink handling and unauthorized local-file disclosure
Risk Level: HighVulnerable Code:
python with zipfile.ZipFile(temp_zip.name, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(dir_path): for file in files: if file.startswith('.') or file.startswith('._'): continue file_path = os.path.join(root, file) arcname = os.path.relpath(file_path, dir_path) if arcname.lower() == "skill.md": arcname = "skill.md" elif arcname.lower().endswith("/skill.md"): arcname = arcname[:-len("/skill.md")] + "/skill.md" while arcname.startswith('./') or arcname.startswith('/'): arcname = arcname.lstrip('./') zipf.write(file_path, arcname)The resulting archive is read and transmitted here:
python with open(file_to_upload, 'rb') as f: file_content = f.read() files = { 'File': (os.path.basename(file_to_upload), file_content, 'application/zip') }Technical Analysis
The directory-packaging routine does not reject symbolic links and does not resolve each file path before verifying that it remains inside the requested scan directory.
zipfile.ZipFile.write()follows a symbolic link to a file and archives the target's contents.Consequently, an untrusted Skill directory can contain a symlink whose apparent path is inside the Skill but whose target is an arbitrary file readable by the scanner process. The target file is incorporated into the generated ZIP and uploaded to the configured scanning service.
This exceeds least privilege because scanning a selected directory should grant access only to regular files contained within that directory, not to ...[truncated 1236 chars]
- Remediation
View remediation
Remediation Suggestions
- Reject symbolic links for both files and directories before adding entries to the archive.
- Resolve the target directory and every candidate path with
os.path.realpath(). - Enforce containment with
os.path.commonpath()before opening or archiving a file. - Use
os.lstat()and require regular files rather than relying on path-based checks that follow links. - Consider opening files with
O_NOFOLLOWon platforms that support it to reduce time-of-check/time-of-use risks. - Abort the scan and report the offending path whenever a symlink or path escaping the target directory is found.
- Add tests covering file symlinks, directory symlinks, nested links, broken links, and links changed concurrently.
Example containment logic:
python root_real = os.path.realpath(dir_path) file_real = os.path.realpath(file_path) if os.path.islink(file_path): raise ValueError(f"Symbolic links are not permitted: {file_path}") if os.path.commonpath([root_real, file_real]) != root_real: raise ValueError(f"Path escapes the scan directory: {file_path}") if not os.path.isfile(file_real): continue
