@posthog/wizard 2.0.2 → 2.1.0

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.
Files changed (50) hide show
  1. package/dist/bin.js +22 -4
  2. package/dist/bin.js.map +1 -1
  3. package/dist/src/__tests__/cli.test.js +50 -3
  4. package/dist/src/__tests__/cli.test.js.map +1 -1
  5. package/dist/src/__tests__/package-json.test.d.ts +1 -0
  6. package/dist/src/__tests__/package-json.test.js +173 -0
  7. package/dist/src/__tests__/package-json.test.js.map +1 -0
  8. package/dist/src/lib/__tests__/agent-interface.test.js +1 -0
  9. package/dist/src/lib/__tests__/agent-interface.test.js.map +1 -1
  10. package/dist/src/lib/__tests__/yara-hooks.test.d.ts +1 -0
  11. package/dist/src/lib/__tests__/yara-hooks.test.js +432 -0
  12. package/dist/src/lib/__tests__/yara-hooks.test.js.map +1 -0
  13. package/dist/src/lib/__tests__/yara-scanner.test.d.ts +1 -0
  14. package/dist/src/lib/__tests__/yara-scanner.test.js +613 -0
  15. package/dist/src/lib/__tests__/yara-scanner.test.js.map +1 -0
  16. package/dist/src/lib/agent-interface.d.ts +4 -2
  17. package/dist/src/lib/agent-interface.js +40 -26
  18. package/dist/src/lib/agent-interface.js.map +1 -1
  19. package/dist/src/lib/agent-runner.js +34 -1
  20. package/dist/src/lib/agent-runner.js.map +1 -1
  21. package/dist/src/lib/commandments.js +1 -0
  22. package/dist/src/lib/commandments.js.map +1 -1
  23. package/dist/src/lib/constants.d.ts +4 -3
  24. package/dist/src/lib/constants.js +3 -2
  25. package/dist/src/lib/constants.js.map +1 -1
  26. package/dist/src/lib/skill-install.d.ts +10 -0
  27. package/dist/src/lib/skill-install.js +23 -0
  28. package/dist/src/lib/skill-install.js.map +1 -0
  29. package/dist/src/lib/version.d.ts +1 -1
  30. package/dist/src/lib/version.js +1 -1
  31. package/dist/src/lib/version.js.map +1 -1
  32. package/dist/src/lib/wizard-session.d.ts +2 -0
  33. package/dist/src/lib/wizard-session.js +1 -0
  34. package/dist/src/lib/wizard-session.js.map +1 -1
  35. package/dist/src/lib/yara-hooks.d.ts +44 -0
  36. package/dist/src/lib/yara-hooks.js +377 -0
  37. package/dist/src/lib/yara-hooks.js.map +1 -0
  38. package/dist/src/lib/yara-scanner.d.ts +61 -0
  39. package/dist/src/lib/yara-scanner.js +328 -0
  40. package/dist/src/lib/yara-scanner.js.map +1 -0
  41. package/dist/src/run.d.ts +3 -0
  42. package/dist/src/run.js +10 -0
  43. package/dist/src/run.js.map +1 -1
  44. package/dist/src/steps/add-mcp-server-to-clients/index.d.ts +2 -1
  45. package/dist/src/steps/add-mcp-server-to-clients/index.js +1 -1
  46. package/dist/src/steps/add-mcp-server-to-clients/index.js.map +1 -1
  47. package/dist/src/utils/rules/universal.md +12 -0
  48. package/dist/src/utils/types.d.ts +9 -0
  49. package/dist/src/utils/types.js.map +1 -1
  50. package/package.json +1 -1
@@ -0,0 +1,377 @@
1
+ "use strict";
2
+ /**
3
+ * YARA hook wiring for the Claude Agent SDK.
4
+ *
5
+ * Creates PreToolUse and PostToolUse hook callback arrays that
6
+ * integrate the YARA scanner into the wizard's agent loop. These
7
+ * hooks are registered in the SDK's query() options alongside the
8
+ * existing Stop hook.
9
+ *
10
+ * PreToolUse hooks block dangerous commands before execution.
11
+ * PostToolUse hooks detect violations in written code and prompt
12
+ * injection in read content, and scan context-mill skill downloads.
13
+ */
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.resetScanReport = resetScanReport;
19
+ exports.formatScanReport = formatScanReport;
20
+ exports.writeScanReport = writeScanReport;
21
+ exports.createPreToolUseYaraHooks = createPreToolUseYaraHooks;
22
+ exports.createPostToolUseYaraHooks = createPostToolUseYaraHooks;
23
+ const fs_1 = __importDefault(require("fs"));
24
+ const path_1 = __importDefault(require("path"));
25
+ const fast_glob_1 = __importDefault(require("fast-glob"));
26
+ const yara_scanner_1 = require("./yara-scanner");
27
+ const debug_1 = require("../utils/debug");
28
+ const analytics_1 = require("../utils/analytics");
29
+ const skill_install_1 = require("./skill-install");
30
+ let scanCount = 0;
31
+ const scanViolations = [];
32
+ function recordScan() {
33
+ scanCount++;
34
+ }
35
+ function recordViolation(entry) {
36
+ scanViolations.push(entry);
37
+ }
38
+ /** Reset counters (for testing) */
39
+ function resetScanReport() {
40
+ scanCount = 0;
41
+ scanViolations.length = 0;
42
+ }
43
+ /** Format the scan report summary. Returns null if no scans occurred */
44
+ function formatScanReport() {
45
+ if (scanCount === 0)
46
+ return null;
47
+ const lines = ['', '— YARA Scanner Summary —'];
48
+ const violationCount = scanViolations.length;
49
+ const cleanCount = scanCount - violationCount;
50
+ lines.push(`✓ ${scanCount} tool calls scanned, ${violationCount} violation${violationCount !== 1 ? 's' : ''} detected`);
51
+ if (violationCount > 0) {
52
+ lines.push('');
53
+ for (const v of scanViolations) {
54
+ const tag = v.action.toUpperCase();
55
+ lines.push(` [${tag}] ${v.rule} (${v.severity.toUpperCase()}) — ${v.phase}:${v.tool}`);
56
+ }
57
+ }
58
+ if (cleanCount > 0) {
59
+ lines.push('');
60
+ lines.push(`No violations: ✓ ${cleanCount} clean scan${cleanCount !== 1 ? 's' : ''}`);
61
+ }
62
+ lines.push('');
63
+ return lines.join('\n');
64
+ }
65
+ const YARA_REPORT_PATH = '/tmp/posthog-wizard-yara-report.json';
66
+ /** Write the scan report to a JSON file. Returns the file path, or null if no scans occurred. */
67
+ function writeScanReport() {
68
+ if (scanCount === 0)
69
+ return null;
70
+ const report = {
71
+ summary: {
72
+ totalScans: scanCount,
73
+ violations: scanViolations.length,
74
+ clean: scanCount - scanViolations.length,
75
+ },
76
+ violations: scanViolations,
77
+ };
78
+ try {
79
+ fs_1.default.writeFileSync(YARA_REPORT_PATH, JSON.stringify(report, null, 2));
80
+ }
81
+ catch (err) {
82
+ (0, debug_1.logToFile)('[YARA] Failed to write scan report:', err);
83
+ return null;
84
+ }
85
+ return YARA_REPORT_PATH;
86
+ }
87
+ // ─── Hook Timeouts (ms) ─────────────────────────────────────────
88
+ /** Timeout for synchronous scan hooks (PreToolUse, PostToolUse Write/Edit/Read) */
89
+ const HOOK_TIMEOUT_MS = 60;
90
+ /** Timeout for skill install hook (involves filesystem I/O) */
91
+ const SKILL_SCAN_HOOK_TIMEOUT_MS = 120;
92
+ // ─── Logging ─────────────────────────────────────────────────────
93
+ function logYaraMatch(phase, tool, match, action) {
94
+ (0, debug_1.logToFile)(`[YARA] ${phase}:${tool} [${action.toUpperCase()}] rule "${match.rule.name}" ` +
95
+ `(severity: ${match.rule.severity}, category: ${match.rule.category})\n` +
96
+ ` Description: ${match.rule.description}\n` +
97
+ ` Matched text: "${match.matchedText.substring(0, 200)}"`);
98
+ analytics_1.analytics.wizardCapture('yara rule matched', {
99
+ rule: match.rule.name,
100
+ severity: match.rule.severity,
101
+ category: match.rule.category,
102
+ action,
103
+ phase,
104
+ tool,
105
+ });
106
+ }
107
+ // ─── Severity helpers ────────────────────────────────────────────
108
+ const SEVERITY_RANK = {
109
+ critical: 4,
110
+ high: 3,
111
+ medium: 2,
112
+ low: 1,
113
+ };
114
+ /** Return the highest-severity match from a list of matches. */
115
+ function highestSeverityMatch(matches) {
116
+ return matches.reduce((worst, m) => (SEVERITY_RANK[m.rule.severity] ?? 0) >
117
+ (SEVERITY_RANK[worst.rule.severity] ?? 0)
118
+ ? m
119
+ : worst);
120
+ }
121
+ // ─── PreToolUse Hooks ────────────────────────────────────────────
122
+ /**
123
+ * Create PreToolUse hook matchers for YARA scanning.
124
+ * Scans Bash commands before execution for exfiltration,
125
+ * destructive operations, and supply chain violations.
126
+ */
127
+ function createPreToolUseYaraHooks() {
128
+ return [
129
+ {
130
+ hooks: [
131
+ (input) => {
132
+ try {
133
+ const toolName = input.tool_name;
134
+ if (toolName !== 'Bash')
135
+ return Promise.resolve({});
136
+ const toolInput = input.tool_input;
137
+ const command = typeof toolInput?.command === 'string' ? toolInput.command : '';
138
+ if (!command)
139
+ return Promise.resolve({});
140
+ recordScan();
141
+ const result = (0, yara_scanner_1.scan)(command, 'PreToolUse', 'Bash');
142
+ if (!result.matched)
143
+ return Promise.resolve({});
144
+ const match = highestSeverityMatch(result.matches);
145
+ logYaraMatch('PreToolUse', 'Bash', match, 'blocked');
146
+ recordViolation({
147
+ rule: match.rule.name,
148
+ severity: match.rule.severity,
149
+ action: 'blocked',
150
+ phase: 'PreToolUse',
151
+ tool: 'Bash',
152
+ });
153
+ return Promise.resolve({
154
+ decision: 'block',
155
+ reason: `[YARA] ${match.rule.name}: ${match.rule.description}. Command blocked for security.`,
156
+ });
157
+ }
158
+ catch (error) {
159
+ (0, debug_1.logToFile)('[YARA] PreToolUse hook error:', error);
160
+ // Fail closed: block the command if scanning fails
161
+ return Promise.resolve({
162
+ decision: 'block',
163
+ reason: '[YARA] Scanner error — command blocked as a precaution.',
164
+ });
165
+ }
166
+ },
167
+ ],
168
+ timeout: HOOK_TIMEOUT_MS,
169
+ },
170
+ ];
171
+ }
172
+ // ─── PostToolUse Hooks ───────────────────────────────────────────
173
+ /**
174
+ * Create PostToolUse hook matchers for YARA scanning.
175
+ *
176
+ * Three matchers:
177
+ * 1. Write/Edit — scan written content for PII, secrets, config violations
178
+ * 2. Read/Grep — scan read content for prompt injection
179
+ * 3. Bash (skill install) — scan downloaded skill files for poisoned content
180
+ */
181
+ function createPostToolUseYaraHooks() {
182
+ return [
183
+ // ── Write/Edit content scanning ──
184
+ {
185
+ hooks: [
186
+ (input) => {
187
+ try {
188
+ const toolName = input.tool_name;
189
+ if (toolName !== 'Write' && toolName !== 'Edit')
190
+ return Promise.resolve({});
191
+ const toolInput = input.tool_input;
192
+ // For Write, scan the content being written
193
+ // For Edit, scan the new_str (replacement text)
194
+ const content = toolName === 'Write'
195
+ ? toolInput?.content ?? ''
196
+ : toolInput?.new_str ?? '';
197
+ if (!content)
198
+ return Promise.resolve({});
199
+ recordScan();
200
+ const tool = toolName;
201
+ const result = (0, yara_scanner_1.scan)(content, 'PostToolUse', tool);
202
+ if (!result.matched)
203
+ return Promise.resolve({});
204
+ const match = highestSeverityMatch(result.matches);
205
+ logYaraMatch('PostToolUse', tool, match, 'reverted');
206
+ recordViolation({
207
+ rule: match.rule.name,
208
+ severity: match.rule.severity,
209
+ action: 'reverted',
210
+ phase: 'PostToolUse',
211
+ tool,
212
+ });
213
+ return Promise.resolve({
214
+ hookSpecificOutput: {
215
+ hookEventName: 'PostToolUse',
216
+ additionalContext: `[YARA VIOLATION] ${match.rule.name}: ${match.rule.description}. ` +
217
+ `You MUST revert this change immediately. The content you just wrote violates security policy.`,
218
+ },
219
+ });
220
+ }
221
+ catch (error) {
222
+ (0, debug_1.logToFile)('[YARA] PostToolUse Write/Edit hook error:', error);
223
+ // Fail closed: instruct the agent to revert if scanning fails
224
+ return Promise.resolve({
225
+ hookSpecificOutput: {
226
+ hookEventName: 'PostToolUse',
227
+ additionalContext: '[YARA] Scanner error — you MUST revert this change as a precaution.',
228
+ },
229
+ });
230
+ }
231
+ },
232
+ ],
233
+ timeout: HOOK_TIMEOUT_MS,
234
+ },
235
+ // ── Read/Grep prompt injection scanning ──
236
+ {
237
+ hooks: [
238
+ (input) => {
239
+ try {
240
+ const toolName = input.tool_name;
241
+ if (toolName !== 'Read' && toolName !== 'Grep')
242
+ return Promise.resolve({});
243
+ const toolResponse = input.tool_response;
244
+ const content = typeof toolResponse === 'string'
245
+ ? toolResponse
246
+ : JSON.stringify(toolResponse ?? '');
247
+ if (!content)
248
+ return Promise.resolve({});
249
+ recordScan();
250
+ const tool = toolName;
251
+ const result = (0, yara_scanner_1.scan)(content, 'PostToolUse', tool);
252
+ if (!result.matched)
253
+ return Promise.resolve({});
254
+ const match = highestSeverityMatch(result.matches);
255
+ if (match.rule.severity === 'critical') {
256
+ logYaraMatch('PostToolUse', tool, match, 'aborted');
257
+ recordViolation({
258
+ rule: match.rule.name,
259
+ severity: match.rule.severity,
260
+ action: 'aborted',
261
+ phase: 'PostToolUse',
262
+ tool,
263
+ });
264
+ // Prompt injection: abort the session — context is poisoned
265
+ return Promise.resolve({
266
+ stopReason: `[YARA CRITICAL] ${match.rule.name}: Prompt injection detected in file content. ` +
267
+ `Agent context is potentially poisoned. Session terminated for safety.`,
268
+ });
269
+ }
270
+ logYaraMatch('PostToolUse', tool, match, 'warned');
271
+ recordViolation({
272
+ rule: match.rule.name,
273
+ severity: match.rule.severity,
274
+ action: 'warned',
275
+ phase: 'PostToolUse',
276
+ tool,
277
+ });
278
+ return Promise.resolve({
279
+ hookSpecificOutput: {
280
+ hookEventName: 'PostToolUse',
281
+ additionalContext: `[YARA WARNING] ${match.rule.name}: ${match.rule.description}`,
282
+ },
283
+ });
284
+ }
285
+ catch (error) {
286
+ (0, debug_1.logToFile)('[YARA] PostToolUse Read/Grep hook error:', error);
287
+ // Fail closed: terminate session if scanning fails on read content
288
+ return Promise.resolve({
289
+ stopReason: '[YARA] Scanner error while scanning read content — session terminated as a precaution.',
290
+ });
291
+ }
292
+ },
293
+ ],
294
+ timeout: HOOK_TIMEOUT_MS,
295
+ },
296
+ // ── Context-mill skill install scanning ──
297
+ {
298
+ hooks: [
299
+ async (input) => {
300
+ try {
301
+ const toolName = input.tool_name;
302
+ if (toolName !== 'Bash')
303
+ return {};
304
+ const toolInput = input.tool_input;
305
+ const command = typeof toolInput?.command === 'string' ? toolInput.command : '';
306
+ // Only scan after skill install commands
307
+ if (!(0, skill_install_1.isSkillInstallCommand)(command))
308
+ return {};
309
+ // Extract skill directory from command
310
+ const dirMatch = command.match(/mkdir -p (.claude\/skills\/[^\s&]+)/);
311
+ if (!dirMatch)
312
+ return {};
313
+ const skillDir = dirMatch[1];
314
+ const cwd = input.cwd ?? process.cwd();
315
+ recordScan();
316
+ const result = await scanSkillFiles(cwd, skillDir);
317
+ if (!result.matched)
318
+ return {};
319
+ const match = highestSeverityMatch(result.matches);
320
+ logYaraMatch('PostToolUse', 'Bash (skill install)', match, 'aborted');
321
+ recordViolation({
322
+ rule: match.rule.name,
323
+ severity: match.rule.severity,
324
+ action: 'aborted',
325
+ phase: 'PostToolUse',
326
+ tool: 'Bash (skill)',
327
+ });
328
+ return {
329
+ stopReason: `[YARA CRITICAL] Poisoned skill detected in ${skillDir}: ${match.rule.name}. ` +
330
+ `The downloaded skill contains potential prompt injection. Session terminated for safety.`,
331
+ };
332
+ }
333
+ catch (error) {
334
+ (0, debug_1.logToFile)('[YARA] PostToolUse skill install hook error:', error);
335
+ // Fail closed: terminate if skill scanning fails
336
+ return {
337
+ stopReason: '[YARA] Scanner error while scanning skill files — session terminated as a precaution.',
338
+ };
339
+ }
340
+ },
341
+ ],
342
+ timeout: SKILL_SCAN_HOOK_TIMEOUT_MS,
343
+ },
344
+ ];
345
+ }
346
+ // ─── Skill File Scanner ──────────────────────────────────────────
347
+ /**
348
+ * Read and scan all text files in a skill directory for prompt injection.
349
+ */
350
+ async function scanSkillFiles(cwd, skillDir) {
351
+ const absoluteDir = path_1.default.resolve(cwd, skillDir);
352
+ if (!fs_1.default.existsSync(absoluteDir)) {
353
+ (0, debug_1.logToFile)(`[YARA] Skill directory does not exist: ${absoluteDir}`);
354
+ return { matched: false };
355
+ }
356
+ const files = await (0, fast_glob_1.default)('**/*.{md,txt,yaml,yml,json,js,ts,py,rb,sh}', {
357
+ cwd: absoluteDir,
358
+ absolute: true,
359
+ });
360
+ const fileContents = [];
361
+ for (const filePath of files) {
362
+ try {
363
+ const content = fs_1.default.readFileSync(filePath, 'utf-8');
364
+ fileContents.push({ path: filePath, content });
365
+ }
366
+ catch (err) {
367
+ (0, debug_1.logToFile)(`[YARA] Could not read skill file ${filePath}:`, err);
368
+ }
369
+ }
370
+ if (fileContents.length === 0) {
371
+ (0, debug_1.logToFile)(`[YARA] No text files found in skill directory: ${absoluteDir}`);
372
+ return { matched: false };
373
+ }
374
+ (0, debug_1.logToFile)(`[YARA] Scanning ${fileContents.length} files in skill directory: ${skillDir}`);
375
+ return (0, yara_scanner_1.scanSkillDirectory)(fileContents);
376
+ }
377
+ //# sourceMappingURL=yara-hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"yara-hooks.js","sourceRoot":"","sources":["../../../src/lib/yara-hooks.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;;;AAsDH,0CAGC;AAGD,4CAkCC;AAKD,0CAmBC;AA6DD,8DA8CC;AAYD,gEA6LC;AAxaD,4CAAoB;AACpB,gDAAwB;AACxB,0DAA2B;AAC3B,iDAA0D;AAE1D,0CAA2C;AAC3C,kDAA+C;AAC/C,mDAAwD;AAiCxD,IAAI,SAAS,GAAG,CAAC,CAAC;AAClB,MAAM,cAAc,GAAsB,EAAE,CAAC;AAE7C,SAAS,UAAU;IACjB,SAAS,EAAE,CAAC;AACd,CAAC;AAED,SAAS,eAAe,CAAC,KAAsB;IAC7C,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC7B,CAAC;AAED,mCAAmC;AACnC,SAAgB,eAAe;IAC7B,SAAS,GAAG,CAAC,CAAC;IACd,cAAc,CAAC,MAAM,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,wEAAwE;AACxE,SAAgB,gBAAgB;IAC9B,IAAI,SAAS,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAEjC,MAAM,KAAK,GAAa,CAAC,EAAE,EAAE,0BAA0B,CAAC,CAAC;IACzD,MAAM,cAAc,GAAG,cAAc,CAAC,MAAM,CAAC;IAC7C,MAAM,UAAU,GAAG,SAAS,GAAG,cAAc,CAAC;IAE9C,KAAK,CAAC,IAAI,CACR,KAAK,SAAS,wBAAwB,cAAc,aAClD,cAAc,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAC/B,WAAW,CACZ,CAAC;IAEF,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;QACvB,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACf,KAAK,MAAM,CAAC,IAAI,cAAc,EAA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AAE,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC;QAChC,IAAA,iBAAS,EAAC,0CAA0C,WAAW,EAAE,CAAC,CAAC;QACnE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,IAAA,mBAAE,EAAC,4CAA4C,EAAE;QACnE,GAAG,EAAE,WAAW;QAChB,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IAEH,MAAM,YAAY,GAA6C,EAAE,CAAC;IAClE,KAAK,MAAM,QAAQ,IAAI,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,YAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YACnD,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAA,iBAAS,EAAC,oCAAoC,QAAQ,GAAG,EAAE,GAAG,CAAC,CAAC;QAClE,CAAC;IACH,CAAC;IAED,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,IAAA,iBAAS,EAAC,kDAAkD,WAAW,EAAE,CAAC,CAAC;QAC3E,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,IAAA,iBAAS,EACP,mBAAmB,YAAY,CAAC,MAAM,8BAA8B,QAAQ,EAAE,CAC/E,CAAC;IACF,OAAO,IAAA,iCAAkB,EAAC,YAAY,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["/**\n * YARA hook wiring for the Claude Agent SDK.\n *\n * Creates PreToolUse and PostToolUse hook callback arrays that\n * integrate the YARA scanner into the wizard's agent loop. These\n * hooks are registered in the SDK's query() options alongside the\n * existing Stop hook.\n *\n * PreToolUse hooks block dangerous commands before execution.\n * PostToolUse hooks detect violations in written code and prompt\n * injection in read content, and scan context-mill skill downloads.\n */\n\nimport fs from 'fs';\nimport path from 'path';\nimport fg from 'fast-glob';\nimport { scan, scanSkillDirectory } from './yara-scanner';\nimport type { YaraMatch, ScanResult } from './yara-scanner';\nimport { logToFile } from '../utils/debug';\nimport { analytics } from '../utils/analytics';\nimport { isSkillInstallCommand } from './skill-install';\n\n// ─── Types ───────────────────────────────────────────────────────\n// Using loose types to avoid tight coupling to SDK version.\n// The SDK hook types are: HookCallbackMatcher[], where each matcher\n// has { matcher?: string, hooks: HookCallback[], timeout?: number }\n\ntype HookInput = Record<string, unknown>;\ntype HookOutput = Record<string, unknown>;\ntype HookCallback = (\n input: HookInput,\n toolUseID: string | undefined,\n options: { signal: AbortSignal },\n) => Promise<HookOutput>;\n\nexport interface HookCallbackMatcher {\n matcher?: string;\n hooks: HookCallback[];\n timeout?: number;\n}\n\n// ─── Scan Report Accumulator ─────────────────────────────────────\n\ntype ScanAction = 'blocked' | 'reverted' | 'warned' | 'aborted';\n\ninterface ScanReportEntry {\n rule: string;\n severity: string;\n action: ScanAction;\n phase: string;\n tool: string;\n}\n\nlet scanCount = 0;\nconst scanViolations: ScanReportEntry[] = [];\n\nfunction recordScan(): void {\n scanCount++;\n}\n\nfunction recordViolation(entry: ScanReportEntry): void {\n scanViolations.push(entry);\n}\n\n/** Reset counters (for testing) */\nexport function resetScanReport(): void {\n scanCount = 0;\n scanViolations.length = 0;\n}\n\n/** Format the scan report summary. Returns null if no scans occurred */\nexport function formatScanReport(): string | null {\n if (scanCount === 0) return null;\n\n const lines: string[] = ['', '— YARA Scanner Summary —'];\n const violationCount = scanViolations.length;\n const cleanCount = scanCount - violationCount;\n\n lines.push(\n `✓ ${scanCount} tool calls scanned, ${violationCount} violation${\n violationCount !== 1 ? 's' : ''\n } detected`,\n );\n\n if (violationCount > 0) {\n lines.push('');\n for (const v of scanViolations) {\n const tag = v.action.toUpperCase();\n lines.push(\n ` [${tag}] ${v.rule} (${v.severity.toUpperCase()}) — ${v.phase}:${\n v.tool\n }`,\n );\n }\n }\n\n if (cleanCount > 0) {\n lines.push('');\n lines.push(\n `No violations: ✓ ${cleanCount} clean scan${cleanCount !== 1 ? 's' : ''}`,\n );\n }\n\n lines.push('');\n return lines.join('\\n');\n}\n\nconst YARA_REPORT_PATH = '/tmp/posthog-wizard-yara-report.json';\n\n/** Write the scan report to a JSON file. Returns the file path, or null if no scans occurred. */\nexport function writeScanReport(): string | null {\n if (scanCount === 0) return null;\n\n const report = {\n summary: {\n totalScans: scanCount,\n violations: scanViolations.length,\n clean: scanCount - scanViolations.length,\n },\n violations: scanViolations,\n };\n\n try {\n fs.writeFileSync(YARA_REPORT_PATH, JSON.stringify(report, null, 2));\n } catch (err) {\n logToFile('[YARA] Failed to write scan report:', err);\n return null;\n }\n return YARA_REPORT_PATH;\n}\n\n// ─── Hook Timeouts (ms) ─────────────────────────────────────────\n\n/** Timeout for synchronous scan hooks (PreToolUse, PostToolUse Write/Edit/Read) */\nconst HOOK_TIMEOUT_MS = 60;\n/** Timeout for skill install hook (involves filesystem I/O) */\nconst SKILL_SCAN_HOOK_TIMEOUT_MS = 120;\n\n// ─── Logging ─────────────────────────────────────────────────────\n\nfunction logYaraMatch(\n phase: string,\n tool: string,\n match: YaraMatch,\n action: ScanAction,\n): void {\n logToFile(\n `[YARA] ${phase}:${tool} [${action.toUpperCase()}] rule \"${\n match.rule.name\n }\" ` +\n `(severity: ${match.rule.severity}, category: ${match.rule.category})\\n` +\n ` Description: ${match.rule.description}\\n` +\n ` Matched text: \"${match.matchedText.substring(0, 200)}\"`,\n );\n analytics.wizardCapture('yara rule matched', {\n rule: match.rule.name,\n severity: match.rule.severity,\n category: match.rule.category,\n action,\n phase,\n tool,\n });\n}\n\n// ─── Severity helpers ────────────────────────────────────────────\n\nconst SEVERITY_RANK: Record<string, number> = {\n critical: 4,\n high: 3,\n medium: 2,\n low: 1,\n};\n\n/** Return the highest-severity match from a list of matches. */\nfunction highestSeverityMatch(matches: YaraMatch[]): YaraMatch {\n return matches.reduce((worst, m) =>\n (SEVERITY_RANK[m.rule.severity] ?? 0) >\n (SEVERITY_RANK[worst.rule.severity] ?? 0)\n ? m\n : worst,\n );\n}\n\n// ─── PreToolUse Hooks ────────────────────────────────────────────\n\n/**\n * Create PreToolUse hook matchers for YARA scanning.\n * Scans Bash commands before execution for exfiltration,\n * destructive operations, and supply chain violations.\n */\nexport function createPreToolUseYaraHooks(): HookCallbackMatcher[] {\n return [\n {\n hooks: [\n (input: HookInput): Promise<HookOutput> => {\n try {\n const toolName = input.tool_name as string;\n if (toolName !== 'Bash') return Promise.resolve({});\n\n const toolInput = input.tool_input as Record<string, unknown>;\n const command =\n typeof toolInput?.command === 'string' ? toolInput.command : '';\n\n if (!command) return Promise.resolve({});\n\n recordScan();\n const result = scan(command, 'PreToolUse', 'Bash');\n if (!result.matched) return Promise.resolve({});\n\n const match = highestSeverityMatch(result.matches);\n logYaraMatch('PreToolUse', 'Bash', match, 'blocked');\n recordViolation({\n rule: match.rule.name,\n severity: match.rule.severity,\n action: 'blocked',\n phase: 'PreToolUse',\n tool: 'Bash',\n });\n\n return Promise.resolve({\n decision: 'block',\n reason: `[YARA] ${match.rule.name}: ${match.rule.description}. Command blocked for security.`,\n });\n } catch (error) {\n logToFile('[YARA] PreToolUse hook error:', error);\n // Fail closed: block the command if scanning fails\n return Promise.resolve({\n decision: 'block',\n reason: '[YARA] Scanner error — command blocked as a precaution.',\n });\n }\n },\n ],\n timeout: HOOK_TIMEOUT_MS,\n },\n ];\n}\n\n// ─── PostToolUse Hooks ───────────────────────────────────────────\n\n/**\n * Create PostToolUse hook matchers for YARA scanning.\n *\n * Three matchers:\n * 1. Write/Edit — scan written content for PII, secrets, config violations\n * 2. Read/Grep — scan read content for prompt injection\n * 3. Bash (skill install) — scan downloaded skill files for poisoned content\n */\nexport function createPostToolUseYaraHooks(): HookCallbackMatcher[] {\n return [\n // ── Write/Edit content scanning ──\n {\n hooks: [\n (input: HookInput): Promise<HookOutput> => {\n try {\n const toolName = input.tool_name as string;\n if (toolName !== 'Write' && toolName !== 'Edit')\n return Promise.resolve({});\n\n const toolInput = input.tool_input as Record<string, unknown>;\n // For Write, scan the content being written\n // For Edit, scan the new_str (replacement text)\n const content =\n toolName === 'Write'\n ? (toolInput?.content as string) ?? ''\n : (toolInput?.new_str as string) ?? '';\n\n if (!content) return Promise.resolve({});\n\n recordScan();\n const tool = toolName;\n const result = scan(content, 'PostToolUse', tool);\n if (!result.matched) return Promise.resolve({});\n\n const match = highestSeverityMatch(result.matches);\n logYaraMatch('PostToolUse', tool, match, 'reverted');\n recordViolation({\n rule: match.rule.name,\n severity: match.rule.severity,\n action: 'reverted',\n phase: 'PostToolUse',\n tool,\n });\n\n return Promise.resolve({\n hookSpecificOutput: {\n hookEventName: 'PostToolUse',\n additionalContext:\n `[YARA VIOLATION] ${match.rule.name}: ${match.rule.description}. ` +\n `You MUST revert this change immediately. The content you just wrote violates security policy.`,\n },\n });\n } catch (error) {\n logToFile('[YARA] PostToolUse Write/Edit hook error:', error);\n // Fail closed: instruct the agent to revert if scanning fails\n return Promise.resolve({\n hookSpecificOutput: {\n hookEventName: 'PostToolUse',\n additionalContext:\n '[YARA] Scanner error — you MUST revert this change as a precaution.',\n },\n });\n }\n },\n ],\n timeout: HOOK_TIMEOUT_MS,\n },\n\n // ── Read/Grep prompt injection scanning ──\n {\n hooks: [\n (input: HookInput): Promise<HookOutput> => {\n try {\n const toolName = input.tool_name as string;\n if (toolName !== 'Read' && toolName !== 'Grep')\n return Promise.resolve({});\n\n const toolResponse = input.tool_response;\n const content =\n typeof toolResponse === 'string'\n ? toolResponse\n : JSON.stringify(toolResponse ?? '');\n\n if (!content) return Promise.resolve({});\n\n recordScan();\n const tool = toolName;\n const result = scan(content, 'PostToolUse', tool);\n if (!result.matched) return Promise.resolve({});\n\n const match = highestSeverityMatch(result.matches);\n\n if (match.rule.severity === 'critical') {\n logYaraMatch('PostToolUse', tool, match, 'aborted');\n recordViolation({\n rule: match.rule.name,\n severity: match.rule.severity,\n action: 'aborted',\n phase: 'PostToolUse',\n tool,\n });\n // Prompt injection: abort the session — context is poisoned\n return Promise.resolve({\n stopReason:\n `[YARA CRITICAL] ${match.rule.name}: Prompt injection detected in file content. ` +\n `Agent context is potentially poisoned. Session terminated for safety.`,\n });\n }\n\n logYaraMatch('PostToolUse', tool, match, 'warned');\n recordViolation({\n rule: match.rule.name,\n severity: match.rule.severity,\n action: 'warned',\n phase: 'PostToolUse',\n tool,\n });\n return Promise.resolve({\n hookSpecificOutput: {\n hookEventName: 'PostToolUse',\n additionalContext: `[YARA WARNING] ${match.rule.name}: ${match.rule.description}`,\n },\n });\n } catch (error) {\n logToFile('[YARA] PostToolUse Read/Grep hook error:', error);\n // Fail closed: terminate session if scanning fails on read content\n return Promise.resolve({\n stopReason:\n '[YARA] Scanner error while scanning read content — session terminated as a precaution.',\n });\n }\n },\n ],\n timeout: HOOK_TIMEOUT_MS,\n },\n\n // ── Context-mill skill install scanning ──\n {\n hooks: [\n async (input: HookInput): Promise<HookOutput> => {\n try {\n const toolName = input.tool_name as string;\n if (toolName !== 'Bash') return {};\n\n const toolInput = input.tool_input as Record<string, unknown>;\n const command =\n typeof toolInput?.command === 'string' ? toolInput.command : '';\n\n // Only scan after skill install commands\n if (!isSkillInstallCommand(command)) return {};\n\n // Extract skill directory from command\n const dirMatch = command.match(\n /mkdir -p (.claude\\/skills\\/[^\\s&]+)/,\n );\n if (!dirMatch) return {};\n\n const skillDir = dirMatch[1];\n const cwd = (input.cwd as string) ?? process.cwd();\n recordScan();\n const result = await scanSkillFiles(cwd, skillDir);\n\n if (!result.matched) return {};\n\n const match = highestSeverityMatch(result.matches);\n logYaraMatch(\n 'PostToolUse',\n 'Bash (skill install)',\n match,\n 'aborted',\n );\n recordViolation({\n rule: match.rule.name,\n severity: match.rule.severity,\n action: 'aborted',\n phase: 'PostToolUse',\n tool: 'Bash (skill)',\n });\n\n return {\n stopReason:\n `[YARA CRITICAL] Poisoned skill detected in ${skillDir}: ${match.rule.name}. ` +\n `The downloaded skill contains potential prompt injection. Session terminated for safety.`,\n };\n } catch (error) {\n logToFile('[YARA] PostToolUse skill install hook error:', error);\n // Fail closed: terminate if skill scanning fails\n return {\n stopReason:\n '[YARA] Scanner error while scanning skill files — session terminated as a precaution.',\n };\n }\n },\n ],\n timeout: SKILL_SCAN_HOOK_TIMEOUT_MS,\n },\n ];\n}\n\n// ─── Skill File Scanner ──────────────────────────────────────────\n\n/**\n * Read and scan all text files in a skill directory for prompt injection.\n */\nasync function scanSkillFiles(\n cwd: string,\n skillDir: string,\n): Promise<ScanResult> {\n const absoluteDir = path.resolve(cwd, skillDir);\n\n if (!fs.existsSync(absoluteDir)) {\n logToFile(`[YARA] Skill directory does not exist: ${absoluteDir}`);\n return { matched: false };\n }\n\n const files = await fg('**/*.{md,txt,yaml,yml,json,js,ts,py,rb,sh}', {\n cwd: absoluteDir,\n absolute: true,\n });\n\n const fileContents: Array<{ path: string; content: string }> = [];\n for (const filePath of files) {\n try {\n const content = fs.readFileSync(filePath, 'utf-8');\n fileContents.push({ path: filePath, content });\n } catch (err) {\n logToFile(`[YARA] Could not read skill file ${filePath}:`, err);\n }\n }\n\n if (fileContents.length === 0) {\n logToFile(`[YARA] No text files found in skill directory: ${absoluteDir}`);\n return { matched: false };\n }\n\n logToFile(\n `[YARA] Scanning ${fileContents.length} files in skill directory: ${skillDir}`,\n );\n return scanSkillDirectory(fileContents);\n}\n"]}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * YARA content scanner for the PostHog wizard.
3
+ *
4
+ * This file is the single source of truth for all wizard YARA rules.
5
+ *
6
+ * Scans tool inputs (pre-execution) and outputs (post-execution) for
7
+ * security violations including PII leakage, hardcoded secrets,
8
+ * prompt injection, and secret exfiltration.
9
+ *
10
+ * We use YARA-style regex rules rather than the real YARA C library to
11
+ * avoid native binary dependencies in an npx-distributed npm package.
12
+ *
13
+ * This is Layer 2 (L2) in the wizard's defense-in-depth model,
14
+ * complementing the prompt-based commandments (L0) and the
15
+ * canUseTool() allowlist (L1).
16
+ */
17
+ export type YaraSeverity = 'critical' | 'high' | 'medium' | 'low';
18
+ export type YaraCategory = 'posthog_pii' | 'posthog_hardcoded_key' | 'posthog_autocapture' | 'posthog_config' | 'prompt_injection' | 'exfiltration' | 'filesystem_safety' | 'supply_chain';
19
+ export type HookPhase = 'PreToolUse' | 'PostToolUse';
20
+ export type ToolTarget = 'Bash' | 'Write' | 'Edit' | 'Read' | 'Grep';
21
+ export interface YaraRule {
22
+ /** Rule name matching the .yar file (e.g. 'pii_in_capture_call') */
23
+ name: string;
24
+ description: string;
25
+ severity: YaraSeverity;
26
+ category: YaraCategory;
27
+ /** Which hook+tool combinations this rule applies to */
28
+ appliesTo: Array<{
29
+ phase: HookPhase;
30
+ tool: ToolTarget;
31
+ }>;
32
+ /** Compiled regex patterns — any match triggers the rule */
33
+ patterns: RegExp[];
34
+ }
35
+ export interface YaraMatch {
36
+ rule: YaraRule;
37
+ /** The matched substring */
38
+ matchedText: string;
39
+ /** Byte offset in the scanned content */
40
+ offset: number;
41
+ }
42
+ export type ScanResult = {
43
+ matched: false;
44
+ } | {
45
+ matched: true;
46
+ matches: YaraMatch[];
47
+ };
48
+ export declare const RULES: YaraRule[];
49
+ /**
50
+ * Scan content against rules applicable to a given hook phase and tool.
51
+ * Returns all matching rules (one match per rule, first pattern wins).
52
+ */
53
+ export declare function scan(content: string, phase: HookPhase, tool: ToolTarget): ScanResult;
54
+ /**
55
+ * Scan all files in a skill directory for prompt injection.
56
+ * Used for context-mill scanning after skill installation.
57
+ */
58
+ export declare function scanSkillDirectory(files: Array<{
59
+ path: string;
60
+ content: string;
61
+ }>): ScanResult;