@contrast/route-coverage 1.5.0 → 1.6.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 +3 -0
- package/lib/index.js +7 -1
- package/lib/install/express.js +138 -0
- package/lib/utils/route-info.js +1 -1
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -17,11 +17,13 @@ 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
28
|
observe(info: Pick<RouteInfo, 'method' | 'url'>): void;
|
|
27
29
|
}
|
|
@@ -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,15 +26,20 @@ 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);
|
|
@@ -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,138 @@
|
|
|
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
|
+
|
|
29
|
+
const { createSignature, patchType } = require('./../utils/route-info');
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {import('..').Core & {
|
|
33
|
+
* routeCoverage: import('..').RouteCoverage & {
|
|
34
|
+
* express?: import('@contrast/common').Installable
|
|
35
|
+
* }
|
|
36
|
+
* }} core
|
|
37
|
+
*/
|
|
38
|
+
module.exports = function init(core) {
|
|
39
|
+
const routers = new Map();
|
|
40
|
+
const { patcher, depHooks, routeCoverage } = core;
|
|
41
|
+
|
|
42
|
+
function emitRouteCoverage(router, url, method) {
|
|
43
|
+
const event = { signature: createSignature(url, method), url, method };
|
|
44
|
+
const layers = routers.get(router) || [];
|
|
45
|
+
layers.push(event);
|
|
46
|
+
routers.set(router, layers);
|
|
47
|
+
routeCoverage.discover(event);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function handleRouteDiscovery(data, method) {
|
|
51
|
+
const [url, fn] = data.args;
|
|
52
|
+
if (!url || !fn) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(url)) {
|
|
56
|
+
url.forEach((path) => {
|
|
57
|
+
emitRouteCoverage(data.result, path, method);
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
emitRouteCoverage(data.result, url, method);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
core.routeCoverage.express = {
|
|
65
|
+
install() {
|
|
66
|
+
depHooks.resolve(
|
|
67
|
+
{ name: 'express' },
|
|
68
|
+
(express) => {
|
|
69
|
+
patcher.patch(express.Router, 'handle', {
|
|
70
|
+
name: 'express.Router.handle',
|
|
71
|
+
patchType,
|
|
72
|
+
post(data) {
|
|
73
|
+
// TODO: Can this handle all route observation?
|
|
74
|
+
const [req] = data.args;
|
|
75
|
+
const method = req?.method?.toLowerCase();
|
|
76
|
+
const url = `${req.baseUrl}${req._parsedUrl.pathname}`;
|
|
77
|
+
if (method) routeCoverage.observe({ url, method });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
patcher.patch(express.Router, 'use', {
|
|
82
|
+
name: 'express.Router.use',
|
|
83
|
+
patchType,
|
|
84
|
+
post({ args, result }) {
|
|
85
|
+
const [prefix, router] = args;
|
|
86
|
+
const layers = routers.get(router);
|
|
87
|
+
const updatedLayers = [];
|
|
88
|
+
if (layers) {
|
|
89
|
+
layers.forEach((layer) => {
|
|
90
|
+
const { url, method } = layer;
|
|
91
|
+
const updatedUrl = `${prefix}${url}`;
|
|
92
|
+
const event = { signature: createSignature(updatedUrl, method), url: updatedUrl, method };
|
|
93
|
+
updatedLayers.push(event);
|
|
94
|
+
routeCoverage.delete(layer);
|
|
95
|
+
routeCoverage.discover(event);
|
|
96
|
+
});
|
|
97
|
+
routers.delete(router);
|
|
98
|
+
routers.set(result, updatedLayers);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
METHODS.forEach((method) => {
|
|
104
|
+
patcher.patch(express.application, method, {
|
|
105
|
+
name: `express.application.${method}`,
|
|
106
|
+
patchType,
|
|
107
|
+
post(data) {
|
|
108
|
+
handleRouteDiscovery(data, method);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
patcher.patch(express.Router, method, {
|
|
113
|
+
name: `express.Router.${method}`,
|
|
114
|
+
patchType,
|
|
115
|
+
post(data) {
|
|
116
|
+
handleRouteDiscovery(data, method);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
['http', 'https', 'spdy'].forEach((name) => {
|
|
124
|
+
depHooks.resolve({ name }, (module) => {
|
|
125
|
+
patcher.patch(module.Server.prototype, 'listen', {
|
|
126
|
+
name: `${name}.Server.prototype.listen`,
|
|
127
|
+
patchType,
|
|
128
|
+
post() {
|
|
129
|
+
routeCoverage.discoveryFinished();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return core.routeCoverage.express;
|
|
138
|
+
};
|
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.6.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.10.0"
|
|
18
18
|
}
|
|
19
19
|
}
|