@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
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,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shubh000/macrc",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Structural audit CLI for AI-generated code: 20 dimensions across Security, Reliability, Observability, and Scale Contract",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"macrc": "cli/macrc.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"cli/",
|
|
11
|
+
"demo/",
|
|
12
|
+
"tests/",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --test tests/*.test.mjs"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"acorn": "^8.11.0"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"audit",
|
|
26
|
+
"ai",
|
|
27
|
+
"security",
|
|
28
|
+
"reliability",
|
|
29
|
+
"mcp",
|
|
30
|
+
"llm",
|
|
31
|
+
"static-analysis",
|
|
32
|
+
"cli"
|
|
33
|
+
],
|
|
34
|
+
"author": "triNetra Research",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/RocKing000/macrc.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/RocKing000/macrc/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://trinetra.life"
|
|
44
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for cli/lib/context-verifier.mjs
|
|
3
|
+
* Run: node --test tests/context-verifier.test.mjs
|
|
4
|
+
*/
|
|
5
|
+
import { test } from 'node:test';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { verifyMatch } from '../cli/lib/context-verifier.mjs';
|
|
8
|
+
|
|
9
|
+
// ── zod_usage ─────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
test('zod_usage: confirmed when safeParse(req.body) in route context', () => {
|
|
12
|
+
const result = verifyMatch('zod_usage', 'const schema = z.object({ name: z.string() })', [
|
|
13
|
+
"router.post('/users', (req, res) => {",
|
|
14
|
+
' const result = schema.safeParse(req.body);',
|
|
15
|
+
' if (!result.success) return res.status(400).json({ error: result.error });',
|
|
16
|
+
' res.json(result.data);',
|
|
17
|
+
'})',
|
|
18
|
+
]);
|
|
19
|
+
assert.equal(result, 'confirmed');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('zod_usage: unlikely when only schema definition, no req reference', () => {
|
|
23
|
+
const result = verifyMatch('zod_usage', 'export const UserSchema = z.object({ id: z.string() })', [
|
|
24
|
+
'import { z } from "zod";',
|
|
25
|
+
'export const UserSchema = z.object({ id: z.string() })',
|
|
26
|
+
'export const OrderSchema = z.object({ amount: z.number() })',
|
|
27
|
+
]);
|
|
28
|
+
assert.equal(result, 'unlikely');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// ── bearer_auth ───────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
test('bearer_auth: confirmed when app.use(authenticate) in context', () => {
|
|
34
|
+
const result = verifyMatch('bearer_auth', "const token = req.headers.authorization.split('Bearer ')[1]", [
|
|
35
|
+
"app.use('/api', authenticate);",
|
|
36
|
+
'function authenticate(req, res, next) {',
|
|
37
|
+
" const token = req.headers.authorization.split('Bearer ')[1]",
|
|
38
|
+
' if (!token) return res.status(401).end();',
|
|
39
|
+
'}',
|
|
40
|
+
]);
|
|
41
|
+
assert.equal(result, 'confirmed');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('bearer_auth: unlikely when "Bearer" is in a fetch client header', () => {
|
|
45
|
+
const result = verifyMatch('bearer_auth', "headers: { Authorization: 'Bearer ' + token }", [
|
|
46
|
+
'const response = await fetch(url, {',
|
|
47
|
+
" headers: { Authorization: 'Bearer ' + token }",
|
|
48
|
+
'});',
|
|
49
|
+
]);
|
|
50
|
+
assert.equal(result, 'unlikely');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// ── health_route ──────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
test('health_route: confirmed when app.get route registration', () => {
|
|
56
|
+
const result = verifyMatch('health_route', "app.get('/health', (req, res) => res.json({ status: 'ok' }))", [
|
|
57
|
+
'const app = express();',
|
|
58
|
+
"app.get('/health', (req, res) => res.json({ status: 'ok' }))",
|
|
59
|
+
'app.listen(3000);',
|
|
60
|
+
]);
|
|
61
|
+
assert.equal(result, 'confirmed');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('health_route: unlikely when "/health" is in a test assertion', () => {
|
|
65
|
+
const result = verifyMatch('health_route', "expect(res.status).toBe(200) // GET /health", [
|
|
66
|
+
"it('should return 200 for health check', async () => {",
|
|
67
|
+
" const res = await request(app).get('/health');",
|
|
68
|
+
" expect(res.status).toBe(200) // GET /health",
|
|
69
|
+
'});',
|
|
70
|
+
]);
|
|
71
|
+
assert.equal(result, 'unlikely');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// ── timeout_config ────────────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
test('timeout_config: confirmed when inside axios.create options', () => {
|
|
77
|
+
const result = verifyMatch('timeout_config', 'timeout: 5000,', [
|
|
78
|
+
'const client = axios.create({',
|
|
79
|
+
' baseURL: process.env.API_URL,',
|
|
80
|
+
' timeout: 5000,',
|
|
81
|
+
' headers: { Accept: "application/json" },',
|
|
82
|
+
'});',
|
|
83
|
+
]);
|
|
84
|
+
assert.equal(result, 'confirmed');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('timeout_config: unlikely when timeout is in a jest config', () => {
|
|
88
|
+
const result = verifyMatch('timeout_config', 'timeout: 30000', [
|
|
89
|
+
'module.exports = {',
|
|
90
|
+
' testEnvironment: "node",',
|
|
91
|
+
' timeout: 30000',
|
|
92
|
+
'};',
|
|
93
|
+
]);
|
|
94
|
+
assert.equal(result, 'unlikely');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// ── env_service_url ───────────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
test('env_service_url: confirmed when used as baseURL in axios.create', () => {
|
|
100
|
+
const result = verifyMatch('env_service_url', 'baseURL: process.env.API_URL,', [
|
|
101
|
+
'const http = axios.create({',
|
|
102
|
+
' baseURL: process.env.API_URL,',
|
|
103
|
+
' timeout: 5000,',
|
|
104
|
+
'});',
|
|
105
|
+
]);
|
|
106
|
+
assert.equal(result, 'confirmed');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('env_service_url: unlikely when only process.env.PORT is read', () => {
|
|
110
|
+
const result = verifyMatch('env_service_url', 'const port = process.env.PORT || 3000;', [
|
|
111
|
+
'const app = express();',
|
|
112
|
+
'const port = process.env.PORT || 3000;',
|
|
113
|
+
'app.listen(port);',
|
|
114
|
+
]);
|
|
115
|
+
assert.equal(result, 'unlikely');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// ── retry_pattern ─────────────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
test('retry_pattern: confirmed when inside axiosRetry call', () => {
|
|
121
|
+
const result = verifyMatch('retry_pattern', 'retries: 3,', [
|
|
122
|
+
'axiosRetry(axios, {',
|
|
123
|
+
' retries: 3,',
|
|
124
|
+
' retryDelay: axiosRetry.exponentialDelay,',
|
|
125
|
+
'});',
|
|
126
|
+
]);
|
|
127
|
+
assert.equal(result, 'confirmed');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('retry_pattern: unlikely when retryCount is in a plain config object', () => {
|
|
131
|
+
const result = verifyMatch('retry_pattern', 'maxRetries: 3,', [
|
|
132
|
+
'const config = {',
|
|
133
|
+
' maxRetries: 3,',
|
|
134
|
+
' pageSize: 100,',
|
|
135
|
+
'};',
|
|
136
|
+
]);
|
|
137
|
+
assert.equal(result, 'unlikely');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// ── py_timeout ────────────────────────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
test('py_timeout: confirmed when timeout= in requests.get call', () => {
|
|
143
|
+
const result = verifyMatch('py_timeout', 'response = requests.get(url, timeout=10)', [
|
|
144
|
+
'import requests',
|
|
145
|
+
'def fetch(url):',
|
|
146
|
+
' response = requests.get(url, timeout=10)',
|
|
147
|
+
' return response.json()',
|
|
148
|
+
]);
|
|
149
|
+
assert.equal(result, 'confirmed');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('py_timeout: unlikely when timeout= is in subprocess call', () => {
|
|
153
|
+
const result = verifyMatch('py_timeout', 'result = subprocess.run(cmd, timeout=30)', [
|
|
154
|
+
'import subprocess',
|
|
155
|
+
'def run_cmd(cmd):',
|
|
156
|
+
' result = subprocess.run(cmd, timeout=30)',
|
|
157
|
+
' return result.stdout',
|
|
158
|
+
]);
|
|
159
|
+
assert.equal(result, 'unlikely');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// ── pydantic_usage ────────────────────────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
test('pydantic_usage: confirmed when BaseModel used in FastAPI POST route', () => {
|
|
165
|
+
const result = verifyMatch('pydantic_usage', 'class UserCreate(BaseModel):', [
|
|
166
|
+
'@app.post("/users")',
|
|
167
|
+
'async def create_user(body: UserCreate):',
|
|
168
|
+
' return {"id": 1}',
|
|
169
|
+
'class UserCreate(BaseModel):',
|
|
170
|
+
' name: str',
|
|
171
|
+
' email: str',
|
|
172
|
+
]);
|
|
173
|
+
assert.equal(result, 'confirmed');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('pydantic_usage: unlikely when plain BaseModel data class with no route nearby', () => {
|
|
177
|
+
const result = verifyMatch('pydantic_usage', 'class Config(BaseModel):', [
|
|
178
|
+
'from pydantic import BaseModel',
|
|
179
|
+
'class Config(BaseModel):',
|
|
180
|
+
' db_host: str = "localhost"',
|
|
181
|
+
' db_port: int = 5432',
|
|
182
|
+
]);
|
|
183
|
+
assert.equal(result, 'unlikely');
|
|
184
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema_version": "1.0",
|
|
3
|
+
"repo_id": "fixture",
|
|
4
|
+
"audited_at": "2026-01-02",
|
|
5
|
+
"auditor": "macrc-heuristic",
|
|
6
|
+
"layers": {
|
|
7
|
+
"security": {
|
|
8
|
+
"auth": { "state": "present", "severity": null, "notes": "fixture: auth wired", "snippets": [] },
|
|
9
|
+
"input_validation": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
10
|
+
"transport_security": { "state": "absent", "severity": null, "notes": "fixture", "snippets": [] },
|
|
11
|
+
"dependency_hygiene": { "state": "pending", "severity": null, "notes": "fixture", "snippets": [] },
|
|
12
|
+
"secrets_management": { "state": "pending", "severity": null, "notes": "fixture", "snippets": [] }
|
|
13
|
+
},
|
|
14
|
+
"reliability": {
|
|
15
|
+
"error_handling": { "state": "absent", "severity": "critical", "notes": "fixture", "snippets": [] },
|
|
16
|
+
"retry_logic": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
17
|
+
"circuit_breaking": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
18
|
+
"graceful_degradation": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
19
|
+
"timeout_handling": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] }
|
|
20
|
+
},
|
|
21
|
+
"observability": {
|
|
22
|
+
"logging": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
23
|
+
"metrics_emission": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
24
|
+
"tracing": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
25
|
+
"alerting": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
26
|
+
"health_checks": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] }
|
|
27
|
+
},
|
|
28
|
+
"scale_contract": {
|
|
29
|
+
"statelessness": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
30
|
+
"load_balancing_awareness": { "state": "absent", "severity": "medium", "notes": "fixture", "snippets": [] },
|
|
31
|
+
"distributed_assumptions": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
32
|
+
"rate_limiting": { "state": "absent", "severity": "high", "notes": "fixture", "snippets": [] },
|
|
33
|
+
"caching_strategy": { "state": "absent", "severity": "low", "notes": "fixture", "snippets": [] }
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|