T09 · Insecure Skill Coding Practices
Error
- Location
- memory-rollback.sh:14
- Finding
- Unvalidated storage paths permit destructive filesystem operations<![CDATA[ ## Vulnerability Details **File Location**: `memory-rollback.sh:14-17, 93-110`; `memory-backup.sh:14-19, 96` **Vulnerability Type**: Unrestricted filesystem path usage **Risk Level**: High ### Vulnerable Code ```bash # memory-rollback.sh if [[ -f "$CONFIG_FILE" ]]; then MEMORY_PATH=$(jq -r '.memory.base_path // "./memory"' "$CONFIG_FILE") BACKUP_PATH=$(jq -r '.backup.path // "./backups"' "$CONFIG_FILE") fi ``` ```bash # memory-rollback.sh # Perform rollback rm -rf "$MEMORY_PATH" mkdir -p "$MEMORY_PATH" if [[ -d "$backup_path" ]]; then # Directory backup if [[ -f "$backup_path/memory.tar.gz" ]]; then tar -xzf "$backup_path/memory.tar.gz" -C "$(dirname "$MEMORY_PATH")" else cp -r "$backup_path/memory"/* "$MEMORY_PATH/" 2>/dev/null || cp -r "$backup_path"/* "$MEMORY_PATH/" fi elif [[ "$backup_path" == *.tar.gz ]]; then # Compressed backup tar -xzf "$backup_path" -C "$(dirname "$MEMORY_PATH")" fi ``` ```bash # memory-backup.sh if [[ -f "$CONFIG_FILE" ]]; then MEMORY_PATH=$(jq -r '.memory.base_path // "./memory"' "$CONFIG_FILE") BACKUP_PATH=$(jq -r '.backup.path // "./backups"' "$CONFIG_FILE") RETENTION_DAYS=$(jq -r '.backup.retention_days // 30' "$CONFIG_FILE") COMPRESSION=$(jq -r '.backup.compression // true' "$CONFIG_FILE") fi ``` ```bash find "$BACKUP_PATH" -name "backup_*" -type d -mtime +$RETENTION_DAYS -exec rm -rf {} + 2>/dev/null || true ``` ### Technical Analysis The scripts read `memory.base_path`, `backup.path`, and `backup.retention_days` from an editable JSON configuration and use those values in recursive deletion operations. The paths are quoted, which prevents shell word splitting and ordinary command injection, but they are not canonicalized or restricted to an approved data directory. In `memory-rollback.sh`, `rm -rf "$MEMORY_PATH"` recursively removes the configured target before restoration. There is no rejection of empty, root-level, home, or unrelated absolute p ...[truncated 1545 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Resolve configured paths with `realpath -m` before any operation. 2. Define an explicit approved data root and verify that both memory and backup paths remain beneath it. 3. Reject empty paths, `/`, the user's home directory, the project root, and other protected locations. 4. Place a dedicated ownership marker in initialized memory directories and refuse recursive deletion unless the marker is present and valid. 5. Validate `retention_days` against a strict bounded integer expression, such as `^[0-9]+$`, and enforce a reasonable maximum. 6. Print the canonical deletion target and require confirmation for destructive operations unless operating under an explicitly configured unattended policy. 7. Prefer renaming the current memory directory to a temporary recovery location and deleting it only after restoration succeeds. 8. Run scheduled maintenance under a dedicated, unprivileged account with access limited to the intended memory and backup directories. ]]>
