@vibecheckai/cli 3.0.3 → 3.0.5

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 (119) hide show
  1. package/bin/cli-hygiene.js +241 -0
  2. package/bin/dev/run-v2-torture.js +30 -0
  3. package/bin/guardrail.js +843 -0
  4. package/bin/runners/cli-utils.js +1070 -0
  5. package/bin/runners/context/ai-task-decomposer.js +337 -0
  6. package/bin/runners/context/analyzer.js +462 -0
  7. package/bin/runners/context/api-contracts.js +427 -0
  8. package/bin/runners/context/context-diff.js +342 -0
  9. package/bin/runners/context/context-pruner.js +291 -0
  10. package/bin/runners/context/dependency-graph.js +414 -0
  11. package/bin/runners/context/generators/claude.js +107 -0
  12. package/bin/runners/context/generators/codex.js +108 -0
  13. package/bin/runners/context/generators/copilot.js +119 -0
  14. package/bin/runners/context/generators/cursor.js +514 -0
  15. package/bin/runners/context/generators/mcp.js +151 -0
  16. package/bin/runners/context/generators/windsurf.js +180 -0
  17. package/bin/runners/context/git-context.js +302 -0
  18. package/bin/runners/context/index.js +1042 -0
  19. package/bin/runners/context/insights.js +173 -0
  20. package/bin/runners/context/mcp-server/generate-rules.js +337 -0
  21. package/bin/runners/context/mcp-server/index.js +1176 -0
  22. package/bin/runners/context/mcp-server/package.json +24 -0
  23. package/bin/runners/context/memory.js +200 -0
  24. package/bin/runners/context/monorepo.js +215 -0
  25. package/bin/runners/context/multi-repo-federation.js +404 -0
  26. package/bin/runners/context/patterns.js +253 -0
  27. package/bin/runners/context/proof-context.js +972 -0
  28. package/bin/runners/context/security-scanner.js +303 -0
  29. package/bin/runners/context/semantic-search.js +350 -0
  30. package/bin/runners/context/shared.js +264 -0
  31. package/bin/runners/context/team-conventions.js +310 -0
  32. package/bin/runners/lib/ai-bridge.js +416 -0
  33. package/bin/runners/lib/analysis-core.js +271 -0
  34. package/bin/runners/lib/analyzers.js +579 -0
  35. package/bin/runners/lib/assets/vibecheck-logo.png +0 -0
  36. package/bin/runners/lib/audit-bridge.js +391 -0
  37. package/bin/runners/lib/auth-truth.js +193 -0
  38. package/bin/runners/lib/auth.js +215 -0
  39. package/bin/runners/lib/backup.js +62 -0
  40. package/bin/runners/lib/billing.js +107 -0
  41. package/bin/runners/lib/claims.js +118 -0
  42. package/bin/runners/lib/cli-ui.js +540 -0
  43. package/bin/runners/lib/compliance-bridge-new.js +0 -0
  44. package/bin/runners/lib/compliance-bridge.js +165 -0
  45. package/bin/runners/lib/contracts/auth-contract.js +202 -0
  46. package/bin/runners/lib/contracts/env-contract.js +181 -0
  47. package/bin/runners/lib/contracts/external-contract.js +206 -0
  48. package/bin/runners/lib/contracts/guard.js +168 -0
  49. package/bin/runners/lib/contracts/index.js +89 -0
  50. package/bin/runners/lib/contracts/plan-validator.js +311 -0
  51. package/bin/runners/lib/contracts/route-contract.js +199 -0
  52. package/bin/runners/lib/contracts.js +804 -0
  53. package/bin/runners/lib/detect.js +89 -0
  54. package/bin/runners/lib/detectors-v2.js +703 -0
  55. package/bin/runners/lib/doctor/autofix.js +254 -0
  56. package/bin/runners/lib/doctor/index.js +37 -0
  57. package/bin/runners/lib/doctor/modules/dependencies.js +325 -0
  58. package/bin/runners/lib/doctor/modules/index.js +46 -0
  59. package/bin/runners/lib/doctor/modules/network.js +250 -0
  60. package/bin/runners/lib/doctor/modules/project.js +312 -0
  61. package/bin/runners/lib/doctor/modules/runtime.js +224 -0
  62. package/bin/runners/lib/doctor/modules/security.js +348 -0
  63. package/bin/runners/lib/doctor/modules/system.js +213 -0
  64. package/bin/runners/lib/doctor/modules/vibecheck.js +394 -0
  65. package/bin/runners/lib/doctor/reporter.js +262 -0
  66. package/bin/runners/lib/doctor/service.js +262 -0
  67. package/bin/runners/lib/doctor/types.js +113 -0
  68. package/bin/runners/lib/doctor/ui.js +263 -0
  69. package/bin/runners/lib/doctor-enhanced.js +233 -0
  70. package/bin/runners/lib/doctor-v2.js +608 -0
  71. package/bin/runners/lib/drift.js +425 -0
  72. package/bin/runners/lib/enforcement.js +72 -0
  73. package/bin/runners/lib/entitlements.js +8 -3
  74. package/bin/runners/lib/env-resolver.js +417 -0
  75. package/bin/runners/lib/extractors/client-calls.js +990 -0
  76. package/bin/runners/lib/extractors/fastify-route-dump.js +573 -0
  77. package/bin/runners/lib/extractors/fastify-routes.js +426 -0
  78. package/bin/runners/lib/extractors/index.js +363 -0
  79. package/bin/runners/lib/extractors/next-routes.js +524 -0
  80. package/bin/runners/lib/extractors/proof-graph.js +431 -0
  81. package/bin/runners/lib/extractors/route-matcher.js +451 -0
  82. package/bin/runners/lib/extractors/truthpack-v2.js +377 -0
  83. package/bin/runners/lib/extractors/ui-bindings.js +547 -0
  84. package/bin/runners/lib/findings-schema.js +281 -0
  85. package/bin/runners/lib/html-report.js +650 -0
  86. package/bin/runners/lib/missions/templates.js +45 -0
  87. package/bin/runners/lib/policy.js +295 -0
  88. package/bin/runners/lib/reality/correlation-detectors.js +359 -0
  89. package/bin/runners/lib/reality/index.js +318 -0
  90. package/bin/runners/lib/reality/request-hashing.js +416 -0
  91. package/bin/runners/lib/reality/request-mapper.js +453 -0
  92. package/bin/runners/lib/reality/safety-rails.js +463 -0
  93. package/bin/runners/lib/reality/semantic-snapshot.js +408 -0
  94. package/bin/runners/lib/reality/toast-detector.js +393 -0
  95. package/bin/runners/lib/route-truth.js +10 -10
  96. package/bin/runners/lib/schema-validator.js +350 -0
  97. package/bin/runners/lib/schemas/contracts.schema.json +160 -0
  98. package/bin/runners/lib/schemas/finding.schema.json +100 -0
  99. package/bin/runners/lib/schemas/mission-pack.schema.json +206 -0
  100. package/bin/runners/lib/schemas/proof-graph.schema.json +176 -0
  101. package/bin/runners/lib/schemas/reality-report.schema.json +162 -0
  102. package/bin/runners/lib/schemas/share-pack.schema.json +180 -0
  103. package/bin/runners/lib/schemas/ship-report.schema.json +117 -0
  104. package/bin/runners/lib/schemas/truthpack-v2.schema.json +303 -0
  105. package/bin/runners/lib/schemas/validator.js +438 -0
  106. package/bin/runners/lib/verdict-engine.js +628 -0
  107. package/bin/runners/runAIAgent.js +228 -1
  108. package/bin/runners/runBadge.js +181 -1
  109. package/bin/runners/runCtxDiff.js +301 -0
  110. package/bin/runners/runInitGha.js +78 -15
  111. package/bin/runners/runLaunch.js +180 -1
  112. package/bin/runners/runProve.js +23 -0
  113. package/bin/runners/runReplay.js +114 -84
  114. package/bin/runners/runScan.js +111 -32
  115. package/bin/runners/runShip.js +23 -2
  116. package/bin/runners/runTruthpack.js +9 -7
  117. package/bin/runners/runValidate.js +161 -1
  118. package/bin/vibecheck.js +6 -1
  119. package/package.json +1 -1
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Auth Contract Builder
3
+ * Builds auth.json contract from truthpack
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * Build auth contract from truthpack
10
+ */
11
+ function buildAuthContract(truthpack) {
12
+ const contract = {
13
+ version: "1.0.0",
14
+ generatedAt: new Date().toISOString(),
15
+ protectedPatterns: [],
16
+ publicPatterns: [],
17
+ roles: [],
18
+ evidence: []
19
+ };
20
+
21
+ // Extract protected patterns from Next middleware
22
+ const nextMiddleware = truthpack?.auth?.nextMiddleware || [];
23
+ const matcherPatterns = truthpack?.auth?.nextMatcherPatterns || [];
24
+
25
+ contract.protectedPatterns = [...new Set(matcherPatterns)];
26
+
27
+ // Add evidence from middleware files
28
+ for (const mw of nextMiddleware) {
29
+ contract.evidence.push({
30
+ file: mw.file,
31
+ type: "next_middleware",
32
+ signals: mw.signalTypes || []
33
+ });
34
+ }
35
+
36
+ // Extract Fastify auth info
37
+ const fastify = truthpack?.auth?.fastify || {};
38
+ if (fastify.hooks?.length) {
39
+ for (const hook of fastify.hooks) {
40
+ contract.evidence.push({
41
+ file: hook.file,
42
+ type: "fastify_hook",
43
+ hookType: hook.hookType,
44
+ line: hook.line
45
+ });
46
+ }
47
+ }
48
+
49
+ // Infer roles from truthpack
50
+ contract.roles = inferRoles(truthpack);
51
+
52
+ // Default public patterns
53
+ contract.publicPatterns = [
54
+ "/api/health",
55
+ "/api/status",
56
+ "/api/public/*",
57
+ "/_next/*",
58
+ "/favicon.ico"
59
+ ];
60
+
61
+ // Deterministic output: sort all arrays
62
+ contract.protectedPatterns.sort();
63
+ contract.publicPatterns.sort();
64
+ contract.roles.sort((a, b) => a.name.localeCompare(b.name));
65
+ for (const role of contract.roles) {
66
+ if (role.routes) role.routes.sort();
67
+ }
68
+
69
+ return contract;
70
+ }
71
+
72
+ /**
73
+ * Infer roles from truthpack
74
+ */
75
+ function inferRoles(truthpack) {
76
+ const roles = [];
77
+ const routes = truthpack?.routes?.server || [];
78
+
79
+ // Check for admin routes
80
+ const adminRoutes = routes.filter(r => r.path.includes("/admin"));
81
+ if (adminRoutes.length > 0) {
82
+ roles.push({
83
+ name: "admin",
84
+ routes: adminRoutes.map(r => r.path),
85
+ evidence: adminRoutes.flatMap(r => r.evidence || [])
86
+ });
87
+ }
88
+
89
+ // Check for user routes (default authenticated)
90
+ const userRoutes = routes.filter(r =>
91
+ !r.path.includes("/admin") &&
92
+ !r.path.includes("/public") &&
93
+ !r.path.includes("/health")
94
+ );
95
+ if (userRoutes.length > 0) {
96
+ roles.push({
97
+ name: "user",
98
+ routes: userRoutes.map(r => r.path),
99
+ evidence: []
100
+ });
101
+ }
102
+
103
+ return roles;
104
+ }
105
+
106
+ /**
107
+ * Validate auth coverage
108
+ */
109
+ function validateAuthContract(contract, routes, realityResults) {
110
+ const violations = [];
111
+
112
+ // Check that all non-public routes are protected
113
+ for (const route of routes) {
114
+ const isPublic = contract.publicPatterns.some(p => matchesPattern(route.path, p));
115
+ const isProtected = contract.protectedPatterns.some(p => matchesPattern(route.path, p));
116
+
117
+ if (!isPublic && !isProtected) {
118
+ // Check if route looks sensitive
119
+ if (looksLikeSensitiveRoute(route.path)) {
120
+ violations.push({
121
+ type: "unprotected_sensitive",
122
+ severity: "WARN",
123
+ route: route.path,
124
+ message: `Sensitive route ${route.path} not covered by auth patterns`,
125
+ evidence: route.evidence || []
126
+ });
127
+ }
128
+ }
129
+ }
130
+
131
+ // Check reality results for auth bypass
132
+ if (realityResults) {
133
+ for (const result of realityResults) {
134
+ if (result.type === "AuthCoverage" && result.severity === "BLOCK") {
135
+ violations.push({
136
+ type: "auth_bypass",
137
+ severity: "BLOCK",
138
+ route: result.page,
139
+ message: result.title,
140
+ evidence: []
141
+ });
142
+ }
143
+ }
144
+ }
145
+
146
+ return violations;
147
+ }
148
+
149
+ function matchesPattern(path, pattern) {
150
+ const normPattern = pattern.replace(/\*/g, ".*").replace(/\//g, "\\/");
151
+ try {
152
+ const rx = new RegExp(`^${normPattern}`, "i");
153
+ return rx.test(path);
154
+ } catch {
155
+ return false;
156
+ }
157
+ }
158
+
159
+ function looksLikeSensitiveRoute(path) {
160
+ const sensitivePatterns = [
161
+ /\/api\/users/i,
162
+ /\/api\/billing/i,
163
+ /\/api\/payment/i,
164
+ /\/api\/subscription/i,
165
+ /\/api\/settings/i,
166
+ /\/api\/profile/i,
167
+ /\/api\/account/i,
168
+ /\/api\/admin/i,
169
+ /\/api\/webhook/i,
170
+ ];
171
+
172
+ return sensitivePatterns.some(p => p.test(path));
173
+ }
174
+
175
+ /**
176
+ * Diff two auth contracts
177
+ */
178
+ function diffAuthContracts(before, after) {
179
+ const diff = {
180
+ protectedAdded: [],
181
+ protectedRemoved: [],
182
+ rolesChanged: []
183
+ };
184
+
185
+ const beforeProtected = new Set(before.protectedPatterns);
186
+ const afterProtected = new Set(after.protectedPatterns);
187
+
188
+ for (const p of afterProtected) {
189
+ if (!beforeProtected.has(p)) diff.protectedAdded.push(p);
190
+ }
191
+ for (const p of beforeProtected) {
192
+ if (!afterProtected.has(p)) diff.protectedRemoved.push(p);
193
+ }
194
+
195
+ return diff;
196
+ }
197
+
198
+ module.exports = {
199
+ buildAuthContract,
200
+ validateAuthContract,
201
+ diffAuthContracts
202
+ };
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Env Contract Builder
3
+ * Builds env.json contract from truthpack
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * Build env contract from truthpack
10
+ */
11
+ function buildEnvContract(truthpack) {
12
+ const contract = {
13
+ version: "1.0.0",
14
+ generatedAt: new Date().toISOString(),
15
+ vars: []
16
+ };
17
+
18
+ const envVars = truthpack?.env?.vars || [];
19
+ const declared = new Set(truthpack?.env?.declared || []);
20
+ const declaredSources = truthpack?.env?.declaredSources || [];
21
+
22
+ for (const v of envVars) {
23
+ const varSpec = {
24
+ name: v.name,
25
+ required: inferRequired(v, declared),
26
+ usedIn: (v.references || []).map(r => r.file).filter(Boolean),
27
+ declaredIn: declaredSources.filter(s => isDeclaredInSource(v.name, s)),
28
+ description: inferDescription(v.name),
29
+ evidence: v.references || []
30
+ };
31
+
32
+ contract.vars.push(varSpec);
33
+ }
34
+
35
+ // Add declared vars that aren't used (might be optional)
36
+ for (const name of declared) {
37
+ if (!envVars.find(v => v.name === name)) {
38
+ contract.vars.push({
39
+ name,
40
+ required: false,
41
+ usedIn: [],
42
+ declaredIn: declaredSources,
43
+ description: inferDescription(name),
44
+ evidence: []
45
+ });
46
+ }
47
+ }
48
+
49
+ // Deterministic output: sort vars by name
50
+ contract.vars.sort((a, b) => a.name.localeCompare(b.name));
51
+
52
+ return contract;
53
+ }
54
+
55
+ /**
56
+ * Infer if env var is required
57
+ */
58
+ function inferRequired(envVar, declared) {
59
+ const name = envVar.name;
60
+
61
+ // Common required patterns
62
+ const requiredPatterns = [
63
+ /^DATABASE_URL$/i,
64
+ /^NEXTAUTH_SECRET$/i,
65
+ /^NEXTAUTH_URL$/i,
66
+ /^JWT_SECRET$/i,
67
+ /^API_KEY$/i,
68
+ /^STRIPE_SECRET_KEY$/i,
69
+ /^STRIPE_WEBHOOK_SECRET$/i,
70
+ /^AUTH0_/i,
71
+ /^CLERK_/i,
72
+ ];
73
+
74
+ for (const pattern of requiredPatterns) {
75
+ if (pattern.test(name)) return true;
76
+ }
77
+
78
+ // If used but not declared, likely required
79
+ if (!declared.has(name) && envVar.references?.length > 0) {
80
+ return true;
81
+ }
82
+
83
+ return false;
84
+ }
85
+
86
+ function isDeclaredInSource(name, source) {
87
+ // Simple heuristic - would need to parse files for accurate check
88
+ return true;
89
+ }
90
+
91
+ function inferDescription(name) {
92
+ const descriptions = {
93
+ DATABASE_URL: "Database connection string",
94
+ NEXTAUTH_SECRET: "NextAuth.js encryption secret",
95
+ NEXTAUTH_URL: "NextAuth.js base URL",
96
+ JWT_SECRET: "JWT signing secret",
97
+ STRIPE_SECRET_KEY: "Stripe API secret key",
98
+ STRIPE_PUBLISHABLE_KEY: "Stripe publishable key",
99
+ STRIPE_WEBHOOK_SECRET: "Stripe webhook signing secret",
100
+ NODE_ENV: "Node environment (development/production)",
101
+ PORT: "Server port",
102
+ HOST: "Server host",
103
+ };
104
+
105
+ return descriptions[name] || undefined;
106
+ }
107
+
108
+ /**
109
+ * Validate code against env contract
110
+ */
111
+ function validateAgainstEnvContract(contract, usedVars) {
112
+ const violations = [];
113
+ const contractVars = new Map(contract.vars.map(v => [v.name, v]));
114
+
115
+ for (const used of usedVars) {
116
+ if (!contractVars.has(used.name)) {
117
+ violations.push({
118
+ type: "undeclared_env",
119
+ severity: "WARN",
120
+ name: used.name,
121
+ usedIn: used.references?.map(r => r.file) || [],
122
+ message: `Env var ${used.name} used but not declared in contract`,
123
+ evidence: used.references || []
124
+ });
125
+ }
126
+ }
127
+
128
+ // Check for required vars that aren't used
129
+ for (const [name, spec] of contractVars) {
130
+ if (spec.required && spec.usedIn.length === 0) {
131
+ violations.push({
132
+ type: "unused_required",
133
+ severity: "WARN",
134
+ name,
135
+ message: `Required env var ${name} declared but not used`,
136
+ evidence: []
137
+ });
138
+ }
139
+ }
140
+
141
+ return violations;
142
+ }
143
+
144
+ /**
145
+ * Diff two env contracts
146
+ */
147
+ function diffEnvContracts(before, after) {
148
+ const diff = {
149
+ added: [],
150
+ removed: [],
151
+ changed: []
152
+ };
153
+
154
+ const beforeMap = new Map(before.vars.map(v => [v.name, v]));
155
+ const afterMap = new Map(after.vars.map(v => [v.name, v]));
156
+
157
+ for (const [name, spec] of afterMap) {
158
+ if (!beforeMap.has(name)) {
159
+ diff.added.push(spec);
160
+ } else {
161
+ const prev = beforeMap.get(name);
162
+ if (prev.required !== spec.required) {
163
+ diff.changed.push({ before: prev, after: spec });
164
+ }
165
+ }
166
+ }
167
+
168
+ for (const [name, spec] of beforeMap) {
169
+ if (!afterMap.has(name)) {
170
+ diff.removed.push(spec);
171
+ }
172
+ }
173
+
174
+ return diff;
175
+ }
176
+
177
+ module.exports = {
178
+ buildEnvContract,
179
+ validateAgainstEnvContract,
180
+ diffEnvContracts
181
+ };
@@ -0,0 +1,206 @@
1
+ /**
2
+ * External Contract Builder
3
+ * Builds external.json contract from truthpack (Stripe, GitHub, etc.)
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * Build external services contract from truthpack
10
+ */
11
+ function buildExternalContract(truthpack) {
12
+ const contract = {
13
+ version: "1.0.0",
14
+ generatedAt: new Date().toISOString(),
15
+ services: []
16
+ };
17
+
18
+ // Extract billing/Stripe info
19
+ const billing = truthpack?.billing || {};
20
+ if (billing.hasStripe || billing.webhookCandidates?.length) {
21
+ const stripeService = {
22
+ name: "stripe",
23
+ envVars: extractStripeEnvVars(truthpack),
24
+ usedIn: billing.webhookCandidates?.map(w => w.file) || [],
25
+ webhooks: billing.webhookCandidates?.map(w => ({
26
+ path: w.path,
27
+ verified: w.hasSignatureVerification || false,
28
+ idempotent: w.hasIdempotency || false
29
+ })) || [],
30
+ evidence: billing.webhookCandidates?.flatMap(w => w.evidence || []) || []
31
+ };
32
+ contract.services.push(stripeService);
33
+ }
34
+
35
+ // Detect other external services from env vars
36
+ const envVars = truthpack?.env?.vars || [];
37
+
38
+ // GitHub
39
+ const githubVars = envVars.filter(v => /github/i.test(v.name));
40
+ if (githubVars.length) {
41
+ contract.services.push({
42
+ name: "github",
43
+ envVars: githubVars.map(v => v.name),
44
+ usedIn: githubVars.flatMap(v => v.references?.map(r => r.file) || []),
45
+ evidence: githubVars.flatMap(v => v.references || [])
46
+ });
47
+ }
48
+
49
+ // SendGrid
50
+ const sendgridVars = envVars.filter(v => /sendgrid/i.test(v.name));
51
+ if (sendgridVars.length) {
52
+ contract.services.push({
53
+ name: "sendgrid",
54
+ envVars: sendgridVars.map(v => v.name),
55
+ usedIn: sendgridVars.flatMap(v => v.references?.map(r => r.file) || []),
56
+ evidence: sendgridVars.flatMap(v => v.references || [])
57
+ });
58
+ }
59
+
60
+ // Twilio
61
+ const twilioVars = envVars.filter(v => /twilio/i.test(v.name));
62
+ if (twilioVars.length) {
63
+ contract.services.push({
64
+ name: "twilio",
65
+ envVars: twilioVars.map(v => v.name),
66
+ usedIn: twilioVars.flatMap(v => v.references?.map(r => r.file) || []),
67
+ evidence: twilioVars.flatMap(v => v.references || [])
68
+ });
69
+ }
70
+
71
+ // AWS
72
+ const awsVars = envVars.filter(v => /^aws/i.test(v.name));
73
+ if (awsVars.length) {
74
+ contract.services.push({
75
+ name: "aws",
76
+ envVars: awsVars.map(v => v.name),
77
+ usedIn: awsVars.flatMap(v => v.references?.map(r => r.file) || []),
78
+ evidence: awsVars.flatMap(v => v.references || [])
79
+ });
80
+ }
81
+
82
+ // Supabase
83
+ const supabaseVars = envVars.filter(v => /supabase/i.test(v.name));
84
+ if (supabaseVars.length) {
85
+ contract.services.push({
86
+ name: "supabase",
87
+ envVars: supabaseVars.map(v => v.name).sort(),
88
+ usedIn: [...new Set(supabaseVars.flatMap(v => v.references?.map(r => r.file) || []))].sort(),
89
+ evidence: supabaseVars.flatMap(v => v.references || [])
90
+ });
91
+ }
92
+
93
+ // Deterministic output: sort services by name, and their internal arrays
94
+ contract.services.sort((a, b) => a.name.localeCompare(b.name));
95
+ for (const svc of contract.services) {
96
+ if (svc.envVars) svc.envVars.sort();
97
+ if (svc.usedIn) svc.usedIn = [...new Set(svc.usedIn)].sort();
98
+ if (svc.webhooks) svc.webhooks.sort((a, b) => a.path.localeCompare(b.path));
99
+ }
100
+
101
+ return contract;
102
+ }
103
+
104
+ function extractStripeEnvVars(truthpack) {
105
+ const envVars = truthpack?.env?.vars || [];
106
+ return envVars
107
+ .filter(v => /stripe/i.test(v.name))
108
+ .map(v => v.name);
109
+ }
110
+
111
+ /**
112
+ * Validate external services contract
113
+ */
114
+ function validateExternalContract(contract) {
115
+ const violations = [];
116
+
117
+ for (const service of contract.services) {
118
+ // Check Stripe webhook verification
119
+ if (service.name === "stripe") {
120
+ for (const webhook of service.webhooks || []) {
121
+ if (!webhook.verified) {
122
+ violations.push({
123
+ type: "unverified_webhook",
124
+ severity: "BLOCK",
125
+ service: "stripe",
126
+ path: webhook.path,
127
+ message: `Stripe webhook at ${webhook.path} missing signature verification`,
128
+ evidence: []
129
+ });
130
+ }
131
+ if (!webhook.idempotent) {
132
+ violations.push({
133
+ type: "non_idempotent_webhook",
134
+ severity: "WARN",
135
+ service: "stripe",
136
+ path: webhook.path,
137
+ message: `Stripe webhook at ${webhook.path} may not be idempotent`,
138
+ evidence: []
139
+ });
140
+ }
141
+ }
142
+ }
143
+
144
+ // Check for missing required env vars
145
+ const requiredVars = getRequiredVarsForService(service.name);
146
+ for (const required of requiredVars) {
147
+ if (!service.envVars.includes(required)) {
148
+ violations.push({
149
+ type: "missing_env",
150
+ severity: "WARN",
151
+ service: service.name,
152
+ envVar: required,
153
+ message: `Service ${service.name} typically requires ${required}`,
154
+ evidence: []
155
+ });
156
+ }
157
+ }
158
+ }
159
+
160
+ return violations;
161
+ }
162
+
163
+ function getRequiredVarsForService(name) {
164
+ const requirements = {
165
+ stripe: ["STRIPE_SECRET_KEY", "STRIPE_WEBHOOK_SECRET"],
166
+ github: ["GITHUB_TOKEN"],
167
+ sendgrid: ["SENDGRID_API_KEY"],
168
+ twilio: ["TWILIO_ACCOUNT_SID", "TWILIO_AUTH_TOKEN"],
169
+ supabase: ["SUPABASE_URL", "SUPABASE_ANON_KEY"]
170
+ };
171
+ return requirements[name] || [];
172
+ }
173
+
174
+ /**
175
+ * Diff two external contracts
176
+ */
177
+ function diffExternalContracts(before, after) {
178
+ const diff = {
179
+ added: [],
180
+ removed: [],
181
+ changed: []
182
+ };
183
+
184
+ const beforeMap = new Map(before.services.map(s => [s.name, s]));
185
+ const afterMap = new Map(after.services.map(s => [s.name, s]));
186
+
187
+ for (const [name, service] of afterMap) {
188
+ if (!beforeMap.has(name)) {
189
+ diff.added.push(service);
190
+ }
191
+ }
192
+
193
+ for (const [name, service] of beforeMap) {
194
+ if (!afterMap.has(name)) {
195
+ diff.removed.push(service);
196
+ }
197
+ }
198
+
199
+ return diff;
200
+ }
201
+
202
+ module.exports = {
203
+ buildExternalContract,
204
+ validateExternalContract,
205
+ diffExternalContracts
206
+ };