T09 · Insecure Skill Coding Practices
- Location
scripts/finish_troubleshoot.py:111- Finding
Invalid Action Values Silently Trigger Destructive Worktree Discard
- Content
View full analysis
Vulnerability Details
File Location:
scripts/finish_troubleshoot.py, lines 111–125
Vulnerability Type: Missing input validation leading to unintended destructive behavior
Risk Level: MediumVulnerable Code
python action = sys.argv[1] # 'apply' or 'discard' solution = sys.argv[2] if len(sys.argv) > 2 else "Fixed" # Load snapshot snapshot = load_snapshot() if not snapshot: print(json.dumps({"error": "No active troubleshooting session"})) sys.exit(1) # Process worktree changes if snapshot.get("mode") == "worktree": if action == "apply": result = apply_worktree_changes(snapshot) else: result = discard_worktree(snapshot) else: result = {"success": True, "mode": "subagent"}The resulting destructive operations are implemented in
discard_worktreeat lines 86–96:python if os.path.exists(worktree_path): subprocess.run( ["git", "worktree", "remove", "-f", worktree_path], capture_output=True, check=True ) # Delete temporary branch subprocess.run( ["git", "branch", "-D", f"trouble/{snapshot['id']}"], capture_output=True )Technical Analysis
The documented command interface accepts only
applyanddiscard, but the program never validates the supplied action against that allowlist. Instead, every value other than exactlyapplyenters the discard branch.This fail-open control flow converts malformed input, such as a typo, unsupported value, or unexpected automation input, into authorization to perform destructive cleanup. The cleanup uses both
git worktree remove -fandgit branch -D, which can delete uncommitted worktree changes and force-delete the associated branch.This is an insecure coding practice because the destructive operation is selected implicitly rather than through an explicit, validated discard request.
Attack Path
- A troubleshooting session is ...[truncated 1326 chars]
- Remediation
View remediation
Remediation Suggestions
-
Validate the action against an explicit allowlist before loading state or performing any repository operation:
python action = sys.argv[1] if action not in {"apply", "discard"}: print(json.dumps({ "error": "Invalid action", "allowed_actions": ["apply", "discard"] })) sys.exit(2) -
Use explicit branches and reject all unexpected values:
python if action == "apply": result = apply_worktree_changes(snapshot) elif action == "discard": result = discard_worktree(snapshot) else: raise ValueError("Unsupported action") -
Require a separate confirmation flag before force-removing a worktree containing uncommitted changes, especially for noninteractive callers.
-
Check worktree status before deletion and refuse destructive cleanup by default when modifications are present. Preserve or export a patch unless the caller explicitly authorizes permanent deletion.
-
Avoid
-fand-Dwhere possible. Use non-forced Git operations first and report a clear error if uncommitted or unmerged work would be lost. -
Archive the session and remove
active.jsononly after the requested operation succeeds. If cleanup fails, retain active state so recovery remains possible. -
Add regression tests proving that empty, misspelled, differently cased, and unsupported action values terminate without changing the worktree, branch, archive, or active-session state.
-
