@contrast/route-coverage 1.5.0 → 1.7.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/lib/index.d.ts +4 -1
- package/lib/index.js +8 -2
- package/lib/install/express.js +197 -0
- package/lib/utils/route-info.js +1 -1
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -17,13 +17,15 @@ import { Installable, Messages, RouteInfo } from '@contrast/common';
|
|
|
17
17
|
import { Logger } from '@contrast/logger';
|
|
18
18
|
import { Patcher } from '@contrast/patcher';
|
|
19
19
|
import RequireHook from '@contrast/require-hook';
|
|
20
|
+
import { Scopes } from '@contrast/scopes';
|
|
20
21
|
|
|
21
22
|
export { RouteInfo };
|
|
22
23
|
|
|
23
24
|
export interface RouteCoverage extends Installable {
|
|
24
25
|
discover(info: RouteInfo): void;
|
|
26
|
+
delete(info: RouteInfo): void;
|
|
25
27
|
discoveryFinished(): void;
|
|
26
|
-
observe(info: Pick<RouteInfo
|
|
28
|
+
observe(info: Pick<RouteInfo>): void;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
export interface Core {
|
|
@@ -31,6 +33,7 @@ export interface Core {
|
|
|
31
33
|
readonly logger: Logger;
|
|
32
34
|
readonly messages: Messages;
|
|
33
35
|
readonly patcher: Patcher;
|
|
36
|
+
readonly scopes: Scopes;
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
declare function init(core: Core): RouteCoverage;
|
package/lib/index.js
CHANGED
|
@@ -26,22 +26,27 @@ const { routeIdentifier } = require('./utils/route-info');
|
|
|
26
26
|
module.exports = function init(core) {
|
|
27
27
|
const { logger, messages, scopes } = core;
|
|
28
28
|
|
|
29
|
-
/** @type {Map<string, import('@contrast/common').RouteInfo} */
|
|
29
|
+
/** @type {Map<string, import('@contrast/common').RouteInfo>} */
|
|
30
30
|
const routeInfo = new Map();
|
|
31
31
|
|
|
32
32
|
core.routeCoverage = {
|
|
33
|
+
|
|
33
34
|
discover(info) {
|
|
34
35
|
routeInfo.set(routeIdentifier(info), info);
|
|
35
36
|
messages.emit(Event.ROUTE_COVERAGE_DISCOVERY, info);
|
|
36
37
|
},
|
|
37
38
|
|
|
39
|
+
delete(info) {
|
|
40
|
+
routeInfo.delete(routeIdentifier(info));
|
|
41
|
+
},
|
|
42
|
+
|
|
38
43
|
discoveryFinished() {
|
|
39
44
|
const routes = Array.from(routeInfo.values());
|
|
40
45
|
messages.emit(Event.ROUTE_COVERAGE_DISCOVERY_FINISHED, routes);
|
|
41
46
|
},
|
|
42
47
|
|
|
43
48
|
observe(info) {
|
|
44
|
-
const route = routeInfo.get(routeIdentifier(info));
|
|
49
|
+
const route = info.signature ? info : routeInfo.get(routeIdentifier(info));
|
|
45
50
|
|
|
46
51
|
if (!route) {
|
|
47
52
|
logger.debug(info, 'unable to observe undiscovered route');
|
|
@@ -65,6 +70,7 @@ module.exports = function init(core) {
|
|
|
65
70
|
},
|
|
66
71
|
};
|
|
67
72
|
|
|
73
|
+
require('./install/express')(core);
|
|
68
74
|
require('./install/fastify')(core);
|
|
69
75
|
require('./install/koa')(core);
|
|
70
76
|
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2022 Contrast Security, Inc
|
|
3
|
+
* Contact: support@contrastsecurity.com
|
|
4
|
+
* License: Commercial
|
|
5
|
+
|
|
6
|
+
* NOTICE: This Software and the patented inventions embodied within may only be
|
|
7
|
+
* used as part of Contrast Security’s commercial offerings. Even though it is
|
|
8
|
+
* made available through public repositories, use of this Software is subject to
|
|
9
|
+
* the applicable End User Licensing Agreement found at
|
|
10
|
+
* https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
|
|
11
|
+
* between Contrast Security and the End User. The Software may not be reverse
|
|
12
|
+
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
|
+
* way not consistent with the End User License Agreement.
|
|
14
|
+
*/
|
|
15
|
+
// @ts-check
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
const METHODS = [
|
|
19
|
+
'all',
|
|
20
|
+
'get',
|
|
21
|
+
'post',
|
|
22
|
+
'put',
|
|
23
|
+
'delete',
|
|
24
|
+
'patch',
|
|
25
|
+
'options',
|
|
26
|
+
'head',
|
|
27
|
+
];
|
|
28
|
+
// eslint-disable-next-line node/no-extraneous-require
|
|
29
|
+
const fnInspect = require('@contrast/fn-inspect');
|
|
30
|
+
const { createSignature, patchType } = require('../utils/route-info');
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {import('..').Core & {
|
|
34
|
+
* routeCoverage: import('..').RouteCoverage & {
|
|
35
|
+
* express?: import('@contrast/common').Installable
|
|
36
|
+
* }
|
|
37
|
+
* }} core
|
|
38
|
+
*/
|
|
39
|
+
module.exports = function init(core) {
|
|
40
|
+
const routers = new Map();
|
|
41
|
+
const signatureMap = new Map();
|
|
42
|
+
const { patcher, depHooks, routeCoverage } = core;
|
|
43
|
+
|
|
44
|
+
function formatUrl(url) {
|
|
45
|
+
if (Array.isArray(url)) {
|
|
46
|
+
return `/[${url.join(', ')}]`;
|
|
47
|
+
} else if (url instanceof RegExp) {
|
|
48
|
+
return `/{${url.toString().replace(/(^\/?)|(\/?$)/g, '')}}`;
|
|
49
|
+
} else {
|
|
50
|
+
return url;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getLastLayer(router) {
|
|
55
|
+
if (router.stack) {
|
|
56
|
+
const len = router.stack.length;
|
|
57
|
+
return router.stack[len - 1];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getLayerHandleMethod(layer) {
|
|
62
|
+
let methodName = 'handle';
|
|
63
|
+
const __handleData = fnInspect.funcInfo(layer.__handle);
|
|
64
|
+
if (__handleData && __handleData.file.includes('express-async-errors')) {
|
|
65
|
+
methodName = '__handle';
|
|
66
|
+
}
|
|
67
|
+
return methodName;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function patchHandler(layer, route) {
|
|
71
|
+
if (!layer) return;
|
|
72
|
+
const handle = getLayerHandleMethod(layer);
|
|
73
|
+
patcher.patch(layer, handle, {
|
|
74
|
+
name: 'express.Router.handle',
|
|
75
|
+
patchType,
|
|
76
|
+
post(data) {
|
|
77
|
+
const [req] = data.args;
|
|
78
|
+
const method = req?.method?.toLowerCase();
|
|
79
|
+
const url = `${req.baseUrl}${req._parsedUrl.pathname}`;
|
|
80
|
+
const { signature } = signatureMap.get(route.signature);
|
|
81
|
+
if (method) routeCoverage.observe({ signature, url, method });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function createRoute(url, method, id) {
|
|
87
|
+
const signature = createSignature(url, method);
|
|
88
|
+
const route = { signature, url, method, id: id || signature };
|
|
89
|
+
signatureMap.set(signature, route);
|
|
90
|
+
return route;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function discoverRoute({ signature, url, method }) {
|
|
94
|
+
routeCoverage.discover({ signature, url, method });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function instrumentRoute(router, route) {
|
|
98
|
+
if (!router) return;
|
|
99
|
+
const layer = getLastLayer(router);
|
|
100
|
+
patchHandler(layer, route);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function updateRoutes(prefix, router, updatedRouter) {
|
|
104
|
+
const routes = routers.get(router);
|
|
105
|
+
const updatedLayers = router === updatedRouter
|
|
106
|
+
? [] : (routers.get(updatedRouter) || []);
|
|
107
|
+
if (routes) {
|
|
108
|
+
routes.forEach((route) => {
|
|
109
|
+
const { url, method, id } = route;
|
|
110
|
+
const newRoute = createRoute(`${prefix}${url}`, method, id);
|
|
111
|
+
updatedLayers.push(newRoute);
|
|
112
|
+
signatureMap.set(id, newRoute);
|
|
113
|
+
});
|
|
114
|
+
routers.set(router, updatedLayers);
|
|
115
|
+
routers.set(updatedRouter, updatedLayers);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
core.routeCoverage.express = {
|
|
120
|
+
install() {
|
|
121
|
+
depHooks.resolve(
|
|
122
|
+
{ name: 'express' },
|
|
123
|
+
(express) => {
|
|
124
|
+
|
|
125
|
+
patcher.patch(express.Router, 'use', {
|
|
126
|
+
name: 'express.Router.use',
|
|
127
|
+
patchType,
|
|
128
|
+
post({ args, result }) {
|
|
129
|
+
const [prefix, router] = args;
|
|
130
|
+
if (typeof prefix === 'string' && prefix !== '/') {
|
|
131
|
+
updateRoutes(prefix, router, result);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
patcher.patch(express.application, 'use', {
|
|
137
|
+
name: 'express.application.use',
|
|
138
|
+
patchType,
|
|
139
|
+
post({ args }) {
|
|
140
|
+
const idx = args.length;
|
|
141
|
+
const router = args[idx - 1];
|
|
142
|
+
const routes = routers.get(router);
|
|
143
|
+
if (routes) {
|
|
144
|
+
routes.forEach((route) => {
|
|
145
|
+
discoverRoute(route);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
METHODS.forEach((method) => {
|
|
152
|
+
patcher.patch(express.application, method, {
|
|
153
|
+
name: `express.application.${method}`,
|
|
154
|
+
patchType,
|
|
155
|
+
post({ args, result }) {
|
|
156
|
+
const [url, fn] = args;
|
|
157
|
+
if (!url || !fn) return;
|
|
158
|
+
const route = createRoute(formatUrl(url), method);
|
|
159
|
+
instrumentRoute(result?._router, route);
|
|
160
|
+
discoverRoute(route);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
patcher.patch(express.Router, method, {
|
|
165
|
+
name: `express.Router.${method}`,
|
|
166
|
+
patchType,
|
|
167
|
+
post({ args, obj: router }) {
|
|
168
|
+
const [url, fn] = args;
|
|
169
|
+
if (!url || !fn) return;
|
|
170
|
+
const route = createRoute(formatUrl(url), method);
|
|
171
|
+
const routes = routers.get(router) || [];
|
|
172
|
+
|
|
173
|
+
instrumentRoute(router, route);
|
|
174
|
+
routes.push(route);
|
|
175
|
+
routers.set(router, routes);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
['http', 'https', 'spdy'].forEach((name) => {
|
|
183
|
+
depHooks.resolve({ name }, (module) => {
|
|
184
|
+
patcher.patch(module.Server.prototype, 'listen', {
|
|
185
|
+
name: `${name}.Server.prototype.listen`,
|
|
186
|
+
patchType,
|
|
187
|
+
post() {
|
|
188
|
+
routeCoverage.discoveryFinished();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
return core.routeCoverage.express;
|
|
197
|
+
};
|
package/lib/utils/route-info.js
CHANGED
|
@@ -22,7 +22,7 @@ const patchType = 'route-coverage';
|
|
|
22
22
|
* @param {string} method
|
|
23
23
|
* @return {string} formatted signature
|
|
24
24
|
*/
|
|
25
|
-
function createSignature(path, method) {
|
|
25
|
+
function createSignature(path, method = '') {
|
|
26
26
|
return `Router.${method}('${path}', [Function])`;
|
|
27
27
|
}
|
|
28
28
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/route-coverage",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
|
|
@@ -14,6 +14,6 @@
|
|
|
14
14
|
"test": "../scripts/test.sh"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@contrast/common": "1.
|
|
17
|
+
"@contrast/common": "1.11.0"
|
|
18
18
|
}
|
|
19
19
|
}
|