T09 · Insecure Skill Coding Practices
- Location
tool.py:83- Finding
Unsafe File Overwrite During Filename Normalization
- Content
View full analysis
Vulnerability Details
File Location:
tool.py, lines 83–88
Vulnerability Type: Unchecked destination collision and destructive file overwrite
Risk Level: Highpython if args.rename: print("\nRenaming files...") for old, new in changes: os.rename(old, new) print(f"Renamed: {os.path.basename(old)} -> {os.path.basename(new)}") print("Done.")Technical Analysis
The application derives destination filenames by removing dates, UUIDs, hexadecimal strings, and separators, but it does not verify whether a destination already exists or whether multiple source files normalize to the same destination.
The resulting paths are passed directly to
os.rename(). On platforms where this operation replaces an existing destination, a pre-existing file can be silently overwritten. This is particularly dangerous because an entirely hexadecimal basename such asabcd.txtis normalized tofile.txt. The code also does not detect duplicate destination paths in the planned batch or revalidate destinations immediately before each operation, leaving collision and time-of-check/time-of-use risks.Attack Path
- An attacker places a crafted source file, such as
abcd.txt, in a directory containing a valuable file namedfile.txt. - The victim supplies that directory to the tool and enables the destructive operation with
--rename. - Filename normalization removes the hexadecimal basename and substitutes the fallback name
file, producingfile.txt. - The rename loop calls
os.rename("abcd.txt", "file.txt")without checking the destination. - On a platform with replacement semantics, the valuable destination file is overwritten by the attacker-controlled source file.
A similar collision can occur when two different source filenames independently normalize to the same destination during one batch.
Impact Assessment
Successful exploitation can cause irreversible rep ...[truncated 332 chars]
- An attacker places a crafted source file, such as
- Remediation
View remediation
Remediation Suggestions
- Build and validate the complete rename plan before modifying the filesystem.
- Reject any operation whose destination already exists unless the source and destination identify the same file.
- Detect duplicate normalized destinations across all planned changes and fail safely with a clear collision report.
- Prefer generating a unique destination suffix rather than replacing an existing file.
- Revalidate destination availability immediately before every rename to reduce race-condition exposure.
- Use an atomic no-replace operation where the operating system supports it.
- Handle rename failures explicitly and report partial completion so users can recover safely.
- Add tests covering existing destinations, multiple sources normalizing to one name, fallback names such as
file.txt, and concurrent destination creation.
