|
2 | 2 | <h1 align="center">Context Guard</h1> |
3 | 3 | <p align="center"> |
4 | 4 | <strong>Defense in depth for Claude Code context.</strong><br> |
5 | | - Never lose your work state to context compaction again. |
| 5 | + State preservation + security layer against prompt injection, leakage, and manipulation. |
6 | 6 | </p> |
7 | 7 | <p align="center"> |
8 | 8 | <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="MIT License"></a> |
9 | 9 | <a href="#requirements"><img src="https://img.shields.io/badge/Dependencies-Zero-brightgreen.svg" alt="Zero Dependencies"></a> |
10 | 10 | <a href="https://www.gnu.org/software/bash/"><img src="https://img.shields.io/badge/Shell-Bash-green.svg" alt="Shell: Bash"></a> |
11 | 11 | <a href="https://docs.anthropic.com/en/docs/claude-code"><img src="https://img.shields.io/badge/Claude_Code-Hooks-8A2BE2.svg" alt="Claude Code"></a> |
12 | | - <a href="CHANGELOG.md"><img src="https://img.shields.io/badge/Version-0.4.1-orange.svg" alt="v0.4.1"></a> |
| 12 | + <a href="CHANGELOG.md"><img src="https://img.shields.io/badge/Version-0.5.0-orange.svg" alt="v0.5.0"></a> |
| 13 | + <a href="#context-security"><img src="https://img.shields.io/badge/Security-Layer-red.svg" alt="Security Layer"></a> |
13 | 14 | </p> |
14 | 15 | </p> |
15 | 16 |
|
@@ -110,9 +111,70 @@ bash <(curl -sL https://raw.githubusercontent.com/jlceaser/context-guard/main/in |
110 | 111 |
|
111 | 112 | --- |
112 | 113 |
|
| 114 | +## Context Security |
| 115 | + |
| 116 | +*New in v0.5.0* — Context Guard now includes a **security layer** that protects against prompt injection, credential leakage, and context manipulation. |
| 117 | + |
| 118 | +``` |
| 119 | +Tool Call ──▶ PreToolUse Hook ──▶ [Injection / File Guard] ──▶ BLOCK / WARN / PASS |
| 120 | + │ |
| 121 | + Tool Executes ▼ |
| 122 | + │ |
| 123 | + ▼ |
| 124 | + PostToolUse Hook ──▶ [Injection / Leakage / Manipulation] ──▶ WARN / LOG |
| 125 | +``` |
| 126 | + |
| 127 | +### 4 Detection Engines |
| 128 | + |
| 129 | +| Engine | What it detects | Example | |
| 130 | +|--------|----------------|---------| |
| 131 | +| **Injection Scanner** | Instruction override, identity reassignment, tag injection, jailbreak | `"ignore previous instructions"`, `<system>override</system>` | |
| 132 | +| **Leakage Scanner** | API keys, AWS credentials, private keys, JWT, tokens | `AKIA...`, `-----BEGIN RSA PRIVATE KEY-----` | |
| 133 | +| **File Guard** | Sensitive file reads/writes | `.env`, `.pem`, `id_rsa`, `credentials.json` | |
| 134 | +| **Manipulation Scanner** | Fake system tags, hidden instructions, zero-width chars | `<system-reminder>fake</system-reminder>` | |
| 135 | + |
| 136 | +### 3 Security Modes |
| 137 | + |
| 138 | +| Mode | PreToolUse | PostToolUse | Use case | |
| 139 | +|------|-----------|-------------|----------| |
| 140 | +| **`warn`** (default) | Allow + systemMessage warning | systemMessage warning | Safe for daily use | |
| 141 | +| **`block`** | Block dangerous calls | systemMessage warning | High-security environments | |
| 142 | +| **`log`** | Silent logging | Silent logging | Audit/monitoring | |
| 143 | + |
| 144 | +### Configuration |
| 145 | + |
| 146 | +```bash |
| 147 | +# Security config: ~/.claude/context-guard/security/config.sh |
| 148 | +CG_SEC_ENABLED=true # Master switch |
| 149 | +CG_SEC_MODE=warn # warn | block | log |
| 150 | +CG_SEC_INJECTION_SCAN=true # Prompt injection detection |
| 151 | +CG_SEC_LEAKAGE_SCAN=true # Credential leakage detection |
| 152 | +CG_SEC_FILE_GUARD=true # Sensitive file protection |
| 153 | +CG_SEC_MANIPULATION_SCAN=true # Context manipulation detection |
| 154 | +CG_SEC_MAX_SCAN_BYTES=50000 # Max bytes to scan per operation |
| 155 | +``` |
| 156 | + |
| 157 | +### Allowlist |
| 158 | + |
| 159 | +Suppress known-safe patterns in `~/.claude/context-guard/security/allowlist.txt`: |
| 160 | + |
| 161 | +``` |
| 162 | +# Format: tool:pattern:reason |
| 163 | +Read:.env.example:Example env file is safe |
| 164 | +Bash:base64.*test:Test fixtures use base64 |
| 165 | +*:bearer_token:Project uses bearer tokens in tests |
| 166 | +``` |
| 167 | + |
| 168 | +### Skills |
| 169 | + |
| 170 | +- `/cg-security-status` — Security dashboard: events, config, hook registration |
| 171 | +- `/cg-security-config` — Manage mode, toggles, and allowlist |
| 172 | + |
| 173 | +--- |
| 174 | + |
113 | 175 | ## Architecture |
114 | 176 |
|
115 | | -Context Guard is a **5-layer defense system**: |
| 177 | +Context Guard is a **6-layer defense system**: |
116 | 178 |
|
117 | 179 | ``` |
118 | 180 | ┌─────────────────────────── Context Guard ────────────────────────────┐ |
@@ -150,7 +212,15 @@ Context Guard is a **5-layer defense system**: |
150 | 212 | │ │ → Claude reads snapshot without being told to │ │ |
151 | 213 | │ └──────────────────────────────────────────────────────────┘ │ |
152 | 214 | │ │ |
153 | | -│ LAYER 5: Protection (rules) │ |
| 215 | +│ LAYER 5: Security (v0.5.0 — context content protection) │ |
| 216 | +│ ┌──────────────┐ ┌───────────────┐ ┌─────────────────┐ │ |
| 217 | +│ │ PreToolUse │ │ PostToolUse │ │ Config + │ │ |
| 218 | +│ │ Injection + │ │ Injection + │ │ Allowlist + │ │ |
| 219 | +│ │ File Guard │ │ Leakage + │ │ Event Log │ │ |
| 220 | +│ │ → block/warn │ │ Manipulation │ │ → audit trail │ │ |
| 221 | +│ └──────────────┘ └───────────────┘ └─────────────────┘ │ |
| 222 | +│ │ |
| 223 | +│ LAYER 6: Protection (rules) │ |
154 | 224 | │ ┌──────────────────────────────────────────────────────────┐ │ |
155 | 225 | │ │ Hookify rules: protect snapshots + installed hooks │ │ |
156 | 226 | │ └──────────────────────────────────────────────────────────┘ │ |
@@ -213,16 +283,20 @@ Changes are automatically categorized: |
213 | 283 | | `compact-guard-post.sh` | Hook (SessionStart) | Auto-detect compaction + inject recovery | |
214 | 284 | | `compact-guard-stop.sh` | Hook (Stop) | Session bookmark for cross-session continuity | |
215 | 285 | | `compact-guard-lib.sh` | Library | Shared functions (git, worktree, domain, diff, JSON) | |
| 286 | +| `security-pre-tool.sh` | Hook (PreToolUse) | Scan inputs for injection + sensitive file access | |
| 287 | +| `security-post-tool.sh` | Hook (PostToolUse) | Scan outputs for injection + leakage + manipulation | |
| 288 | +| `context-security-lib.sh` | Library | Security scanning functions (4 detection engines) | |
216 | 289 | | `/cg-snapshot` | Skill | Create manual checkpoint on demand | |
217 | 290 | | `/cg-restore` | Skill | Read and summarize latest snapshot | |
218 | 291 | | `/cg-context-status` | Skill | System health dashboard | |
219 | 292 | | `/cg-setup` | Skill | Initial setup and configuration | |
| 293 | +| `/cg-security-status` | Skill | Security dashboard: events, config, hooks | |
| 294 | +| `/cg-security-config` | Skill | Manage security mode, toggles, allowlist | |
220 | 295 | | `context-keeper` | Agent | Intelligent multi-snapshot recovery analysis | |
221 | | -| `CLAUDE.md.template` | Template | Auto-recovery instructions for Claude | |
222 | | -| `settings.json.template` | Template | Reference hook configuration | |
223 | 296 | | `context-health.sh` | Widget | StatusLine context health indicator | |
224 | 297 | | `compact-guard-rules.md` | Rules | Hookify protection for snapshots/hooks | |
225 | | -| `test.sh` | Test Suite | Validate installation and hook functionality | |
| 298 | +| `context-security-rules.md` | Rules | Hookify protection for security config/log | |
| 299 | +| `test.sh` | Test Suite | Validate installation, hooks, and security layer | |
226 | 300 |
|
227 | 301 | --- |
228 | 302 |
|
@@ -405,16 +479,17 @@ Options: |
405 | 479 |
|
406 | 480 | ## Philosophy |
407 | 481 |
|
408 | | -Context compaction is inevitable. Rather than fighting it, Context Guard embraces it: |
| 482 | +Context Guard protects both your **work state** and your **context integrity**: |
409 | 483 |
|
410 | 484 | 1. **Capture everything** — git + diffs + disk + worktrees + environment + Claude ecosystem |
411 | 485 | 2. **Inject into the summary** — systemMessage survives compaction |
412 | 486 | 3. **Auto-recover** — SessionStart detects and injects recovery context |
413 | | -4. **Multi-layer defense** — hooks (auto) + skills (manual) + agent (intelligent) + guidance (passive) + rules (protective) |
414 | | -5. **Session continuity** — bookmarks and session chain across multiple sessions |
415 | | -6. **Zero dependencies** — pure bash, works everywhere Claude Code runs |
| 487 | +4. **Guard the context** — scan inputs/outputs for injection, leakage, and manipulation |
| 488 | +5. **Multi-layer defense** — hooks (auto) + skills (manual) + agent (intelligent) + security (active) + rules (protective) |
| 489 | +6. **Session continuity** — bookmarks and session chain across multiple sessions |
| 490 | +7. **Zero dependencies** — pure bash, works everywhere Claude Code runs |
416 | 491 |
|
417 | | -The result: compaction becomes a **minor hiccup** instead of a **full reset**. |
| 492 | +The result: compaction becomes a **minor hiccup** and malicious content gets **flagged before it can cause harm**. |
418 | 493 |
|
419 | 494 | --- |
420 | 495 |
|
|
0 commit comments