Skip to content

alirezarezvani/claude-code-skill-factory / generated-skills/hook-factory

hook-factory

Flagged

Generate production-ready Claude Code hooks with interactive Q&A, automated installation, and enhanced validation. Supports 10 templates across 7 event types for comprehensive workflow automation.

examples/pre-tool-validation/README.md

View on GitHub
# Pre-Tool Use Validation Hook

Validates file permissions and protects sensitive files before Write/Edit operations.

## What This Hook Does

- Checks write permissions before modifying files
- Prevents modifications to protected files (.env, credentials, AWS/SSH configs)
- Blocks operations that would fail due to permission errors
- Fast validation (<10 seconds)

## Event Type

**PreToolUse** - Triggers BEFORE Write/Edit tools execute (BLOCKING)

## Installation

### Automatic (Recommended)
```bash
# Copy to user-level (all projects)
cp -r examples/pre-tool-validation ~/.claude/hooks/

# OR copy to project-level (this project only)
cp -r examples/pre-tool-validation .claude/hooks/
```

### Manual
1. Copy `hook.json` content
2. Add to `~/.claude/settings.json` or `.claude/settings.json`
3. Merge into `PreToolUse` array (not PostToolUse!)

## How It Works

1. **Permission Check**: Verifies write permission on target file
2. **Protected Files**: Checks against protected patterns:
   - `/.env` files (environment variables)
   - `/secrets` directories
   - `/credentials` files
   - `/.aws/` AWS configurations
   - `/.ssh/` SSH keys
3. **Exit Codes**:
   - `0` = Validation passed, allow tool to execute
   - `1` = Validation failed, block tool execution

## Example Output

**Success**:
```
Validation passed
```

**Blocked (No Permission)**:
```
Error: No write permission for /path/to/file.txt
```

**Blocked (Protected File)**:
```
Error: Cannot modify protected file: /home/user/.env
```

## Customization

### Add More Protected Patterns

Edit the `PROTECTED_PATTERNS` array in `hook.json`:

```bash
PROTECTED_PATTERNS=(
    "/.env$"           # Environment files
    "/secrets"         # Secrets directories
    "/credentials"     # Credential files
    "/.aws/"           # AWS config
    "/.ssh/"           # SSH keys
    "/production.yml"  # Custom: Production configs
    "/api_keys.json"   # Custom: API keys
)
```

### Change Timeout

Default is 10 seconds (fast validation). Adjust in `hook.json`:

```json
"timeout": 10  // Change to 5 for faster, 20 for more thorough checks
```

## Testing

1. **Test Normal Operation**:
   ```bash
   echo "test" > test_file.txt
   # Should allow write (no protection)
   ```

2. **Test Protected File**:
   ```bash
   touch .env
   echo "SECRET=123" > .env
   # Should block (protected pattern)
   ```

3. **Test Read-Only File**:
   ```bash
   touch readonly.txt
   chmod 444 readonly.txt
   # Should block (no write permission)
   ```

## Safety Notes

- **BLOCKING**: This hook can prevent Write/Edit operations
- **Fast**: Must complete in <10 seconds
- **Use Cases**: Prevent accidental modifications to critical files
- **Limitations**: Only checks permissions, not file content

## Troubleshooting

**Hook doesn't block writes:**
- Verify hook is in `PreToolUse` section (not PostToolUse)
- Check CLAUDE_TOOL_FILE_PATH is set
- Test protected pattern regex with your file path

**Too restrictive:**
- Remove patterns from PROTECTED_PATTERNS array
- Adjust regex patterns to be more specific

**Timeout errors:**
- Reduce timeout from 10s to 5s
- Simplify validation logic
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall