Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions bin/gstack-team-init
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ Install it:

Then restart your AI coding tool.
MSG
echo '{"permissionDecision":"deny","message":"gstack is required but not installed. See stderr for install instructions."}'
# The decision MUST nest under hookSpecificOutput — Claude Code dispatches on
# hookSpecificOutput.permissionDecision. A bare top-level {"permissionDecision":
# "deny"} parses fine and is then ignored, so the "BLOCKED" message above would
# print while the call proceeds anyway.
printf '%s\n' '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"gstack is required but not installed. See stderr for install instructions."}}'
exit 0
fi

Expand All @@ -138,8 +142,15 @@ HOOK_EOF
echo " + .claude/hooks/check-gstack.sh — enforcement hook"

# Add hook to project-level settings.json
# Hook command passed via env to avoid a bash->JS->JSON escaping stack.
# `bash -c` is required for Windows: the harness spawns hooks through
# cmd.exe /d /s /c, which neither expands $CLAUDE_PROJECT_DIR nor can execute
# a .sh file directly. Wrapping in bash -c moves expansion and execution into
# bash, where CLAUDE_PROJECT_DIR (injected into the hook env by Claude Code)
# resolves on every platform.
GSTACK_HOOK_CMD='bash -c "exec \"$CLAUDE_PROJECT_DIR/.claude/hooks/check-gstack.sh\""'
if command -v bun >/dev/null 2>&1; then
GSTACK_SETTINGS_PATH="$SETTINGS" bun -e "
GSTACK_HOOK_CMD="$GSTACK_HOOK_CMD" GSTACK_SETTINGS_PATH="$SETTINGS" bun -e "
const fs = require('fs');
const settingsPath = process.env.GSTACK_SETTINGS_PATH;

Expand All @@ -160,7 +171,7 @@ HOOK_EOF
matcher: 'Skill',
hooks: [{
type: 'command',
command: '\"\$CLAUDE_PROJECT_DIR/.claude/hooks/check-gstack.sh\"'
command: process.env.GSTACK_HOOK_CMD
}]
});
}
Expand Down
62 changes: 51 additions & 11 deletions careful/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
---
name: careful
version: 0.1.0
description: Safety guardrails for destructive commands. (gstack)
version: 0.2.0
description: "Safety guardrails for destructive commands — a PreToolUse hook prompts for confirmation before rm -rf, DROP TABLE, TRUNCATE, force-push, git reset --hard, git checkout ., kubectl delete and docker prune, and the user can still override each prompt; triggers: be careful / safety mode / prod mode / warn before destructive / guard against rm -rf; the ask must stay nested under hookSpecificOutput and careful/tests/test-check-careful.sh must stay green (gstack)"
triggers:
- be careful
- warn before destructive
- safety mode
- prod mode
- guard against rm -rf
allowed-tools:
- Bash
- Read
Expand All @@ -14,7 +16,8 @@ hooks:
- matcher: "Bash"
hooks:
- type: command
command: "bash $HOME/.claude/skills/gstack/careful/bin/check-careful.sh"
# bash -c so bash (not cmd.exe) expands $HOME — see "Hook command form" below
command: 'bash -c "exec \"$HOME/.claude/skills/gstack/careful/bin/check-careful.sh\""'
statusMessage: "Checking for destructive commands..."
---
<!-- AUTO-GENERATED from SKILL.md.tmpl — do not edit directly -->
Expand All @@ -23,11 +26,11 @@ hooks:

## When to invoke this skill

Warns before rm -rf, DROP TABLE,
force-push, git reset --hard, kubectl delete, and similar destructive operations.
User can override each warning. Use when touching prod, debugging live systems,
or working in a shared environment. Use when asked to "be careful", "safety mode",
"prod mode", or "careful mode".
Warns before rm -rf, DROP TABLE, force-push, git reset --hard, kubectl
delete, and similar destructive operations. User can override each warning.
Use when touching prod, debugging live systems, or working in a shared
environment. Use when asked to "be careful", "safety mode", "prod mode",
or "careful mode".

# /careful — Destructive Command Guardrails

Expand Down Expand Up @@ -61,7 +64,44 @@ These patterns are allowed without warning:
## How it works

The hook reads the command from the tool input JSON, checks it against the
patterns above, and returns `permissionDecision: "ask"` with a warning message
if a match is found. You can always override the warning and proceed.
patterns above, and on a match emits:

To deactivate, end the conversation or start a new one. Hooks are session-scoped.
```json
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"[careful] ..."}}
```

The nesting is load-bearing: Claude Code dispatches on
`hookSpecificOutput.permissionDecision`. A bare top-level
`{"permissionDecision":"ask"}` parses fine and is then **ignored** — no prompt
appears and the destructive command runs unchallenged, while the skill looks
armed. Valid decisions are `allow` / `deny` / `ask` / `defer`; `careful` uses
`ask`, so you can always override and proceed.

### Hook command form

`command:` is `bash -c "exec \"$HOME/...\""`, not `bash $HOME/...`. On Windows the
harness spawns hooks through `cmd.exe /d /s /c`, which does not expand `$HOME`;
the bare form passes a literal `$HOME/...` to bash and dies with exit 127 before
any check runs. Wrapping in `bash -c` moves expansion inside bash, where it works
on every platform. `${CLAUDE_SKILL_DIR}` is **not** an option here — the harness
interpolates it only in skill body text, never in hook commands.

## Regression test

`careful/tests/test-check-careful.sh` feeds representative destructive and benign
commands through the script and asserts the warn/allow output schema, the safe
exceptions, and that the `command:` above actually spawns through the harness's
`cmd.exe` wrapper:

```bash
bash ~/.claude/skills/gstack/careful/tests/test-check-careful.sh
```

## Notes

- Warnings are advisory (`ask`), never a hard block — you stay in control
- The warning text never includes your command's contents, only the pattern name
- Detection is regex over the command string, not a shell parser: obfuscated or
indirect forms (a destructive command inside a script you invoke) are not caught
- To deactivate, end the conversation or start a new one. Hooks are session-scoped
— unlike `/freeze`, `careful` keeps no state on disk
56 changes: 46 additions & 10 deletions careful/SKILL.md.tmpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
---
name: careful
version: 0.1.0
version: 0.2.0
description: |
Safety guardrails for destructive commands. Warns before rm -rf, DROP TABLE,
force-push, git reset --hard, kubectl delete, and similar destructive operations.
User can override each warning. Use when touching prod, debugging live systems,
or working in a shared environment. Use when asked to "be careful", "safety mode",
"prod mode", or "careful mode". (gstack)
Safety guardrails for destructive commands — a PreToolUse hook prompts for confirmation before rm -rf, DROP TABLE, TRUNCATE, force-push, git reset --hard, git checkout ., kubectl delete and docker prune, and the user can still override each prompt; triggers: be careful / safety mode / prod mode / warn before destructive / guard against rm -rf; the ask must stay nested under hookSpecificOutput and careful/tests/test-check-careful.sh must stay green (gstack)
triggers:
- be careful
- warn before destructive
- safety mode
- prod mode
- guard against rm -rf
allowed-tools:
- Bash
- Read
Expand All @@ -19,7 +17,8 @@ hooks:
- matcher: "Bash"
hooks:
- type: command
command: "bash $HOME/.claude/skills/gstack/careful/bin/check-careful.sh"
# bash -c so bash (not cmd.exe) expands $HOME — see "Hook command form" below
command: 'bash -c "exec \"$HOME/.claude/skills/gstack/careful/bin/check-careful.sh\""'
statusMessage: "Checking for destructive commands..."
sensitive: true
---
Expand Down Expand Up @@ -56,7 +55,44 @@ These patterns are allowed without warning:
## How it works

The hook reads the command from the tool input JSON, checks it against the
patterns above, and returns `permissionDecision: "ask"` with a warning message
if a match is found. You can always override the warning and proceed.
patterns above, and on a match emits:

To deactivate, end the conversation or start a new one. Hooks are session-scoped.
```json
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"[careful] ..."}}
```

The nesting is load-bearing: Claude Code dispatches on
`hookSpecificOutput.permissionDecision`. A bare top-level
`{"permissionDecision":"ask"}` parses fine and is then **ignored** — no prompt
appears and the destructive command runs unchallenged, while the skill looks
armed. Valid decisions are `allow` / `deny` / `ask` / `defer`; `careful` uses
`ask`, so you can always override and proceed.

### Hook command form

`command:` is `bash -c "exec \"$HOME/...\""`, not `bash $HOME/...`. On Windows the
harness spawns hooks through `cmd.exe /d /s /c`, which does not expand `$HOME`;
the bare form passes a literal `$HOME/...` to bash and dies with exit 127 before
any check runs. Wrapping in `bash -c` moves expansion inside bash, where it works
on every platform. `${CLAUDE_SKILL_DIR}` is **not** an option here — the harness
interpolates it only in skill body text, never in hook commands.

## Regression test

`careful/tests/test-check-careful.sh` feeds representative destructive and benign
commands through the script and asserts the warn/allow output schema, the safe
exceptions, and that the `command:` above actually spawns through the harness's
`cmd.exe` wrapper:

```bash
bash ~/.claude/skills/gstack/careful/tests/test-check-careful.sh
```

## Notes

- Warnings are advisory (`ask`), never a hard block — you stay in control
- The warning text never includes your command's contents, only the pattern name
- Detection is regex over the command string, not a shell parser: obfuscated or
indirect forms (a destructive command inside a script you invoke) are not caught
- To deactivate, end the conversation or start a new one. Hooks are session-scoped
— unlike `/freeze`, `careful` keeps no state on disk
48 changes: 40 additions & 8 deletions careful/bin/check-careful.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
#!/usr/bin/env bash
# check-careful.sh — PreToolUse hook for /careful skill
# Reads JSON from stdin, checks Bash command for destructive patterns.
# Returns {"permissionDecision":"ask","message":"..."} to warn, or {} to allow.
# Reads JSON from stdin, checks the Bash command for destructive patterns.
#
# Output contract (verified against cli.js 2.1.92 — the harness dispatches ONLY
# on the nested hookSpecificOutput.permissionDecision field; a bare top-level
# {"permissionDecision":"ask"} parses fine and is then ignored, so the warning
# never appears and the destructive command runs unchallenged):
# warn -> {"hookSpecificOutput":{"hookEventName":"PreToolUse",
# "permissionDecision":"ask","permissionDecisionReason":"..."}}
# allow -> {} (no opinion; normal permission flow continues)
# Valid decisions are allow | deny | ask | defer. "ask" prompts the user, who can
# still proceed — this hook warns, it does not block.
#
# Regression: ../tests/test-check-careful.sh. Keep it green.
set -euo pipefail

# Read stdin (JSON with tool_input)
Expand All @@ -11,9 +22,17 @@ INPUT=$(cat)
# Try grep/sed first (handles 99% of cases), fall back to Python for escaped quotes
CMD=$(printf '%s' "$INPUT" | grep -o '"command"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | sed 's/.*:[[:space:]]*"//;s/"$//' || true)

# Python fallback if grep returned empty (e.g., escaped quotes in command)
# Python fallback if grep returned empty (e.g., escaped quotes in command).
# Try python3 then python: many Windows installs ship only `python`, and if the
# interpreter is missing this silently yields an empty CMD, i.e. a destructive
# command with escaped quotes would sail through unchecked.
if [ -z "$CMD" ]; then
CMD=$(printf '%s' "$INPUT" | python3 -c 'import sys,json; print(json.loads(sys.stdin.read()).get("tool_input",{}).get("command",""))' 2>/dev/null || true)
for _py in python3 python; do
if command -v "$_py" >/dev/null 2>&1; then
CMD=$(printf '%s' "$INPUT" | "$_py" -c 'import sys,json; print(json.loads(sys.stdin.read()).get("tool_input",{}).get("command",""))' 2>/dev/null || true)
if [ -n "$CMD" ]; then break; fi
fi
done
fi

# If we still couldn't extract a command, allow
Expand Down Expand Up @@ -101,12 +120,25 @@ fi

# --- Output ---
if [ -n "$WARN" ]; then
# Analytics state root — same chain as gstack/bin/gstack-paths, so the log
# lands where the rest of gstack looks for it.
if [ -n "${GSTACK_HOME:-}" ]; then
STATE_DIR="$GSTACK_HOME"
elif [ -n "${CLAUDE_PLUGIN_DATA:-}" ] && printf '%s' "${CLAUDE_PLUGIN_ROOT:-}" | grep -qi "gstack"; then
STATE_DIR="$CLAUDE_PLUGIN_DATA"
elif [ -n "${HOME:-}" ]; then
STATE_DIR="$HOME/.gstack"
else
STATE_DIR=".gstack"
fi

# Log hook fire event (pattern name only, never command content)
mkdir -p ~/.gstack/analytics 2>/dev/null || true
echo '{"event":"hook_fire","skill":"careful","pattern":"'"$PATTERN"'","ts":"'$(date -u +%Y-%m-%dT%H:%M:%SZ)'","repo":"'$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")'"}' >> ~/.gstack/analytics/skill-usage.jsonl 2>/dev/null || true
mkdir -p "$STATE_DIR/analytics" 2>/dev/null || true
echo '{"event":"hook_fire","skill":"careful","pattern":"'"$PATTERN"'","ts":"'$(date -u +%Y-%m-%dT%H:%M:%SZ)'","repo":"'$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")'"}' >> "$STATE_DIR/analytics/skill-usage.jsonl" 2>/dev/null || true

WARN_ESCAPED=$(printf '%s' "$WARN" | sed 's/"/\\"/g')
printf '{"permissionDecision":"ask","message":"[careful] %s"}\n' "$WARN_ESCAPED"
WARN_ESCAPED=$(printf '%s' "$WARN" | sed 's/\\/\\\\/g; s/"/\\"/g')
printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"[careful] %s"}}\n' "$WARN_ESCAPED"
else
echo '{}'
fi
exit 0
Loading