@shubh000/macrc 0.0.0-stage → 1.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.
- package/README.md +278 -2
- package/cli/lib/context-verifier.mjs +247 -0
- package/cli/lib/heuristic.mjs +440 -0
- package/cli/lib/output.mjs +109 -0
- package/cli/lib/policy.mjs +104 -0
- package/cli/lib/remediator.mjs +656 -0
- package/cli/lib/syntax-queries.mjs +169 -0
- package/cli/lib/targeted-llm.mjs +233 -0
- package/cli/macrc.js +2 -0
- package/cli/macrc.mjs +299 -0
- package/demo/bad-express-app/package.json +14 -0
- package/demo/bad-express-app/src/index.js +19 -0
- package/demo/bad-express-app/src/routes/users.js +16 -0
- package/demo/bad-express-app/src/services/api.js +21 -0
- package/package.json +42 -4
- package/tests/context-verifier.test.mjs +184 -0
- package/tests/fixtures/diff-after.json +36 -0
- package/tests/fixtures/diff-before.json +36 -0
- package/tests/integration.test.mjs +82 -0
- package/tests/remediator.test.mjs +181 -0
- package/tests/syntax-queries.test.mjs +98 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syntax-tree queries for Tier-1 dimensions with high grep false-positive rates.
|
|
3
|
+
*
|
|
4
|
+
* Uses acorn (pure-JS ES2022 parser, no native deps) for JS/TS files.
|
|
5
|
+
* Returns null when the file is not parseable or has no relevant calls —
|
|
6
|
+
* callers should fall back to the grep result in that case.
|
|
7
|
+
*
|
|
8
|
+
* Exported functions:
|
|
9
|
+
* queryTimeoutHandling(filePath) → { state, callsFound, callsWithTimeout } | null
|
|
10
|
+
* queryDistributedAssumptions(filePath) → { state, hardcodedUrls } | null
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { readFileSync } from 'fs';
|
|
14
|
+
import { createRequire } from 'module';
|
|
15
|
+
|
|
16
|
+
// acorn is a CJS package — load via createRequire
|
|
17
|
+
const require = createRequire(import.meta.url);
|
|
18
|
+
const { parse } = require('acorn');
|
|
19
|
+
|
|
20
|
+
const PARSE_OPTS = { ecmaVersion: 2022, sourceType: 'module' };
|
|
21
|
+
|
|
22
|
+
// ── AST walker — depth-first ───────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
function walk(node, visit, parent = null) {
|
|
25
|
+
if (!node || typeof node !== 'object' || !node.type) return;
|
|
26
|
+
visit(node, parent);
|
|
27
|
+
for (const key of Object.keys(node)) {
|
|
28
|
+
const child = node[key];
|
|
29
|
+
if (Array.isArray(child)) {
|
|
30
|
+
child.forEach(c => walk(c, visit, node));
|
|
31
|
+
} else if (child && typeof child === 'object' && child.type) {
|
|
32
|
+
walk(child, visit, node);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Return callee name as a string, e.g. "axios.get" or "fetch"
|
|
38
|
+
function getCalleeName(callee) {
|
|
39
|
+
if (!callee) return '';
|
|
40
|
+
if (callee.type === 'Identifier') return callee.name;
|
|
41
|
+
if (callee.type === 'MemberExpression') {
|
|
42
|
+
const obj = getCalleeName(callee.object);
|
|
43
|
+
const prop = callee.computed ? '' : (callee.property?.name ?? '');
|
|
44
|
+
return prop ? `${obj}.${prop}` : obj;
|
|
45
|
+
}
|
|
46
|
+
return '';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Known HTTP client call patterns that accept a config object with timeout/signal
|
|
50
|
+
const HTTP_CALLEE_RE = /^(axios\.(get|post|put|delete|patch|request)|fetch)$/;
|
|
51
|
+
|
|
52
|
+
// Keys that signal timeout configuration in an options object
|
|
53
|
+
const TIMEOUT_KEYS = new Set(['timeout', 'signal']);
|
|
54
|
+
|
|
55
|
+
function hasTimeoutOption(args) {
|
|
56
|
+
return args.some(arg =>
|
|
57
|
+
arg.type === 'ObjectExpression' &&
|
|
58
|
+
arg.properties.some(p => {
|
|
59
|
+
const key = p.key?.name ?? p.key?.value ?? '';
|
|
60
|
+
return TIMEOUT_KEYS.has(key);
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── queryTimeoutHandling ───────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Parse a JS/TS file and check whether axios/fetch calls have timeout config.
|
|
69
|
+
*
|
|
70
|
+
* Returns:
|
|
71
|
+
* null – file not parseable or no HTTP calls found
|
|
72
|
+
* { state: 'absent', callsFound, callsWithTimeout }
|
|
73
|
+
* { state: 'partial', callsFound, callsWithTimeout }
|
|
74
|
+
* { state: 'present', callsFound, callsWithTimeout }
|
|
75
|
+
*/
|
|
76
|
+
export function queryTimeoutHandling(filePath) {
|
|
77
|
+
let src;
|
|
78
|
+
try { src = readFileSync(filePath, 'utf8'); } catch { return null; }
|
|
79
|
+
|
|
80
|
+
let ast;
|
|
81
|
+
try { ast = parse(src, PARSE_OPTS); } catch {
|
|
82
|
+
// Try script mode as fallback (CJS files)
|
|
83
|
+
try { ast = parse(src, { ...PARSE_OPTS, sourceType: 'script' }); } catch { return null; }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let callsFound = 0;
|
|
87
|
+
let callsWithTimeout = 0;
|
|
88
|
+
|
|
89
|
+
walk(ast, node => {
|
|
90
|
+
if (node.type !== 'CallExpression') return;
|
|
91
|
+
const name = getCalleeName(node.callee);
|
|
92
|
+
if (!HTTP_CALLEE_RE.test(name)) return;
|
|
93
|
+
callsFound++;
|
|
94
|
+
if (hasTimeoutOption(node.arguments)) callsWithTimeout++;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (callsFound === 0) return null;
|
|
98
|
+
if (callsWithTimeout === 0) return { state: 'absent', callsFound, callsWithTimeout };
|
|
99
|
+
if (callsWithTimeout < callsFound) return { state: 'partial', callsFound, callsWithTimeout };
|
|
100
|
+
return { state: 'present', callsFound, callsWithTimeout };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ── queryDistributedAssumptions ────────────────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
const LOCALHOST_RE = /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?/;
|
|
106
|
+
|
|
107
|
+
// Test-block callee names that make a localhost string safe to ignore
|
|
108
|
+
const TEST_CALLEE_NAMES = new Set(['describe', 'it', 'test', 'beforeEach', 'afterEach', 'before', 'after', 'beforeAll', 'afterAll']);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Parse a JS/TS file and check whether localhost string literals appear
|
|
112
|
+
* as call arguments outside of test blocks.
|
|
113
|
+
*
|
|
114
|
+
* Returns:
|
|
115
|
+
* null – not parseable or no localhost in call args
|
|
116
|
+
* { state: 'absent', hardcodedUrls }
|
|
117
|
+
*/
|
|
118
|
+
export function queryDistributedAssumptions(filePath) {
|
|
119
|
+
let src;
|
|
120
|
+
try { src = readFileSync(filePath, 'utf8'); } catch { return null; }
|
|
121
|
+
|
|
122
|
+
let ast;
|
|
123
|
+
try { ast = parse(src, PARSE_OPTS); } catch {
|
|
124
|
+
try { ast = parse(src, { ...PARSE_OPTS, sourceType: 'script' }); } catch { return null; }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Build parent map so we can walk ancestors
|
|
128
|
+
const parentMap = new Map();
|
|
129
|
+
walk(ast, (node, parent) => { if (parent) parentMap.set(node, parent); });
|
|
130
|
+
|
|
131
|
+
function isInsideTestBlock(node) {
|
|
132
|
+
let cur = parentMap.get(node);
|
|
133
|
+
while (cur) {
|
|
134
|
+
if (cur.type === 'CallExpression') {
|
|
135
|
+
const name = getCalleeName(cur.callee);
|
|
136
|
+
if (TEST_CALLEE_NAMES.has(name)) return true;
|
|
137
|
+
}
|
|
138
|
+
cur = parentMap.get(cur);
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function isCallArgument(node) {
|
|
144
|
+
const parent = parentMap.get(node);
|
|
145
|
+
if (!parent) return false;
|
|
146
|
+
if (parent.type === 'CallExpression') {
|
|
147
|
+
return parent.arguments.includes(node);
|
|
148
|
+
}
|
|
149
|
+
// Template literal expression inside a call
|
|
150
|
+
if (parent.type === 'TemplateLiteral') {
|
|
151
|
+
const grandparent = parentMap.get(parent);
|
|
152
|
+
if (grandparent?.type === 'CallExpression' && grandparent.arguments.includes(parent)) return true;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const hardcodedUrls = [];
|
|
158
|
+
|
|
159
|
+
walk(ast, node => {
|
|
160
|
+
if (node.type !== 'Literal' || typeof node.value !== 'string') return;
|
|
161
|
+
if (!LOCALHOST_RE.test(node.value)) return;
|
|
162
|
+
if (isInsideTestBlock(node)) return;
|
|
163
|
+
if (!isCallArgument(node)) return;
|
|
164
|
+
hardcodedUrls.push(node.value);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
if (hardcodedUrls.length === 0) return null;
|
|
168
|
+
return { state: 'absent', hardcodedUrls };
|
|
169
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Targeted LLM verification for Tier-1 dimensions.
|
|
3
|
+
*
|
|
4
|
+
* Called only when heuristic says partial or present on a Tier-1 dim.
|
|
5
|
+
* Uses the Anthropic API (ANTHROPIC_API_KEY env var).
|
|
6
|
+
*
|
|
7
|
+
* Exports: verifyDim(cloneDir, dim, heuristicResult, options) → DimResult
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFileSync, existsSync } from 'fs';
|
|
11
|
+
import { resolve } from 'path';
|
|
12
|
+
import { execSync } from 'child_process';
|
|
13
|
+
|
|
14
|
+
const ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages';
|
|
15
|
+
|
|
16
|
+
const PROMPTS = {
|
|
17
|
+
input_validation: (snippets) => `
|
|
18
|
+
You are auditing a software repo for input validation at HTTP entry points.
|
|
19
|
+
Snippets below come from route/controller files only.
|
|
20
|
+
|
|
21
|
+
Validation library found in dependencies. Does it actually protect entry points?
|
|
22
|
+
|
|
23
|
+
Snippets:
|
|
24
|
+
${snippets}
|
|
25
|
+
|
|
26
|
+
Answer with ONLY valid JSON:
|
|
27
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
28
|
+
|
|
29
|
+
- present: validation library applied at request boundaries for all major routes
|
|
30
|
+
- partial: validation exists in some routes but not all, or only on internal helpers
|
|
31
|
+
- absent: library imported but not wired to request handlers
|
|
32
|
+
`.trim(),
|
|
33
|
+
|
|
34
|
+
auth: (snippets) => `
|
|
35
|
+
You are auditing a software repo for authentication enforcement.
|
|
36
|
+
Snippets below come from route/middleware files only.
|
|
37
|
+
|
|
38
|
+
Auth library found in dependencies. Is middleware actually protecting routes?
|
|
39
|
+
|
|
40
|
+
Snippets:
|
|
41
|
+
${snippets}
|
|
42
|
+
|
|
43
|
+
Answer with ONLY valid JSON:
|
|
44
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
45
|
+
|
|
46
|
+
- present: auth middleware applied to all protected routes
|
|
47
|
+
- partial: some routes protected, some not; or JWT verified but no route guard
|
|
48
|
+
- absent: auth library imported but not applied to routes
|
|
49
|
+
`.trim(),
|
|
50
|
+
|
|
51
|
+
distributed_assumptions: (snippets) => `
|
|
52
|
+
You are auditing a software repo for distributed-system assumptions.
|
|
53
|
+
Snippets show env-var usage and any service URL patterns.
|
|
54
|
+
|
|
55
|
+
Question: are inter-service calls consistently using env-var-configured addresses?
|
|
56
|
+
|
|
57
|
+
Snippets:
|
|
58
|
+
${snippets}
|
|
59
|
+
|
|
60
|
+
Answer with ONLY valid JSON:
|
|
61
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
62
|
+
|
|
63
|
+
- present: all service-to-service calls use env-configured addresses
|
|
64
|
+
- partial: mix of env vars and hardcoded service names/localhost
|
|
65
|
+
- absent: hardcoded service addresses or localhost in production code paths
|
|
66
|
+
`.trim(),
|
|
67
|
+
|
|
68
|
+
statelessness: (snippets) => `
|
|
69
|
+
You are auditing a software repo for statelessness (no in-process session state).
|
|
70
|
+
Snippets show module-level variables and any state management.
|
|
71
|
+
|
|
72
|
+
Question: does the app hold per-request or per-user state in module-level variables or in-process stores?
|
|
73
|
+
|
|
74
|
+
Snippets:
|
|
75
|
+
${snippets}
|
|
76
|
+
|
|
77
|
+
Answer with ONLY valid JSON:
|
|
78
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
79
|
+
|
|
80
|
+
- present: no in-process request state; all state in external store or JWT
|
|
81
|
+
- partial: some in-process state but mitigated (Redis-backed sessions, etc.)
|
|
82
|
+
- absent: in-process maps/arrays used to store per-user or per-request data
|
|
83
|
+
`.trim(),
|
|
84
|
+
|
|
85
|
+
timeout_handling: (snippets) => `
|
|
86
|
+
You are auditing a software repo for timeout handling on outbound calls.
|
|
87
|
+
Snippets show HTTP client usage and any timeout configuration.
|
|
88
|
+
|
|
89
|
+
Question: are outbound HTTP/DB/external calls protected by explicit timeouts?
|
|
90
|
+
|
|
91
|
+
Snippets:
|
|
92
|
+
${snippets}
|
|
93
|
+
|
|
94
|
+
Answer with ONLY valid JSON:
|
|
95
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
96
|
+
|
|
97
|
+
- present: timeouts set on all significant outbound call sites
|
|
98
|
+
- partial: some call sites have timeouts, others rely on platform defaults
|
|
99
|
+
- absent: no explicit timeouts; connections can hang indefinitely
|
|
100
|
+
`.trim(),
|
|
101
|
+
|
|
102
|
+
health_checks: (snippets) => `
|
|
103
|
+
You are auditing a software repo for health check implementation.
|
|
104
|
+
Snippets show route definitions and any health-related files.
|
|
105
|
+
|
|
106
|
+
Question: is there a real health check endpoint that probes internal dependencies?
|
|
107
|
+
|
|
108
|
+
Snippets:
|
|
109
|
+
${snippets}
|
|
110
|
+
|
|
111
|
+
Answer with ONLY valid JSON:
|
|
112
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
113
|
+
|
|
114
|
+
- present: health endpoint that checks internal dependencies (DB, cache, etc.)
|
|
115
|
+
- partial: health endpoint exists but always returns 200 without probing deps
|
|
116
|
+
- absent: no health check endpoint
|
|
117
|
+
`.trim(),
|
|
118
|
+
|
|
119
|
+
load_balancing_awareness: (snippets) => `
|
|
120
|
+
You are auditing a software repo for load-balancing readiness.
|
|
121
|
+
Snippets show deployment config and health check patterns.
|
|
122
|
+
|
|
123
|
+
Question: is the app designed for horizontal scaling behind a load balancer?
|
|
124
|
+
|
|
125
|
+
Snippets:
|
|
126
|
+
${snippets}
|
|
127
|
+
|
|
128
|
+
Answer with ONLY valid JSON:
|
|
129
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
130
|
+
|
|
131
|
+
- present: health endpoint + stateless design + multi-replica config
|
|
132
|
+
- partial: health endpoint present but state or sticky-session issues remain
|
|
133
|
+
- absent: no health endpoint, relies on single-instance deployment
|
|
134
|
+
`.trim(),
|
|
135
|
+
|
|
136
|
+
retry_logic: (snippets) => `
|
|
137
|
+
You are auditing a software repo for retry logic on external calls.
|
|
138
|
+
Snippets show HTTP client usage and any retry patterns.
|
|
139
|
+
|
|
140
|
+
Question: are retries applied to outbound calls that can transiently fail?
|
|
141
|
+
|
|
142
|
+
Snippets:
|
|
143
|
+
${snippets}
|
|
144
|
+
|
|
145
|
+
Answer with ONLY valid JSON:
|
|
146
|
+
{"state":"present"|"partial"|"absent","severity":null|"low"|"medium"|"high"|"critical","notes":"<one sentence>"}
|
|
147
|
+
|
|
148
|
+
- present: retry library or explicit retry loop applied to external call sites
|
|
149
|
+
- partial: retries on some calls; others fire once and fail permanently
|
|
150
|
+
- absent: no retry logic; single failures propagate immediately
|
|
151
|
+
`.trim(),
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
function collectSnippets(cloneDir, dim) {
|
|
155
|
+
const SOURCE_EXTS = /\.(js|ts|jsx|tsx|mjs|cjs|py|go|rs)$/;
|
|
156
|
+
const ROUTE_PATH = /route|controller|handler|api|endpoint|view|middleware/i;
|
|
157
|
+
let files = [];
|
|
158
|
+
try {
|
|
159
|
+
files = execSync('git ls-files', { cwd: cloneDir, timeout: 10000 })
|
|
160
|
+
.toString().split('\n')
|
|
161
|
+
.filter(f => f && SOURCE_EXTS.test(f) && ROUTE_PATH.test(f));
|
|
162
|
+
} catch {}
|
|
163
|
+
|
|
164
|
+
const snippets = [];
|
|
165
|
+
for (const relPath of files.slice(0, 20)) {
|
|
166
|
+
let content = '';
|
|
167
|
+
try { content = readFileSync(resolve(cloneDir, relPath), 'utf8'); } catch { continue; }
|
|
168
|
+
const lines = content.split('\n');
|
|
169
|
+
snippets.push(`\n--- ${relPath} (${lines.length} lines) ---`);
|
|
170
|
+
snippets.push(lines.slice(0, 60).join('\n'));
|
|
171
|
+
}
|
|
172
|
+
return snippets.join('\n').slice(0, 8000);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function verifyDim(cloneDir, dim, heuristicResult, options = {}) {
|
|
176
|
+
const apiKey = process.env.ANTHROPIC_API_KEY;
|
|
177
|
+
if (!apiKey) throw new Error('ANTHROPIC_API_KEY not set');
|
|
178
|
+
|
|
179
|
+
const promptFn = PROMPTS[dim];
|
|
180
|
+
if (!promptFn) throw new Error(`No targeted prompt for dim: ${dim}`);
|
|
181
|
+
|
|
182
|
+
const snippets = collectSnippets(cloneDir, dim);
|
|
183
|
+
const prompt = promptFn(snippets);
|
|
184
|
+
|
|
185
|
+
const model = options.model || 'claude-haiku-4-5';
|
|
186
|
+
|
|
187
|
+
const body = JSON.stringify({
|
|
188
|
+
model,
|
|
189
|
+
max_tokens: 256,
|
|
190
|
+
temperature: 0,
|
|
191
|
+
messages: [{ role: 'user', content: prompt }],
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const { default: https } = await import('https');
|
|
195
|
+
const raw = await new Promise((resolve, reject) => {
|
|
196
|
+
const url = new URL(ANTHROPIC_API_URL);
|
|
197
|
+
const req = https.request({
|
|
198
|
+
hostname: url.hostname,
|
|
199
|
+
path: url.pathname,
|
|
200
|
+
method: 'POST',
|
|
201
|
+
headers: {
|
|
202
|
+
'Content-Type': 'application/json',
|
|
203
|
+
'x-api-key': apiKey,
|
|
204
|
+
'anthropic-version': '2023-06-01',
|
|
205
|
+
'Content-Length': Buffer.byteLength(body),
|
|
206
|
+
},
|
|
207
|
+
}, (res) => {
|
|
208
|
+
let data = '';
|
|
209
|
+
res.on('data', chunk => data += chunk);
|
|
210
|
+
res.on('end', () => resolve(data));
|
|
211
|
+
});
|
|
212
|
+
req.on('error', reject);
|
|
213
|
+
req.write(body);
|
|
214
|
+
req.end();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const response = JSON.parse(raw);
|
|
218
|
+
if (response.error) throw new Error(`Anthropic API error: ${response.error.message}`);
|
|
219
|
+
|
|
220
|
+
const text = response.content?.[0]?.text?.trim() || '';
|
|
221
|
+
const jsonMatch = text.match(/\{[\s\S]*\}/);
|
|
222
|
+
if (!jsonMatch) throw new Error(`LLM returned non-JSON: ${text.slice(0, 200)}`);
|
|
223
|
+
|
|
224
|
+
const result = JSON.parse(jsonMatch[0]);
|
|
225
|
+
return {
|
|
226
|
+
state: result.state,
|
|
227
|
+
severity: result.severity || null,
|
|
228
|
+
notes: result.notes || '',
|
|
229
|
+
snippets: [],
|
|
230
|
+
_source: 'targeted-llm',
|
|
231
|
+
_model: model,
|
|
232
|
+
};
|
|
233
|
+
}
|
package/cli/macrc.js
ADDED