agentshield-sdk 7.0.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 (84) hide show
  1. package/CHANGELOG.md +191 -0
  2. package/LICENSE +21 -0
  3. package/README.md +975 -0
  4. package/bin/agent-shield.js +680 -0
  5. package/package.json +118 -0
  6. package/src/adaptive.js +330 -0
  7. package/src/agent-protocol.js +998 -0
  8. package/src/alert-tuning.js +480 -0
  9. package/src/allowlist.js +603 -0
  10. package/src/audit-immutable.js +914 -0
  11. package/src/audit-streaming.js +469 -0
  12. package/src/badges.js +196 -0
  13. package/src/behavior-profiling.js +289 -0
  14. package/src/benchmark-harness.js +804 -0
  15. package/src/canary.js +271 -0
  16. package/src/certification.js +563 -0
  17. package/src/circuit-breaker.js +321 -0
  18. package/src/compliance.js +617 -0
  19. package/src/confidence-tuning.js +324 -0
  20. package/src/confused-deputy.js +624 -0
  21. package/src/context-scoring.js +360 -0
  22. package/src/conversation.js +494 -0
  23. package/src/cost-optimizer.js +1024 -0
  24. package/src/ctf.js +462 -0
  25. package/src/detector-core.js +1999 -0
  26. package/src/distributed.js +359 -0
  27. package/src/document-scanner.js +795 -0
  28. package/src/embedding.js +307 -0
  29. package/src/encoding.js +429 -0
  30. package/src/enterprise.js +405 -0
  31. package/src/errors.js +100 -0
  32. package/src/eu-ai-act.js +523 -0
  33. package/src/fuzzer.js +764 -0
  34. package/src/honeypot.js +328 -0
  35. package/src/i18n-patterns.js +523 -0
  36. package/src/index.js +430 -0
  37. package/src/integrations.js +528 -0
  38. package/src/llm-redteam.js +670 -0
  39. package/src/main.js +741 -0
  40. package/src/main.mjs +38 -0
  41. package/src/mcp-bridge.js +542 -0
  42. package/src/mcp-certification.js +846 -0
  43. package/src/mcp-sdk-integration.js +355 -0
  44. package/src/mcp-security-runtime.js +741 -0
  45. package/src/mcp-server.js +740 -0
  46. package/src/middleware.js +208 -0
  47. package/src/model-finetuning.js +884 -0
  48. package/src/model-fingerprint.js +1042 -0
  49. package/src/multi-agent-trust.js +453 -0
  50. package/src/multi-agent.js +404 -0
  51. package/src/multimodal.js +296 -0
  52. package/src/nist-mapping.js +505 -0
  53. package/src/observability.js +330 -0
  54. package/src/openclaw.js +450 -0
  55. package/src/otel.js +544 -0
  56. package/src/owasp-2025.js +483 -0
  57. package/src/pii.js +390 -0
  58. package/src/plugin-marketplace.js +628 -0
  59. package/src/plugin-system.js +349 -0
  60. package/src/policy-dsl.js +775 -0
  61. package/src/policy-extended.js +635 -0
  62. package/src/policy.js +443 -0
  63. package/src/presets.js +409 -0
  64. package/src/production.js +557 -0
  65. package/src/prompt-leakage.js +321 -0
  66. package/src/rag-vulnerability.js +579 -0
  67. package/src/redteam.js +475 -0
  68. package/src/response-handler.js +429 -0
  69. package/src/scanners.js +357 -0
  70. package/src/self-healing.js +363 -0
  71. package/src/semantic.js +339 -0
  72. package/src/shield-score.js +250 -0
  73. package/src/sso-saml.js +897 -0
  74. package/src/stream-scanner.js +806 -0
  75. package/src/testing.js +505 -0
  76. package/src/threat-encyclopedia.js +629 -0
  77. package/src/threat-intel-network.js +1017 -0
  78. package/src/token-analysis.js +467 -0
  79. package/src/tool-guard.js +412 -0
  80. package/src/tool-output-validator.js +354 -0
  81. package/src/utils.js +83 -0
  82. package/src/watermark.js +235 -0
  83. package/src/worker-scanner.js +601 -0
  84. package/types/index.d.ts +2088 -0
@@ -0,0 +1,617 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Agent Shield — Compliance, Audit Trail & Incident Playbook
5
+ *
6
+ * Features:
7
+ * - Compliance report generation (OWASP LLM Top 10, EU AI Act, SOC2)
8
+ * - Audit trail export (JSON, CSV)
9
+ * - Incident response playbooks
10
+ * - Security checklist generator
11
+ */
12
+
13
+ // =========================================================================
14
+ // Compliance Frameworks
15
+ // =========================================================================
16
+
17
+ const COMPLIANCE_FRAMEWORKS = {
18
+ owasp_llm: {
19
+ name: 'OWASP LLM Top 10',
20
+ version: '1.1',
21
+ controls: [
22
+ { id: 'LLM01', name: 'Prompt Injection', check: 'injection_scanning', description: 'Validate and sanitize all inputs to the LLM' },
23
+ { id: 'LLM02', name: 'Insecure Output Handling', check: 'output_scanning', description: 'Validate and sanitize LLM outputs' },
24
+ { id: 'LLM03', name: 'Training Data Poisoning', check: 'data_validation', description: 'Ensure training data integrity' },
25
+ { id: 'LLM04', name: 'Model Denial of Service', check: 'rate_limiting', description: 'Implement rate limiting and resource controls' },
26
+ { id: 'LLM05', name: 'Supply Chain Vulnerabilities', check: 'supply_chain', description: 'Audit third-party components and plugins' },
27
+ { id: 'LLM06', name: 'Sensitive Information Disclosure', check: 'pii_protection', description: 'Implement PII detection and DLP' },
28
+ { id: 'LLM07', name: 'Insecure Plugin Design', check: 'tool_permissions', description: 'Restrict and validate tool/plugin access' },
29
+ { id: 'LLM08', name: 'Excessive Agency', check: 'permission_boundaries', description: 'Limit agent capabilities and require human approval' },
30
+ { id: 'LLM09', name: 'Overreliance', check: 'output_validation', description: 'Validate AI outputs before acting on them' },
31
+ { id: 'LLM10', name: 'Model Theft', check: 'access_control', description: 'Protect model access and API keys' }
32
+ ]
33
+ },
34
+
35
+ eu_ai_act: {
36
+ name: 'EU AI Act',
37
+ version: '2024',
38
+ controls: [
39
+ { id: 'AIA-1', name: 'Risk Assessment', check: 'risk_assessment', description: 'Document AI system risk level' },
40
+ { id: 'AIA-2', name: 'Transparency', check: 'logging', description: 'Log all AI interactions for audit' },
41
+ { id: 'AIA-3', name: 'Human Oversight', check: 'human_in_loop', description: 'Maintain human oversight of AI decisions' },
42
+ { id: 'AIA-4', name: 'Data Governance', check: 'pii_protection', description: 'Protect personal data in AI processing' },
43
+ { id: 'AIA-5', name: 'Technical Documentation', check: 'documentation', description: 'Maintain documentation of AI system capabilities' },
44
+ { id: 'AIA-6', name: 'Record Keeping', check: 'audit_trail', description: 'Keep records of AI system operation' },
45
+ { id: 'AIA-7', name: 'Accuracy & Robustness', check: 'accuracy_testing', description: 'Test AI system for accuracy and adversarial robustness' }
46
+ ]
47
+ },
48
+
49
+ soc2: {
50
+ name: 'SOC 2 (AI Controls)',
51
+ version: '2024',
52
+ controls: [
53
+ { id: 'SOC2-CC6.1', name: 'Logical Access', check: 'access_control', description: 'Control access to AI systems and data' },
54
+ { id: 'SOC2-CC6.3', name: 'Authorization', check: 'permission_boundaries', description: 'Authorize AI agent actions appropriately' },
55
+ { id: 'SOC2-CC7.1', name: 'Detection', check: 'injection_scanning', description: 'Detect unauthorized activities in AI systems' },
56
+ { id: 'SOC2-CC7.2', name: 'Monitoring', check: 'logging', description: 'Monitor AI system activities' },
57
+ { id: 'SOC2-CC8.1', name: 'Change Management', check: 'supply_chain', description: 'Control changes to AI system components' },
58
+ { id: 'SOC2-P3.1', name: 'Privacy Notice', check: 'pii_protection', description: 'Protect personal information in AI processing' }
59
+ ]
60
+ },
61
+
62
+ nist_ai: {
63
+ name: 'NIST AI RMF',
64
+ version: '1.0',
65
+ controls: [
66
+ { id: 'GOVERN-1', name: 'AI Risk Culture', check: 'documentation', description: 'Establish AI risk management culture' },
67
+ { id: 'MAP-1', name: 'Context Mapping', check: 'risk_assessment', description: 'Map AI system context and intended use' },
68
+ { id: 'MEASURE-1', name: 'Risk Measurement', check: 'accuracy_testing', description: 'Measure AI risks through testing' },
69
+ { id: 'MANAGE-1', name: 'Risk Treatment', check: 'injection_scanning', description: 'Treat identified AI risks' },
70
+ { id: 'MANAGE-2', name: 'Incident Response', check: 'incident_response', description: 'Respond to AI-related incidents' }
71
+ ]
72
+ }
73
+ };
74
+
75
+ // What Agent Shield features satisfy which checks
76
+ const FEATURE_CHECK_MAP = {
77
+ injection_scanning: { module: 'detector-core', description: 'Input/output scanning for injection patterns' },
78
+ output_scanning: { module: 'detector-core', description: 'Output scanning for dangerous content' },
79
+ rate_limiting: { module: 'circuit-breaker', description: 'Rate limiting and circuit breaker' },
80
+ pii_protection: { module: 'pii', description: 'PII detection and redaction' },
81
+ tool_permissions: { module: 'tool-guard', description: 'Tool permission boundaries' },
82
+ permission_boundaries: { module: 'tool-guard', description: 'Permission boundaries and input quarantine' },
83
+ logging: { module: 'policy', description: 'Structured logging and audit trail' },
84
+ supply_chain: { module: 'multi-agent', description: 'Agent firewall and delegation chain validation' },
85
+ access_control: { module: 'canary', description: 'Canary tokens and credential detection' },
86
+ output_validation: { module: 'watermark', description: 'Output watermarking and validation' },
87
+ audit_trail: { module: 'policy', description: 'Structured logging to file/webhook' },
88
+ incident_response: { module: 'compliance', description: 'Incident response playbooks' },
89
+ data_validation: { available: false, description: 'Not yet implemented — training data validation' },
90
+ risk_assessment: { available: false, description: 'Manual — document using Shield Score' },
91
+ human_in_loop: { available: false, description: 'Manual — implement approval workflows' },
92
+ documentation: { available: false, description: 'Manual — maintain system documentation' },
93
+ accuracy_testing: { module: 'shield-score', description: 'Shield Score and benchmarking suite' }
94
+ };
95
+
96
+ // =========================================================================
97
+ // Compliance Report Generator
98
+ // =========================================================================
99
+
100
+ class ComplianceReporter {
101
+ constructor(options = {}) {
102
+ this.enabledModules = options.enabledModules || Object.keys(FEATURE_CHECK_MAP).filter(k => FEATURE_CHECK_MAP[k].module);
103
+ this.framework = options.framework || 'owasp_llm';
104
+ }
105
+
106
+ /**
107
+ * Generate a compliance report for a specific framework.
108
+ */
109
+ generateReport(frameworkId) {
110
+ const fw = COMPLIANCE_FRAMEWORKS[frameworkId || this.framework];
111
+ if (!fw) throw new Error(`Unknown framework: ${frameworkId}`);
112
+
113
+ const controls = fw.controls.map(ctrl => {
114
+ const feature = FEATURE_CHECK_MAP[ctrl.check];
115
+ const implemented = feature && feature.module && this.enabledModules.includes(ctrl.check);
116
+ const available = feature && feature.module;
117
+
118
+ return {
119
+ ...ctrl,
120
+ status: implemented ? 'compliant' : (available ? 'available' : 'manual'),
121
+ feature: feature ? feature.description : 'Not mapped',
122
+ module: feature ? feature.module : null
123
+ };
124
+ });
125
+
126
+ const compliant = controls.filter(c => c.status === 'compliant').length;
127
+ const available = controls.filter(c => c.status === 'available').length;
128
+ const manual = controls.filter(c => c.status === 'manual').length;
129
+
130
+ return {
131
+ framework: fw.name,
132
+ version: fw.version,
133
+ date: new Date().toISOString(),
134
+ summary: {
135
+ total: controls.length,
136
+ compliant,
137
+ available,
138
+ manual,
139
+ complianceRate: `${((compliant / controls.length) * 100).toFixed(1)}%`
140
+ },
141
+ controls
142
+ };
143
+ }
144
+
145
+ /**
146
+ * Generate reports for all frameworks.
147
+ */
148
+ generateAllReports() {
149
+ const reports = {};
150
+ for (const key of Object.keys(COMPLIANCE_FRAMEWORKS)) {
151
+ reports[key] = this.generateReport(key);
152
+ }
153
+ return reports;
154
+ }
155
+
156
+ /**
157
+ * Format a compliance report for console output.
158
+ */
159
+ formatReport(report) {
160
+ const lines = [];
161
+ lines.push('');
162
+ lines.push(`╔══════════════════════════════════════════════════════╗`);
163
+ lines.push(`║ COMPLIANCE REPORT: ${report.framework.padEnd(24)}║`);
164
+ lines.push(`╚══════════════════════════════════════════════════════╝`);
165
+ lines.push('');
166
+ lines.push(` Version: ${report.version}`);
167
+ lines.push(` Date: ${report.date}`);
168
+ lines.push(` Compliance: ${report.summary.complianceRate} (${report.summary.compliant}/${report.summary.total})`);
169
+ lines.push('');
170
+
171
+ for (const ctrl of report.controls) {
172
+ const icon = ctrl.status === 'compliant' ? '✓' : (ctrl.status === 'available' ? '○' : '✗');
173
+ const color = ctrl.status === 'compliant' ? '\x1b[32m' : (ctrl.status === 'available' ? '\x1b[33m' : '\x1b[31m');
174
+ lines.push(` ${color}${icon}\x1b[0m ${ctrl.id.padEnd(12)} ${ctrl.name}`);
175
+ lines.push(` ${ctrl.description}`);
176
+ if (ctrl.status !== 'compliant') {
177
+ lines.push(` \x1b[90m→ ${ctrl.feature}\x1b[0m`);
178
+ }
179
+ }
180
+
181
+ lines.push('');
182
+ lines.push(` Legend: ✓ Compliant ○ Available (enable module) ✗ Manual action needed`);
183
+ lines.push('');
184
+ return lines.join('\n');
185
+ }
186
+ }
187
+
188
+ // =========================================================================
189
+ // Audit Trail
190
+ // =========================================================================
191
+
192
+ class AuditTrail {
193
+ constructor(options = {}) {
194
+ this.events = [];
195
+ this.maxEvents = options.maxEvents || 10000;
196
+ this.autoFlush = options.autoFlush || false;
197
+ this.flushPath = options.flushPath || null;
198
+ }
199
+
200
+ /**
201
+ * Record an audit event.
202
+ */
203
+ record(event) {
204
+ const entry = {
205
+ id: `evt_${Date.now()}_${Math.random().toString(36).slice(2, 8).padEnd(6, '0')}`,
206
+ timestamp: new Date().toISOString(),
207
+ ...event
208
+ };
209
+
210
+ this.events.push(entry);
211
+
212
+ // Remove oldest event when at capacity (O(1) vs slice which is O(n))
213
+ while (this.events.length > this.maxEvents) {
214
+ this.events.shift();
215
+ }
216
+
217
+ if (this.autoFlush && this.flushPath) {
218
+ this.flush();
219
+ }
220
+
221
+ return entry;
222
+ }
223
+
224
+ /**
225
+ * Record a scan event.
226
+ */
227
+ recordScan(input, result, metadata = {}) {
228
+ return this.record({
229
+ type: 'scan',
230
+ input: input.substring(0, 200),
231
+ status: result.status,
232
+ threatCount: result.threats ? result.threats.length : 0,
233
+ blocked: result.blocked || false,
234
+ threats: (result.threats || []).map(t => ({ severity: t.severity, category: t.category, description: t.description })),
235
+ ...metadata
236
+ });
237
+ }
238
+
239
+ /**
240
+ * Record a block event.
241
+ */
242
+ recordBlock(reason, input, threats, metadata = {}) {
243
+ const threatList = Array.isArray(threats) ? threats : [];
244
+ return this.record({
245
+ type: 'block',
246
+ reason,
247
+ blocked: true,
248
+ input: input.substring(0, 200),
249
+ threats: threatList.map(t => ({ severity: t.severity, category: t.category })),
250
+ ...metadata
251
+ });
252
+ }
253
+
254
+ /**
255
+ * Record a tool call.
256
+ */
257
+ recordToolCall(tool, args, result, metadata = {}) {
258
+ return this.record({
259
+ type: 'tool_call',
260
+ tool,
261
+ args: typeof args === 'object' ? JSON.stringify(args).substring(0, 200) : String(args).substring(0, 200),
262
+ allowed: result.allowed !== false,
263
+ ...metadata
264
+ });
265
+ }
266
+
267
+ /**
268
+ * Export events as JSON.
269
+ */
270
+ exportJSON() {
271
+ return JSON.stringify(this.events, null, 2);
272
+ }
273
+
274
+ /**
275
+ * Export events as CSV.
276
+ */
277
+ exportCSV() {
278
+ if (this.events.length === 0) return '';
279
+
280
+ const headers = ['id', 'timestamp', 'type', 'status', 'blocked', 'threatCount', 'input'];
281
+ const rows = [headers.join(',')];
282
+
283
+ for (const evt of this.events) {
284
+ rows.push([
285
+ evt.id,
286
+ evt.timestamp,
287
+ evt.type || '',
288
+ evt.status || '',
289
+ evt.blocked || false,
290
+ evt.threatCount || 0,
291
+ `"${(evt.input || '').replace(/"/g, '""')}"`
292
+ ].join(','));
293
+ }
294
+
295
+ return rows.join('\n');
296
+ }
297
+
298
+ /**
299
+ * Flush events to file.
300
+ */
301
+ flush(filePath) {
302
+ const targetPath = filePath || this.flushPath;
303
+ if (!targetPath) return;
304
+
305
+ const fs = require('fs');
306
+ const ext = require('path').extname(targetPath).toLowerCase();
307
+
308
+ if (ext === '.csv') {
309
+ fs.writeFileSync(targetPath, this.exportCSV());
310
+ } else {
311
+ fs.writeFileSync(targetPath, this.exportJSON());
312
+ }
313
+ }
314
+
315
+ /**
316
+ * Query events.
317
+ */
318
+ query(filters = {}) {
319
+ const sinceDate = filters.since ? new Date(filters.since) : null;
320
+ const untilDate = filters.until ? new Date(filters.until) : null;
321
+
322
+ return this.events.filter(e => {
323
+ if (filters.type && e.type !== filters.type) return false;
324
+ if (filters.blocked !== undefined && e.blocked !== filters.blocked) return false;
325
+ if (sinceDate && new Date(e.timestamp) < sinceDate) return false;
326
+ if (untilDate && new Date(e.timestamp) > untilDate) return false;
327
+ if (filters.minThreats && (e.threatCount || 0) < filters.minThreats) return false;
328
+ return true;
329
+ });
330
+ }
331
+
332
+ /**
333
+ * Get summary statistics.
334
+ */
335
+ getSummary() {
336
+ const total = this.events.length;
337
+ const blocks = this.events.filter(e => e.blocked).length;
338
+ const scans = this.events.filter(e => e.type === 'scan').length;
339
+ const toolCalls = this.events.filter(e => e.type === 'tool_call').length;
340
+ const threats = this.events.reduce((sum, e) => sum + (e.threatCount || 0), 0);
341
+
342
+ return { total, scans, blocks, toolCalls, threats };
343
+ }
344
+
345
+ /**
346
+ * Clear all events.
347
+ */
348
+ clear() {
349
+ this.events = [];
350
+ }
351
+ }
352
+
353
+ // =========================================================================
354
+ // Incident Playbook
355
+ // =========================================================================
356
+
357
+ const INCIDENT_PLAYBOOKS = {
358
+ prompt_injection: {
359
+ name: 'Prompt Injection Detected',
360
+ severity: 'high',
361
+ steps: [
362
+ { action: 'block', description: 'Immediately block the request' },
363
+ { action: 'log', description: 'Log the full request with context (IP, session, user agent)' },
364
+ { action: 'alert', description: 'Send alert to security team via webhook' },
365
+ { action: 'rate_limit', description: 'Apply rate limiting to the source' },
366
+ { action: 'review', description: 'Review recent requests from the same source for patterns' },
367
+ { action: 'update', description: 'Check if the attack pattern is new and update detection rules' }
368
+ ]
369
+ },
370
+
371
+ data_exfiltration: {
372
+ name: 'Data Exfiltration Attempt',
373
+ severity: 'critical',
374
+ steps: [
375
+ { action: 'block', description: 'Block the output immediately' },
376
+ { action: 'quarantine', description: 'Quarantine the conversation/session' },
377
+ { action: 'log', description: 'Log the full conversation history' },
378
+ { action: 'alert', description: 'Send critical alert to security team and CISO' },
379
+ { action: 'investigate', description: 'Determine what data was targeted' },
380
+ { action: 'notify', description: 'If data was exposed, trigger data breach notification process' },
381
+ { action: 'remediate', description: 'Rotate any potentially exposed credentials' }
382
+ ]
383
+ },
384
+
385
+ credential_leak: {
386
+ name: 'Credential Exposure',
387
+ severity: 'critical',
388
+ steps: [
389
+ { action: 'block', description: 'Block the output immediately' },
390
+ { action: 'rotate', description: 'Immediately rotate the exposed credential' },
391
+ { action: 'audit', description: 'Audit usage of the credential for unauthorized access' },
392
+ { action: 'log', description: 'Log the incident with full context' },
393
+ { action: 'alert', description: 'Alert security team and credential owner' },
394
+ { action: 'review', description: 'Review all locations where the credential is used' }
395
+ ]
396
+ },
397
+
398
+ jailbreak: {
399
+ name: 'Jailbreak Attempt',
400
+ severity: 'high',
401
+ steps: [
402
+ { action: 'block', description: 'Block the request' },
403
+ { action: 'log', description: 'Log the attempt with attack category' },
404
+ { action: 'monitor', description: 'Increase monitoring for the source' },
405
+ { action: 'rate_limit', description: 'Apply stricter rate limits' },
406
+ { action: 'review', description: 'Check if the jailbreak technique is new' }
407
+ ]
408
+ },
409
+
410
+ circuit_breaker_trip: {
411
+ name: 'Circuit Breaker Tripped',
412
+ severity: 'high',
413
+ steps: [
414
+ { action: 'alert', description: 'Alert operations team' },
415
+ { action: 'investigate', description: 'Determine if this is an active attack or false positives' },
416
+ { action: 'review', description: 'Review all blocked requests during the window' },
417
+ { action: 'adjust', description: 'Adjust circuit breaker thresholds if needed' },
418
+ { action: 'resume', description: 'Manually reset circuit breaker after investigation' }
419
+ ]
420
+ },
421
+
422
+ pii_exposure: {
423
+ name: 'PII Exposure',
424
+ severity: 'high',
425
+ steps: [
426
+ { action: 'redact', description: 'Redact PII from all outputs' },
427
+ { action: 'log', description: 'Log PII types detected (not the PII itself)' },
428
+ { action: 'alert', description: 'Alert privacy/compliance team' },
429
+ { action: 'investigate', description: 'Determine the source of PII in the system' },
430
+ { action: 'notify', description: 'If PII was exposed externally, follow breach notification procedures' }
431
+ ]
432
+ }
433
+ };
434
+
435
+ class IncidentPlaybook {
436
+ constructor() {
437
+ this.playbooks = INCIDENT_PLAYBOOKS;
438
+ }
439
+
440
+ /**
441
+ * Get a playbook by threat type.
442
+ */
443
+ get(threatType) {
444
+ return this.playbooks[threatType] || null;
445
+ }
446
+
447
+ /**
448
+ * Get all playbooks.
449
+ */
450
+ getAll() {
451
+ return Object.entries(this.playbooks).map(([key, val]) => ({ key, ...val }));
452
+ }
453
+
454
+ /**
455
+ * Get the recommended playbook for a scan result.
456
+ */
457
+ recommend(scanResult) {
458
+ if (!scanResult.threats || scanResult.threats.length === 0) return null;
459
+
460
+ // Find the most severe threat category
461
+ const categories = scanResult.threats.map(t => t.category);
462
+
463
+ if (categories.includes('data_exfiltration')) return { key: 'data_exfiltration', ...this.playbooks.data_exfiltration };
464
+ if (categories.includes('credential_leak')) return { key: 'credential_leak', ...this.playbooks.credential_leak };
465
+ if (categories.includes('pii')) return { key: 'pii_exposure', ...this.playbooks.pii_exposure };
466
+ if (categories.includes('prompt_injection') || categories.includes('instruction_override')) return { key: 'prompt_injection', ...this.playbooks.prompt_injection };
467
+ if (categories.includes('jailbreak')) return { key: 'jailbreak', ...this.playbooks.jailbreak };
468
+
469
+ return { key: 'prompt_injection', ...this.playbooks.prompt_injection };
470
+ }
471
+
472
+ /**
473
+ * Format a playbook for console output.
474
+ */
475
+ format(playbook) {
476
+ if (!playbook) return 'No playbook available.';
477
+
478
+ const lines = [];
479
+ lines.push(`\n Incident Playbook: ${playbook.name}`);
480
+ lines.push(` Severity: ${playbook.severity.toUpperCase()}`);
481
+ lines.push('');
482
+ playbook.steps.forEach((step, i) => {
483
+ lines.push(` ${i + 1}. [${step.action.toUpperCase()}] ${step.description}`);
484
+ });
485
+ lines.push('');
486
+ return lines.join('\n');
487
+ }
488
+ }
489
+
490
+ // =========================================================================
491
+ // Security Checklist Generator
492
+ // =========================================================================
493
+
494
+ const CHECKLIST_ITEMS = [
495
+ // Input protection
496
+ { category: 'Input Protection', item: 'Enable input scanning for prompt injection', env: ['development', 'staging', 'production'], priority: 'critical' },
497
+ { category: 'Input Protection', item: 'Set sensitivity to "high" for production', env: ['production'], priority: 'high' },
498
+ { category: 'Input Protection', item: 'Enable PII detection on user inputs', env: ['staging', 'production'], priority: 'high' },
499
+ { category: 'Input Protection', item: 'Configure input quarantine for untrusted sources', env: ['production'], priority: 'medium' },
500
+ { category: 'Input Protection', item: 'Enable encoding/steganography detection', env: ['production'], priority: 'medium' },
501
+
502
+ // Output protection
503
+ { category: 'Output Protection', item: 'Enable output scanning', env: ['development', 'staging', 'production'], priority: 'critical' },
504
+ { category: 'Output Protection', item: 'Enable PII redaction on outputs', env: ['staging', 'production'], priority: 'high' },
505
+ { category: 'Output Protection', item: 'Deploy canary tokens in system prompts', env: ['production'], priority: 'high' },
506
+ { category: 'Output Protection', item: 'Enable output watermarking', env: ['production'], priority: 'low' },
507
+ { category: 'Output Protection', item: 'Configure DLP rules for sensitive data', env: ['production'], priority: 'high' },
508
+
509
+ // Tool protection
510
+ { category: 'Tool Protection', item: 'Define permission boundaries for all tools', env: ['development', 'staging', 'production'], priority: 'critical' },
511
+ { category: 'Tool Protection', item: 'Block dangerous tools (bash, exec, eval)', env: ['production'], priority: 'critical' },
512
+ { category: 'Tool Protection', item: 'Enable tool sequence analysis', env: ['staging', 'production'], priority: 'high' },
513
+ { category: 'Tool Protection', item: 'Set per-tool rate limits', env: ['production'], priority: 'medium' },
514
+ { category: 'Tool Protection', item: 'Configure path restrictions for file tools', env: ['production'], priority: 'high' },
515
+
516
+ // Availability
517
+ { category: 'Availability', item: 'Configure circuit breaker', env: ['staging', 'production'], priority: 'high' },
518
+ { category: 'Availability', item: 'Set up rate limiting', env: ['production'], priority: 'high' },
519
+ { category: 'Availability', item: 'Define blockOnThreat threshold', env: ['production'], priority: 'medium' },
520
+
521
+ // Monitoring
522
+ { category: 'Monitoring', item: 'Enable structured logging', env: ['development', 'staging', 'production'], priority: 'high' },
523
+ { category: 'Monitoring', item: 'Configure webhook alerts for critical threats', env: ['production'], priority: 'high' },
524
+ { category: 'Monitoring', item: 'Set up audit trail export', env: ['production'], priority: 'medium' },
525
+ { category: 'Monitoring', item: 'Enable behavioral fingerprinting', env: ['production'], priority: 'low' },
526
+
527
+ // Conversation
528
+ { category: 'Conversation', item: 'Enable fragmentation detection for multi-turn', env: ['staging', 'production'], priority: 'medium' },
529
+ { category: 'Conversation', item: 'Configure instruction hierarchy', env: ['production'], priority: 'medium' },
530
+ { category: 'Conversation', item: 'Enable language switch detection', env: ['production'], priority: 'low' },
531
+
532
+ // Testing
533
+ { category: 'Testing', item: 'Run Shield Score benchmark', env: ['development', 'staging'], priority: 'high' },
534
+ { category: 'Testing', item: 'Run red team attack suite', env: ['development', 'staging'], priority: 'high' },
535
+ { category: 'Testing', item: 'Test with custom attack payloads', env: ['staging'], priority: 'medium' },
536
+ { category: 'Testing', item: 'Run performance benchmarks', env: ['staging'], priority: 'medium' },
537
+ { category: 'Testing', item: 'Verify false positive rate is acceptable', env: ['staging'], priority: 'high' },
538
+
539
+ // Compliance
540
+ { category: 'Compliance', item: 'Generate OWASP LLM Top 10 compliance report', env: ['production'], priority: 'medium' },
541
+ { category: 'Compliance', item: 'Document incident response procedures', env: ['production'], priority: 'high' },
542
+ { category: 'Compliance', item: 'Set up regular audit trail exports', env: ['production'], priority: 'medium' }
543
+ ];
544
+
545
+ class SecurityChecklistGenerator {
546
+ constructor() {
547
+ this.items = CHECKLIST_ITEMS;
548
+ }
549
+
550
+ /**
551
+ * Generate a checklist for a specific environment.
552
+ */
553
+ generate(environment = 'production') {
554
+ const env = environment.toLowerCase();
555
+ const items = this.items
556
+ .filter(item => item.env.includes(env))
557
+ .sort((a, b) => {
558
+ const priorityOrder = { critical: 0, high: 1, medium: 2, low: 3 };
559
+ return (priorityOrder[a.priority] || 99) - (priorityOrder[b.priority] || 99);
560
+ });
561
+
562
+ // Group by category
563
+ const grouped = {};
564
+ for (const item of items) {
565
+ if (!grouped[item.category]) grouped[item.category] = [];
566
+ grouped[item.category].push(item);
567
+ }
568
+
569
+ return {
570
+ environment: env,
571
+ date: new Date().toISOString(),
572
+ totalItems: items.length,
573
+ categories: Object.entries(grouped).map(([name, items]) => ({
574
+ name,
575
+ items: items.map(i => ({ item: i.item, priority: i.priority, checked: false }))
576
+ }))
577
+ };
578
+ }
579
+
580
+ /**
581
+ * Format a checklist for console output.
582
+ */
583
+ format(checklist) {
584
+ const lines = [];
585
+ lines.push('');
586
+ lines.push(`╔══════════════════════════════════════════════════════╗`);
587
+ lines.push(`║ SECURITY CHECKLIST: ${checklist.environment.toUpperCase().padEnd(23)}║`);
588
+ lines.push(`╚══════════════════════════════════════════════════════╝`);
589
+ lines.push('');
590
+ lines.push(` ${checklist.totalItems} items for ${checklist.environment} environment`);
591
+ lines.push('');
592
+
593
+ for (const cat of checklist.categories) {
594
+ lines.push(` ── ${cat.name} ──`);
595
+ for (const item of cat.items) {
596
+ const priorityTag = item.priority === 'critical' ? '\x1b[31m[CRITICAL]\x1b[0m' :
597
+ item.priority === 'high' ? '\x1b[33m[HIGH]\x1b[0m' :
598
+ item.priority === 'medium' ? '\x1b[90m[MEDIUM]\x1b[0m' :
599
+ '\x1b[90m[LOW]\x1b[0m';
600
+ lines.push(` [ ] ${priorityTag} ${item.item}`);
601
+ }
602
+ lines.push('');
603
+ }
604
+
605
+ return lines.join('\n');
606
+ }
607
+ }
608
+
609
+ module.exports = {
610
+ ComplianceReporter,
611
+ AuditTrail,
612
+ IncidentPlaybook,
613
+ SecurityChecklistGenerator,
614
+ COMPLIANCE_FRAMEWORKS,
615
+ INCIDENT_PLAYBOOKS,
616
+ CHECKLIST_ITEMS
617
+ };