@vibecheckai/cli 3.5.0 → 3.5.2

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 (224) hide show
  1. package/bin/registry.js +214 -237
  2. package/bin/runners/cli-utils.js +33 -2
  3. package/bin/runners/context/analyzer.js +52 -1
  4. package/bin/runners/context/generators/cursor.js +2 -49
  5. package/bin/runners/context/git-context.js +3 -1
  6. package/bin/runners/context/team-conventions.js +33 -7
  7. package/bin/runners/lib/analysis-core.js +25 -5
  8. package/bin/runners/lib/analyzers.js +431 -481
  9. package/bin/runners/lib/default-config.js +127 -0
  10. package/bin/runners/lib/doctor/modules/security.js +3 -1
  11. package/bin/runners/lib/engine/ast-cache.js +210 -0
  12. package/bin/runners/lib/engine/auth-extractor.js +211 -0
  13. package/bin/runners/lib/engine/billing-extractor.js +112 -0
  14. package/bin/runners/lib/engine/enforcement-extractor.js +100 -0
  15. package/bin/runners/lib/engine/env-extractor.js +207 -0
  16. package/bin/runners/lib/engine/express-extractor.js +208 -0
  17. package/bin/runners/lib/engine/extractors.js +849 -0
  18. package/bin/runners/lib/engine/index.js +207 -0
  19. package/bin/runners/lib/engine/repo-index.js +514 -0
  20. package/bin/runners/lib/engine/types.js +124 -0
  21. package/bin/runners/lib/engines/accessibility-engine.js +18 -218
  22. package/bin/runners/lib/engines/api-consistency-engine.js +30 -335
  23. package/bin/runners/lib/engines/cross-file-analysis-engine.js +27 -292
  24. package/bin/runners/lib/engines/empty-catch-engine.js +17 -127
  25. package/bin/runners/lib/engines/mock-data-engine.js +10 -53
  26. package/bin/runners/lib/engines/performance-issues-engine.js +36 -176
  27. package/bin/runners/lib/engines/security-vulnerabilities-engine.js +54 -382
  28. package/bin/runners/lib/engines/type-aware-engine.js +39 -263
  29. package/bin/runners/lib/engines/vibecheck-engines/index.js +13 -122
  30. package/bin/runners/lib/engines/vibecheck-engines/lib/ast-cache.js +164 -0
  31. package/bin/runners/lib/engines/vibecheck-engines/lib/code-quality-engine.js +291 -0
  32. package/bin/runners/lib/engines/vibecheck-engines/lib/console-logs-engine.js +83 -0
  33. package/bin/runners/lib/engines/vibecheck-engines/lib/dead-code-engine.js +198 -0
  34. package/bin/runners/lib/engines/vibecheck-engines/lib/deprecated-api-engine.js +275 -0
  35. package/bin/runners/lib/engines/vibecheck-engines/lib/empty-catch-engine.js +167 -0
  36. package/bin/runners/lib/engines/vibecheck-engines/lib/file-filter.js +217 -0
  37. package/bin/runners/lib/engines/vibecheck-engines/lib/hardcoded-secrets-engine.js +73 -373
  38. package/bin/runners/lib/engines/vibecheck-engines/lib/mock-data-engine.js +140 -0
  39. package/bin/runners/lib/engines/vibecheck-engines/lib/parallel-processor.js +164 -0
  40. package/bin/runners/lib/engines/vibecheck-engines/lib/performance-issues-engine.js +234 -0
  41. package/bin/runners/lib/engines/vibecheck-engines/lib/type-aware-engine.js +217 -0
  42. package/bin/runners/lib/engines/vibecheck-engines/lib/unsafe-regex-engine.js +78 -0
  43. package/bin/runners/lib/entitlements-v2.js +73 -97
  44. package/bin/runners/lib/error-handler.js +44 -3
  45. package/bin/runners/lib/error-messages.js +289 -0
  46. package/bin/runners/lib/evidence-pack.js +7 -1
  47. package/bin/runners/lib/finding-id.js +69 -0
  48. package/bin/runners/lib/finding-sorter.js +89 -0
  49. package/bin/runners/lib/html-proof-report.js +700 -350
  50. package/bin/runners/lib/missions/plan.js +6 -46
  51. package/bin/runners/lib/missions/templates.js +0 -232
  52. package/bin/runners/lib/next-action.js +560 -0
  53. package/bin/runners/lib/prerequisites.js +149 -0
  54. package/bin/runners/lib/route-detection.js +137 -68
  55. package/bin/runners/lib/scan-output.js +91 -76
  56. package/bin/runners/lib/scan-runner.js +135 -0
  57. package/bin/runners/lib/schemas/ajv-validator.js +464 -0
  58. package/bin/runners/lib/schemas/error-envelope.schema.json +105 -0
  59. package/bin/runners/lib/schemas/finding-v3.schema.json +151 -0
  60. package/bin/runners/lib/schemas/report-artifact.schema.json +120 -0
  61. package/bin/runners/lib/schemas/run-request.schema.json +108 -0
  62. package/bin/runners/lib/schemas/validator.js +27 -0
  63. package/bin/runners/lib/schemas/verdict.schema.json +140 -0
  64. package/bin/runners/lib/ship-output-enterprise.js +23 -23
  65. package/bin/runners/lib/ship-output.js +75 -31
  66. package/bin/runners/lib/terminal-ui.js +6 -113
  67. package/bin/runners/lib/truth.js +351 -10
  68. package/bin/runners/lib/unified-cli-output.js +430 -603
  69. package/bin/runners/lib/unified-output.js +13 -9
  70. package/bin/runners/runAIAgent.js +10 -5
  71. package/bin/runners/runAgent.js +0 -3
  72. package/bin/runners/runAllowlist.js +389 -0
  73. package/bin/runners/runApprove.js +0 -33
  74. package/bin/runners/runAuth.js +73 -45
  75. package/bin/runners/runCheckpoint.js +51 -11
  76. package/bin/runners/runClassify.js +85 -21
  77. package/bin/runners/runContext.js +0 -3
  78. package/bin/runners/runDoctor.js +41 -28
  79. package/bin/runners/runEvidencePack.js +362 -0
  80. package/bin/runners/runFirewall.js +0 -3
  81. package/bin/runners/runFirewallHook.js +0 -3
  82. package/bin/runners/runFix.js +66 -76
  83. package/bin/runners/runGuard.js +18 -411
  84. package/bin/runners/runInit.js +113 -30
  85. package/bin/runners/runLabs.js +424 -0
  86. package/bin/runners/runMcp.js +19 -25
  87. package/bin/runners/runPolish.js +64 -240
  88. package/bin/runners/runPromptFirewall.js +12 -5
  89. package/bin/runners/runProve.js +57 -22
  90. package/bin/runners/runQuickstart.js +531 -0
  91. package/bin/runners/runReality.js +59 -68
  92. package/bin/runners/runReport.js +38 -33
  93. package/bin/runners/runRuntime.js +8 -5
  94. package/bin/runners/runScan.js +1413 -190
  95. package/bin/runners/runShip.js +113 -719
  96. package/bin/runners/runTruth.js +0 -3
  97. package/bin/runners/runValidate.js +13 -9
  98. package/bin/runners/runWatch.js +23 -14
  99. package/bin/scan.js +6 -1
  100. package/bin/vibecheck.js +204 -185
  101. package/mcp-server/deprecation-middleware.js +282 -0
  102. package/mcp-server/handlers/index.ts +15 -0
  103. package/mcp-server/handlers/tool-handler.ts +554 -0
  104. package/mcp-server/index-v1.js +698 -0
  105. package/mcp-server/index.js +210 -238
  106. package/mcp-server/lib/cache-wrapper.cjs +383 -0
  107. package/mcp-server/lib/error-envelope.js +138 -0
  108. package/mcp-server/lib/executor.ts +499 -0
  109. package/mcp-server/lib/index.ts +19 -0
  110. package/mcp-server/lib/rate-limiter.js +166 -0
  111. package/mcp-server/lib/sandbox.test.ts +519 -0
  112. package/mcp-server/lib/sandbox.ts +395 -0
  113. package/mcp-server/lib/types.ts +267 -0
  114. package/mcp-server/package.json +12 -3
  115. package/mcp-server/registry/tool-registry.js +794 -0
  116. package/mcp-server/registry/tools.json +605 -0
  117. package/mcp-server/registry.test.ts +334 -0
  118. package/mcp-server/tests/tier-gating.test.js +297 -0
  119. package/mcp-server/tier-auth.js +378 -45
  120. package/mcp-server/tools-v3.js +353 -442
  121. package/mcp-server/tsconfig.json +37 -0
  122. package/mcp-server/vibecheck-2.0-tools.js +14 -1
  123. package/package.json +1 -1
  124. package/bin/runners/lib/agent-firewall/learning/learning-engine.js +0 -849
  125. package/bin/runners/lib/audit-logger.js +0 -532
  126. package/bin/runners/lib/authority/authorities/architecture.js +0 -364
  127. package/bin/runners/lib/authority/authorities/compliance.js +0 -341
  128. package/bin/runners/lib/authority/authorities/human.js +0 -343
  129. package/bin/runners/lib/authority/authorities/quality.js +0 -420
  130. package/bin/runners/lib/authority/authorities/security.js +0 -228
  131. package/bin/runners/lib/authority/index.js +0 -293
  132. package/bin/runners/lib/bundle/bundle-intelligence.js +0 -846
  133. package/bin/runners/lib/cli-charts.js +0 -368
  134. package/bin/runners/lib/cli-config-display.js +0 -405
  135. package/bin/runners/lib/cli-demo.js +0 -275
  136. package/bin/runners/lib/cli-errors.js +0 -438
  137. package/bin/runners/lib/cli-help-formatter.js +0 -439
  138. package/bin/runners/lib/cli-interactive-menu.js +0 -509
  139. package/bin/runners/lib/cli-prompts.js +0 -441
  140. package/bin/runners/lib/cli-scan-cards.js +0 -362
  141. package/bin/runners/lib/compliance-reporter.js +0 -710
  142. package/bin/runners/lib/conductor/index.js +0 -671
  143. package/bin/runners/lib/easy/README.md +0 -123
  144. package/bin/runners/lib/easy/index.js +0 -140
  145. package/bin/runners/lib/easy/interactive-wizard.js +0 -788
  146. package/bin/runners/lib/easy/one-click-firewall.js +0 -564
  147. package/bin/runners/lib/easy/zero-config-reality.js +0 -714
  148. package/bin/runners/lib/engines/async-patterns-engine.js +0 -444
  149. package/bin/runners/lib/engines/bundle-size-engine.js +0 -433
  150. package/bin/runners/lib/engines/confidence-scoring.js +0 -276
  151. package/bin/runners/lib/engines/context-detection.js +0 -264
  152. package/bin/runners/lib/engines/database-patterns-engine.js +0 -429
  153. package/bin/runners/lib/engines/duplicate-code-engine.js +0 -354
  154. package/bin/runners/lib/engines/env-variables-engine.js +0 -458
  155. package/bin/runners/lib/engines/error-handling-engine.js +0 -437
  156. package/bin/runners/lib/engines/false-positive-prevention.js +0 -630
  157. package/bin/runners/lib/engines/framework-adapters/index.js +0 -607
  158. package/bin/runners/lib/engines/framework-detection.js +0 -508
  159. package/bin/runners/lib/engines/import-order-engine.js +0 -429
  160. package/bin/runners/lib/engines/naming-conventions-engine.js +0 -544
  161. package/bin/runners/lib/engines/noise-reduction-engine.js +0 -452
  162. package/bin/runners/lib/engines/orchestrator.js +0 -334
  163. package/bin/runners/lib/engines/react-patterns-engine.js +0 -457
  164. package/bin/runners/lib/engines/vibecheck-engines/lib/ai-hallucination-engine.js +0 -806
  165. package/bin/runners/lib/engines/vibecheck-engines/lib/smart-fix-engine.js +0 -577
  166. package/bin/runners/lib/engines/vibecheck-engines/lib/vibe-score-engine.js +0 -543
  167. package/bin/runners/lib/engines/vibecheck-engines.js +0 -514
  168. package/bin/runners/lib/enhanced-features/index.js +0 -305
  169. package/bin/runners/lib/enhanced-output.js +0 -631
  170. package/bin/runners/lib/enterprise.js +0 -300
  171. package/bin/runners/lib/firewall/command-validator.js +0 -351
  172. package/bin/runners/lib/firewall/config.js +0 -341
  173. package/bin/runners/lib/firewall/content-validator.js +0 -519
  174. package/bin/runners/lib/firewall/index.js +0 -101
  175. package/bin/runners/lib/firewall/path-validator.js +0 -256
  176. package/bin/runners/lib/intelligence/cross-repo-intelligence.js +0 -817
  177. package/bin/runners/lib/mcp-utils.js +0 -425
  178. package/bin/runners/lib/output/index.js +0 -1022
  179. package/bin/runners/lib/policy-engine.js +0 -652
  180. package/bin/runners/lib/polish/autofix/accessibility-fixes.js +0 -333
  181. package/bin/runners/lib/polish/autofix/async-handlers.js +0 -273
  182. package/bin/runners/lib/polish/autofix/dead-code.js +0 -280
  183. package/bin/runners/lib/polish/autofix/imports-optimizer.js +0 -344
  184. package/bin/runners/lib/polish/autofix/index.js +0 -200
  185. package/bin/runners/lib/polish/autofix/remove-consoles.js +0 -209
  186. package/bin/runners/lib/polish/autofix/strengthen-types.js +0 -245
  187. package/bin/runners/lib/polish/backend-checks.js +0 -148
  188. package/bin/runners/lib/polish/documentation-checks.js +0 -111
  189. package/bin/runners/lib/polish/frontend-checks.js +0 -168
  190. package/bin/runners/lib/polish/index.js +0 -71
  191. package/bin/runners/lib/polish/infrastructure-checks.js +0 -131
  192. package/bin/runners/lib/polish/library-detection.js +0 -175
  193. package/bin/runners/lib/polish/performance-checks.js +0 -100
  194. package/bin/runners/lib/polish/security-checks.js +0 -148
  195. package/bin/runners/lib/polish/utils.js +0 -203
  196. package/bin/runners/lib/prompt-builder.js +0 -540
  197. package/bin/runners/lib/proof-certificate.js +0 -634
  198. package/bin/runners/lib/reality/accessibility-audit.js +0 -946
  199. package/bin/runners/lib/reality/api-contract-validator.js +0 -1012
  200. package/bin/runners/lib/reality/chaos-engineering.js +0 -1084
  201. package/bin/runners/lib/reality/performance-tracker.js +0 -1077
  202. package/bin/runners/lib/reality/scenario-generator.js +0 -1404
  203. package/bin/runners/lib/reality/visual-regression.js +0 -852
  204. package/bin/runners/lib/reality-profiler.js +0 -717
  205. package/bin/runners/lib/replay/flight-recorder-viewer.js +0 -1160
  206. package/bin/runners/lib/review/ai-code-review.js +0 -832
  207. package/bin/runners/lib/rules/custom-rule-engine.js +0 -985
  208. package/bin/runners/lib/sbom-generator.js +0 -641
  209. package/bin/runners/lib/scan-output-enhanced.js +0 -512
  210. package/bin/runners/lib/security/owasp-scanner.js +0 -939
  211. package/bin/runners/lib/validators/contract-validator.js +0 -283
  212. package/bin/runners/lib/validators/dead-export-detector.js +0 -279
  213. package/bin/runners/lib/validators/dep-audit.js +0 -245
  214. package/bin/runners/lib/validators/env-validator.js +0 -319
  215. package/bin/runners/lib/validators/index.js +0 -120
  216. package/bin/runners/lib/validators/license-checker.js +0 -252
  217. package/bin/runners/lib/validators/route-validator.js +0 -290
  218. package/bin/runners/runAuthority.js +0 -528
  219. package/bin/runners/runConductor.js +0 -772
  220. package/bin/runners/runContainer.js +0 -366
  221. package/bin/runners/runEasy.js +0 -410
  222. package/bin/runners/runIaC.js +0 -372
  223. package/bin/runners/runVibe.js +0 -791
  224. package/mcp-server/tools.js +0 -495
@@ -1,532 +0,0 @@
1
- /**
2
- * Enterprise Audit Logger
3
- *
4
- * Comprehensive audit logging for:
5
- * - Security events
6
- * - Compliance tracking
7
- * - User actions
8
- * - System changes
9
- * - Evidence generation
10
- */
11
-
12
- "use strict";
13
-
14
- const crypto = require("crypto");
15
- const fs = require("fs");
16
- const path = require("path");
17
- const os = require("os");
18
-
19
- /**
20
- * Audit event categories
21
- */
22
- const AUDIT_CATEGORIES = {
23
- SECURITY: "security",
24
- COMPLIANCE: "compliance",
25
- ACCESS: "access",
26
- CHANGE: "change",
27
- SYSTEM: "system",
28
- USER: "user",
29
- SCAN: "scan",
30
- DEPLOY: "deploy",
31
- };
32
-
33
- /**
34
- * Audit event severity levels
35
- */
36
- const AUDIT_SEVERITY = {
37
- CRITICAL: "critical",
38
- HIGH: "high",
39
- MEDIUM: "medium",
40
- LOW: "low",
41
- INFO: "info",
42
- };
43
-
44
- /**
45
- * Audit event types
46
- */
47
- const AUDIT_EVENTS = {
48
- // Security events
49
- SECRET_DETECTED: { category: AUDIT_CATEGORIES.SECURITY, severity: AUDIT_SEVERITY.CRITICAL },
50
- VULNERABILITY_FOUND: { category: AUDIT_CATEGORIES.SECURITY, severity: AUDIT_SEVERITY.HIGH },
51
- AUTH_FAILURE: { category: AUDIT_CATEGORIES.SECURITY, severity: AUDIT_SEVERITY.HIGH },
52
- AUTH_SUCCESS: { category: AUDIT_CATEGORIES.SECURITY, severity: AUDIT_SEVERITY.INFO },
53
- POLICY_VIOLATION: { category: AUDIT_CATEGORIES.SECURITY, severity: AUDIT_SEVERITY.HIGH },
54
-
55
- // Compliance events
56
- SCAN_COMPLETED: { category: AUDIT_CATEGORIES.COMPLIANCE, severity: AUDIT_SEVERITY.INFO },
57
- COMPLIANCE_CHECK: { category: AUDIT_CATEGORIES.COMPLIANCE, severity: AUDIT_SEVERITY.INFO },
58
- CERTIFICATE_GENERATED: { category: AUDIT_CATEGORIES.COMPLIANCE, severity: AUDIT_SEVERITY.INFO },
59
- REPORT_GENERATED: { category: AUDIT_CATEGORIES.COMPLIANCE, severity: AUDIT_SEVERITY.INFO },
60
-
61
- // Access events
62
- API_KEY_CREATED: { category: AUDIT_CATEGORIES.ACCESS, severity: AUDIT_SEVERITY.MEDIUM },
63
- API_KEY_REVOKED: { category: AUDIT_CATEGORIES.ACCESS, severity: AUDIT_SEVERITY.MEDIUM },
64
- API_KEY_USED: { category: AUDIT_CATEGORIES.ACCESS, severity: AUDIT_SEVERITY.INFO },
65
- PERMISSION_CHANGED: { category: AUDIT_CATEGORIES.ACCESS, severity: AUDIT_SEVERITY.MEDIUM },
66
-
67
- // Change events
68
- CONFIG_CHANGED: { category: AUDIT_CATEGORIES.CHANGE, severity: AUDIT_SEVERITY.MEDIUM },
69
- POLICY_CHANGED: { category: AUDIT_CATEGORIES.CHANGE, severity: AUDIT_SEVERITY.MEDIUM },
70
- RULE_ADDED: { category: AUDIT_CATEGORIES.CHANGE, severity: AUDIT_SEVERITY.LOW },
71
- RULE_REMOVED: { category: AUDIT_CATEGORIES.CHANGE, severity: AUDIT_SEVERITY.MEDIUM },
72
-
73
- // System events
74
- CLI_STARTED: { category: AUDIT_CATEGORIES.SYSTEM, severity: AUDIT_SEVERITY.INFO },
75
- CLI_COMPLETED: { category: AUDIT_CATEGORIES.SYSTEM, severity: AUDIT_SEVERITY.INFO },
76
- ERROR_OCCURRED: { category: AUDIT_CATEGORIES.SYSTEM, severity: AUDIT_SEVERITY.HIGH },
77
-
78
- // User events
79
- FIX_APPLIED: { category: AUDIT_CATEGORIES.USER, severity: AUDIT_SEVERITY.MEDIUM },
80
- FINDING_SUPPRESSED: { category: AUDIT_CATEGORIES.USER, severity: AUDIT_SEVERITY.LOW },
81
- OVERRIDE_REQUESTED: { category: AUDIT_CATEGORIES.USER, severity: AUDIT_SEVERITY.MEDIUM },
82
- OVERRIDE_APPROVED: { category: AUDIT_CATEGORIES.USER, severity: AUDIT_SEVERITY.MEDIUM },
83
-
84
- // Scan events
85
- SCAN_STARTED: { category: AUDIT_CATEGORIES.SCAN, severity: AUDIT_SEVERITY.INFO },
86
- FINDING_DETECTED: { category: AUDIT_CATEGORIES.SCAN, severity: AUDIT_SEVERITY.INFO },
87
- VERDICT_ISSUED: { category: AUDIT_CATEGORIES.SCAN, severity: AUDIT_SEVERITY.INFO },
88
-
89
- // Deploy events
90
- DEPLOY_BLOCKED: { category: AUDIT_CATEGORIES.DEPLOY, severity: AUDIT_SEVERITY.HIGH },
91
- DEPLOY_APPROVED: { category: AUDIT_CATEGORIES.DEPLOY, severity: AUDIT_SEVERITY.INFO },
92
- GATE_CHECK_FAILED: { category: AUDIT_CATEGORIES.DEPLOY, severity: AUDIT_SEVERITY.HIGH },
93
- GATE_CHECK_PASSED: { category: AUDIT_CATEGORIES.DEPLOY, severity: AUDIT_SEVERITY.INFO },
94
- };
95
-
96
- /**
97
- * Audit log entry structure
98
- */
99
- class AuditEntry {
100
- constructor(eventType, data = {}) {
101
- const eventConfig = AUDIT_EVENTS[eventType] || {
102
- category: AUDIT_CATEGORIES.SYSTEM,
103
- severity: AUDIT_SEVERITY.INFO
104
- };
105
-
106
- this.id = crypto.randomUUID();
107
- this.timestamp = new Date().toISOString();
108
- this.eventType = eventType;
109
- this.category = eventConfig.category;
110
- this.severity = eventConfig.severity;
111
- this.data = data;
112
- this.context = this.buildContext();
113
- this.hash = null;
114
-
115
- // Generate tamper-evident hash
116
- this.hash = this.generateHash();
117
- }
118
-
119
- buildContext() {
120
- return {
121
- hostname: os.hostname(),
122
- platform: os.platform(),
123
- arch: os.arch(),
124
- nodeVersion: process.version,
125
- pid: process.pid,
126
- cwd: process.cwd(),
127
- user: os.userInfo().username,
128
- environment: process.env.NODE_ENV || "development",
129
- };
130
- }
131
-
132
- generateHash() {
133
- const content = JSON.stringify({
134
- id: this.id,
135
- timestamp: this.timestamp,
136
- eventType: this.eventType,
137
- category: this.category,
138
- data: this.data,
139
- });
140
- return crypto.createHash("sha256").update(content).digest("hex").substring(0, 16);
141
- }
142
-
143
- toJSON() {
144
- return {
145
- id: this.id,
146
- timestamp: this.timestamp,
147
- eventType: this.eventType,
148
- category: this.category,
149
- severity: this.severity,
150
- data: this.data,
151
- context: this.context,
152
- hash: this.hash,
153
- };
154
- }
155
- }
156
-
157
- /**
158
- * Audit logger class
159
- */
160
- class AuditLogger {
161
- constructor(options = {}) {
162
- this.options = {
163
- logDir: options.logDir || path.join(process.cwd(), ".vibecheck", "audit"),
164
- maxLogSize: options.maxLogSize || 10 * 1024 * 1024, // 10MB
165
- maxLogFiles: options.maxLogFiles || 30,
166
- enableConsole: options.enableConsole !== false,
167
- enableFile: options.enableFile !== false,
168
- enableRemote: options.enableRemote || false,
169
- remoteEndpoint: options.remoteEndpoint,
170
- encryptLogs: options.encryptLogs || false,
171
- encryptionKey: options.encryptionKey,
172
- };
173
-
174
- this.sessionId = crypto.randomUUID();
175
- this.entryCount = 0;
176
- this.previousHash = null;
177
-
178
- if (this.options.enableFile) {
179
- this.ensureLogDir();
180
- }
181
- }
182
-
183
- ensureLogDir() {
184
- if (!fs.existsSync(this.options.logDir)) {
185
- fs.mkdirSync(this.options.logDir, { recursive: true });
186
- }
187
- }
188
-
189
- /**
190
- * Log an audit event
191
- */
192
- log(eventType, data = {}) {
193
- const entry = new AuditEntry(eventType, {
194
- ...data,
195
- sessionId: this.sessionId,
196
- entryNumber: ++this.entryCount,
197
- previousHash: this.previousHash,
198
- });
199
-
200
- this.previousHash = entry.hash;
201
-
202
- // Write to various destinations
203
- if (this.options.enableFile) {
204
- this.writeToFile(entry);
205
- }
206
-
207
- if (this.options.enableRemote) {
208
- this.sendToRemote(entry);
209
- }
210
-
211
- return entry;
212
- }
213
-
214
- /**
215
- * Log security event
216
- */
217
- security(eventType, data = {}) {
218
- return this.log(eventType, { ...data, _auditCategory: AUDIT_CATEGORIES.SECURITY });
219
- }
220
-
221
- /**
222
- * Log compliance event
223
- */
224
- compliance(eventType, data = {}) {
225
- return this.log(eventType, { ...data, _auditCategory: AUDIT_CATEGORIES.COMPLIANCE });
226
- }
227
-
228
- /**
229
- * Log access event
230
- */
231
- access(eventType, data = {}) {
232
- return this.log(eventType, { ...data, _auditCategory: AUDIT_CATEGORIES.ACCESS });
233
- }
234
-
235
- /**
236
- * Write entry to log file
237
- */
238
- writeToFile(entry) {
239
- const date = new Date().toISOString().split("T")[0];
240
- const logFile = path.join(this.options.logDir, `audit-${date}.jsonl`);
241
-
242
- let line = JSON.stringify(entry.toJSON()) + "\n";
243
-
244
- if (this.options.encryptLogs && this.options.encryptionKey) {
245
- line = this.encryptLine(line);
246
- }
247
-
248
- try {
249
- fs.appendFileSync(logFile, line);
250
- this.rotateLogsIfNeeded();
251
- } catch (error) {
252
- console.error("Failed to write audit log:", error.message);
253
- }
254
- }
255
-
256
- /**
257
- * Encrypt a log line
258
- */
259
- encryptLine(line) {
260
- const iv = crypto.randomBytes(16);
261
- const cipher = crypto.createCipheriv("aes-256-gcm", this.options.encryptionKey, iv);
262
- let encrypted = cipher.update(line, "utf8", "hex");
263
- encrypted += cipher.final("hex");
264
- const tag = cipher.getAuthTag();
265
- return JSON.stringify({
266
- iv: iv.toString("hex"),
267
- tag: tag.toString("hex"),
268
- data: encrypted,
269
- }) + "\n";
270
- }
271
-
272
- /**
273
- * Send entry to remote endpoint
274
- */
275
- async sendToRemote(entry) {
276
- if (!this.options.remoteEndpoint) return;
277
-
278
- try {
279
- const response = await fetch(this.options.remoteEndpoint, {
280
- method: "POST",
281
- headers: {
282
- "Content-Type": "application/json",
283
- },
284
- body: JSON.stringify(entry.toJSON()),
285
- });
286
-
287
- if (!response.ok) {
288
- console.error("Failed to send audit log to remote:", response.status);
289
- }
290
- } catch (error) {
291
- console.error("Failed to send audit log to remote:", error.message);
292
- }
293
- }
294
-
295
- /**
296
- * Rotate log files if needed
297
- */
298
- rotateLogsIfNeeded() {
299
- try {
300
- const files = fs.readdirSync(this.options.logDir)
301
- .filter(f => f.startsWith("audit-") && f.endsWith(".jsonl"))
302
- .sort()
303
- .reverse();
304
-
305
- // Remove old files
306
- while (files.length > this.options.maxLogFiles) {
307
- const oldFile = files.pop();
308
- fs.unlinkSync(path.join(this.options.logDir, oldFile));
309
- }
310
- } catch (error) {
311
- // Ignore rotation errors
312
- }
313
- }
314
-
315
- /**
316
- * Query audit logs
317
- */
318
- query(options = {}) {
319
- const {
320
- startDate,
321
- endDate,
322
- eventTypes,
323
- categories,
324
- severities,
325
- limit = 1000,
326
- } = options;
327
-
328
- const results = [];
329
- const files = fs.readdirSync(this.options.logDir)
330
- .filter(f => f.startsWith("audit-") && f.endsWith(".jsonl"))
331
- .sort()
332
- .reverse();
333
-
334
- for (const file of files) {
335
- const filePath = path.join(this.options.logDir, file);
336
- const content = fs.readFileSync(filePath, "utf-8");
337
- const lines = content.trim().split("\n");
338
-
339
- for (const line of lines) {
340
- if (results.length >= limit) break;
341
-
342
- try {
343
- const entry = JSON.parse(line);
344
-
345
- // Apply filters
346
- if (startDate && new Date(entry.timestamp) < new Date(startDate)) continue;
347
- if (endDate && new Date(entry.timestamp) > new Date(endDate)) continue;
348
- if (eventTypes && !eventTypes.includes(entry.eventType)) continue;
349
- if (categories && !categories.includes(entry.category)) continue;
350
- if (severities && !severities.includes(entry.severity)) continue;
351
-
352
- results.push(entry);
353
- } catch {
354
- // Skip malformed lines
355
- }
356
- }
357
-
358
- if (results.length >= limit) break;
359
- }
360
-
361
- return results;
362
- }
363
-
364
- /**
365
- * Verify audit log integrity
366
- */
367
- verifyIntegrity() {
368
- const results = {
369
- verified: 0,
370
- failed: 0,
371
- gaps: 0,
372
- issues: [],
373
- };
374
-
375
- const files = fs.readdirSync(this.options.logDir)
376
- .filter(f => f.startsWith("audit-") && f.endsWith(".jsonl"))
377
- .sort();
378
-
379
- let previousHash = null;
380
-
381
- for (const file of files) {
382
- const filePath = path.join(this.options.logDir, file);
383
- const content = fs.readFileSync(filePath, "utf-8");
384
- const lines = content.trim().split("\n");
385
-
386
- for (const line of lines) {
387
- try {
388
- const entry = JSON.parse(line);
389
-
390
- // Verify hash
391
- const expectedHash = crypto.createHash("sha256")
392
- .update(JSON.stringify({
393
- id: entry.id,
394
- timestamp: entry.timestamp,
395
- eventType: entry.eventType,
396
- category: entry.category,
397
- data: entry.data,
398
- }))
399
- .digest("hex")
400
- .substring(0, 16);
401
-
402
- if (entry.hash !== expectedHash) {
403
- results.failed++;
404
- results.issues.push({
405
- id: entry.id,
406
- issue: "hash_mismatch",
407
- expected: expectedHash,
408
- actual: entry.hash,
409
- });
410
- } else {
411
- results.verified++;
412
- }
413
-
414
- // Verify chain
415
- if (entry.data?.previousHash && entry.data.previousHash !== previousHash) {
416
- results.gaps++;
417
- results.issues.push({
418
- id: entry.id,
419
- issue: "chain_break",
420
- expected: previousHash,
421
- actual: entry.data.previousHash,
422
- });
423
- }
424
-
425
- previousHash = entry.hash;
426
- } catch {
427
- results.failed++;
428
- }
429
- }
430
- }
431
-
432
- return results;
433
- }
434
-
435
- /**
436
- * Export audit logs for compliance
437
- */
438
- exportForCompliance(options = {}) {
439
- const { format = "json", startDate, endDate } = options;
440
- const entries = this.query({ startDate, endDate, limit: Infinity });
441
-
442
- if (format === "json") {
443
- return JSON.stringify(entries, null, 2);
444
- }
445
-
446
- if (format === "csv") {
447
- const headers = ["id", "timestamp", "eventType", "category", "severity", "hash"];
448
- const rows = entries.map(e => [
449
- e.id,
450
- e.timestamp,
451
- e.eventType,
452
- e.category,
453
- e.severity,
454
- e.hash,
455
- ]);
456
- return [headers.join(","), ...rows.map(r => r.join(","))].join("\n");
457
- }
458
-
459
- if (format === "siem") {
460
- // Common Event Format (CEF) for SIEM integration
461
- return entries.map(e => {
462
- const severity = { critical: 10, high: 7, medium: 4, low: 2, info: 1 }[e.severity] || 1;
463
- return `CEF:0|VibeCheck|CLI|1.0|${e.eventType}|${e.eventType}|${severity}|rt=${new Date(e.timestamp).getTime()} src=${e.context?.hostname} suser=${e.context?.user}`;
464
- }).join("\n");
465
- }
466
-
467
- return JSON.stringify(entries);
468
- }
469
-
470
- /**
471
- * Get audit statistics
472
- */
473
- getStatistics(days = 30) {
474
- const startDate = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
475
- const entries = this.query({ startDate });
476
-
477
- const stats = {
478
- totalEvents: entries.length,
479
- byCategory: {},
480
- bySeverity: {},
481
- byEventType: {},
482
- dailyTrend: {},
483
- };
484
-
485
- for (const entry of entries) {
486
- // By category
487
- stats.byCategory[entry.category] = (stats.byCategory[entry.category] || 0) + 1;
488
-
489
- // By severity
490
- stats.bySeverity[entry.severity] = (stats.bySeverity[entry.severity] || 0) + 1;
491
-
492
- // By event type
493
- stats.byEventType[entry.eventType] = (stats.byEventType[entry.eventType] || 0) + 1;
494
-
495
- // Daily trend
496
- const date = entry.timestamp.split("T")[0];
497
- stats.dailyTrend[date] = (stats.dailyTrend[date] || 0) + 1;
498
- }
499
-
500
- return stats;
501
- }
502
- }
503
-
504
- // Singleton instance
505
- let _instance = null;
506
-
507
- /**
508
- * Get or create audit logger instance
509
- */
510
- function getAuditLogger(options = {}) {
511
- if (!_instance) {
512
- _instance = new AuditLogger(options);
513
- }
514
- return _instance;
515
- }
516
-
517
- /**
518
- * Quick log helper
519
- */
520
- function auditLog(eventType, data = {}) {
521
- return getAuditLogger().log(eventType, data);
522
- }
523
-
524
- module.exports = {
525
- AuditLogger,
526
- AuditEntry,
527
- getAuditLogger,
528
- auditLog,
529
- AUDIT_CATEGORIES,
530
- AUDIT_SEVERITY,
531
- AUDIT_EVENTS,
532
- };