@contrast/route-coverage 1.12.0 → 1.14.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/LICENSE +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +3 -1
- package/lib/install/express.js +4 -3
- package/lib/install/fastify.js +7 -5
- package/lib/install/hapi.js +70 -0
- package/lib/install/http.js +1 -1
- package/lib/install/koa.js +5 -4
- package/lib/utils/route-info.js +2 -2
- package/package.json +3 -2
package/LICENSE
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -57,6 +57,7 @@ module.exports = function init(core) {
|
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
route.url = info.url;
|
|
60
61
|
const store = scopes.sources.getStore();
|
|
61
62
|
if (store && !store.route) {
|
|
62
63
|
store.route = route;
|
|
@@ -82,6 +83,7 @@ module.exports = function init(core) {
|
|
|
82
83
|
require('./install/http')(core);
|
|
83
84
|
require('./install/express')(core);
|
|
84
85
|
require('./install/fastify')(core);
|
|
86
|
+
require('./install/hapi')(core);
|
|
85
87
|
require('./install/koa')(core);
|
|
86
88
|
|
|
87
89
|
return routeCoverage;
|
package/lib/install/express.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -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 = {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2024 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
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
const { toLowerCase } = require('@contrast/common');
|
|
18
|
+
const { createSignature, patchType } = require('./../utils/route-info');
|
|
19
|
+
|
|
20
|
+
module.exports = function init(core) {
|
|
21
|
+
const { patcher, depHooks, routeCoverage } = core;
|
|
22
|
+
|
|
23
|
+
function emitRouteCoverage(url, method) {
|
|
24
|
+
method = toLowerCase(method);
|
|
25
|
+
const event = { signature: createSignature(url, method), url, method, normalizedUrl: url };
|
|
26
|
+
routeCoverage.discover(event);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return core.routeCoverage.hapi = {
|
|
30
|
+
install() {
|
|
31
|
+
return depHooks.resolve({ name: '@hapi/hapi', version: '>=18 <22' }, (hapi) => {
|
|
32
|
+
['server', 'Server'].forEach((server) => {
|
|
33
|
+
patcher.patch(hapi, server, {
|
|
34
|
+
name: `hapi.${server}`,
|
|
35
|
+
patchType,
|
|
36
|
+
post(data) {
|
|
37
|
+
patcher.patch(data.result._core.router, 'add', {
|
|
38
|
+
name: '_core.router.add',
|
|
39
|
+
patchType,
|
|
40
|
+
post(data) {
|
|
41
|
+
if (!data.args[0] || !data.result) return;
|
|
42
|
+
|
|
43
|
+
const [{ method, path }] = data.args;
|
|
44
|
+
if (!method || !path) return;
|
|
45
|
+
|
|
46
|
+
if (Array.isArray(method)) {
|
|
47
|
+
method.forEach((verb) => {
|
|
48
|
+
emitRouteCoverage(path, verb);
|
|
49
|
+
});
|
|
50
|
+
} else {
|
|
51
|
+
emitRouteCoverage(path, method);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
patcher.patch(data.result.route.settings, 'handler', {
|
|
55
|
+
name: 'route.settings.handler',
|
|
56
|
+
patchType,
|
|
57
|
+
post({ args }) {
|
|
58
|
+
const [{ method, path, route }] = args;
|
|
59
|
+
routeCoverage.observe({ url: path, method: toLowerCase(method), normalizedUrl: route.path });
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
};
|
package/lib/install/http.js
CHANGED
package/lib/install/koa.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -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.14.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,7 @@
|
|
|
17
17
|
"test": "../scripts/test.sh"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@contrast/common": "1.
|
|
20
|
+
"@contrast/common": "1.16.0",
|
|
21
|
+
"@contrast/fn-inspect": "^4.0.0"
|
|
21
22
|
}
|
|
22
23
|
}
|