@contrast/route-coverage 1.11.2 → 1.13.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.js +9 -2
- package/lib/install/express.js +3 -2
- package/lib/install/fastify.js +6 -4
- package/lib/install/koa.js +4 -3
- package/lib/utils/route-info.js +1 -1
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -32,8 +32,9 @@ module.exports = function init(core) {
|
|
|
32
32
|
|
|
33
33
|
/** @type {Map<string, import('@contrast/common').RouteInfo>} */
|
|
34
34
|
const routeInfo = new Map();
|
|
35
|
+
const recentlyObserved = new Set();
|
|
35
36
|
|
|
36
|
-
core.routeCoverage = {
|
|
37
|
+
const routeCoverage = core.routeCoverage = {
|
|
37
38
|
discover(info) {
|
|
38
39
|
routeInfo.set(routeIdentifier(info), info);
|
|
39
40
|
messages.emit(Event.ROUTE_COVERAGE_DISCOVERY, info);
|
|
@@ -56,11 +57,16 @@ module.exports = function init(core) {
|
|
|
56
57
|
return;
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
route.url = info.url;
|
|
59
61
|
const store = scopes.sources.getStore();
|
|
60
62
|
if (store && !store.route) {
|
|
61
63
|
store.route = route;
|
|
62
64
|
}
|
|
63
65
|
|
|
66
|
+
if (recentlyObserved.has(route.signature)) return;
|
|
67
|
+
|
|
68
|
+
recentlyObserved.add(route.signature);
|
|
69
|
+
|
|
64
70
|
// these events need source correlation
|
|
65
71
|
messages.emit(Event.ROUTE_COVERAGE_OBSERVATION, {
|
|
66
72
|
...route,
|
|
@@ -70,6 +76,7 @@ module.exports = function init(core) {
|
|
|
70
76
|
|
|
71
77
|
install() {
|
|
72
78
|
callChildComponentMethodsSync(this, 'install');
|
|
79
|
+
setInterval(() => recentlyObserved.clear(), 10000).unref();
|
|
73
80
|
},
|
|
74
81
|
};
|
|
75
82
|
|
|
@@ -78,5 +85,5 @@ module.exports = function init(core) {
|
|
|
78
85
|
require('./install/fastify')(core);
|
|
79
86
|
require('./install/koa')(core);
|
|
80
87
|
|
|
81
|
-
return
|
|
88
|
+
return routeCoverage;
|
|
82
89
|
};
|
package/lib/install/express.js
CHANGED
|
@@ -78,8 +78,9 @@ module.exports = function init(core) {
|
|
|
78
78
|
const [req] = data.args;
|
|
79
79
|
const method = req?.method && toLowerCase(req.method);
|
|
80
80
|
const url = `${req.baseUrl}${req._parsedUrl.pathname}`;
|
|
81
|
+
const normalizedUrl = req.route.path;
|
|
81
82
|
const { signature } = signatureMap.get(route.signature);
|
|
82
|
-
if (method) routeCoverage.observe({ signature, url, method });
|
|
83
|
+
if (method) routeCoverage.observe({ signature, url, method, normalizedUrl });
|
|
83
84
|
}
|
|
84
85
|
});
|
|
85
86
|
}
|
|
@@ -92,7 +93,7 @@ module.exports = function init(core) {
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
function discoverRoute({ signature, url, method }) {
|
|
95
|
-
routeCoverage.discover({ signature, url, method });
|
|
96
|
+
routeCoverage.discover({ signature, url, method, normalizedUrl: url });
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
function instrumentRoute(router, route) {
|
package/lib/install/fastify.js
CHANGED
|
@@ -58,7 +58,8 @@ module.exports = function init(core) {
|
|
|
58
58
|
pre(data) {
|
|
59
59
|
const [request] = data.args;
|
|
60
60
|
const { method } = request.raw;
|
|
61
|
-
|
|
61
|
+
const [parsedUrl] = request.url.split(/\?/);
|
|
62
|
+
emitObservation(parsedUrl, url, method);
|
|
62
63
|
},
|
|
63
64
|
});
|
|
64
65
|
}
|
|
@@ -69,17 +70,18 @@ module.exports = function init(core) {
|
|
|
69
70
|
*/
|
|
70
71
|
function emitRouteCoverage(url, method) {
|
|
71
72
|
method = toLowerCase(method);
|
|
72
|
-
const event = { signature: createSignature(url, method), url, method };
|
|
73
|
+
const event = { signature: createSignature(url, method), url, method, normalizedUrl: url };
|
|
73
74
|
routeCoverage.discover(event);
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
/**
|
|
77
78
|
* @param {string} url
|
|
79
|
+
* @param {string} normalizedUrl
|
|
78
80
|
* @param {string=} method
|
|
79
81
|
*/
|
|
80
|
-
function emitObservation(url, method) {
|
|
82
|
+
function emitObservation(url, normalizedUrl, method) {
|
|
81
83
|
method = method && toLowerCase(method);
|
|
82
|
-
routeCoverage.observe({ method, url });
|
|
84
|
+
routeCoverage.observe({ method, url, normalizedUrl });
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
return core.routeCoverage.fastify = {
|
package/lib/install/koa.js
CHANGED
|
@@ -21,7 +21,7 @@ module.exports = function init(core) {
|
|
|
21
21
|
const { patcher, depHooks, routeCoverage } = core;
|
|
22
22
|
|
|
23
23
|
function emitRouteCoverage(url, method) {
|
|
24
|
-
const event = { signature: createSignature(url, method), url, method };
|
|
24
|
+
const event = { signature: createSignature(url, method), url, method, normalizedUrl: url };
|
|
25
25
|
routeCoverage.discover(event);
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -30,8 +30,9 @@ module.exports = function init(core) {
|
|
|
30
30
|
const req = ctx.request;
|
|
31
31
|
|
|
32
32
|
if (req) {
|
|
33
|
-
const { method } = req;
|
|
34
|
-
|
|
33
|
+
const { url: reqUrl, method } = req;
|
|
34
|
+
const [url] = reqUrl.split(/\?/);
|
|
35
|
+
routeCoverage.observe({ url, method: toLowerCase(method || ''), normalizedUrl: path });
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
await next();
|
package/lib/utils/route-info.js
CHANGED
|
@@ -31,6 +31,6 @@ function createSignature(path, method = '') {
|
|
|
31
31
|
* @param {Pick<import('../index').RouteInfo, 'method' | 'url'>} info
|
|
32
32
|
* @return {string}
|
|
33
33
|
*/
|
|
34
|
-
const routeIdentifier = (info) => `${info.method}.${info.
|
|
34
|
+
const routeIdentifier = (info) => `${info.method}.${info.normalizedUrl}`;
|
|
35
35
|
|
|
36
36
|
module.exports = { createSignature, routeIdentifier, patchType };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/route-coverage",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Handles route discovery and observation",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
|
|
@@ -17,6 +17,6 @@
|
|
|
17
17
|
"test": "../scripts/test.sh"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@contrast/common": "1.
|
|
20
|
+
"@contrast/common": "1.16.0"
|
|
21
21
|
}
|
|
22
22
|
}
|