T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/organize_desktop.py:37
- Finding
- Existing Destination Files May Be Silently Overwritten## Vulnerability Details **File Location**: `scripts/organize_desktop.py`, lines 37–43 **Vulnerability Type**: Unsafe file move and silent exception handling **Risk Level**: Medium ### Vulnerable Code ```python dest = dest_dir / item.name try: shutil.move(str(item), str(dest)) moved[target_folder] += 1 except Exception: # skip locked or move errors pass ``` ### Technical Analysis The script constructs the destination path from the original filename but does not check whether that path already exists before calling `shutil.move`. On platforms where the underlying rename operation replaces an existing file, a file such as `Desktop/report.pdf` can overwrite an existing `Desktop/PDFs/report.pdf`. Behavior can vary by operating system and filesystem. Some environments may reject the operation instead, but the broad `except Exception` handler silently suppresses that failure. Consequently, the user is not informed about filename collisions, permission errors, locked files, or other move failures. The aggregate result counters alone cannot distinguish ignored files from failed operations. ### Attack Path 1. A valuable file already exists at a categorized destination, such as `Desktop/PDFs/report.pdf`. 2. A same-named file is placed at `Desktop/report.pdf`, either accidentally or by an attacker who can create files on the user's desktop. 3. The user invokes the Desktop Organizer Skill. 4. The script calculates `Desktop/PDFs/report.pdf` as the destination without checking for a collision. 5. On a platform permitting replacement, the existing destination file is overwritten. On a platform rejecting the move, the exception is silently discarded and the user receives no failure details. ### Impact Assessment The issue can cause loss or replacement of files within the desktop category folders. It does not provide privilege escalation, persistence, arbitrary code execution, or access beyond the permissions of the user ru ...[truncated 359 chars]
- Remediation
- ## Remediation Suggestions 1. Check `dest.exists()` before invoking `shutil.move`. 2. Apply an explicit collision policy: - Skip the move and report the collision; - Generate a unique filename such as `report (1).pdf`; or - Require explicit user confirmation before replacing an existing file. 3. Prefer a non-destructive default and never overwrite an existing destination silently. 4. Replace `except Exception` with specific exception handling, such as `PermissionError`, `FileNotFoundError`, and relevant `OSError` cases. 5. Record and report every skipped or failed move, including its source, intended destination, and failure reason. 6. Consider rechecking destination existence immediately before the move and using platform-appropriate exclusive or atomic operations where race conditions are a concern.
