@shubh000/macrc 0.0.0-stage → 0.1.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 +246 -2
- package/cli/lib/ast-verifier.mjs +247 -0
- package/cli/lib/heuristic.mjs +395 -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/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 +37 -4
- package/tests/ast-verifier.test.mjs +184 -0
- package/tests/remediator.test.mjs +181 -0
|
@@ -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
package/cli/macrc.mjs
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* macrc: MACRC CLI
|
|
4
|
+
*
|
|
5
|
+
* Subcommands:
|
|
6
|
+
* audit <dir> Run full heuristic audit + optional targeted LLM on Tier-1 dims
|
|
7
|
+
* gate <dir> Like audit, but exits non-zero on policy violations (for CI)
|
|
8
|
+
* diff <audit1> <audit2> Compare two audit JSON files
|
|
9
|
+
*
|
|
10
|
+
* Options:
|
|
11
|
+
* --format json|table|sarif Output format (default: table)
|
|
12
|
+
* --llm Enable targeted LLM verification for Tier-1 dims
|
|
13
|
+
* --policy <dir> Directory to search for .macrc.yml (default: <dir>)
|
|
14
|
+
* --stack-category <cat> Explicit stack category override
|
|
15
|
+
* --model <id> LLM model for targeted verification (default: claude-haiku-4-5)
|
|
16
|
+
* --out <file> Write output to file instead of stdout
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
20
|
+
import { resolve, dirname } from 'path';
|
|
21
|
+
import { fileURLToPath } from 'url';
|
|
22
|
+
|
|
23
|
+
import { runHeuristic, computeHeuristicSummary } from './lib/heuristic.mjs';
|
|
24
|
+
import { verifyDim } from './lib/targeted-llm.mjs';
|
|
25
|
+
import { loadPolicy, applyPolicy, formatPolicyReport } from './lib/policy.mjs';
|
|
26
|
+
import { formatJson, formatTable, formatSarif } from './lib/output.mjs';
|
|
27
|
+
import { buildPatches, applyPatches, formatPatchReport } from './lib/remediator.mjs';
|
|
28
|
+
|
|
29
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
30
|
+
|
|
31
|
+
const TIER1_DIMS = new Set([
|
|
32
|
+
'input_validation','distributed_assumptions','auth','statelessness',
|
|
33
|
+
'timeout_handling','health_checks','load_balancing_awareness','retry_logic',
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
function usage() {
|
|
37
|
+
console.log(`
|
|
38
|
+
macrc: MACRC audit CLI
|
|
39
|
+
|
|
40
|
+
Usage:
|
|
41
|
+
macrc audit <dir> [options]
|
|
42
|
+
macrc gate <dir> [options]
|
|
43
|
+
macrc fix <dir> [options]
|
|
44
|
+
macrc diff <audit1.json> <audit2.json>
|
|
45
|
+
|
|
46
|
+
Options:
|
|
47
|
+
--format json|table|sarif Output format for audit/gate (default: table)
|
|
48
|
+
--llm Run targeted LLM verification on Tier-1 dims
|
|
49
|
+
--no-verify Skip contextual grep verification (use raw regex counts)
|
|
50
|
+
--policy <dir> Policy file search root (default: <dir>)
|
|
51
|
+
--stack-category <cat> Override stack category (ai-web-app|api-proxy|mcp-server|
|
|
52
|
+
agent-framework|cli-tool|desktop-native|
|
|
53
|
+
plugin-extension|skills-config)
|
|
54
|
+
--model <id> LLM model for targeted verification
|
|
55
|
+
--out <file> Write output to file
|
|
56
|
+
--no-color Disable ANSI color in table output
|
|
57
|
+
|
|
58
|
+
fix-specific:
|
|
59
|
+
--dry-run Show patches without applying them
|
|
60
|
+
--dims <d1,d2,...> Only patch specific dims (default: all absent/partial Tier-1)
|
|
61
|
+
--from <audit.json> Use existing audit file instead of re-running scan
|
|
62
|
+
`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function parseArgs(argv) {
|
|
66
|
+
const args = { cmd: null, positional: [], format: 'table', llm: false, color: true, verified: true };
|
|
67
|
+
for (let i = 0; i < argv.length; i++) {
|
|
68
|
+
const a = argv[i];
|
|
69
|
+
if (a === '--help' || a === '-h') { usage(); process.exit(0); }
|
|
70
|
+
if (a === '--format') { args.format = argv[++i]; continue; }
|
|
71
|
+
if (a === '--llm') { args.llm = true; continue; }
|
|
72
|
+
if (a === '--no-verify') { args.verified = false; continue; }
|
|
73
|
+
if (a === '--policy') { args.policyDir = argv[++i]; continue; }
|
|
74
|
+
if (a === '--stack-category') { args.stackCategory = argv[++i]; continue; }
|
|
75
|
+
if (a === '--model') { args.model = argv[++i]; continue; }
|
|
76
|
+
if (a === '--out') { args.out = argv[++i]; continue; }
|
|
77
|
+
if (a === '--no-color') { args.color = false; continue; }
|
|
78
|
+
if (a === '--dry-run') { args.dryRun = true; continue; }
|
|
79
|
+
if (a === '--dims') { args.dims = new Set(argv[++i].split(',')); continue; }
|
|
80
|
+
if (a === '--from') { args.fromAudit = argv[++i]; continue; }
|
|
81
|
+
if (!a.startsWith('--')) {
|
|
82
|
+
if (!args.cmd) args.cmd = a;
|
|
83
|
+
else args.positional.push(a);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return args;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function detectStackCategory(dir) {
|
|
90
|
+
try {
|
|
91
|
+
const pkg = JSON.parse(readFileSync(resolve(dir, 'package.json'), 'utf8'));
|
|
92
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
93
|
+
if (deps['@modelcontextprotocol/sdk'] || deps['@anthropic-ai/mcp']) return 'mcp-server';
|
|
94
|
+
if (deps['next'] || deps['react'] || deps['vue'] || deps['nuxt']) return 'ai-web-app';
|
|
95
|
+
if (deps['express'] || deps['fastify'] || deps['hono'] || deps['koa']) return 'api-proxy';
|
|
96
|
+
} catch {}
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function runAudit(dir, args) {
|
|
101
|
+
if (!existsSync(dir)) {
|
|
102
|
+
console.error(`ERROR: directory not found: ${dir}`);
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const stackCategory = args.stackCategory || detectStackCategory(dir);
|
|
107
|
+
console.error(`[macrc] scanning ${dir} stack=${stackCategory || '(auto)'} verified=${args.verified}`);
|
|
108
|
+
|
|
109
|
+
const heuristicLayers = runHeuristic(dir, stackCategory, { verified: args.verified });
|
|
110
|
+
|
|
111
|
+
// Targeted LLM pass on Tier-1 dims where heuristic says partial or present
|
|
112
|
+
if (args.llm) {
|
|
113
|
+
const llmOpts = { model: args.model };
|
|
114
|
+
for (const [layer, dims] of Object.entries(heuristicLayers)) {
|
|
115
|
+
if (layer === 'secEnhancement') continue;
|
|
116
|
+
for (const [dim, result] of Object.entries(dims)) {
|
|
117
|
+
if (!TIER1_DIMS.has(dim)) continue;
|
|
118
|
+
if (result.state !== 'partial' && result.state !== 'present') continue;
|
|
119
|
+
try {
|
|
120
|
+
console.error(`[macrc] targeted LLM: ${layer}.${dim} (heuristic=${result.state})`);
|
|
121
|
+
const llmResult = await verifyDim(dir, dim, result, llmOpts);
|
|
122
|
+
dims[dim] = llmResult;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
console.error(`[macrc] LLM verification failed for ${dim}: ${err.message}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Build security layer from heuristic secEnhancement as baseline
|
|
131
|
+
const security = {
|
|
132
|
+
auth: heuristicLayers.secEnhancement.auth || { state: 'absent', severity: null, notes: 'no auth signals found', snippets: [] },
|
|
133
|
+
input_validation: heuristicLayers.secEnhancement.input_validation || { state: 'absent', severity: null, notes: 'no validation signals found', snippets: [] },
|
|
134
|
+
transport_security: heuristicLayers.secEnhancement.transport_security || { state: 'absent', severity: null, notes: 'no TLS config found', snippets: [] },
|
|
135
|
+
dependency_hygiene: { state: 'pending', severity: null, notes: 'npm audit not run in standalone CLI mode', snippets: [] },
|
|
136
|
+
secrets_management: { state: 'pending', severity: null, notes: 'TruffleHog not run in standalone CLI mode', snippets: [] },
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const layers = {
|
|
140
|
+
security,
|
|
141
|
+
reliability: heuristicLayers.reliability,
|
|
142
|
+
observability: heuristicLayers.observability,
|
|
143
|
+
scale_contract: heuristicLayers.scale_contract,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const summary = computeHeuristicSummary(layers);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
schema_version: '1.0',
|
|
150
|
+
repo_id: dir,
|
|
151
|
+
audited_at: new Date().toISOString().split('T')[0],
|
|
152
|
+
auditor: args.llm ? 'macrc-two-pass' : 'macrc-heuristic',
|
|
153
|
+
layers,
|
|
154
|
+
summary,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function diffAudits(path1, path2) {
|
|
159
|
+
for (const p of [path1, path2]) {
|
|
160
|
+
if (!existsSync(p)) {
|
|
161
|
+
console.error(`ERROR: file not found: ${p}`);
|
|
162
|
+
process.exit(1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const a1 = JSON.parse(readFileSync(path1, 'utf8'));
|
|
166
|
+
const a2 = JSON.parse(readFileSync(path2, 'utf8'));
|
|
167
|
+
|
|
168
|
+
const rows = [];
|
|
169
|
+
for (const layer of ['security','reliability','observability','scale_contract']) {
|
|
170
|
+
for (const [dim, d1] of Object.entries(a1.layers?.[layer] || {})) {
|
|
171
|
+
const d2 = a2.layers?.[layer]?.[dim];
|
|
172
|
+
if (!d2) continue;
|
|
173
|
+
if (d1.state !== d2.state) {
|
|
174
|
+
rows.push({ layer, dim, before: d1.state, after: d2.state });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (rows.length === 0) {
|
|
180
|
+
console.log('No dimension state changes between the two audits.');
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
console.log(`\nDiff: ${path1} → ${path2}\n`);
|
|
185
|
+
for (const r of rows) {
|
|
186
|
+
const arrow = r.before === 'absent' && r.after === 'present' ? '↑ IMPROVED' :
|
|
187
|
+
r.before === 'present' && r.after === 'absent' ? '↓ REGRESSED' : '→ changed';
|
|
188
|
+
console.log(` ${arrow} ${r.layer}.${r.dim} ${r.before} → ${r.after}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function main() {
|
|
193
|
+
const args = parseArgs(process.argv.slice(2));
|
|
194
|
+
|
|
195
|
+
if (!args.cmd) { usage(); process.exit(1); }
|
|
196
|
+
|
|
197
|
+
if (args.cmd === 'fix') {
|
|
198
|
+
const dir = resolve(args.positional[0] || process.cwd());
|
|
199
|
+
if (!existsSync(dir)) {
|
|
200
|
+
console.error(`ERROR: directory not found: ${dir}`);
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let audit;
|
|
205
|
+
if (args.fromAudit) {
|
|
206
|
+
audit = JSON.parse(readFileSync(resolve(args.fromAudit), 'utf8'));
|
|
207
|
+
} else {
|
|
208
|
+
console.error('[macrc] running audit to build patch list...');
|
|
209
|
+
audit = await runAudit(dir, { ...args, format: 'json' });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const allPatches = buildPatches(audit, dir);
|
|
213
|
+
const patches = args.dims
|
|
214
|
+
? allPatches.filter(p => args.dims.has(p._dim))
|
|
215
|
+
: allPatches;
|
|
216
|
+
|
|
217
|
+
if (patches.length === 0) {
|
|
218
|
+
console.log('No patches to apply, all Tier-1 dims are present or n/a.');
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
console.error(`[macrc] ${patches.length} patch(es) for dims: ${[...new Set(patches.map(p => p._dim))].join(', ')}`);
|
|
223
|
+
if (args.dryRun) console.error('[macrc] dry-run, no files will be modified\n');
|
|
224
|
+
|
|
225
|
+
const report = applyPatches(patches, dir, args.dryRun || false);
|
|
226
|
+
const output = formatPatchReport(report, { color: args.color });
|
|
227
|
+
|
|
228
|
+
if (args.out) {
|
|
229
|
+
const outPath = resolve(args.out);
|
|
230
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
231
|
+
writeFileSync(outPath, output + '\n');
|
|
232
|
+
console.error(`[macrc] written to ${args.out}`);
|
|
233
|
+
} else {
|
|
234
|
+
console.log(output);
|
|
235
|
+
}
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (args.cmd === 'diff') {
|
|
240
|
+
const [p1, p2] = [args.positional[0] || process.argv[3], args.positional[1] || process.argv[4]];
|
|
241
|
+
if (!p1 || !p2) { console.error('Usage: macrc diff <audit1.json> <audit2.json>'); process.exit(1); }
|
|
242
|
+
diffAudits(p1, p2);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (args.cmd !== 'audit' && args.cmd !== 'gate') {
|
|
247
|
+
console.error(`Unknown command: ${args.cmd}`);
|
|
248
|
+
usage();
|
|
249
|
+
process.exit(1);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const dir = args.positional[0] || process.cwd();
|
|
253
|
+
const audit = await runAudit(dir, args);
|
|
254
|
+
|
|
255
|
+
// Policy check
|
|
256
|
+
const policyDir = args.policyDir || dir;
|
|
257
|
+
const policy = loadPolicy(policyDir);
|
|
258
|
+
const pr = applyPolicy(policy, audit.layers);
|
|
259
|
+
|
|
260
|
+
// Format output
|
|
261
|
+
let output = '';
|
|
262
|
+
if (args.format === 'json') output = formatJson(audit);
|
|
263
|
+
else if (args.format === 'sarif') output = formatSarif(audit);
|
|
264
|
+
else output = formatTable(audit, { color: args.color });
|
|
265
|
+
|
|
266
|
+
if (args.format !== 'json' && args.format !== 'sarif') {
|
|
267
|
+
output += '\n\nPolicy:\n' + formatPolicyReport(pr);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (args.out) {
|
|
271
|
+
const outPath = resolve(args.out);
|
|
272
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
273
|
+
writeFileSync(outPath, output + '\n');
|
|
274
|
+
console.error(`[macrc] written to ${args.out}`);
|
|
275
|
+
} else {
|
|
276
|
+
console.log(output);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (args.cmd === 'gate') {
|
|
280
|
+
// Exit non-zero if any dims are explicitly blocked OR if there are warnings and no .macrc.yml
|
|
281
|
+
// was found (default policy = any absent/partial dim fails the gate)
|
|
282
|
+
const hasBlocks = !pr.passed;
|
|
283
|
+
const hasWarns = pr.warnCount > 0;
|
|
284
|
+
const noPolicy = !loadPolicy(args.policyDir || dir)._hasFile;
|
|
285
|
+
if (hasBlocks || (hasWarns && noPolicy)) {
|
|
286
|
+
const msg = hasBlocks
|
|
287
|
+
? `gate BLOCKED: policy violations present`
|
|
288
|
+
: `gate FAILED: ${pr.warnCount} absent dim(s) (add .macrc.yml to customise)`;
|
|
289
|
+
console.error(`\n[macrc] ${msg}`);
|
|
290
|
+
process.exit(1);
|
|
291
|
+
}
|
|
292
|
+
console.error('\n[macrc] gate PASSED');
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
main().catch(err => {
|
|
297
|
+
console.error('[macrc] FATAL:', err.message);
|
|
298
|
+
process.exit(1);
|
|
299
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bad-express-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Intentionally under-hardened Express app — MACRC demo target",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"axios": "^1.6.0",
|
|
11
|
+
"express": "^4.18.2",
|
|
12
|
+
"express-session": "^1.17.3"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import session from 'express-session';
|
|
3
|
+
import { userRouter } from './routes/users.js';
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
app.use(express.json());
|
|
7
|
+
|
|
8
|
+
// In-memory session, not safe for multi-instance deployments
|
|
9
|
+
app.use(session({
|
|
10
|
+
secret: 'keyboard cat',
|
|
11
|
+
resave: false,
|
|
12
|
+
saveUninitialized: true,
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
app.use('/users', userRouter);
|
|
16
|
+
|
|
17
|
+
app.listen(3000, () => {
|
|
18
|
+
console.log('Server running on port 3000');
|
|
19
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { fetchUserData } from '../services/api.js';
|
|
3
|
+
|
|
4
|
+
export const userRouter = Router();
|
|
5
|
+
|
|
6
|
+
// No input validation, req.body goes straight to the DB call
|
|
7
|
+
userRouter.post('/', async (req, res) => {
|
|
8
|
+
const { name, email } = req.body;
|
|
9
|
+
const profile = await fetchUserData(req.body.id);
|
|
10
|
+
res.json({ saved: true, name, email, profile });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
userRouter.get('/:id', async (req, res) => {
|
|
14
|
+
const data = await fetchUserData(req.params.id);
|
|
15
|
+
res.json(data);
|
|
16
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
// Hardcoded localhost, breaks in any multi-service deployment
|
|
4
|
+
const BASE = 'http://localhost:8080';
|
|
5
|
+
|
|
6
|
+
export async function fetchUserData(userId) {
|
|
7
|
+
// No timeout, hangs if the upstream is slow
|
|
8
|
+
const res = await axios.get(`${BASE}/users/${userId}`);
|
|
9
|
+
return res.data;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function fetchInventory() {
|
|
13
|
+
const res = await axios.get('http://localhost:8080/inventory');
|
|
14
|
+
return res.data;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function postOrder(payload) {
|
|
18
|
+
// No retry, a transient failure loses the order
|
|
19
|
+
const res = await axios.post('http://localhost:8081/orders', payload);
|
|
20
|
+
return res.data;
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shubh000/macrc",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Structural audit CLI for AI-generated code: 20 dimensions across Security, Reliability, Observability, and Scale Contract",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": "cli/macrc.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"cli/",
|
|
9
|
+
"demo/",
|
|
10
|
+
"tests/",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=18"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test tests/*.test.mjs"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"audit",
|
|
21
|
+
"ai",
|
|
22
|
+
"security",
|
|
23
|
+
"reliability",
|
|
24
|
+
"mcp",
|
|
25
|
+
"llm",
|
|
26
|
+
"static-analysis",
|
|
27
|
+
"cli"
|
|
28
|
+
],
|
|
29
|
+
"author": "triNetra Research",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/RocKing000/macrc.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/RocKing000/macrc/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://trinetra.life"
|
|
39
|
+
}
|