T09 · Insecure Skill Coding Practices
- Location
scripts/file_sorter.py:145- Finding
Untrusted Undo Log Enables Arbitrary File Deletion or Relocation
- Content
View full analysis
Vulnerability Details
File Location:
scripts/file_sorter.py:39-42andscripts/file_sorter.py:145-175
Vulnerability Type: Unvalidated file operations based on an editable operation log
Risk Level: HighVulnerable Code
python def load_backup(self): if self.backup_file.exists(): with open(self.backup_file, 'r', encoding='utf-8') as f: self.backup_data = json.load(f) return self.backup_datapython def undo(self): backup = self.load_backup() if not backup: print("没有找到备份文件,无法撤销") return False count = 0 # 反向操作,从后往前 for op in reversed(backup): source = Path(op["source"]) target = Path(op["target"]) if op["action"] == "move": if target.exists() and not source.exists(): source.parent.mkdir(parents=True, exist_ok=True) shutil.move(str(target), str(source)) print(f"撤销移动: {target.name} -> {source.parent.name}/{target.name}") count += 1 elif op["action"] == "copy": if target.exists(): target.unlink() print(f"撤销复制: 删除 {target.name}") count += 1 elif op["action"] == "link": if target.exists() and target.is_symlink(): target.unlink() print(f"撤销链接: 删除 {target.name}") count += 1Technical Analysis
The undo operation treats
.file-sorter-backup.jsonas a trusted source of file paths and actions. The JSON document is loaded without schema validation, integrity verification, file-identity checks, or path-containment enforcement.Each record can supply arbitrary
source,target, andactionvalues:- A forged
copyoperation causestarget.unlink()to delete the specified file. - A forged
moveoperation causes the specified target file to be relo ...[truncated 2031 chars]
- A forged
- Remediation
View remediation
Remediation Suggestions
- Define and enforce a strict schema for every operation record, including an allowlist of supported actions and required string fields.
- Record the canonical input and output roots in the backup metadata. Resolve all operation paths with
Path.resolve()and reject any path outside those roots. - Do not permit undo records to create arbitrary parent directories outside the approved input root.
- Store file identity metadata, such as device and inode values where supported, and verify that the current target matches the originally processed file before modifying it.
- Create the backup file with restrictive permissions and reject backup files owned by an unexpected user or writable by untrusted users.
- Protect the operation log with authenticated integrity, such as an HMAC using a key stored outside the output directory, if hostile local modification is within the threat model.
- Reject symbolic-link traversal in parent path components and use race-resistant, descriptor-relative filesystem operations where available.
- Display the exact paths that undo will modify and require explicit confirmation before destructive operations.
- Handle malformed records and partial failures without continuing with unsafe operations.
