T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/file_sorter.py:145
- Finding
- Untrusted Undo Log Enables Arbitrary File Deletion or Relocation## Vulnerability Details **File Location**: `scripts/file_sorter.py:39-42` and `scripts/file_sorter.py:145-175` **Vulnerability Type**: Unvalidated file operations based on an editable operation log **Risk Level**: High ### Vulnerable 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_data ``` ```python 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 += 1 ``` ### Technical Analysis The undo operation treats `.file-sorter-backup.json` as 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`, and `action` values: - A forged `copy` operation causes `target.unlink()` to delete the specified file. - A forged `move` operation causes the specified target file to be relo ...[truncated 2031 chars]
- Remediation
- ## Remediation Suggestions 1. Define and enforce a strict schema for every operation record, including an allowlist of supported actions and required string fields. 2. 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. 3. Do not permit undo records to create arbitrary parent directories outside the approved input root. 4. 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. 5. Create the backup file with restrictive permissions and reject backup files owned by an unexpected user or writable by untrusted users. 6. 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. 7. Reject symbolic-link traversal in parent path components and use race-resistant, descriptor-relative filesystem operations where available. 8. Display the exact paths that undo will modify and require explicit confirmation before destructive operations. 9. Handle malformed records and partial failures without continuing with unsafe operations.
