deployready 0.2.0 → 0.3.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 +37 -5
- package/dist/active/accessControl.js +62 -10
- package/dist/active/accessControl.js.map +1 -1
- package/dist/active/requestPlan.js +52 -26
- package/dist/active/requestPlan.js.map +1 -1
- package/dist/active/runner.js +46 -18
- package/dist/active/runner.js.map +1 -1
- package/dist/active/tamper.js +17 -4
- package/dist/active/tamper.js.map +1 -1
- package/dist/agent/FileEditor.js +91 -17
- package/dist/agent/FileEditor.js.map +1 -1
- package/dist/agent/FixManager.js +104 -3
- package/dist/agent/FixManager.js.map +1 -1
- package/dist/ai/CliHandler.js +80 -0
- package/dist/ai/CliHandler.js.map +1 -0
- package/dist/ai/ModelRouter.js +42 -1
- package/dist/ai/ModelRouter.js.map +1 -1
- package/dist/ai/cli/argv.js +67 -0
- package/dist/ai/cli/argv.js.map +1 -0
- package/dist/ai/cli/env.js +46 -0
- package/dist/ai/cli/env.js.map +1 -0
- package/dist/ai/cli/errors.js +15 -0
- package/dist/ai/cli/errors.js.map +1 -0
- package/dist/ai/cli/parseOutput.js +151 -0
- package/dist/ai/cli/parseOutput.js.map +1 -0
- package/dist/ai/cli/providers.js +30 -0
- package/dist/ai/cli/providers.js.map +1 -0
- package/dist/ai/cli/ready.js +121 -0
- package/dist/ai/cli/ready.js.map +1 -0
- package/dist/ai/cli/resolve.js +108 -0
- package/dist/ai/cli/resolve.js.map +1 -0
- package/dist/ai/cli/runCli.js +265 -0
- package/dist/ai/cli/runCli.js.map +1 -0
- package/dist/ai/parseAiResponse.js +24 -11
- package/dist/ai/parseAiResponse.js.map +1 -1
- package/dist/ai/verifyAiFindings.js +106 -0
- package/dist/ai/verifyAiFindings.js.map +1 -0
- package/dist/analysis/ScoreCalculator.js +22 -2
- package/dist/analysis/ScoreCalculator.js.map +1 -1
- package/dist/analysis/SeverityScorer.js +6 -0
- package/dist/analysis/SeverityScorer.js.map +1 -1
- package/dist/analysis/VulnerabilityDetector.js +15 -7
- package/dist/analysis/VulnerabilityDetector.js.map +1 -1
- package/dist/analysis/rules.js +13 -1
- package/dist/analysis/rules.js.map +1 -1
- package/dist/cli/Commands.js +10 -3
- package/dist/cli/Commands.js.map +1 -1
- package/dist/cli/InteractiveShell.js +72 -22
- package/dist/cli/InteractiveShell.js.map +1 -1
- package/dist/cli/Onboarding.js +13 -1
- package/dist/cli/Onboarding.js.map +1 -1
- package/dist/cli/cliSetup.js +48 -0
- package/dist/cli/cliSetup.js.map +1 -0
- package/dist/config/ConfigManager.js +11 -0
- package/dist/config/ConfigManager.js.map +1 -1
- package/dist/core/CiGate.js +33 -16
- package/dist/core/CiGate.js.map +1 -1
- package/dist/core/Orchestrator.js +154 -19
- package/dist/core/Orchestrator.js.map +1 -1
- package/dist/core/Verifier.js +7 -1
- package/dist/core/Verifier.js.map +1 -1
- package/dist/dynamic/DynamicTester.js +92 -8
- package/dist/dynamic/DynamicTester.js.map +1 -1
- package/dist/dynamic/EndpointMapper.js +54 -24
- package/dist/dynamic/EndpointMapper.js.map +1 -1
- package/dist/dynamic/LocalhostDetector.js +11 -23
- package/dist/dynamic/LocalhostDetector.js.map +1 -1
- package/dist/dynamic/SecurityTester.js +55 -10
- package/dist/dynamic/SecurityTester.js.map +1 -1
- package/dist/dynamic/adminEvidence.js +47 -0
- package/dist/dynamic/adminEvidence.js.map +1 -0
- package/dist/dynamic/advertised.js +293 -0
- package/dist/dynamic/advertised.js.map +1 -0
- package/dist/dynamic/catchAll.js +57 -0
- package/dist/dynamic/catchAll.js.map +1 -0
- package/dist/dynamic/commonPaths/evidence.js +112 -0
- package/dist/dynamic/commonPaths/evidence.js.map +1 -0
- package/dist/dynamic/commonPaths/prober.js +210 -0
- package/dist/dynamic/commonPaths/prober.js.map +1 -0
- package/dist/dynamic/commonPaths/signatures.js +381 -0
- package/dist/dynamic/commonPaths/signatures.js.map +1 -0
- package/dist/parser/CodeParser.js +138 -28
- package/dist/parser/CodeParser.js.map +1 -1
- package/dist/parser/adapters/BabelAdapter.js +558 -27
- package/dist/parser/adapters/BabelAdapter.js.map +1 -1
- package/dist/parser/adapters/PythonAdapter.js +360 -14
- package/dist/parser/adapters/PythonAdapter.js.map +1 -1
- package/dist/parser/adapters/authMiddleware.js +256 -0
- package/dist/parser/adapters/authMiddleware.js.map +1 -0
- package/dist/parser/adapters/nest.js +162 -0
- package/dist/parser/adapters/nest.js.map +1 -0
- package/dist/parser/miniYaml.js +308 -0
- package/dist/parser/miniYaml.js.map +1 -0
- package/dist/parser/nextRoutes.js +204 -0
- package/dist/parser/nextRoutes.js.map +1 -0
- package/dist/parser/openapi.js +180 -0
- package/dist/parser/openapi.js.map +1 -0
- package/dist/parser/pathUtil.js +22 -0
- package/dist/parser/pathUtil.js.map +1 -0
- package/dist/ui/ExportManager.js +61 -3
- package/dist/ui/ExportManager.js.map +1 -1
- package/dist/ui/HtmlReporter.js +47 -5
- package/dist/ui/HtmlReporter.js.map +1 -1
- package/dist/ui/IssuePresenter.js +54 -5
- package/dist/ui/IssuePresenter.js.map +1 -1
- package/dist/ui/commonPathSection.js +42 -0
- package/dist/ui/commonPathSection.js.map +1 -0
- package/dist/utils/FileWalker.js +125 -16
- package/dist/utils/FileWalker.js.map +1 -1
- package/dist/utils/GitIgnore.js +162 -0
- package/dist/utils/GitIgnore.js.map +1 -0
- package/dist/utils/finding.js +5 -0
- package/dist/utils/finding.js.map +1 -1
- package/dist/utils/logger.js +4 -0
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/sanitize.js +30 -0
- package/dist/utils/sanitize.js.map +1 -0
- package/docs/sample-config.json +2 -1
- package/package.json +12 -4
|
@@ -6,17 +6,53 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.BabelAdapter = void 0;
|
|
7
7
|
/**
|
|
8
8
|
* JS/TS parser using @babel/parser — pure JavaScript, no native build step.
|
|
9
|
-
* Extracts imports, exports, and Express/Koa/Fastify-style route definitions.
|
|
9
|
+
* Extracts imports, exports, and Express/Koa/Fastify/Hono-style route definitions.
|
|
10
|
+
*
|
|
11
|
+
* Path rule: a route or mount prefix is only reported when it is a string
|
|
12
|
+
* literal, or a template/array/never-reassigned `const` that resolves to
|
|
13
|
+
* strings inside this same file. Anything else (imports, env vars, calls) is
|
|
14
|
+
* skipped, never guessed.
|
|
10
15
|
*
|
|
11
16
|
* Defensive: parsing arbitrary user code can throw; every parse is wrapped and
|
|
12
17
|
* degrades to an empty result rather than crashing the scan.
|
|
13
18
|
*/
|
|
14
19
|
const parser_1 = require("@babel/parser");
|
|
15
20
|
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
21
|
+
const authMiddleware_1 = require("./authMiddleware");
|
|
22
|
+
const pathUtil_1 = require("../pathUtil");
|
|
23
|
+
const nest_1 = require("./nest");
|
|
16
24
|
// @babel/traverse ships as a CJS module with a `.default` in some setups.
|
|
17
25
|
const traverse = traverse_1.default.default ?? traverse_1.default;
|
|
18
26
|
// `use` is handled separately as a mount, not an HTTP verb.
|
|
19
27
|
const HTTP_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete', 'options', 'head', 'all']);
|
|
28
|
+
// A module path segment like routes/, auth.routes, controllers/ means the binding is a router, not a guard.
|
|
29
|
+
const ROUTER_PATH_WORDS = new Set(['route', 'routes', 'router', 'routers', 'controller', 'controllers']);
|
|
30
|
+
// Prefix characters that make a `use` prefix a pattern we cannot match segment-wise.
|
|
31
|
+
const PATTERN_PREFIX = /[*()?+\\[\]{}]/;
|
|
32
|
+
const MAX_RESOLVE_DEPTH = 6;
|
|
33
|
+
// Receivers whose `.get('x')` reads a header/cookie/query value and never registers a route.
|
|
34
|
+
const NON_ROUTER_RECEIVERS = new Set(['headers', 'cookies', 'searchparams', 'params', 'query', 'formdata', 'localstorage', 'sessionstorage', 'env']);
|
|
35
|
+
// NestJS calls that change the final path of every controller.
|
|
36
|
+
const NEST_GLOBAL_CALLS = new Map([
|
|
37
|
+
['setglobalprefix', 'setGlobalPrefix'],
|
|
38
|
+
['enableversioning', 'enableVersioning'],
|
|
39
|
+
]);
|
|
40
|
+
// Names that read as a path prefix (`API_PREFIX`, `basePath`, `usersUrl`); only used to tell `use(PREFIX, router)` from `use(middleware, router)`.
|
|
41
|
+
const PREFIX_NAME = /^[A-Z][A-Z0-9_]*$/;
|
|
42
|
+
const PREFIX_LIKE = /prefix|(path|url|base|root|mount)$/i;
|
|
43
|
+
// First `use` argument shapes that might be a path prefix rather than middleware.
|
|
44
|
+
const POSSIBLE_PREFIX_TYPES = new Set([
|
|
45
|
+
'Identifier',
|
|
46
|
+
'MemberExpression',
|
|
47
|
+
'TemplateLiteral',
|
|
48
|
+
'BinaryExpression',
|
|
49
|
+
'ArrayExpression',
|
|
50
|
+
'RegExpLiteral',
|
|
51
|
+
'SpreadElement',
|
|
52
|
+
'ConditionalExpression',
|
|
53
|
+
'LogicalExpression',
|
|
54
|
+
'TSAsExpression',
|
|
55
|
+
]);
|
|
20
56
|
/** True if a node is `require('some-string')`. */
|
|
21
57
|
function requireSource(node) {
|
|
22
58
|
if (node &&
|
|
@@ -28,6 +64,277 @@ function requireSource(node) {
|
|
|
28
64
|
}
|
|
29
65
|
return null;
|
|
30
66
|
}
|
|
67
|
+
/** `require('x')` or `import('x')` (Fastify accepts a dynamic import as a plugin). */
|
|
68
|
+
function moduleSource(node) {
|
|
69
|
+
const req = requireSource(node);
|
|
70
|
+
if (req)
|
|
71
|
+
return req;
|
|
72
|
+
if (node && node.type === 'CallExpression' && node.callee.type === 'Import' && node.arguments[0]?.type === 'StringLiteral') {
|
|
73
|
+
return node.arguments[0].value;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
/** Stable name for the object a route/use is registered on (`app`, `this.router`), or null. */
|
|
78
|
+
function objectKey(node) {
|
|
79
|
+
if (node.type === 'Identifier')
|
|
80
|
+
return node.name;
|
|
81
|
+
if (node.type === 'ThisExpression')
|
|
82
|
+
return 'this';
|
|
83
|
+
if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier') {
|
|
84
|
+
const base = objectKey(node.object);
|
|
85
|
+
return base ? `${base}.${node.property.name}` : null;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function segments(p) {
|
|
90
|
+
return p.split('/').filter(Boolean);
|
|
91
|
+
}
|
|
92
|
+
/** Segment-aware prefix test: `/admin` covers `/admin/stats` but not `/administrator`. */
|
|
93
|
+
function covers(prefix, target) {
|
|
94
|
+
return prefix.length <= target.length && prefix.every((s, i) => s === target[i]);
|
|
95
|
+
}
|
|
96
|
+
function uniq(names) {
|
|
97
|
+
return [...new Set(names)];
|
|
98
|
+
}
|
|
99
|
+
function withNames(obj, names) {
|
|
100
|
+
const u = uniq(names);
|
|
101
|
+
return u.length ? { ...obj, guardNames: u } : obj;
|
|
102
|
+
}
|
|
103
|
+
function isRouterSpecifier(spec) {
|
|
104
|
+
return spec
|
|
105
|
+
.toLowerCase()
|
|
106
|
+
.split(/[/.\\]/)
|
|
107
|
+
.some((s) => ROUTER_PATH_WORDS.has(s));
|
|
108
|
+
}
|
|
109
|
+
function unwrap(node) {
|
|
110
|
+
let n = node;
|
|
111
|
+
while (n.type === 'TSAsExpression' ||
|
|
112
|
+
n.type === 'TSSatisfiesExpression' ||
|
|
113
|
+
n.type === 'TSNonNullExpression' ||
|
|
114
|
+
n.type === 'TSTypeAssertion' ||
|
|
115
|
+
n.type === 'ParenthesizedExpression') {
|
|
116
|
+
n = n.expression;
|
|
117
|
+
}
|
|
118
|
+
return n;
|
|
119
|
+
}
|
|
120
|
+
/** The string a node evaluates to from literals, template literals and never-reassigned same-file constants; null for anything else. */
|
|
121
|
+
function resolveStr(node, scope, depth = 0) {
|
|
122
|
+
if (!node || depth > MAX_RESOLVE_DEPTH)
|
|
123
|
+
return null;
|
|
124
|
+
const n = unwrap(node);
|
|
125
|
+
if (n.type === 'StringLiteral')
|
|
126
|
+
return n.value;
|
|
127
|
+
if (n.type === 'TemplateLiteral') {
|
|
128
|
+
let out = '';
|
|
129
|
+
for (let i = 0; i < n.quasis.length; i++) {
|
|
130
|
+
const cooked = n.quasis[i].value.cooked;
|
|
131
|
+
if (cooked === null || cooked === undefined)
|
|
132
|
+
return null;
|
|
133
|
+
out += cooked;
|
|
134
|
+
const expr = n.expressions[i];
|
|
135
|
+
if (expr) {
|
|
136
|
+
const v = resolveStr(expr, scope, depth + 1);
|
|
137
|
+
if (v === null)
|
|
138
|
+
return null;
|
|
139
|
+
out += v;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
if (n.type === 'Identifier') {
|
|
145
|
+
const b = scope.getBinding(n.name);
|
|
146
|
+
const decl = b?.path.node;
|
|
147
|
+
if (b && b.constant && decl?.type === 'VariableDeclarator' && decl.id.type === 'Identifier' && decl.init) {
|
|
148
|
+
return resolveStr(decl.init, b.scope, depth + 1);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
/** `request.headers.get(..)`, `cookies().get(..)`, `searchParams.get(..)`: a value lookup, not a route registration. */
|
|
154
|
+
function isNonRouterReceiver(obj) {
|
|
155
|
+
const o = unwrap(obj);
|
|
156
|
+
if (o.type === 'CallExpression') {
|
|
157
|
+
const name = calleeName(o.callee);
|
|
158
|
+
return name !== null && NON_ROUTER_RECEIVERS.has(name.toLowerCase());
|
|
159
|
+
}
|
|
160
|
+
const key = objectKey(o);
|
|
161
|
+
return key !== null && NON_ROUTER_RECEIVERS.has(key.split('.').pop().toLowerCase());
|
|
162
|
+
}
|
|
163
|
+
/** Route path argument: a string literal, or a constant/template/array of them (non-literal forms must look like a path). */
|
|
164
|
+
function resolvePaths(node, scope) {
|
|
165
|
+
if (!node)
|
|
166
|
+
return null;
|
|
167
|
+
const n = unwrap(node);
|
|
168
|
+
// Express settings getters (`app.get('env')`) and header lookups pass a bare word: a route path starts with '/' (or is a '*' wildcard).
|
|
169
|
+
if (n.type === 'StringLiteral')
|
|
170
|
+
return n.value.startsWith('/') || n.value.startsWith('*') ? [n.value] : null;
|
|
171
|
+
if (n.type === 'ArrayExpression') {
|
|
172
|
+
const out = [];
|
|
173
|
+
for (const el of n.elements) {
|
|
174
|
+
if (!el || el.type === 'SpreadElement')
|
|
175
|
+
continue;
|
|
176
|
+
const v = resolveStr(el, scope);
|
|
177
|
+
if (v !== null && v.startsWith('/'))
|
|
178
|
+
out.push(v);
|
|
179
|
+
}
|
|
180
|
+
return out.length ? out : null;
|
|
181
|
+
}
|
|
182
|
+
const v = resolveStr(n, scope);
|
|
183
|
+
return v !== null && v.startsWith('/') ? [v] : null;
|
|
184
|
+
}
|
|
185
|
+
/** Strings in an array whose elements all resolve (a `use` prefix list), or null. */
|
|
186
|
+
function resolveStrArray(node, scope) {
|
|
187
|
+
const n = unwrap(node);
|
|
188
|
+
if (n.type !== 'ArrayExpression' || n.elements.length === 0)
|
|
189
|
+
return null;
|
|
190
|
+
const out = [];
|
|
191
|
+
for (const el of n.elements) {
|
|
192
|
+
if (!el || el.type === 'SpreadElement')
|
|
193
|
+
return null;
|
|
194
|
+
const v = resolveStr(el, scope);
|
|
195
|
+
if (v === null)
|
|
196
|
+
return null;
|
|
197
|
+
out.push(v);
|
|
198
|
+
}
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
/** Object literal behind an options argument (inline, or a same-file const), or null. */
|
|
202
|
+
function objectLiteral(node, scope, depth = 0) {
|
|
203
|
+
if (!node || depth > MAX_RESOLVE_DEPTH)
|
|
204
|
+
return null;
|
|
205
|
+
const n = unwrap(node);
|
|
206
|
+
if (n.type === 'ObjectExpression')
|
|
207
|
+
return n;
|
|
208
|
+
if (n.type === 'Identifier') {
|
|
209
|
+
const b = scope.getBinding(n.name);
|
|
210
|
+
const decl = b?.path.node;
|
|
211
|
+
if (b && b.constant && decl?.type === 'VariableDeclarator' && decl.id.type === 'Identifier' && decl.init) {
|
|
212
|
+
return objectLiteral(decl.init, b.scope, depth + 1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
/** Value of `key` in an object literal: undefined if absent, null if the literal has spreads/computed keys/methods we cannot read. */
|
|
218
|
+
function propValue(obj, key) {
|
|
219
|
+
let found;
|
|
220
|
+
for (const prop of obj.properties) {
|
|
221
|
+
if (prop.type === 'SpreadElement')
|
|
222
|
+
return null;
|
|
223
|
+
if (prop.computed)
|
|
224
|
+
return null;
|
|
225
|
+
const name = prop.key.type === 'Identifier' ? prop.key.name : prop.key.type === 'StringLiteral' ? prop.key.value : null;
|
|
226
|
+
if (name !== key)
|
|
227
|
+
continue;
|
|
228
|
+
if (prop.type !== 'ObjectProperty')
|
|
229
|
+
return null;
|
|
230
|
+
found = prop.value;
|
|
231
|
+
}
|
|
232
|
+
return found;
|
|
233
|
+
}
|
|
234
|
+
function calleeName(callee) {
|
|
235
|
+
if (callee.type === 'Identifier')
|
|
236
|
+
return callee.name;
|
|
237
|
+
if (callee.type === 'MemberExpression' && !callee.computed && callee.property.type === 'Identifier')
|
|
238
|
+
return callee.property.name;
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
/** Join prefix parts into one normalized path; '' when every part is empty. */
|
|
242
|
+
function combine(...parts) {
|
|
243
|
+
const real = parts.filter(Boolean);
|
|
244
|
+
return real.length ? real.reduce((acc, part) => (0, pathUtil_1.joinPath)(acc, part), '') : '';
|
|
245
|
+
}
|
|
246
|
+
/** Prefix an object carries from the call that created it: `new Router({ prefix })`, Hono `.basePath('/x')`. '' = none, null = not resolvable. */
|
|
247
|
+
function creationPrefix(init, scope) {
|
|
248
|
+
const n = unwrap(init);
|
|
249
|
+
if (n.type === 'CallExpression' && n.callee.type === 'MemberExpression' && calleeName(n.callee) === 'basePath' && n.arguments[0]) {
|
|
250
|
+
const base = creationPrefix(n.callee.object, scope);
|
|
251
|
+
const own = resolveStr(n.arguments[0], scope);
|
|
252
|
+
return base === null || own === null ? null : combine(base, own);
|
|
253
|
+
}
|
|
254
|
+
if ((n.type === 'NewExpression' || n.type === 'CallExpression') && /Router$/.test(calleeName(n.callee) ?? '')) {
|
|
255
|
+
const opts = n.arguments[0];
|
|
256
|
+
if (!opts)
|
|
257
|
+
return '';
|
|
258
|
+
const obj = objectLiteral(opts, scope);
|
|
259
|
+
if (!obj)
|
|
260
|
+
return null;
|
|
261
|
+
const v = propValue(obj, 'prefix');
|
|
262
|
+
if (v === undefined)
|
|
263
|
+
return '';
|
|
264
|
+
return v === null ? null : resolveStr(v, scope);
|
|
265
|
+
}
|
|
266
|
+
return '';
|
|
267
|
+
}
|
|
268
|
+
/** Fastify `register(plugin, opts)`: the prefix list the plugin's routes get; null when opts is present but its prefix is not readable. */
|
|
269
|
+
function registerPrefix(opts, scope) {
|
|
270
|
+
if (!opts)
|
|
271
|
+
return [''];
|
|
272
|
+
const obj = objectLiteral(opts, scope);
|
|
273
|
+
if (!obj)
|
|
274
|
+
return null;
|
|
275
|
+
if (propValue(obj, 'routes') !== undefined)
|
|
276
|
+
return null;
|
|
277
|
+
const v = propValue(obj, 'prefix');
|
|
278
|
+
if (v === undefined)
|
|
279
|
+
return [''];
|
|
280
|
+
const s = v === null ? null : resolveStr(v, scope);
|
|
281
|
+
return s === null ? null : [s];
|
|
282
|
+
}
|
|
283
|
+
/** Does the first `use` argument read as a path prefix we cannot resolve (so the mount is skipped, not guessed)? */
|
|
284
|
+
function looksLikeUnresolvedPrefix(first, argCount, scope) {
|
|
285
|
+
const n = unwrap(first);
|
|
286
|
+
if (n.type === 'TemplateLiteral' || n.type === 'RegExpLiteral')
|
|
287
|
+
return true;
|
|
288
|
+
if (n.type === 'BinaryExpression')
|
|
289
|
+
return n.operator === '+';
|
|
290
|
+
if (argCount < 2)
|
|
291
|
+
return false;
|
|
292
|
+
if (n.type === 'ArrayExpression')
|
|
293
|
+
return n.elements.some((e) => e && (e.type === 'StringLiteral' || e.type === 'TemplateLiteral'));
|
|
294
|
+
if (n.type === 'Identifier') {
|
|
295
|
+
const decl = scope.getBinding(n.name)?.path.node;
|
|
296
|
+
if (decl?.type === 'VariableDeclarator' && decl.init) {
|
|
297
|
+
const initType = unwrap(decl.init).type;
|
|
298
|
+
const stringish = ['StringLiteral', 'TemplateLiteral', 'Identifier', 'MemberExpression', 'BinaryExpression', 'ConditionalExpression', 'LogicalExpression'];
|
|
299
|
+
if (!stringish.includes(initType))
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
return PREFIX_NAME.test(n.name) || PREFIX_LIKE.test(n.name);
|
|
303
|
+
}
|
|
304
|
+
if (n.type === 'MemberExpression') {
|
|
305
|
+
if (n.computed || n.property.type !== 'Identifier')
|
|
306
|
+
return false;
|
|
307
|
+
return PREFIX_NAME.test(n.property.name) || PREFIX_LIKE.test(n.property.name) || objectKey(n.object) === 'process.env';
|
|
308
|
+
}
|
|
309
|
+
if (n.type === 'ConditionalExpression' || n.type === 'LogicalExpression') {
|
|
310
|
+
const leaves = n.type === 'ConditionalExpression' ? [n.consequent, n.alternate] : [n.left, n.right];
|
|
311
|
+
return leaves.some((l) => {
|
|
312
|
+
const u = unwrap(l);
|
|
313
|
+
return u.type === 'StringLiteral' && u.value.startsWith('/');
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
/** `app.route('/x').get(h).post(h)`: the `.route('/x')` call at the base of a verb chain, plus the arguments of every `.all(...)` step below the verb. */
|
|
319
|
+
function routeChainBase(node) {
|
|
320
|
+
let cur = node;
|
|
321
|
+
const allArgs = [];
|
|
322
|
+
for (let i = 0; i < 20; i++) {
|
|
323
|
+
if (cur.type !== 'CallExpression' || cur.callee.type !== 'MemberExpression')
|
|
324
|
+
return null;
|
|
325
|
+
const name = calleeName(cur.callee)?.toLowerCase();
|
|
326
|
+
if (!name)
|
|
327
|
+
return null;
|
|
328
|
+
if (name === 'route')
|
|
329
|
+
return cur.arguments.length === 1 ? { base: cur, allArgs } : null;
|
|
330
|
+
if (!HTTP_METHODS.has(name))
|
|
331
|
+
return null;
|
|
332
|
+
if (name === 'all')
|
|
333
|
+
allArgs.push(...cur.arguments);
|
|
334
|
+
cur = cur.callee.object;
|
|
335
|
+
}
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
31
338
|
class BabelAdapter {
|
|
32
339
|
extensions = new Set(['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs']);
|
|
33
340
|
parse(relPath, source) {
|
|
@@ -37,8 +344,112 @@ class BabelAdapter {
|
|
|
37
344
|
const mounts = [];
|
|
38
345
|
// local identifier -> module specifier it was imported/required from.
|
|
39
346
|
const bindings = new Map();
|
|
40
|
-
// mounts
|
|
41
|
-
const
|
|
347
|
+
// mounts of imported/required routers; resolved to prefixes after traversal.
|
|
348
|
+
const fileMounts = [];
|
|
349
|
+
// routes recorded with the object they were registered on; prefixed after traversal.
|
|
350
|
+
const routeRecs = [];
|
|
351
|
+
// same-file mounts, keyed by the mounted router's declaring identifier (or, for Fastify plugins, the plugin function node).
|
|
352
|
+
const localMounts = new Map();
|
|
353
|
+
// declaring identifiers of routers that had `.prefix(...)` called on them (Koa): their final prefix is not tracked.
|
|
354
|
+
const prefixCalled = new Set();
|
|
355
|
+
// object key (`app`, `router`) -> path-prefix segments (and names) of auth middleware applied via `use`, in source order.
|
|
356
|
+
const scopeGuards = new Map();
|
|
357
|
+
// classes seen (NestJS controllers are read from these after the traversal, once every import is known) and Nest calls that change every controller path.
|
|
358
|
+
const classes = [];
|
|
359
|
+
const nestGlobal = new Set();
|
|
360
|
+
const scopedNames = (key, target) => key === null ? [] : (scopeGuards.get(key) ?? []).filter((g) => covers(g.scope, target)).flatMap((g) => g.names);
|
|
361
|
+
// A recognised auth middleware, unless it is an identifier imported from a routes/controllers module.
|
|
362
|
+
const isGuard = (a) => {
|
|
363
|
+
if (!(0, authMiddleware_1.isAuthMiddleware)(a))
|
|
364
|
+
return false;
|
|
365
|
+
if (a.type !== 'Identifier')
|
|
366
|
+
return true;
|
|
367
|
+
const spec = bindings.get(a.name);
|
|
368
|
+
return !(spec && isRouterSpecifier(spec));
|
|
369
|
+
};
|
|
370
|
+
const addLocalMount = (key, m) => {
|
|
371
|
+
const list = localMounts.get(key) ?? [];
|
|
372
|
+
list.push(m);
|
|
373
|
+
localMounts.set(key, list);
|
|
374
|
+
};
|
|
375
|
+
/**
|
|
376
|
+
* Record that `target` is mounted on `obj` at `prefixes`. An imported router
|
|
377
|
+
* becomes a file mount; a router/plugin defined in this file is tracked locally.
|
|
378
|
+
* `allowFunctions` is only true for Fastify `register`: elsewhere a function
|
|
379
|
+
* argument is a handler, not a plugin.
|
|
380
|
+
*/
|
|
381
|
+
const recordMount = (obj, scope, prefixes, target, guarded, guardNames, allowFunctions) => {
|
|
382
|
+
const src = moduleSource(target);
|
|
383
|
+
if (src) {
|
|
384
|
+
fileMounts.push({ obj, scope, prefixes, source: src, guarded, guardNames });
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (allowFunctions && (target.type === 'FunctionExpression' || target.type === 'ArrowFunctionExpression')) {
|
|
388
|
+
addLocalMount(target, { obj, scope, prefixes });
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (target.type !== 'Identifier')
|
|
392
|
+
return;
|
|
393
|
+
const imported = bindings.get(target.name);
|
|
394
|
+
if (imported) {
|
|
395
|
+
fileMounts.push({ obj, scope, prefixes, source: imported, guarded, guardNames });
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const b = scope.getBinding(target.name);
|
|
399
|
+
if (!b)
|
|
400
|
+
return;
|
|
401
|
+
const decl = b.path.node;
|
|
402
|
+
if (decl.type === 'FunctionDeclaration') {
|
|
403
|
+
if (allowFunctions)
|
|
404
|
+
addLocalMount(decl, { obj, scope, prefixes });
|
|
405
|
+
}
|
|
406
|
+
else if (decl.type === 'VariableDeclarator' && decl.init && isFunctionLike(decl.init)) {
|
|
407
|
+
if (allowFunctions)
|
|
408
|
+
addLocalMount(unwrap(decl.init), { obj, scope, prefixes });
|
|
409
|
+
}
|
|
410
|
+
else if (decl.type === 'VariableDeclarator') {
|
|
411
|
+
addLocalMount(b.identifier, { obj, scope, prefixes });
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
const isFunctionLike = (n) => {
|
|
415
|
+
const u = unwrap(n);
|
|
416
|
+
return u.type === 'FunctionExpression' || u.type === 'ArrowFunctionExpression';
|
|
417
|
+
};
|
|
418
|
+
const ownPrefix = (b) => {
|
|
419
|
+
if (prefixCalled.has(b.identifier))
|
|
420
|
+
return null;
|
|
421
|
+
const decl = b.path.node;
|
|
422
|
+
if (decl.type !== 'VariableDeclarator' || !decl.init)
|
|
423
|
+
return '';
|
|
424
|
+
return creationPrefix(decl.init, b.scope);
|
|
425
|
+
};
|
|
426
|
+
/** Every full prefix the routes registered on `obj` live under: its own prefix plus the same-file mounts above it. Empty = unknowable, skip. */
|
|
427
|
+
const ownerPrefixes = (obj, scope, depth = 0) => {
|
|
428
|
+
if (!obj || obj.type !== 'Identifier' || depth > MAX_RESOLVE_DEPTH)
|
|
429
|
+
return [''];
|
|
430
|
+
const b = scope.getBinding(obj.name);
|
|
431
|
+
if (!b)
|
|
432
|
+
return [''];
|
|
433
|
+
const own = ownPrefix(b);
|
|
434
|
+
if (own === null)
|
|
435
|
+
return [];
|
|
436
|
+
const regs = [...(localMounts.get(b.identifier) ?? [])];
|
|
437
|
+
const fn = b.scope.block;
|
|
438
|
+
if (b.kind === 'param' && 'params' in fn && fn.params[0] === b.identifier)
|
|
439
|
+
regs.push(...(localMounts.get(fn) ?? []));
|
|
440
|
+
if (regs.length === 0)
|
|
441
|
+
return [own];
|
|
442
|
+
const out = new Set();
|
|
443
|
+
for (const r of regs) {
|
|
444
|
+
if (r.prefixes === null)
|
|
445
|
+
continue;
|
|
446
|
+
for (const above of ownerPrefixes(r.obj, r.scope, depth + 1)) {
|
|
447
|
+
for (const rp of r.prefixes)
|
|
448
|
+
out.add(combine(above, rp, own));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return [...out];
|
|
452
|
+
};
|
|
42
453
|
let ast;
|
|
43
454
|
try {
|
|
44
455
|
ast = (0, parser_1.parse)(source, {
|
|
@@ -86,39 +497,137 @@ class BabelAdapter {
|
|
|
86
497
|
return;
|
|
87
498
|
const method = callee.property.name.toLowerCase();
|
|
88
499
|
const args = p.node.arguments;
|
|
89
|
-
|
|
500
|
+
const key = objectKey(callee.object);
|
|
501
|
+
const scope = p.scope;
|
|
502
|
+
// NestJS: app.setGlobalPrefix(..), app.enableVersioning(..), RouterModule.register(..) rewrite the path of every controller.
|
|
503
|
+
const nestCall = NEST_GLOBAL_CALLS.get(method);
|
|
504
|
+
if (nestCall)
|
|
505
|
+
nestGlobal.add(nestCall);
|
|
506
|
+
else if (method === 'register' && key === 'RouterModule')
|
|
507
|
+
nestGlobal.add('RouterModule');
|
|
508
|
+
// koa-router `router.prefix('/x')` rewrites the prefix of every route; we do not track it.
|
|
509
|
+
if (method === 'prefix' && callee.object.type === 'Identifier') {
|
|
510
|
+
const b = scope.getBinding(callee.object.name);
|
|
511
|
+
if (b)
|
|
512
|
+
prefixCalled.add(b.identifier);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
// Fastify: fastify.register(plugin, { prefix: '/api' })
|
|
516
|
+
if (method === 'register' && args[0]) {
|
|
517
|
+
const prefixes = registerPrefix(args[1], scope);
|
|
518
|
+
recordMount(callee.object, scope, prefixes, args[0], false, [], true);
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
// Hono: app.route('/admin', adminApp) (Express' one-argument app.route('/x') is a chain base, handled with its verbs)
|
|
522
|
+
if (method === 'route' && args.length >= 2 && args[1]) {
|
|
523
|
+
const prefix = resolveStr(args[0], scope);
|
|
524
|
+
recordMount(callee.object, scope, prefix === null ? null : [prefix], args[1], false, [], false);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
// app.use('/api/auth', authRoutes) — a sub-router mount; app.use(auth) — a guard.
|
|
90
528
|
if (method === 'use') {
|
|
91
529
|
let prefix = '';
|
|
530
|
+
let prefixes = [''];
|
|
92
531
|
let i = 0;
|
|
93
|
-
|
|
94
|
-
|
|
532
|
+
let scopeKnown = true;
|
|
533
|
+
const first = args[0];
|
|
534
|
+
if (first?.type === 'StringLiteral') {
|
|
535
|
+
prefix = first.value;
|
|
536
|
+
prefixes = [prefix];
|
|
95
537
|
i = 1;
|
|
538
|
+
if (PATTERN_PREFIX.test(prefix))
|
|
539
|
+
scopeKnown = false;
|
|
540
|
+
}
|
|
541
|
+
else if (first && !isGuard(first) && POSSIBLE_PREFIX_TYPES.has(first.type)) {
|
|
542
|
+
scopeKnown = false; // could be a dynamic prefix: do not invent a guard
|
|
543
|
+
const constant = resolveStr(first, scope);
|
|
544
|
+
const list = constant === null ? resolveStrArray(first, scope) : null;
|
|
545
|
+
if (constant !== null) {
|
|
546
|
+
// app.use(API_PREFIX, router) with a same-file constant
|
|
547
|
+
prefix = constant;
|
|
548
|
+
prefixes = [constant];
|
|
549
|
+
i = 1;
|
|
550
|
+
scopeKnown = !PATTERN_PREFIX.test(constant);
|
|
551
|
+
}
|
|
552
|
+
else if (list) {
|
|
553
|
+
prefixes = list;
|
|
554
|
+
i = 1;
|
|
555
|
+
}
|
|
556
|
+
else if (looksLikeUnresolvedPrefix(first, args.length, scope)) {
|
|
557
|
+
prefixes = null; // a prefix we cannot read: mounted routes are skipped
|
|
558
|
+
i = 1;
|
|
559
|
+
}
|
|
96
560
|
}
|
|
561
|
+
// Mounts are guarded by an auth middleware earlier in this call or an earlier `use` covering the prefix.
|
|
562
|
+
let guardSeen = false;
|
|
563
|
+
const callGuardNames = [];
|
|
564
|
+
const mountGuarded = () => guardSeen || (key !== null && (scopeGuards.get(key) ?? []).some((g) => covers(g.scope, segments(prefix))));
|
|
565
|
+
const mountNames = () => [...callGuardNames, ...scopedNames(key, segments(prefix))];
|
|
97
566
|
for (; i < args.length; i++) {
|
|
98
567
|
const a = args[i];
|
|
99
|
-
if (a
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
568
|
+
if (isGuard(a)) {
|
|
569
|
+
guardSeen = true;
|
|
570
|
+
const names = (0, authMiddleware_1.guardEvidenceNames)(a);
|
|
571
|
+
callGuardNames.push(...names);
|
|
572
|
+
if (scopeKnown && key) {
|
|
573
|
+
const list = scopeGuards.get(key) ?? [];
|
|
574
|
+
list.push({ scope: segments(prefix), names });
|
|
575
|
+
scopeGuards.set(key, list);
|
|
576
|
+
}
|
|
577
|
+
continue;
|
|
105
578
|
}
|
|
579
|
+
recordMount(callee.object, scope, prefixes, a, mountGuarded(), mountNames(), false);
|
|
106
580
|
}
|
|
107
581
|
return;
|
|
108
582
|
}
|
|
109
|
-
// app.get('/path', ...), router.post('/x', auth, handler)
|
|
583
|
+
// app.get('/path', ...), router.post('/x', auth, handler), app.route('/x').get(h).post(h)
|
|
110
584
|
if (!HTTP_METHODS.has(method))
|
|
111
585
|
return;
|
|
112
|
-
const
|
|
113
|
-
|
|
586
|
+
const chain = routeChainBase(callee.object);
|
|
587
|
+
let paths;
|
|
588
|
+
let owner;
|
|
589
|
+
let routeGuards;
|
|
590
|
+
if (chain) {
|
|
591
|
+
const baseCallee = chain.base.callee;
|
|
592
|
+
owner = baseCallee.object;
|
|
593
|
+
paths = resolvePaths(chain.base.arguments[0], scope);
|
|
594
|
+
// `.all(requireAuth)` below this verb guards it too; a verb whose arguments are all guards is a guard step, not a route.
|
|
595
|
+
if (method === 'all' && args.length > 0 && args.every((a) => isGuard(a)))
|
|
596
|
+
return;
|
|
597
|
+
routeGuards = [...chain.allArgs, ...args.slice(0, -1)].filter((a) => isGuard(a));
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
const first = args[0];
|
|
601
|
+
// A registration has a handler after the path, and its receiver is a router/app, not headers/cookies/params.
|
|
602
|
+
if (!first || args.length < 2 || isNonRouterReceiver(callee.object))
|
|
603
|
+
return;
|
|
604
|
+
owner = callee.object;
|
|
605
|
+
paths = resolvePaths(first, scope);
|
|
606
|
+
// Middleware sits between the path and the final handler; the handler itself is never a guard.
|
|
607
|
+
routeGuards = args.slice(1, -1).filter((a) => isGuard(a));
|
|
608
|
+
}
|
|
609
|
+
if (!paths)
|
|
114
610
|
return;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
611
|
+
const routeKey = chain ? objectKey(owner) : key;
|
|
612
|
+
for (const routePath of paths) {
|
|
613
|
+
const target = segments(routePath);
|
|
614
|
+
const scoped = routeKey !== null && (scopeGuards.get(routeKey) ?? []).some((g) => covers(g.scope, target));
|
|
615
|
+
const names = [...routeGuards.flatMap(authMiddleware_1.guardEvidenceNames), ...scopedNames(routeKey, target)];
|
|
616
|
+
routeRecs.push({
|
|
617
|
+
obj: owner,
|
|
618
|
+
scope,
|
|
619
|
+
hit: withNames({
|
|
620
|
+
method: method === 'all' ? 'ALL' : method.toUpperCase(),
|
|
621
|
+
path: routePath,
|
|
622
|
+
line: p.node.loc?.start.line,
|
|
623
|
+
guarded: routeGuards.length > 0 || scoped,
|
|
624
|
+
}, names),
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
ClassDeclaration(p) {
|
|
629
|
+
if (p.node.decorators?.length)
|
|
630
|
+
classes.push({ node: p.node, scope: p.scope });
|
|
122
631
|
},
|
|
123
632
|
ExportNamedDeclaration(p) {
|
|
124
633
|
const decl = p.node.declaration;
|
|
@@ -152,17 +661,39 @@ class BabelAdapter {
|
|
|
152
661
|
catch {
|
|
153
662
|
// Traversal can still throw on exotic nodes — keep whatever we gathered.
|
|
154
663
|
}
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
664
|
+
// NestJS controllers (decorators imported from @nestjs/common only).
|
|
665
|
+
try {
|
|
666
|
+
routes.push(...(0, nest_1.nestRoutes)(classes, resolveStr, (name) => bindings.get(name) === '@nestjs/common'));
|
|
667
|
+
}
|
|
668
|
+
catch {
|
|
669
|
+
// A decorator shape we do not understand: keep the other routes.
|
|
670
|
+
}
|
|
671
|
+
// Routes live under the prefixes of the router/plugin they were registered on; none resolvable = skipped.
|
|
672
|
+
for (const r of routeRecs) {
|
|
673
|
+
for (const above of ownerPrefixes(r.obj, r.scope)) {
|
|
674
|
+
routes.push(above ? { ...r.hit, path: (0, pathUtil_1.joinPath)(above, r.hit.path) } : r.hit);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
// Imported routers: mounts keep the prefix of whatever they were registered on; an unreadable prefix marks the mount unresolved.
|
|
678
|
+
for (const m of fileMounts) {
|
|
679
|
+
const flags = { ...(m.guarded ? { guarded: true } : {}) };
|
|
680
|
+
const above = ownerPrefixes(m.obj, m.scope);
|
|
681
|
+
if (m.prefixes === null || above.length === 0) {
|
|
682
|
+
mounts.push(withNames({ prefix: '', source: m.source, unresolved: true, ...flags }, m.guardNames));
|
|
683
|
+
continue;
|
|
684
|
+
}
|
|
685
|
+
for (const a of above) {
|
|
686
|
+
for (const prefix of m.prefixes) {
|
|
687
|
+
mounts.push(withNames({ prefix: a ? combine(a, prefix) : prefix, source: m.source, ...flags }, m.guardNames));
|
|
688
|
+
}
|
|
689
|
+
}
|
|
160
690
|
}
|
|
161
691
|
return {
|
|
162
692
|
imports: [...imports],
|
|
163
693
|
exports: [...exports],
|
|
164
694
|
routes,
|
|
165
695
|
mounts,
|
|
696
|
+
...(nestGlobal.size ? { nestGlobalConfig: [...nestGlobal] } : {}),
|
|
166
697
|
};
|
|
167
698
|
}
|
|
168
699
|
}
|