T09 · Insecure Skill Coding Practices
- Location
scripts/split.py:42- Finding
Predictable Output Paths Permit File Truncation and Symlink-Based Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
scripts/split.py, lines 42–44
Vulnerability Type: Unsafe file creation and symbolic-link following
Risk Level: Mediumpython part_path = f"{prefix}{part_num:03d}" with open(part_path, 'wb') as out: out.write(chunk)Technical Analysis
The script creates output files using a predictable, sequential naming scheme such as
x000,x001, andx002. It opens each path inwbmode without checking whether the path already exists or is a symbolic link.The
wbmode truncates an existing file before writing. It also follows symbolic links under normal filesystem semantics. Consequently, an attacker who can create files or links in the output directory can make a predictable output path refer to another file writable by the user running the Skill. The script will then truncate and replace content in that target with a fragment of the selected input file.A caller-supplied prefix can alter the destination path, but the documented default is sufficient for exploitation when an attacker controls or shares the current working directory.
Attack Path
- The attacker obtains write access to the directory from which the victim will run the Skill.
- The attacker predicts the first output name, which is
x000when the default prefix is used. - The attacker creates
x000as a symbolic link to a target file that the victim is permitted to write. - The victim invokes the split tool with its default output prefix.
- The script opens
x000usingwb, follows the symbolic link, and truncates the linked target. - The script writes the first input fragment into the target file, corrupting or replacing its prior content.
The same destructive behavior applies to pre-existing ordinary files whose names collide with generated output paths, even without a symbolic link.
Impact Assessment
Exploitation does not directly ...[truncated 640 chars]
- Remediation
View remediation
Remediation Suggestions
- Create output files exclusively so existing paths cannot be silently truncated, for example with
open(part_path, "xb"). - Reject existing output paths and symbolic links, and terminate with a clear error rather than overwriting them.
- Write fragments into a dedicated output directory created with restrictive permissions.
- Resolve and validate the output directory and ensure generated paths remain inside it.
- If replacement is an intended feature, require an explicit overwrite option and still defend against symbolic-link attacks.
- For stronger race-condition resistance on supported platforms, use low-level file creation with
os.openand flags such asO_CREAT | O_EXCL | O_WRONLY, plusO_NOFOLLOWwhere available, then wrap the descriptor withos.fdopen. - Avoid performing a separate check followed by a normal
open, because that introduces a time-of-check-to-time-of-use race.
- Create output files exclusively so existing paths cannot be silently truncated, for example with
