ai-code-guardrails
v1.0.0
Published
CLI wrapper for AI coding assistants that prevents destructive operations. Intercepts dangerous commands before execution.
Maintainers
Readme
AI Code Guardrails
Prevent destructive operations from AI coding assistants. A CLI wrapper that intercepts dangerous commands before execution.
Works with Cursor, GitHub Copilot, Claude, ChatGPT, and any AI assistant that generates shell commands.
Features
- Shell Command Safety: Blocks
rm -rf /,chmod 777, disk formatting, and other dangerous shell operations - SQL Injection Prevention: Detects
DROP TABLE,DELETEwithoutWHERE,TRUNCATE, and other destructive SQL - Git Protection: Prevents force pushes, hard resets, and accidental pushes to protected branches
- File System Guards: Protects system directories and critical paths from deletion
- Interactive Prompts: Clear warnings with risk levels and confirmation for dangerous operations
- Configurable Rules: Whitelist/blacklist commands, customize protection levels
- Multiple Modes: Interactive, strict (block all), or warn-only mode
Installation
npm install -g ai-code-guardrailsOr use with npx:
npx ai-code-guardrails check "rm -rf /"Quick Start
Check a Command
Analyze a command without executing it:
guardrails check "rm -rf /tmp/test"
guardrails check "git push --force origin main"
guardrails check "DROP TABLE users"Run with Protection
Execute a command with safety checks:
guardrails run "rm -rf ./node_modules"
guardrails run "git reset --hard HEAD~3"Interactive Shell Mode
Start a protected shell session:
guardrails wrap
# All commands will be analyzed before execution
guardrails> rm -rf /
# [CRITICAL] Recursive deletion from root directory
# Do you want to execute this dangerous command? (y/N)Usage
Commands
| Command | Description |
|---------|-------------|
| guardrails check <command> | Analyze a command without executing |
| guardrails run <command> | Analyze and execute with confirmation |
| guardrails wrap | Start interactive protected shell |
| guardrails init | Create a config file |
| guardrails whitelist add <cmd> | Add command to whitelist |
| guardrails blacklist add <cmd> | Add command to blacklist |
Options
guardrails check --json "command" # Output as JSON
guardrails run --mode strict "cmd" # Strict mode (block dangerous)
guardrails run --mode warn "cmd" # Warn mode (show warnings only)
guardrails run --yes "cmd" # Skip confirmation (dangerous!)
guardrails run -c ./config.yml "cmd" # Use custom configModes
| Mode | Behavior |
|------|----------|
| interactive | Prompt for confirmation on dangerous commands (default) |
| strict | Block all dangerous commands without prompting |
| warn | Show warnings but allow execution |
Configuration
Create a .guardrails.yml file in your project or home directory:
guardrails initExample Configuration
enabled: true
mode: interactive
whitelist:
commands:
- git status
- git log
- npm install
paths: []
patterns: []
blacklist:
commands: []
paths: []
patterns: []
rules:
shell:
enabled: true
blockRmRf: true
blockChmod777: true
blockDd: true
blockMkfsFormat: true
git:
enabled: true
blockForcePush: true
blockHardReset: true
blockMainBranchPush: true
protectedBranches:
- main
- master
- production
sql:
enabled: true
blockDrop: true
blockDelete: true
blockTruncate: true
requireWhereClause: true
filesystem:
enabled: true
protectedPaths:
- /
- /etc
- /usr
blockRecursiveDelete: true
blockSystemDirs: trueWhat It Protects Against
Shell Commands
rm -rf /- Root directory deletionrm -rf *- Wildcard deletionchmod 777- World-writable permissionsdd of=/dev/sda- Direct disk writesmkfs.ext4 /dev/sda- Disk formattingcurl | sh- Piped execution from remote- Fork bombs
Git Operations
git push --force- Force pushesgit reset --hard- Hard resetsgit clean -fdx- Aggressive cleaning- Push to main/master/production branches
- Deleting protected branches
SQL Statements
DROP TABLE/DROP DATABASEDELETE FROMwithout WHERETRUNCATE TABLEUPDATEwithout WHERE- Operations on production databases
Filesystem Operations
- Deleting system directories (
/,/etc,/usr, etc.) - Deleting home directory
- Overwriting system config files
- Dangerous permission changes
Programmatic Usage
import { analyzeCommand, CommandAnalyzer } from 'ai-code-guardrails';
// Quick analysis
const result = analyzeCommand('rm -rf /');
console.log(result.safe); // false
console.log(result.riskLevel); // 'critical'
console.log(result.issues); // Array of detected issues
// With custom config
const analyzer = new CommandAnalyzer({
rules: {
git: {
protectedBranches: ['main', 'develop']
}
}
});
const analysis = analyzer.analyze('git push --force origin main');Integration with AI Tools
Cursor
Add to your Cursor settings:
{
"terminal.integrated.shellIntegration.enabled": true,
"terminal.integrated.profiles.linux": {
"guardrails": {
"path": "guardrails",
"args": ["wrap"]
}
}
}Git Hooks
Add a pre-commit hook to check SQL migrations:
#!/bin/bash
# .git/hooks/pre-commit
for file in $(git diff --cached --name-only | grep '\.sql$'); do
guardrails check "$(cat $file)" || exit 1
doneCI/CD Pipeline
See the GitHub Action in .github/workflows/guardrails.yml for CI integration.
Risk Levels
| Level | Color | Meaning |
|-------|-------|---------|
| low | Green | Safe to execute |
| medium | Yellow | Proceed with caution |
| high | Red | Potentially dangerous |
| critical | Red BG | Extremely dangerous - requires explicit confirmation |
Exit Codes
| Code | Meaning | |------|---------| | 0 | Command is safe / executed successfully | | 1 | Command blocked or has risks |
Contributing
Issues and PRs welcome! Please check existing issues before submitting.
License
MIT
