@vibecheckai/cli 3.8.0 → 3.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/runners/lib/agent-firewall/enforcement/index.js +98 -98
- package/bin/runners/lib/agent-firewall/enforcement/mode.js +318 -318
- package/bin/runners/lib/agent-firewall/enforcement/orchestrator.js +484 -484
- package/bin/runners/lib/agent-firewall/enforcement/proof-artifact.js +418 -418
- package/bin/runners/lib/agent-firewall/enforcement/verdict-v2.js +333 -333
- package/bin/runners/lib/agent-firewall/intent/alignment-engine.js +634 -622
- package/bin/runners/lib/agent-firewall/intent/index.js +102 -102
- package/bin/runners/lib/agent-firewall/intent/schema.js +352 -352
- package/bin/runners/lib/agent-firewall/intent/store.js +283 -283
- package/bin/runners/lib/agent-firewall/interceptor/base.js +7 -3
- package/bin/runners/lib/engine/ast-cache.js +210 -210
- package/bin/runners/lib/engine/auth-extractor.js +211 -211
- package/bin/runners/lib/engine/billing-extractor.js +112 -112
- package/bin/runners/lib/engine/enforcement-extractor.js +100 -100
- package/bin/runners/lib/engine/env-extractor.js +207 -207
- package/bin/runners/lib/engine/express-extractor.js +208 -208
- package/bin/runners/lib/engine/extractors.js +849 -849
- package/bin/runners/lib/engine/index.js +207 -207
- package/bin/runners/lib/engine/repo-index.js +514 -514
- package/bin/runners/lib/engine/types.js +124 -124
- package/bin/runners/lib/unified-cli-output.js +16 -0
- package/bin/runners/runCI.js +353 -0
- package/bin/runners/runCheckpoint.js +2 -2
- package/bin/runners/runIntent.js +906 -906
- package/bin/runners/runPacks.js +2089 -2089
- package/bin/runners/runReality.js +178 -1
- package/bin/runners/runShield.js +1282 -1282
- package/mcp-server/handlers/index.ts +2 -2
- package/mcp-server/handlers/tool-handler.ts +47 -8
- package/mcp-server/lib/executor.ts +5 -5
- package/mcp-server/lib/index.ts +14 -4
- package/mcp-server/lib/sandbox.test.ts +4 -4
- package/mcp-server/lib/sandbox.ts +2 -2
- package/mcp-server/package.json +1 -1
- package/mcp-server/registry.test.ts +18 -12
- package/mcp-server/tsconfig.json +1 -0
- package/package.json +2 -1
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
// bin/runners/lib/engine/enforcement-extractor.js
|
|
2
|
-
// Optimized enforcement extraction using RepoIndex
|
|
3
|
-
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const fs = require("fs");
|
|
6
|
-
|
|
7
|
-
const ENFORCE_HINTS = [
|
|
8
|
-
"enforceFeature",
|
|
9
|
-
"enforceLimit",
|
|
10
|
-
"getEntitlements",
|
|
11
|
-
"requirePlan",
|
|
12
|
-
"requireTier",
|
|
13
|
-
"requireSubscription",
|
|
14
|
-
"checkAccess",
|
|
15
|
-
"entitlements",
|
|
16
|
-
"plan",
|
|
17
|
-
"tier",
|
|
18
|
-
"subscription",
|
|
19
|
-
"stripe",
|
|
20
|
-
"credits"
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
function looksPaidSurface(routePath) {
|
|
24
|
-
const p = String(routePath || "");
|
|
25
|
-
return (
|
|
26
|
-
p.includes("/api/ship") ||
|
|
27
|
-
p.includes("/api/verdict") ||
|
|
28
|
-
p.includes("/api/fix") ||
|
|
29
|
-
p.includes("/api/autopilot") ||
|
|
30
|
-
p.includes("/api/missions") ||
|
|
31
|
-
p.includes("/api/pr") ||
|
|
32
|
-
p.includes("/api/credits") ||
|
|
33
|
-
p.includes("/api/billing") ||
|
|
34
|
-
p.includes("/api/stripe") ||
|
|
35
|
-
p.includes("/api/entitlements")
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Check if handler has enforcement signal using RepoIndex
|
|
41
|
-
* @param {import('./repo-index').RepoIndex} index
|
|
42
|
-
* @param {string} handlerRel
|
|
43
|
-
* @returns {{ ok: boolean, hits: string[] }}
|
|
44
|
-
*/
|
|
45
|
-
function handlerHasEnforcementSignalV2(index, handlerRel) {
|
|
46
|
-
const fileAbs = path.join(index.repoRoot, handlerRel);
|
|
47
|
-
|
|
48
|
-
// Try to get content from index first (fast path)
|
|
49
|
-
let content = index.getContent(fileAbs);
|
|
50
|
-
|
|
51
|
-
// Fall back to direct read if not in index
|
|
52
|
-
if (!content) {
|
|
53
|
-
try {
|
|
54
|
-
content = fs.readFileSync(fileAbs, "utf8");
|
|
55
|
-
} catch {
|
|
56
|
-
return { ok: false, hits: [] };
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const hits = ENFORCE_HINTS.filter(k => content.includes(k));
|
|
61
|
-
return { ok: hits.length > 0, hits };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Build enforcement truth using RepoIndex (optimized)
|
|
66
|
-
* @param {import('./repo-index').RepoIndex} index
|
|
67
|
-
* @param {Array} serverRoutes
|
|
68
|
-
* @returns {Object}
|
|
69
|
-
*/
|
|
70
|
-
function buildEnforcementTruthV2(index, serverRoutes) {
|
|
71
|
-
const checked = [];
|
|
72
|
-
const routeList = serverRoutes || [];
|
|
73
|
-
|
|
74
|
-
for (const r of routeList) {
|
|
75
|
-
if (!r.handler) continue;
|
|
76
|
-
if (!looksPaidSurface(r.path)) continue;
|
|
77
|
-
|
|
78
|
-
const res = handlerHasEnforcementSignalV2(index, r.handler);
|
|
79
|
-
checked.push({
|
|
80
|
-
method: r.method,
|
|
81
|
-
path: r.path,
|
|
82
|
-
handler: r.handler,
|
|
83
|
-
enforced: res.ok,
|
|
84
|
-
hits: res.hits
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
checkedCount: checked.length,
|
|
90
|
-
enforcedCount: checked.filter(x => x.enforced).length,
|
|
91
|
-
missingCount: checked.filter(x => !x.enforced).length,
|
|
92
|
-
checks: checked
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
module.exports = {
|
|
97
|
-
buildEnforcementTruthV2,
|
|
98
|
-
ENFORCE_HINTS,
|
|
99
|
-
looksPaidSurface,
|
|
100
|
-
};
|
|
1
|
+
// bin/runners/lib/engine/enforcement-extractor.js
|
|
2
|
+
// Optimized enforcement extraction using RepoIndex
|
|
3
|
+
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
|
|
7
|
+
const ENFORCE_HINTS = [
|
|
8
|
+
"enforceFeature",
|
|
9
|
+
"enforceLimit",
|
|
10
|
+
"getEntitlements",
|
|
11
|
+
"requirePlan",
|
|
12
|
+
"requireTier",
|
|
13
|
+
"requireSubscription",
|
|
14
|
+
"checkAccess",
|
|
15
|
+
"entitlements",
|
|
16
|
+
"plan",
|
|
17
|
+
"tier",
|
|
18
|
+
"subscription",
|
|
19
|
+
"stripe",
|
|
20
|
+
"credits"
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
function looksPaidSurface(routePath) {
|
|
24
|
+
const p = String(routePath || "");
|
|
25
|
+
return (
|
|
26
|
+
p.includes("/api/ship") ||
|
|
27
|
+
p.includes("/api/verdict") ||
|
|
28
|
+
p.includes("/api/fix") ||
|
|
29
|
+
p.includes("/api/autopilot") ||
|
|
30
|
+
p.includes("/api/missions") ||
|
|
31
|
+
p.includes("/api/pr") ||
|
|
32
|
+
p.includes("/api/credits") ||
|
|
33
|
+
p.includes("/api/billing") ||
|
|
34
|
+
p.includes("/api/stripe") ||
|
|
35
|
+
p.includes("/api/entitlements")
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Check if handler has enforcement signal using RepoIndex
|
|
41
|
+
* @param {import('./repo-index').RepoIndex} index
|
|
42
|
+
* @param {string} handlerRel
|
|
43
|
+
* @returns {{ ok: boolean, hits: string[] }}
|
|
44
|
+
*/
|
|
45
|
+
function handlerHasEnforcementSignalV2(index, handlerRel) {
|
|
46
|
+
const fileAbs = path.join(index.repoRoot, handlerRel);
|
|
47
|
+
|
|
48
|
+
// Try to get content from index first (fast path)
|
|
49
|
+
let content = index.getContent(fileAbs);
|
|
50
|
+
|
|
51
|
+
// Fall back to direct read if not in index
|
|
52
|
+
if (!content) {
|
|
53
|
+
try {
|
|
54
|
+
content = fs.readFileSync(fileAbs, "utf8");
|
|
55
|
+
} catch {
|
|
56
|
+
return { ok: false, hits: [] };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const hits = ENFORCE_HINTS.filter(k => content.includes(k));
|
|
61
|
+
return { ok: hits.length > 0, hits };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build enforcement truth using RepoIndex (optimized)
|
|
66
|
+
* @param {import('./repo-index').RepoIndex} index
|
|
67
|
+
* @param {Array} serverRoutes
|
|
68
|
+
* @returns {Object}
|
|
69
|
+
*/
|
|
70
|
+
function buildEnforcementTruthV2(index, serverRoutes) {
|
|
71
|
+
const checked = [];
|
|
72
|
+
const routeList = serverRoutes || [];
|
|
73
|
+
|
|
74
|
+
for (const r of routeList) {
|
|
75
|
+
if (!r.handler) continue;
|
|
76
|
+
if (!looksPaidSurface(r.path)) continue;
|
|
77
|
+
|
|
78
|
+
const res = handlerHasEnforcementSignalV2(index, r.handler);
|
|
79
|
+
checked.push({
|
|
80
|
+
method: r.method,
|
|
81
|
+
path: r.path,
|
|
82
|
+
handler: r.handler,
|
|
83
|
+
enforced: res.ok,
|
|
84
|
+
hits: res.hits
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
checkedCount: checked.length,
|
|
90
|
+
enforcedCount: checked.filter(x => x.enforced).length,
|
|
91
|
+
missingCount: checked.filter(x => !x.enforced).length,
|
|
92
|
+
checks: checked
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = {
|
|
97
|
+
buildEnforcementTruthV2,
|
|
98
|
+
ENFORCE_HINTS,
|
|
99
|
+
looksPaidSurface,
|
|
100
|
+
};
|
|
@@ -1,207 +1,207 @@
|
|
|
1
|
-
// bin/runners/lib/engine/env-extractor.js
|
|
2
|
-
// Optimized env var extraction using RepoIndex
|
|
3
|
-
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const fs = require("fs");
|
|
6
|
-
const crypto = require("crypto");
|
|
7
|
-
const traverse = require("@babel/traverse").default;
|
|
8
|
-
const t = require("@babel/types");
|
|
9
|
-
const { globalASTCache } = require("./ast-cache");
|
|
10
|
-
|
|
11
|
-
function sha256(text) {
|
|
12
|
-
return "sha256:" + crypto.createHash("sha256").update(text).digest("hex");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function isEnvName(s) {
|
|
16
|
-
return typeof s === "string" && /^[A-Z0-9_]+$/.test(s);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function extractEnvFromMemberExpr(node) {
|
|
20
|
-
if (!t.isMemberExpression(node)) return null;
|
|
21
|
-
|
|
22
|
-
// process.env.NAME
|
|
23
|
-
if (t.isMemberExpression(node.object) &&
|
|
24
|
-
t.isIdentifier(node.object.object, { name: "process" }) &&
|
|
25
|
-
t.isIdentifier(node.object.property, { name: "env" }) &&
|
|
26
|
-
t.isIdentifier(node.property)) {
|
|
27
|
-
return node.property.name;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// process.env["NAME"]
|
|
31
|
-
if (t.isMemberExpression(node.object) &&
|
|
32
|
-
t.isIdentifier(node.object.object, { name: "process" }) &&
|
|
33
|
-
t.isIdentifier(node.object.property, { name: "env" }) &&
|
|
34
|
-
t.isStringLiteral(node.property)) {
|
|
35
|
-
return node.property.value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// import.meta.env.NAME
|
|
39
|
-
if (t.isMemberExpression(node.object) &&
|
|
40
|
-
t.isMemberExpression(node.object.object) &&
|
|
41
|
-
t.isIdentifier(node.object.object.object, { name: "import" }) &&
|
|
42
|
-
t.isIdentifier(node.object.object.property, { name: "meta" }) &&
|
|
43
|
-
t.isIdentifier(node.object.property, { name: "env" }) &&
|
|
44
|
-
t.isIdentifier(node.property)) {
|
|
45
|
-
return node.property.name;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function detectDefaultSignals(parentPath) {
|
|
52
|
-
const p = parentPath?.parentPath;
|
|
53
|
-
if (!p) return { hasDefault: false };
|
|
54
|
-
|
|
55
|
-
if (p.isLogicalExpression() && (p.node.operator === "||" || p.node.operator === "??")) {
|
|
56
|
-
return { hasDefault: true };
|
|
57
|
-
}
|
|
58
|
-
if (p.isConditionalExpression()) return { hasDefault: true };
|
|
59
|
-
if (p.isAssignmentExpression() && p.node.operator === "||=") return { hasDefault: true };
|
|
60
|
-
return { hasDefault: false };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function evidenceFromContent(content, fileRel, loc, reason) {
|
|
64
|
-
if (!loc || !content) return null;
|
|
65
|
-
const lines = content.split(/\r?\n/);
|
|
66
|
-
const start = Math.max(1, loc.start?.line || 1);
|
|
67
|
-
const end = Math.max(start, loc.end?.line || start);
|
|
68
|
-
const snippet = lines.slice(start - 1, end).join("\n");
|
|
69
|
-
return {
|
|
70
|
-
id: `ev_${crypto.randomBytes(4).toString("hex")}`,
|
|
71
|
-
file: fileRel,
|
|
72
|
-
lines: `${start}-${end}`,
|
|
73
|
-
snippetHash: sha256(snippet),
|
|
74
|
-
reason
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function parseDotEnvLike(content) {
|
|
79
|
-
const out = new Set();
|
|
80
|
-
const lines = content.split(/\r?\n/);
|
|
81
|
-
for (const raw of lines) {
|
|
82
|
-
const line = raw.trim();
|
|
83
|
-
if (!line || line.startsWith("#")) continue;
|
|
84
|
-
const l = line.startsWith("export ") ? line.slice(7).trim() : line;
|
|
85
|
-
const eq = l.indexOf("=");
|
|
86
|
-
if (eq <= 0) continue;
|
|
87
|
-
const key = l.slice(0, eq).trim();
|
|
88
|
-
if (isEnvName(key)) out.add(key);
|
|
89
|
-
}
|
|
90
|
-
return out;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Extract env var usage using RepoIndex
|
|
95
|
-
* @param {import('./repo-index').RepoIndex} index
|
|
96
|
-
* @param {Object} stats
|
|
97
|
-
* @returns {Object} - Map of env var name to usage info
|
|
98
|
-
*/
|
|
99
|
-
function extractEnvUsage(index, stats) {
|
|
100
|
-
// Use token prefilter - only scan files that reference process.env or import.meta.env
|
|
101
|
-
const candidateAbs = index.getByAnyToken(["process.env", "import.meta.env"]);
|
|
102
|
-
|
|
103
|
-
// Filter to JS/TS files
|
|
104
|
-
const jsExtensions = new Set([".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"]);
|
|
105
|
-
const files = candidateAbs.filter(abs => {
|
|
106
|
-
const ext = path.extname(abs).toLowerCase();
|
|
107
|
-
return jsExtensions.has(ext);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const usageMap = {};
|
|
111
|
-
|
|
112
|
-
for (const fileAbs of files) {
|
|
113
|
-
const fileRel = index.relPath(fileAbs);
|
|
114
|
-
const content = index.getContent(fileAbs);
|
|
115
|
-
if (!content) continue;
|
|
116
|
-
|
|
117
|
-
const { ast, error } = globalASTCache.parse(content, fileAbs);
|
|
118
|
-
if (error || !ast) {
|
|
119
|
-
stats.parseErrors++;
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
traverse(ast, {
|
|
125
|
-
MemberExpression(p) {
|
|
126
|
-
const name = extractEnvFromMemberExpr(p.node);
|
|
127
|
-
if (!name || !isEnvName(name)) return;
|
|
128
|
-
|
|
129
|
-
usageMap[name] = usageMap[name] || { name, references: [], signals: { hasDefault: false } };
|
|
130
|
-
|
|
131
|
-
const ev = evidenceFromContent(content, fileRel, p.node.loc, `Env usage: ${name}`);
|
|
132
|
-
if (ev) usageMap[name].references.push(ev);
|
|
133
|
-
|
|
134
|
-
const sig = detectDefaultSignals(p);
|
|
135
|
-
if (sig.hasDefault) usageMap[name].signals.hasDefault = true;
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
} catch {
|
|
139
|
-
stats.parseErrors++;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return usageMap;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Extract declared env vars from .env files
|
|
148
|
-
* @param {string} repoRoot
|
|
149
|
-
* @returns {{ declared: string[], sources: string[] }}
|
|
150
|
-
*/
|
|
151
|
-
function extractEnvDeclared(repoRoot) {
|
|
152
|
-
const candidates = [
|
|
153
|
-
".env.example", ".env.template", ".env.sample",
|
|
154
|
-
".env.local.example", ".env.development.example",
|
|
155
|
-
".env"
|
|
156
|
-
];
|
|
157
|
-
|
|
158
|
-
const declared = new Set();
|
|
159
|
-
const sources = [];
|
|
160
|
-
|
|
161
|
-
for (const rel of candidates) {
|
|
162
|
-
const abs = path.join(repoRoot, rel);
|
|
163
|
-
try {
|
|
164
|
-
if (!fs.existsSync(abs)) continue;
|
|
165
|
-
const content = fs.readFileSync(abs, "utf8");
|
|
166
|
-
const keys = parseDotEnvLike(content);
|
|
167
|
-
if (keys.size) {
|
|
168
|
-
for (const k of keys) declared.add(k);
|
|
169
|
-
sources.push(rel);
|
|
170
|
-
}
|
|
171
|
-
} catch {}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return { declared: Array.from(declared).sort(), sources };
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Build env truth using RepoIndex (optimized)
|
|
179
|
-
* @param {import('./repo-index').RepoIndex} index
|
|
180
|
-
* @param {Object} stats
|
|
181
|
-
* @returns {Object}
|
|
182
|
-
*/
|
|
183
|
-
function buildEnvTruthV2(index, stats) {
|
|
184
|
-
const usageMap = extractEnvUsage(index, stats);
|
|
185
|
-
const declared = extractEnvDeclared(index.repoRoot);
|
|
186
|
-
|
|
187
|
-
const used = Object.values(usageMap).sort((a, b) => a.name.localeCompare(b.name));
|
|
188
|
-
|
|
189
|
-
const vars = used.map(u => ({
|
|
190
|
-
name: u.name,
|
|
191
|
-
required: !u.signals?.hasDefault,
|
|
192
|
-
references: u.references || [],
|
|
193
|
-
notes: u.signals?.hasDefault ? "Has default/fallback usage signal" : ""
|
|
194
|
-
}));
|
|
195
|
-
|
|
196
|
-
return {
|
|
197
|
-
vars,
|
|
198
|
-
declared: declared.declared,
|
|
199
|
-
declaredSources: declared.sources
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
module.exports = {
|
|
204
|
-
extractEnvUsage,
|
|
205
|
-
extractEnvDeclared,
|
|
206
|
-
buildEnvTruthV2,
|
|
207
|
-
};
|
|
1
|
+
// bin/runners/lib/engine/env-extractor.js
|
|
2
|
+
// Optimized env var extraction using RepoIndex
|
|
3
|
+
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const crypto = require("crypto");
|
|
7
|
+
const traverse = require("@babel/traverse").default;
|
|
8
|
+
const t = require("@babel/types");
|
|
9
|
+
const { globalASTCache } = require("./ast-cache");
|
|
10
|
+
|
|
11
|
+
function sha256(text) {
|
|
12
|
+
return "sha256:" + crypto.createHash("sha256").update(text).digest("hex");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isEnvName(s) {
|
|
16
|
+
return typeof s === "string" && /^[A-Z0-9_]+$/.test(s);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function extractEnvFromMemberExpr(node) {
|
|
20
|
+
if (!t.isMemberExpression(node)) return null;
|
|
21
|
+
|
|
22
|
+
// process.env.NAME
|
|
23
|
+
if (t.isMemberExpression(node.object) &&
|
|
24
|
+
t.isIdentifier(node.object.object, { name: "process" }) &&
|
|
25
|
+
t.isIdentifier(node.object.property, { name: "env" }) &&
|
|
26
|
+
t.isIdentifier(node.property)) {
|
|
27
|
+
return node.property.name;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// process.env["NAME"]
|
|
31
|
+
if (t.isMemberExpression(node.object) &&
|
|
32
|
+
t.isIdentifier(node.object.object, { name: "process" }) &&
|
|
33
|
+
t.isIdentifier(node.object.property, { name: "env" }) &&
|
|
34
|
+
t.isStringLiteral(node.property)) {
|
|
35
|
+
return node.property.value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// import.meta.env.NAME
|
|
39
|
+
if (t.isMemberExpression(node.object) &&
|
|
40
|
+
t.isMemberExpression(node.object.object) &&
|
|
41
|
+
t.isIdentifier(node.object.object.object, { name: "import" }) &&
|
|
42
|
+
t.isIdentifier(node.object.object.property, { name: "meta" }) &&
|
|
43
|
+
t.isIdentifier(node.object.property, { name: "env" }) &&
|
|
44
|
+
t.isIdentifier(node.property)) {
|
|
45
|
+
return node.property.name;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function detectDefaultSignals(parentPath) {
|
|
52
|
+
const p = parentPath?.parentPath;
|
|
53
|
+
if (!p) return { hasDefault: false };
|
|
54
|
+
|
|
55
|
+
if (p.isLogicalExpression() && (p.node.operator === "||" || p.node.operator === "??")) {
|
|
56
|
+
return { hasDefault: true };
|
|
57
|
+
}
|
|
58
|
+
if (p.isConditionalExpression()) return { hasDefault: true };
|
|
59
|
+
if (p.isAssignmentExpression() && p.node.operator === "||=") return { hasDefault: true };
|
|
60
|
+
return { hasDefault: false };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function evidenceFromContent(content, fileRel, loc, reason) {
|
|
64
|
+
if (!loc || !content) return null;
|
|
65
|
+
const lines = content.split(/\r?\n/);
|
|
66
|
+
const start = Math.max(1, loc.start?.line || 1);
|
|
67
|
+
const end = Math.max(start, loc.end?.line || start);
|
|
68
|
+
const snippet = lines.slice(start - 1, end).join("\n");
|
|
69
|
+
return {
|
|
70
|
+
id: `ev_${crypto.randomBytes(4).toString("hex")}`,
|
|
71
|
+
file: fileRel,
|
|
72
|
+
lines: `${start}-${end}`,
|
|
73
|
+
snippetHash: sha256(snippet),
|
|
74
|
+
reason
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseDotEnvLike(content) {
|
|
79
|
+
const out = new Set();
|
|
80
|
+
const lines = content.split(/\r?\n/);
|
|
81
|
+
for (const raw of lines) {
|
|
82
|
+
const line = raw.trim();
|
|
83
|
+
if (!line || line.startsWith("#")) continue;
|
|
84
|
+
const l = line.startsWith("export ") ? line.slice(7).trim() : line;
|
|
85
|
+
const eq = l.indexOf("=");
|
|
86
|
+
if (eq <= 0) continue;
|
|
87
|
+
const key = l.slice(0, eq).trim();
|
|
88
|
+
if (isEnvName(key)) out.add(key);
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Extract env var usage using RepoIndex
|
|
95
|
+
* @param {import('./repo-index').RepoIndex} index
|
|
96
|
+
* @param {Object} stats
|
|
97
|
+
* @returns {Object} - Map of env var name to usage info
|
|
98
|
+
*/
|
|
99
|
+
function extractEnvUsage(index, stats) {
|
|
100
|
+
// Use token prefilter - only scan files that reference process.env or import.meta.env
|
|
101
|
+
const candidateAbs = index.getByAnyToken(["process.env", "import.meta.env"]);
|
|
102
|
+
|
|
103
|
+
// Filter to JS/TS files
|
|
104
|
+
const jsExtensions = new Set([".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"]);
|
|
105
|
+
const files = candidateAbs.filter(abs => {
|
|
106
|
+
const ext = path.extname(abs).toLowerCase();
|
|
107
|
+
return jsExtensions.has(ext);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const usageMap = {};
|
|
111
|
+
|
|
112
|
+
for (const fileAbs of files) {
|
|
113
|
+
const fileRel = index.relPath(fileAbs);
|
|
114
|
+
const content = index.getContent(fileAbs);
|
|
115
|
+
if (!content) continue;
|
|
116
|
+
|
|
117
|
+
const { ast, error } = globalASTCache.parse(content, fileAbs);
|
|
118
|
+
if (error || !ast) {
|
|
119
|
+
stats.parseErrors++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
traverse(ast, {
|
|
125
|
+
MemberExpression(p) {
|
|
126
|
+
const name = extractEnvFromMemberExpr(p.node);
|
|
127
|
+
if (!name || !isEnvName(name)) return;
|
|
128
|
+
|
|
129
|
+
usageMap[name] = usageMap[name] || { name, references: [], signals: { hasDefault: false } };
|
|
130
|
+
|
|
131
|
+
const ev = evidenceFromContent(content, fileRel, p.node.loc, `Env usage: ${name}`);
|
|
132
|
+
if (ev) usageMap[name].references.push(ev);
|
|
133
|
+
|
|
134
|
+
const sig = detectDefaultSignals(p);
|
|
135
|
+
if (sig.hasDefault) usageMap[name].signals.hasDefault = true;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
} catch {
|
|
139
|
+
stats.parseErrors++;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return usageMap;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Extract declared env vars from .env files
|
|
148
|
+
* @param {string} repoRoot
|
|
149
|
+
* @returns {{ declared: string[], sources: string[] }}
|
|
150
|
+
*/
|
|
151
|
+
function extractEnvDeclared(repoRoot) {
|
|
152
|
+
const candidates = [
|
|
153
|
+
".env.example", ".env.template", ".env.sample",
|
|
154
|
+
".env.local.example", ".env.development.example",
|
|
155
|
+
".env"
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
const declared = new Set();
|
|
159
|
+
const sources = [];
|
|
160
|
+
|
|
161
|
+
for (const rel of candidates) {
|
|
162
|
+
const abs = path.join(repoRoot, rel);
|
|
163
|
+
try {
|
|
164
|
+
if (!fs.existsSync(abs)) continue;
|
|
165
|
+
const content = fs.readFileSync(abs, "utf8");
|
|
166
|
+
const keys = parseDotEnvLike(content);
|
|
167
|
+
if (keys.size) {
|
|
168
|
+
for (const k of keys) declared.add(k);
|
|
169
|
+
sources.push(rel);
|
|
170
|
+
}
|
|
171
|
+
} catch {}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return { declared: Array.from(declared).sort(), sources };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Build env truth using RepoIndex (optimized)
|
|
179
|
+
* @param {import('./repo-index').RepoIndex} index
|
|
180
|
+
* @param {Object} stats
|
|
181
|
+
* @returns {Object}
|
|
182
|
+
*/
|
|
183
|
+
function buildEnvTruthV2(index, stats) {
|
|
184
|
+
const usageMap = extractEnvUsage(index, stats);
|
|
185
|
+
const declared = extractEnvDeclared(index.repoRoot);
|
|
186
|
+
|
|
187
|
+
const used = Object.values(usageMap).sort((a, b) => a.name.localeCompare(b.name));
|
|
188
|
+
|
|
189
|
+
const vars = used.map(u => ({
|
|
190
|
+
name: u.name,
|
|
191
|
+
required: !u.signals?.hasDefault,
|
|
192
|
+
references: u.references || [],
|
|
193
|
+
notes: u.signals?.hasDefault ? "Has default/fallback usage signal" : ""
|
|
194
|
+
}));
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
vars,
|
|
198
|
+
declared: declared.declared,
|
|
199
|
+
declaredSources: declared.sources
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
module.exports = {
|
|
204
|
+
extractEnvUsage,
|
|
205
|
+
extractEnvDeclared,
|
|
206
|
+
buildEnvTruthV2,
|
|
207
|
+
};
|