T09 · Insecure Skill Coding Practices
Error
- Location
- gateway-auto-rollback.py:158
- Finding
- Watch mode restores the already-modified configuration instead of a known-good backup## Vulnerability Details **File Location**: `gateway-auto-rollback.py`, lines 158-164; related rollback logic at lines 84-94 **Vulnerability Type**: Incorrect backup sequencing and ineffective rollback **Risk Level**: High ### Vulnerable Code ```python if new_hash != old_hash: log_event("WARN", f"⚠️ 检测到修改: {file_path.name}") change_detected = True consecutive_healthy = 0 backup = create_backup(file_path) if not post_modification_verify(file_path, backup): log_event("ERROR", "修改验证失败,已回滚") ``` The resulting backup is subsequently restored using: ```python config_file = CONFIG_DIR / backup_path.name.split(".")[0] try: shutil.copy2(backup_path, config_file) ``` ### Technical Analysis Watch mode detects a modification by comparing the current file hash with a previously recorded hash. This comparison necessarily occurs after the configuration file has already been changed. Once a difference is detected, the watcher calls `create_backup(file_path)`. The backup therefore contains the new, potentially malformed or service-breaking configuration rather than the previous known-good content. If JSON or Gateway validation subsequently fails, `post_modification_verify()` passes this newly created backup to `rollback_to_backup()`, which copies the same changed content back over the configuration file. The old hash cannot support recovery because it only identifies the previous content and does not preserve that content. Consequently, the documented automatic rollback protection is ineffective in watch mode. ### Attack Path 1. An attacker or faulty automation with permission to modify a monitored configuration changes `openclaw.json`, `exec-approvals.json`, or `skills.json`. 2. The watcher observes that the current hash differs from its stored hash. 3. The watcher creates a backup from the already-modified file. 4. The changed configuration fails JSON validation or causes the G ...[truncated 843 chars]
- Remediation
- ## Remediation Suggestions - Preserve a known-good snapshot before any modification occurs. Watch mode should maintain an actual copy of the last validated content, not only its hash. - When a change is detected, validate the current file before replacing the known-good snapshot. - If validation fails, restore the previously retained known-good snapshot. - Update the known-good snapshot and stored hash only after JSON validation and Gateway health validation both succeed. - Use atomic operations: write backups to a temporary file, flush them, and atomically rename them into place. - Reject a missing or invalid backup path before attempting rollback. - After restoration, recalculate the active file hash and repeat JSON and Gateway validation to confirm that rollback succeeded. - Add an integration test that writes an invalid configuration, invokes the watcher flow, and verifies byte-for-byte restoration of the original file.
