T09 · Insecure Skill Coding Practices
- Location
scripts/gateway-restore.sh:67- Finding
Errexit Prevents the Intended Configuration Rollback
- Content
View full analysis
Vulnerability Details
File Location:
scripts/gateway-restore.sh, lines 8 and 67-79
Vulnerability Type: Improper error handling and rollback failure
Risk Level: HighVulnerable Code:
bash set -euo pipefailbash bash "$RESTART_SCRIPT" "restore to known-good config" EXIT=$? if [ $EXIT -eq 0 ]; then log "✅ $GATEWAY gateway restored and healthy" elif [ $EXIT -eq 1 ]; then log "⚠️ Restart failed — rolled back to previous config" cp "$CURRENT_BACKUP" "$CONFIG" else err "CRITICAL: Restart + rollback failed — check $GATEWAY gateway manually" fi exit $EXITTechnical Analysis
The script enables Bash's
errexitbehavior withset -e. The restart command is then executed as a standalone command before its exit status is captured.If
"$RESTART_SCRIPT"returns any nonzero exit status, Bash terminatesgateway-restore.shimmediately. Consequently,EXIT=$?and the subsequent conditional rollback logic are not executed. In particular, the command intended to restore the pre-operation configuration:bash cp "$CURRENT_BACKUP" "$CONFIG"is unreachable under the ordinary restart-failure condition.
This behavior contradicts the Skill's documented guarantee that a failed restart automatically restores the previous configuration. The
elsebranch also does not perform a rollback despite reporting that both restart and rollback failed; no rollback has actually been attempted by that branch.Attack Path
- A restore operation copies the current configuration to a timestamped backup.
- The known-good configuration is copied over the live configuration.
- The fixed restart script encounters an operational error or is caused to return a nonzero status.
- Because
set -eis active,gateway-restore.shexits at the failedbash "$RESTART_SCRIPT"command. - The exit status is never captured and the rollback branch is never entered.
...[truncated 943 chars]
- Remediation
View remediation
Remediation Suggestions
Run the restart command in a conditional context where
set -edoes not terminate the script before its status is handled:bash if bash "$RESTART_SCRIPT" "restore to known-good config"; then log "✅ $GATEWAY gateway restored and healthy" exit 0 else restart_exit=$? err "Restart failed with status $restart_exit; restoring previous config" if cp -- "$CURRENT_BACKUP" "$CONFIG"; then log "Previous $GATEWAY configuration restored" else err "CRITICAL: Failed to restore previous configuration" exit 2 fi exit "$restart_exit" fiAdditional hardening should include:
- Attempt rollback for every nonzero restart status rather than only exit code
1. - Report restart failure and rollback failure as separate events.
- Validate that the restored configuration matches the backup before reporting rollback success.
- Define and document consistent exit codes for successful restart, successful rollback after restart failure, and failed rollback.
- Add automated tests in which the restart script returns
1,2, and other nonzero statuses. - Consider using an
ERRtrap only if it can guarantee safe, idempotent rollback without obscuring the primary failure.
- Attempt rollback for every nonzero restart status rather than only exit code
