-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsafe-grep.sh
More file actions
executable file
·37 lines (32 loc) · 886 Bytes
/
safe-grep.sh
File metadata and controls
executable file
·37 lines (32 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash
# safe-grep — sandboxed grep for Claude Code Review
# Prevents directory traversal outside the repository root.
# Usage: safe-grep <pattern> [search_path]
set -euo pipefail
REPO_DIR="${REPO_DIR:?REPO_DIR must be set}"
PATTERN="${1:-}"
SEARCH_PATH="${2:-}"
if [[ -z "$PATTERN" ]]; then
echo "ERROR: Pattern required"
exit 1
fi
if [[ -n "$SEARCH_PATH" ]]; then
RESOLVED=$(realpath -m "$SEARCH_PATH" 2>/dev/null)
if [[ "$RESOLVED" != "$REPO_DIR" && "$RESOLVED" != "$REPO_DIR"/* ]]; then
echo "ERROR: Access denied. Can only search within the repository."
exit 1
fi
TARGET="$RESOLVED"
else
TARGET="$REPO_DIR"
fi
if [[ ! -e "$TARGET" ]]; then
echo "ERROR: Path not found: $SEARCH_PATH"
exit 1
fi
grep -rn \
--exclude-dir=".git" \
--exclude=".env" \
--exclude=".env.*" \
-- "$PATTERN" "$TARGET" 2>/dev/null \
| head -500 || true