@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,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for cli/lib/ast-verifier.mjs
|
|
3
|
+
* Run: node --test tests/ast-verifier.test.mjs
|
|
4
|
+
*/
|
|
5
|
+
import { test } from 'node:test';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { verifyMatch } from '../cli/lib/ast-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,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for cli/lib/remediator.mjs
|
|
3
|
+
* Run: node --test tests/remediator.test.mjs
|
|
4
|
+
*/
|
|
5
|
+
import { test } from 'node:test';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { mkdirSync, writeFileSync, readFileSync, rmSync } from 'fs';
|
|
8
|
+
import { join, resolve } from 'path';
|
|
9
|
+
import { tmpdir } from 'os';
|
|
10
|
+
import { randomBytes } from 'crypto';
|
|
11
|
+
import { buildPatches, applyPatches } from '../cli/lib/remediator.mjs';
|
|
12
|
+
|
|
13
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
function makeTmp() {
|
|
16
|
+
const dir = join(tmpdir(), 'macrc-test-' + randomBytes(4).toString('hex'));
|
|
17
|
+
mkdirSync(dir, { recursive: true });
|
|
18
|
+
mkdirSync(join(dir, 'src'), { recursive: true });
|
|
19
|
+
// Minimal package.json so detectLanguage returns 'js'
|
|
20
|
+
writeFileSync(join(dir, 'package.json'), JSON.stringify({
|
|
21
|
+
name: 'test-app', type: 'module', dependencies: { express: '^4', axios: '^1' }
|
|
22
|
+
}));
|
|
23
|
+
return dir;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function cleanup(dir) {
|
|
27
|
+
try { rmSync(dir, { recursive: true, force: true }); } catch {}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function fakeAudit(absentDims) {
|
|
31
|
+
const layers = {
|
|
32
|
+
security: { auth: absent(), input_validation: absent(), transport_security: present(), dependency_hygiene: pending(), secrets_management: pending() },
|
|
33
|
+
reliability: { error_handling: present(), retry_logic: present(), circuit_breaking: present(), graceful_degradation: present(), timeout_handling: present() },
|
|
34
|
+
observability: { logging: present(), metrics_emission: present(), tracing: present(), alerting: present(), health_checks: present() },
|
|
35
|
+
scale_contract: { statelessness: present(), load_balancing_awareness: present(), distributed_assumptions: present(), rate_limiting: present(), caching_strategy: present() },
|
|
36
|
+
};
|
|
37
|
+
for (const dim of absentDims) {
|
|
38
|
+
for (const layer of Object.values(layers)) {
|
|
39
|
+
if (dim in layer) layer[dim] = absent();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { schema_version: '1.0', layers };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const absent = () => ({ state: 'absent', severity: 'high', notes: '', snippets: [] });
|
|
46
|
+
const present = () => ({ state: 'present', severity: null, notes: '', snippets: [] });
|
|
47
|
+
const pending = () => ({ state: 'pending', severity: null, notes: '', snippets: [] });
|
|
48
|
+
|
|
49
|
+
// ── health_checks ─────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
test('health_checks: injects /health endpoint before app.listen', () => {
|
|
52
|
+
const dir = makeTmp();
|
|
53
|
+
try {
|
|
54
|
+
writeFileSync(join(dir, 'src', 'index.js'),
|
|
55
|
+
"import express from 'express';\nconst app = express();\napp.listen(3000);\n");
|
|
56
|
+
const audit = fakeAudit(['health_checks']);
|
|
57
|
+
const patches = buildPatches(audit, dir);
|
|
58
|
+
applyPatches(patches, dir, false);
|
|
59
|
+
const result = readFileSync(join(dir, 'src', 'index.js'), 'utf8');
|
|
60
|
+
assert.ok(result.includes("app.get('/health'"), "health route injected");
|
|
61
|
+
assert.ok(result.includes('app.listen('), 'listen call preserved');
|
|
62
|
+
} finally { cleanup(dir); }
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ── timeout_handling ──────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
test('timeout_handling: adds axios instance with timeout when bare axios calls found', () => {
|
|
68
|
+
const dir = makeTmp();
|
|
69
|
+
try {
|
|
70
|
+
writeFileSync(join(dir, 'src', 'api.js'),
|
|
71
|
+
"import axios from 'axios';\nexport async function fetch(url) { return axios.get(url); }\n");
|
|
72
|
+
const audit = fakeAudit(['timeout_handling']);
|
|
73
|
+
const patches = buildPatches(audit, dir);
|
|
74
|
+
applyPatches(patches, dir, false);
|
|
75
|
+
const result = readFileSync(join(dir, 'src', 'api.js'), 'utf8');
|
|
76
|
+
assert.ok(result.includes('axios.create({ timeout: 10000 })'), 'timeout instance created');
|
|
77
|
+
} finally { cleanup(dir); }
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// ── retry_logic ───────────────────────────────────────────────────────────────
|
|
81
|
+
|
|
82
|
+
test('retry_logic: adds axios-retry import and wires it', () => {
|
|
83
|
+
const dir = makeTmp();
|
|
84
|
+
try {
|
|
85
|
+
writeFileSync(join(dir, 'src', 'api.js'),
|
|
86
|
+
"import axios from 'axios';\nexport async function call(url) { return axios.get(url); }\n");
|
|
87
|
+
const audit = fakeAudit(['retry_logic']);
|
|
88
|
+
const patches = buildPatches(audit, dir);
|
|
89
|
+
applyPatches(patches, dir, false);
|
|
90
|
+
const result = readFileSync(join(dir, 'src', 'api.js'), 'utf8');
|
|
91
|
+
assert.ok(result.includes("from 'axios-retry'"), 'axios-retry imported');
|
|
92
|
+
assert.ok(result.includes('axiosRetry(axios'), 'axiosRetry wired');
|
|
93
|
+
} finally { cleanup(dir); }
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// ── input_validation ──────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
test('input_validation: injects validateBody factory after last import in route file', () => {
|
|
99
|
+
const dir = makeTmp();
|
|
100
|
+
try {
|
|
101
|
+
mkdirSync(join(dir, 'src', 'routes'), { recursive: true });
|
|
102
|
+
writeFileSync(join(dir, 'src', 'routes', 'users.js'),
|
|
103
|
+
"import { Router } from 'express';\nimport { db } from '../db.js';\nconst router = Router();\n");
|
|
104
|
+
const audit = fakeAudit(['input_validation']);
|
|
105
|
+
const patches = buildPatches(audit, dir);
|
|
106
|
+
applyPatches(patches, dir, false);
|
|
107
|
+
const result = readFileSync(join(dir, 'src', 'routes', 'users.js'), 'utf8');
|
|
108
|
+
assert.ok(result.includes('function validateBody(schema)'), 'validateBody injected');
|
|
109
|
+
// Must appear after the import block
|
|
110
|
+
const importEnd = result.lastIndexOf("import { db }");
|
|
111
|
+
const fnStart = result.indexOf('function validateBody');
|
|
112
|
+
assert.ok(fnStart > importEnd, 'validateBody placed after imports');
|
|
113
|
+
} finally { cleanup(dir); }
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// ── distributed_assumptions ───────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
test('distributed_assumptions: replaces localhost URLs with env vars', () => {
|
|
119
|
+
const dir = makeTmp();
|
|
120
|
+
try {
|
|
121
|
+
writeFileSync(join(dir, 'src', 'api.js'),
|
|
122
|
+
"import axios from 'axios';\nconst res = await axios.get('http://localhost:8080/items');\n");
|
|
123
|
+
const audit = fakeAudit(['distributed_assumptions']);
|
|
124
|
+
const patches = buildPatches(audit, dir);
|
|
125
|
+
applyPatches(patches, dir, false);
|
|
126
|
+
const result = readFileSync(join(dir, 'src', 'api.js'), 'utf8');
|
|
127
|
+
// localhost is kept only as a fallback value inside the env expression
|
|
128
|
+
assert.ok(result.includes('process.env.SERVICE_URL_8080'), 'env var substituted');
|
|
129
|
+
assert.ok(!result.includes("axios.get('http://localhost"), 'bare localhost call replaced');
|
|
130
|
+
} finally { cleanup(dir); }
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// ── auth ──────────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
test('auth: injects requireAuth stub after last import in router file', () => {
|
|
136
|
+
const dir = makeTmp();
|
|
137
|
+
try {
|
|
138
|
+
mkdirSync(join(dir, 'src', 'routes'), { recursive: true });
|
|
139
|
+
writeFileSync(join(dir, 'src', 'routes', 'api.js'),
|
|
140
|
+
"import { Router } from 'express';\nimport { db } from '../db.js';\nconst router = Router();\nrouter.get('/', (req, res) => res.json([]));\n");
|
|
141
|
+
const audit = fakeAudit(['auth']);
|
|
142
|
+
const patches = buildPatches(audit, dir);
|
|
143
|
+
applyPatches(patches, dir, false);
|
|
144
|
+
const result = readFileSync(join(dir, 'src', 'routes', 'api.js'), 'utf8');
|
|
145
|
+
assert.ok(result.includes('function requireAuth(req, res, next)'), 'requireAuth injected');
|
|
146
|
+
const lastImport = result.lastIndexOf("import { db }");
|
|
147
|
+
const fnStart = result.indexOf('function requireAuth');
|
|
148
|
+
assert.ok(fnStart > lastImport, 'requireAuth placed after imports');
|
|
149
|
+
} finally { cleanup(dir); }
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ── statelessness ─────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
test('statelessness: adds connect-redis import for ESM express-session', () => {
|
|
155
|
+
const dir = makeTmp();
|
|
156
|
+
try {
|
|
157
|
+
writeFileSync(join(dir, 'src', 'index.js'),
|
|
158
|
+
"import express from 'express';\nimport session from 'express-session';\nconst app = express();\napp.use(session({ secret: 'x', resave: false, saveUninitialized: true }));\napp.listen(3000);\n");
|
|
159
|
+
const audit = fakeAudit(['statelessness']);
|
|
160
|
+
const patches = buildPatches(audit, dir);
|
|
161
|
+
applyPatches(patches, dir, false);
|
|
162
|
+
const result = readFileSync(join(dir, 'src', 'index.js'), 'utf8');
|
|
163
|
+
assert.ok(result.includes("from 'connect-redis'"), 'connect-redis imported');
|
|
164
|
+
assert.ok(result.includes('RedisStore'), 'RedisStore defined');
|
|
165
|
+
} finally { cleanup(dir); }
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// ── load_balancing_awareness ──────────────────────────────────────────────────
|
|
169
|
+
|
|
170
|
+
test('load_balancing_awareness: injects /health endpoint (prerequisite for LB probes)', () => {
|
|
171
|
+
const dir = makeTmp();
|
|
172
|
+
try {
|
|
173
|
+
writeFileSync(join(dir, 'src', 'index.js'),
|
|
174
|
+
"import express from 'express';\nconst app = express();\napp.listen(8080);\n");
|
|
175
|
+
const audit = fakeAudit(['load_balancing_awareness']);
|
|
176
|
+
const patches = buildPatches(audit, dir);
|
|
177
|
+
applyPatches(patches, dir, false);
|
|
178
|
+
const result = readFileSync(join(dir, 'src', 'index.js'), 'utf8');
|
|
179
|
+
assert.ok(result.includes("app.get('/health'"), 'health probe endpoint injected');
|
|
180
|
+
} finally { cleanup(dir); }
|
|
181
|
+
});
|