T09 · Insecure Skill Coding Practices
- Location
scripts/organize_inbox.py:145- Finding
Archive Name Collision Can Overwrite Existing Files
- Content
View full analysis
Vulnerability Details
File Location:
scripts/organize_inbox.py, lines 145–153
Vulnerability Type: Unsafe destination handling during archival
Risk Level: MediumVulnerable Code
python for item in scratch.iterdir(): if item.is_file(): mtime = datetime.fromtimestamp(item.stat().st_mtime) if mtime < cutoff: results["cleaned"].append(str(item)) if not dry_run: # Move into archives archives = root / "archives" archives.mkdir(exist_ok=True) shutil.move(str(item), str(archives / item.name))Technical Analysis
The archival operation constructs its destination directly from the source filename and calls
shutil.movewithout checking whether that destination already exists:python archives / item.nameOn platforms and filesystem configurations where moving to an existing file replaces that file, an old item from
scratch/can overwrite a same-named item already stored inarchives/. This is inconsistent with the Skill's documented safety guarantees that files should not be overwritten or deleted and that archived versions should remain recoverable.The inbox organization routine contains collision-safe renaming logic, but the old-temporary-file archival routine does not reuse it. The issue can be triggered through normal manual execution or through the optional scheduled organization workflow.
Attack Path
- A file already exists at
archives/report.txt. - A different file named
report.txtis placed inscratch/. - The scratch file's modification time becomes older than the configured retention threshold.
- The user or an approved scheduled task executes
organize_inbox.py. clean_old_temp()calculates the destination asarchives/report.txt.shutil.move()may replace the existing archived file without preserving the previous versio ...[truncated 628 chars]
- A file already exists at
- Remediation
View remediation
Remediation Suggestions
- Check whether the archive destination exists before moving the source file.
- Reuse the collision-safe naming strategy already implemented for inbox organization, producing names such as
report_01.txt. - Perform collision handling atomically where possible to avoid time-of-check/time-of-use races.
- Catch and report filesystem exceptions instead of allowing a partial organization run to fail silently.
- Resolve and validate both source and destination paths, ensuring they remain beneath the configured workspace directories.
- Reject or handle symlinked archive directories explicitly to prevent destinations from being redirected outside the intended workspace.
- Add tests covering existing archive destinations, repeated filenames, concurrent operations, and platform-specific move semantics.
Example hardened logic:
python archives = root / "archives" archives.mkdir(parents=True, exist_ok=True) target = archives / item.name counter = 1 while target.exists(): target = archives / f"{item.stem}_{counter:02d}{item.suffix}" counter += 1 shutil.move(str(item), str(target))
