-
Notifications
You must be signed in to change notification settings - Fork 26
Fix/broken hooks example link #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
factory-ben
wants to merge
1
commit into
main
Choose a base branch
from
fix/broken-hooks-example-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Bash Command Validator Hook for Droid | ||
| # | ||
| # This PreToolUse hook validates bash commands before execution, | ||
| # blocking potentially dangerous operations and providing feedback. | ||
| # | ||
| # Exit codes: | ||
| # 0 - Allow the command | ||
| # 2 - Block the command (with feedback to Droid) | ||
| # | ||
| # Usage: | ||
| # Register this script as a PreToolUse hook with matcher "Bash" | ||
| # | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Read the JSON input from stdin | ||
| INPUT=$(cat) | ||
|
|
||
| # Extract the command using jq | ||
| COMMAND=$(printf '%s\n' "$INPUT" | jq -r '.tool_input.command // empty') | ||
|
|
||
| if [[ -z "$COMMAND" ]]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Define blocked patterns (customize these for your needs) | ||
| BLOCKED_PATTERNS=( | ||
| # Dangerous file operations | ||
| 'rm -rf /' | ||
| 'rm -rf ~' | ||
| 'rm -rf [*]' | ||
| 'rm -rf /[*]' | ||
| '> /dev/sda' | ||
| 'mkfs\.' | ||
| 'dd if=.* of=/dev/' | ||
|
|
||
| # Fork bombs and resource exhaustion | ||
| ':\(\)\{.*\}:' | ||
| 'fork bomb' | ||
|
|
||
| # Privilege escalation without explicit approval | ||
| 'chmod 777' | ||
| 'chmod -R 777' | ||
|
|
||
| # Network exfiltration patterns | ||
| 'curl .* \| bash' | ||
| 'wget .* \| bash' | ||
| 'curl .* \| sh' | ||
| 'wget .* \| sh' | ||
|
|
||
| # Sensitive file access | ||
| '/etc/shadow' | ||
| '/etc/passwd' | ||
| '\.ssh/id_' | ||
|
|
||
| # History/credential manipulation | ||
| 'history -c' | ||
| 'export.*PASSWORD' | ||
| 'export.*SECRET' | ||
| 'export.*API_KEY' | ||
| ) | ||
|
|
||
| # Define warning patterns (allow but notify) | ||
| WARNING_PATTERNS=( | ||
| 'sudo' | ||
| 'su -' | ||
| 'chmod' | ||
| 'chown' | ||
| 'kill -9' | ||
| 'pkill' | ||
| 'killall' | ||
| ) | ||
|
|
||
| # Check for blocked patterns | ||
| for pattern in "${BLOCKED_PATTERNS[@]}"; do | ||
| if printf '%s\n' "$COMMAND" | grep -qiE "$pattern"; then | ||
| echo "BLOCKED: Command matches dangerous pattern: $pattern" | ||
| echo "Command was: $COMMAND" | ||
| echo "" | ||
| echo "If this command is necessary, please modify it to be safer or request manual execution." | ||
| exit 2 | ||
| fi | ||
| done | ||
|
|
||
| # Check for warning patterns (log but allow) | ||
| for pattern in "${WARNING_PATTERNS[@]}"; do | ||
| if printf '%s\n' "$COMMAND" | grep -qiE "$pattern"; then | ||
| echo "WARNING: Command uses potentially sensitive operation: $pattern" >&2 | ||
| # Continue execution but log the warning | ||
| fi | ||
| done | ||
|
|
||
| # Command passed validation | ||
| exit 0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "hooks": { | ||
| "PreToolUse": [ | ||
| { | ||
| "matcher": "Bash", | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "\"$FACTORY_PROJECT_DIR\"/.factory/hooks/bash-validator.sh" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.