T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/cleanup_sessions.sh:38
- Finding
- Cleanup Logic Can Recursively Delete Active and Non-Stale Sessions<![CDATA[ ## Vulnerability Details **File Location**: `scripts/cleanup_sessions.sh`, lines 38–47 **Vulnerability Type**: Unsafe recursive deletion caused by an overly broad `find` expression **Risk Level**: High ### Vulnerable Code ```bash # 1. Stale subagent sessions (older than 1h) if [ -d /home/nvi/.openclaw/sessions ]; then stale_count=$(find /home/nvi/.openclaw/sessions -maxdepth 1 -type d -mmin +60 2>/dev/null | wc -l) if [ "$stale_count" -gt 1 ]; then echo "Found $((stale_count - 1)) stale session(s)" if [ "$DRY_RUN" = true ]; then find /home/nvi/.openclaw/sessions -maxdepth 1 -type d -mmin +60 -exec echo "[DRY-RUN] Would remove: {}" \; else find /home/nvi/.openclaw/sessions -maxdepth 1 -type d -mmin +60 -exec rm -rf {} + 2>/dev/null || true echo "[CLEANED] Stale sessions removed" fi fi fi ``` ### Technical Analysis The `find` commands use `-maxdepth 1` but omit `-mindepth 1`. Consequently, the search may include `/home/nvi/.openclaw/sessions` itself when that root directory satisfies `-type d -mmin +60`. If both the session root and at least one child directory match, `stale_count` is greater than one and the deletion branch executes. Passing the root to `rm -rf` recursively removes everything under it, including child sessions that are newer than one hour or currently active. The count adjustment does not prevent deletion of the root; it only changes the displayed count. The code also suppresses errors with `2>/dev/null || true` and unconditionally reports that stale sessions were removed. This can conceal partial failures or unintended deletion behavior. ### Attack Path 1. `/home/nvi/.openclaw/sessions` has a modification time older than one hour. 2. At least one immediate child session directory also has a modification time older than one hour. 3. A user follows the documented instructions and runs `cleanup_sessions.sh` without `--dry-run`. 4. The count check succeeds because both the root and child dire ...[truncated 796 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Exclude the search root explicitly by adding `-mindepth 1`: ```bash find /home/nvi/.openclaw/sessions \ -mindepth 1 -maxdepth 1 -type d -mmin +60 ``` 2. Collect candidates first, validate that every resolved path is an immediate child of the expected session directory, and then delete only those validated entries. 3. Exclude sessions with active-process markers, lock files, or other OpenClaw-specific indications that they are in use. 4. Prefer an OpenClaw-supported session cleanup API or command, if one exists, rather than deleting internal state directly. 5. Resolve the current user's OpenClaw data directory dynamically instead of hard-coding `/home/nvi`. 6. Display the exact deletion list and request confirmation before destructive cleanup unless an explicit non-interactive option is supplied. 7. Do not suppress all deletion errors. Report failures accurately and only print a success message when deletion succeeds. 8. Add automated tests covering an old root containing both stale and recent child sessions, verifying that the root and recent sessions remain intact. ]]>
