instar 0.6.10 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instar",
3
- "version": "0.6.10",
3
+ "version": "0.6.11",
4
4
  "description": "Persistent autonomy infrastructure for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,377 @@
1
+ ---
2
+ name: credential-leak-detector
3
+ description: PostToolUse hook that scans Bash tool output for leaked credentials — API keys, tokens, private keys, and secrets — before they reach the conversation. Blocks critical leaks, redacts high-severity matches, and warns on suspicious patterns. 14 detection patterns covering OpenAI, Anthropic, AWS, GitHub, Stripe, Google, Slack, SendGrid, Twilio, PEM keys, bearer tokens, and generic secrets. No external dependencies. Trigger words: security, credential leak, secret exposure, key detection, token scan, API key leaked, credential guard, secret scanner, prevent credential leak.
4
+ license: MIT
5
+ metadata:
6
+ author: sagemindai
7
+ version: "1.0"
8
+ homepage: https://instar.sh
9
+ ---
10
+
11
+ # credential-leak-detector — Catch Leaked Credentials Before They Spread
12
+
13
+ Every time your agent runs a Bash command, the output flows back into the conversation — and into your API provider's logs, any monitoring tools, and the model's context window. If that output contains an API key, a private key, or a database password, the credential is now exposed in places you never intended.
14
+
15
+ This hook scans every Bash tool response for 14 credential patterns before the output reaches the agent. Critical matches (API keys, AWS credentials, private keys) get blocked entirely. High-severity matches get redacted with a warning. Suspicious patterns get flagged as advisories. No external dependencies — just Python stdlib.
16
+
17
+ ---
18
+
19
+ ## What Gets Detected
20
+
21
+ ### Critical (Blocks the response)
22
+
23
+ | Pattern | Example Match |
24
+ |---------|--------------|
25
+ | OpenAI API keys | `sk-proj-abc123...` |
26
+ | Anthropic API keys | `sk-ant-api03-...` |
27
+ | AWS access keys | `AKIA1234567890ABCDEF` |
28
+ | GitHub tokens (classic) | `ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx` |
29
+ | GitHub fine-grained PATs | `github_pat_xxxxxx...` |
30
+ | Stripe secret keys | `sk_live_xxxx...xxxx` |
31
+ | PEM private keys | `-----BEGIN RSA PRIVATE KEY-----` |
32
+
33
+ ### High (Blocks or redacts with warning)
34
+
35
+ | Pattern | Action |
36
+ |---------|--------|
37
+ | Google API keys | Block |
38
+ | Slack tokens | Block |
39
+ | SendGrid API keys | Block |
40
+ | Twilio auth keys | Redact + warn |
41
+ | Bearer auth tokens | Redact + warn |
42
+
43
+ ### Medium (Advisory warning)
44
+
45
+ | Pattern | Note |
46
+ |---------|------|
47
+ | Generic `password=`, `secret=`, `api_key=` assignments | Common in config output |
48
+ | 64-character hex strings | Possible SHA-256 hashes or keys |
49
+
50
+ ---
51
+
52
+ ## Installation
53
+
54
+ ### Step 1: Create the hook script
55
+
56
+ ```bash
57
+ mkdir -p .claude/hooks
58
+ ```
59
+
60
+ Create `.claude/hooks/credential-leak-detector.py` with the contents below.
61
+
62
+ ---
63
+
64
+ ## The Script
65
+
66
+ Save this as `.claude/hooks/credential-leak-detector.py`:
67
+
68
+ ```python
69
+ #!/usr/bin/env python3
70
+ """
71
+ credential-leak-detector.py — PostToolUse hook that scans Bash output
72
+ for leaked credentials. Blocks critical leaks, redacts high-severity
73
+ matches, warns on suspicious patterns.
74
+
75
+ Exit code 2 = block (critical credential found)
76
+ Exit code 0 = allow (clean or advisory-only)
77
+ """
78
+ import sys
79
+ import json
80
+ import re
81
+
82
+ # --- Masking ---
83
+
84
+ def mask(value):
85
+ """Mask a credential: first 4 + **** + last 4, or full mask if short."""
86
+ v = value.strip()
87
+ if len(v) < 12:
88
+ return "*" * len(v)
89
+ return v[:4] + "****" + v[-4:]
90
+
91
+
92
+ # --- Pattern Definitions ---
93
+ # (name, regex, severity, action)
94
+ # severity: critical, high, medium
95
+ # action: block, redact, warn
96
+
97
+ PATTERNS = [
98
+ # Critical — Block
99
+ ("OpenAI API key",
100
+ r'(sk-(?:proj-)?[a-zA-Z0-9]{20,})',
101
+ "critical", "block"),
102
+
103
+ ("Anthropic API key",
104
+ r'(sk-ant-api[a-zA-Z0-9_-]{90,})',
105
+ "critical", "block"),
106
+
107
+ ("AWS access key",
108
+ r'(AKIA[0-9A-Z]{16})',
109
+ "critical", "block"),
110
+
111
+ ("GitHub token (classic)",
112
+ r'(gh[pousr]_[A-Za-z0-9_]{36,})',
113
+ "critical", "block"),
114
+
115
+ ("GitHub fine-grained PAT",
116
+ r'(github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})',
117
+ "critical", "block"),
118
+
119
+ ("Stripe secret key",
120
+ r'(sk_(?:live|test)_[a-zA-Z0-9]{24,})',
121
+ "critical", "block"),
122
+
123
+ ("PEM private key",
124
+ r'(-----BEGIN (?:RSA |EC |DSA )?PRIVATE KEY-----)',
125
+ "critical", "block"),
126
+
127
+ # High — Block
128
+ ("Google API key",
129
+ r'(AIza[0-9A-Za-z_-]{35})',
130
+ "high", "block"),
131
+
132
+ ("Slack token",
133
+ r'(xox[bpors]-[0-9a-zA-Z-]{10,})',
134
+ "high", "block"),
135
+
136
+ ("SendGrid API key",
137
+ r'(SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43})',
138
+ "high", "block"),
139
+
140
+ # High — Redact
141
+ ("Twilio auth key",
142
+ r'(SK[0-9a-fA-F]{32})',
143
+ "high", "redact"),
144
+
145
+ ("Bearer auth token",
146
+ r'(?:Authorization|Bearer)\s*[:=]\s*Bearer\s+([^\s]{20,})',
147
+ "high", "redact"),
148
+
149
+ # Medium — Warn
150
+ ("Generic secret assignment",
151
+ r'(?:password|secret|token|api_key)\s*[=:]\s*[\'"]?([^\s\'"]{16,})',
152
+ "medium", "warn"),
153
+
154
+ ("High-entropy hex string",
155
+ r'\b([a-fA-F0-9]{64})\b',
156
+ "medium", "warn"),
157
+ ]
158
+
159
+
160
+ # --- Main ---
161
+
162
+ def scan(text):
163
+ """Scan text for credential patterns. Returns list of findings."""
164
+ findings = []
165
+ for name, pattern, severity, action in PATTERNS:
166
+ matches = re.findall(pattern, text)
167
+ for m in matches:
168
+ findings.append({
169
+ "name": name,
170
+ "severity": severity,
171
+ "action": action,
172
+ "matched": m if isinstance(m, str) else m[0],
173
+ })
174
+ return findings
175
+
176
+
177
+ def main():
178
+ try:
179
+ payload = json.load(sys.stdin)
180
+ except Exception:
181
+ sys.exit(0)
182
+
183
+ tool_name = payload.get("tool_name", "")
184
+ tool_response = payload.get("tool_response", "")
185
+
186
+ # Only scan Bash output
187
+ if tool_name != "Bash":
188
+ sys.exit(0)
189
+
190
+ if not tool_response:
191
+ sys.exit(0)
192
+
193
+ # Handle tool_response as string or dict
194
+ if isinstance(tool_response, dict):
195
+ text = tool_response.get("stdout", "") + tool_response.get("stderr", "")
196
+ else:
197
+ text = str(tool_response)
198
+
199
+ if not text:
200
+ sys.exit(0)
201
+
202
+ findings = scan(text)
203
+
204
+ if not findings:
205
+ sys.exit(0)
206
+
207
+ # Classify findings
208
+ blockers = [f for f in findings if f["action"] == "block"]
209
+ redacts = [f for f in findings if f["action"] == "redact"]
210
+ warnings = [f for f in findings if f["action"] == "warn"]
211
+
212
+ # Critical/high blockers — stop the response
213
+ if blockers:
214
+ details = []
215
+ for f in blockers:
216
+ details.append(
217
+ f" - {f['name']} [{f['severity']}]: {mask(f['matched'])}"
218
+ )
219
+ reason = (
220
+ "[credential-leak-detector] Credential(s) detected in command output. "
221
+ "Response blocked to prevent exposure.\n\n"
222
+ "Detected:\n" + "\n".join(details) + "\n\n"
223
+ "The command output contained live credentials. Do NOT re-run this "
224
+ "command or attempt to extract these values. If you need to verify "
225
+ "a credential exists, check the env var or file without printing "
226
+ "its value."
227
+ )
228
+ print(json.dumps({"decision": "block", "reason": reason}))
229
+ sys.exit(2)
230
+
231
+ # Redact findings — allow but warn with masked values
232
+ messages = []
233
+ if redacts:
234
+ parts = []
235
+ for f in redacts:
236
+ parts.append(f" - {f['name']}: {mask(f['matched'])}")
237
+ messages.append(
238
+ "[credential-leak-detector] Possible credential(s) in output "
239
+ "(redact-level):\n" + "\n".join(parts) + "\n"
240
+ "Avoid storing, logging, or repeating these values."
241
+ )
242
+
243
+ # Warn findings — advisory only
244
+ if warnings:
245
+ parts = []
246
+ for f in warnings:
247
+ parts.append(f" - {f['name']}: {mask(f['matched'])}")
248
+ messages.append(
249
+ "[credential-leak-detector] Suspicious pattern(s) in output "
250
+ "(advisory):\n" + "\n".join(parts) + "\n"
251
+ "These may be secrets. Avoid including them in commits, logs, "
252
+ "or messages."
253
+ )
254
+
255
+ if messages:
256
+ print(json.dumps({"additionalContext": "\n".join(messages)}))
257
+
258
+ sys.exit(0)
259
+
260
+
261
+ if __name__ == "__main__":
262
+ main()
263
+ ```
264
+
265
+ Make it executable:
266
+
267
+ ```bash
268
+ chmod +x .claude/hooks/credential-leak-detector.py
269
+ ```
270
+
271
+ ---
272
+
273
+ ### Step 2: Register the hook in .claude/settings.json
274
+
275
+ If `.claude/settings.json` doesn't exist, create it. If it does, add to the `hooks` section:
276
+
277
+ ```json
278
+ {
279
+ "hooks": {
280
+ "PostToolUse": [
281
+ {
282
+ "matcher": "Bash",
283
+ "hooks": [
284
+ {
285
+ "type": "command",
286
+ "command": "python3 .claude/hooks/credential-leak-detector.py"
287
+ }
288
+ ]
289
+ }
290
+ ]
291
+ }
292
+ }
293
+ ```
294
+
295
+ If you already have a `PostToolUse` array, add the matcher object to it.
296
+
297
+ ---
298
+
299
+ ### Step 3: Verify it works
300
+
301
+ Restart Claude Code, then run a command that would expose a credential:
302
+
303
+ ```bash
304
+ echo "sk-ant-api03-test1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrst"
305
+ ```
306
+
307
+ The hook should block the response and show a masked version of the detected key.
308
+
309
+ ---
310
+
311
+ ## Customizing Patterns
312
+
313
+ Edit the `PATTERNS` list in `credential-leak-detector.py` to add project-specific rules.
314
+
315
+ **Example: Detect your internal API keys**
316
+
317
+ ```python
318
+ ("Internal service key",
319
+ r'(myco_sk_[a-zA-Z0-9]{32,})',
320
+ "critical", "block"),
321
+ ```
322
+
323
+ **Example: Detect database connection strings**
324
+
325
+ ```python
326
+ ("Database connection string",
327
+ r'((?:postgres|mysql|mongodb)://[^\s]{20,})',
328
+ "high", "block"),
329
+ ```
330
+
331
+ **Example: Downgrade a pattern to warn-only**
332
+
333
+ Change the action from `"block"` to `"warn"` for patterns that produce too many false positives in your environment:
334
+
335
+ ```python
336
+ # Change high-entropy hex from warn to ignored by removing it,
337
+ # or keep it but change action:
338
+ ("High-entropy hex string",
339
+ r'\b([a-fA-F0-9]{64})\b',
340
+ "medium", "warn"), # or remove this entry entirely
341
+ ```
342
+
343
+ ---
344
+
345
+ ## How It Differs from command-guard
346
+
347
+ The **command-guard** skill is a PreToolUse hook — it blocks dangerous *commands* before they execute. This skill is a PostToolUse hook — it scans *output* after commands run. They complement each other:
348
+
349
+ | Layer | Hook | What It Catches |
350
+ |-------|------|----------------|
351
+ | command-guard | PreToolUse | `cat .env`, `echo $SECRET`, destructive commands |
352
+ | credential-leak-detector | PostToolUse | Credentials that appear in output from any command |
353
+
354
+ A command like `docker inspect` or `kubectl describe` might not look dangerous, but its output can contain mounted secrets. command-guard wouldn't catch it. This hook does.
355
+
356
+ ---
357
+
358
+ ## Going Further
359
+
360
+ This hook catches credentials after they appear in output. But in an autonomous session — a scheduled job running at 3 AM, a background worker processing a queue — "catching" means the block fires, the agent sees the message, and... then what? If nobody's watching, the agent might retry the command, work around the block, or simply stall.
361
+
362
+ **Instar turns detection into infrastructure.** Every hook event across every session gets audit-logged with timestamps, session IDs, and the masked credential type. If a credential-leak-detector block fires during a 2 AM job, it shows up in the morning's activity feed — you see exactly which command triggered it, in which session, and what the agent did next. The Telegram integration means you can get notified the moment a block fires, not hours later.
363
+
364
+ And credential scanning is just one layer. With Instar, your agent also gets:
365
+ - **Audit logging** — every tool call, every hook event, searchable and timestamped
366
+ - **Identity grounding** — hooks that fire before external messages, so the agent stays in character
367
+ - **A job scheduler** — recurring tasks on cron, running while you sleep
368
+ - **Background sessions** — parallel workers for deep tasks
369
+ - **Telegram integration** — real-time notifications and two-way control from your phone
370
+
371
+ One command, about 2 minutes:
372
+
373
+ ```bash
374
+ npx instar
375
+ ```
376
+
377
+ Your agent goes from guarded-when-you're-watching to safe-autonomously. [instar.sh](https://instar.sh)