T09 · Insecure Skill Coding Practices
- Location
SKILL.md:76- Finding
Destination Filename Collision May Cause Existing File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
SKILL.md, lines 76–85
Vulnerability Type: Unsafe file move and destination collision handling
Risk Level: Mediumpython if category: cat_dir = os.path.join(target, f"_{category}") if not dry_run: os.makedirs(cat_dir, exist_ok=True) try: shutil.move(fpath, os.path.join(cat_dir, fname)) report["organized"] += 1 report["by_category"][category] = report["by_category"].get(category, 0) + 1 except (shutil.Error, PermissionError) as e: report["errors"].append(f"移动失败: {fname} -> {e}")Technical Analysis
The code moves each classified file to a destination derived directly from its original filename without first checking whether that destination already exists. The accompanying safety documentation claims that
shutil.movealways raises an error on a same-name conflict and preserves the existing file, but this behavior is not portable.Depending on the operating system, filesystem, and move implementation selected by Python, the underlying rename operation may replace an existing destination file. In particular, a same-filesystem rename can overwrite the destination on platforms where that behavior is supported. Consequently, the implementation does not enforce its stated no-overwrite guarantee.
The
tryblock does not prevent this condition because an overwrite can succeed without raisingshutil.ErrororPermissionError. The code also omits an explicit collision policy, unique-name generation, backup creation, and atomic no-replace semantics.Attack Path
- A file such as
Downloads/report.pdfexists and is selected for classification. - The destination directory
Downloads/_Documentsalready contains a different file namedreport.pdf. - The user invokes
organize(dry_run=False). - The code constructs
Downloads/_Documents/report.pdfas the destination w ...[truncated 941 chars]
- A file such as
- Remediation
View remediation
Remediation Suggestions
- Construct the destination path before moving and explicitly test it with
os.path.lexists(destination). - Adopt a documented collision policy: skip the source file, generate a unique destination filename, or require explicit user confirmation before replacement.
- Prefer atomic no-replace filesystem operations where supported to avoid a time-of-check/time-of-use race between the existence check and move.
- If unique names are generated, use deterministic suffixes such as
report (1).pdfand repeat the check until an unused path is reserved safely. - Catch relevant
OSErrorsubclasses and report collisions separately from permission or filesystem failures. - Do not count a file as organized unless the move completes under the selected collision policy.
- Update the safety documentation so it does not claim that
shutil.moveuniversally preserves existing destination files. - Add tests covering same-name collisions on every supported operating system and filesystem.
- Construct the destination path before moving and explicitly test it with
