@contrast/route-coverage 1.2.0 → 1.4.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 +19 -4
- package/lib/index.js +41 -23
- package/lib/install/fastify.js +51 -20
- package/lib/install/koa.js +90 -0
- package/lib/utils/route-info.js +5 -3
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -13,11 +13,26 @@
|
|
|
13
13
|
* way not consistent with the End User License Agreement.
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
import { Installable, Messages, RouteInfo } from '@contrast/common';
|
|
17
|
+
import { Logger } from '@contrast/logger';
|
|
18
|
+
import { Patcher } from '@contrast/patcher';
|
|
19
|
+
import RequireHook from '@contrast/require-hook';
|
|
20
|
+
|
|
21
|
+
export { RouteInfo };
|
|
22
|
+
|
|
23
|
+
export interface RouteCoverage extends Installable {
|
|
24
|
+
discover(info: RouteInfo): void;
|
|
25
|
+
discoveryFinished(): void;
|
|
26
|
+
observe(info: Pick<RouteInfo, 'method' | 'url'>): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Core {
|
|
30
|
+
readonly depHooks: RequireHook;
|
|
31
|
+
readonly logger: Logger;
|
|
32
|
+
readonly messages: Messages;
|
|
33
|
+
readonly patcher: Patcher;
|
|
19
34
|
}
|
|
20
35
|
|
|
21
|
-
declare function init(core:
|
|
36
|
+
declare function init(core: Core): RouteCoverage;
|
|
22
37
|
|
|
23
38
|
export = init;
|
package/lib/index.js
CHANGED
|
@@ -12,42 +12,60 @@
|
|
|
12
12
|
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
13
|
* way not consistent with the End User License Agreement.
|
|
14
14
|
*/
|
|
15
|
+
// @ts-check
|
|
15
16
|
'use strict';
|
|
16
17
|
|
|
17
18
|
const { callChildComponentMethodsSync, Event } = require('@contrast/common');
|
|
18
19
|
const { routeIdentifier } = require('./utils/route-info');
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* @param {import('.').Core & {
|
|
23
|
+
* routeCoverage: import('.').RouteCoverage
|
|
24
|
+
* }} core
|
|
25
|
+
*/
|
|
26
|
+
module.exports = function init(core) {
|
|
27
|
+
const { logger, messages, scopes } = core;
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
/** @type {Map<string, import('@contrast/common').RouteInfo} */
|
|
24
30
|
const routeInfo = new Map();
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
core.routeCoverage = {
|
|
33
|
+
discover(info) {
|
|
34
|
+
routeInfo.set(routeIdentifier(info), info);
|
|
35
|
+
messages.emit(Event.ROUTE_COVERAGE_DISCOVERY, info);
|
|
36
|
+
},
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
discoveryFinished() {
|
|
39
|
+
const routes = Array.from(routeInfo.values());
|
|
40
|
+
messages.emit(Event.ROUTE_COVERAGE_DISCOVERY_FINISHED, routes);
|
|
41
|
+
},
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
logger.debug('Unable to observe routes not discovered %j', info);
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
43
|
+
observe(info) {
|
|
44
|
+
const route = routeInfo.get(routeIdentifier(info));
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
46
|
+
if (!route) {
|
|
47
|
+
logger.debug(info, 'unable to observe undiscovered route');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
42
50
|
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
const store = scopes.sources.getStore();
|
|
52
|
+
if (store && !store.route) {
|
|
53
|
+
store.route = route;
|
|
54
|
+
}
|
|
55
|
+
// these events need source correlation
|
|
56
|
+
messages.emit(Event.ROUTE_COVERAGE_OBSERVATION, {
|
|
57
|
+
...route,
|
|
58
|
+
sourceInfo: store?.sourceInfo,
|
|
59
|
+
});
|
|
60
|
+
},
|
|
45
61
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
callChildComponentMethodsSync(routeCoverage, 'install');
|
|
62
|
+
install() {
|
|
63
|
+
callChildComponentMethodsSync(this, 'install');
|
|
64
|
+
},
|
|
50
65
|
};
|
|
51
66
|
|
|
52
|
-
|
|
67
|
+
require('./install/fastify')(core);
|
|
68
|
+
require('./install/koa')(core);
|
|
69
|
+
|
|
70
|
+
return core.routeCoverage;
|
|
53
71
|
};
|
package/lib/install/fastify.js
CHANGED
|
@@ -12,15 +12,24 @@
|
|
|
12
12
|
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
13
|
* way not consistent with the End User License Agreement.
|
|
14
14
|
*/
|
|
15
|
+
// @ts-check
|
|
15
16
|
'use strict';
|
|
16
17
|
|
|
17
18
|
const { createSignature } = require('./../utils/route-info');
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
const { patcher, depHooks, routeCoverage } = core;
|
|
20
|
+
/** @typedef {Parameters<import('fastify3.0.0').onRouteHookHandler>[0]} RouteOptions */
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* @param {import('..').Core & {
|
|
24
|
+
* routeCoverage: import('..').RouteCoverage & {
|
|
25
|
+
* fastify?: import('@contrast/common').Installable
|
|
26
|
+
* }
|
|
27
|
+
* }} core
|
|
28
|
+
*/
|
|
29
|
+
module.exports = function init(core) {
|
|
30
|
+
const { patcher, depHooks, routeCoverage } = core;
|
|
23
31
|
|
|
32
|
+
/** @param {RouteOptions} routeOptions */
|
|
24
33
|
function registerRouteHandler(routeOptions) {
|
|
25
34
|
if (!routeOptions || !routeOptions.method || !routeOptions.url) return;
|
|
26
35
|
|
|
@@ -37,6 +46,10 @@ module.exports = function (core) {
|
|
|
37
46
|
observationListener(routeOptions, url);
|
|
38
47
|
}
|
|
39
48
|
|
|
49
|
+
/**
|
|
50
|
+
* @param {RouteOptions} routeOptions
|
|
51
|
+
* @param {string} url
|
|
52
|
+
*/
|
|
40
53
|
function observationListener(routeOptions, url) {
|
|
41
54
|
patcher.patch(routeOptions, 'handler', {
|
|
42
55
|
name: 'fastify.routeOptions.handler',
|
|
@@ -44,35 +57,53 @@ module.exports = function (core) {
|
|
|
44
57
|
pre(data) {
|
|
45
58
|
const [request] = data.args;
|
|
46
59
|
const { method } = request.raw;
|
|
47
|
-
emitObservation(
|
|
60
|
+
emitObservation(url, method);
|
|
48
61
|
},
|
|
49
62
|
});
|
|
50
63
|
}
|
|
51
64
|
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} url
|
|
67
|
+
* @param {string} method
|
|
68
|
+
*/
|
|
52
69
|
function emitRouteCoverage(url, method) {
|
|
53
70
|
method = method.toLowerCase();
|
|
54
71
|
const event = { signature: createSignature(url, method), url, method };
|
|
55
72
|
routeCoverage.discover(event);
|
|
56
73
|
}
|
|
57
74
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
75
|
+
/**
|
|
76
|
+
* @param {string} url
|
|
77
|
+
* @param {string=} method
|
|
78
|
+
*/
|
|
79
|
+
function emitObservation(url, method) {
|
|
80
|
+
method = method?.toLowerCase();
|
|
81
|
+
routeCoverage.observe({ method, url });
|
|
61
82
|
}
|
|
62
83
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
name: 'fastify.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
84
|
+
core.routeCoverage.fastify = {
|
|
85
|
+
install() {
|
|
86
|
+
depHooks.resolve(
|
|
87
|
+
{ name: 'fastify', version: '>=3.0.0' },
|
|
88
|
+
/** @param {import("fastify3.0.0").fastify} fastify */
|
|
89
|
+
(fastify) =>
|
|
90
|
+
patcher.patch(fastify, {
|
|
91
|
+
name: 'fastify.build',
|
|
92
|
+
patchType: 'route-coverage',
|
|
93
|
+
post({ result: server }) {
|
|
94
|
+
server.addHook('onRoute', (routeOptions) => {
|
|
95
|
+
registerRouteHandler(routeOptions);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
server.addHook('onReady', (done) => {
|
|
99
|
+
routeCoverage.discoveryFinished();
|
|
100
|
+
return done();
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
}),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
75
106
|
};
|
|
76
107
|
|
|
77
|
-
return
|
|
108
|
+
return core.routeCoverage.fastify;
|
|
78
109
|
};
|
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
const { createSignature, patchType } = require('./../utils/route-info');
|
|
18
|
+
|
|
19
|
+
module.exports = function init(core) {
|
|
20
|
+
const { patcher, depHooks, routeCoverage, logger } = core;
|
|
21
|
+
|
|
22
|
+
function emitRouteCoverage(url, method) {
|
|
23
|
+
const event = { signature: createSignature(url, method), url, method };
|
|
24
|
+
routeCoverage.discover(event);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function emitObservation(url, method) {
|
|
28
|
+
routeCoverage.observe({ method, url });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function routeObservationMiddleware(ctx, next) {
|
|
32
|
+
const req = ctx.request;
|
|
33
|
+
|
|
34
|
+
if (req) {
|
|
35
|
+
const { url, method } = req;
|
|
36
|
+
try {
|
|
37
|
+
// Arbitrary basename needed to parse out path
|
|
38
|
+
const parsedUrl = new URL(url, 'file://').pathname;
|
|
39
|
+
emitObservation(parsedUrl, method.toLowerCase());
|
|
40
|
+
} catch (err) {
|
|
41
|
+
logger.error({ err, url }, 'failed to parse route');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
await next();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function registerRouteHandler(Router) {
|
|
49
|
+
patcher.patch(Router.prototype, 'register', {
|
|
50
|
+
name: 'koaRouter.prototype',
|
|
51
|
+
patchType,
|
|
52
|
+
post: ({ args, result: layer }) => {
|
|
53
|
+
const [path, methods] = args;
|
|
54
|
+
if (!path || !methods || Array.isArray(path)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (methods.length === 0) {
|
|
59
|
+
emitRouteCoverage(path, 'use');
|
|
60
|
+
} else {
|
|
61
|
+
methods.forEach((method) => {
|
|
62
|
+
emitRouteCoverage(path, method.toLowerCase());
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
layer.stack.unshift(routeObservationMiddleware.bind(this));
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
patcher.patch(Router.prototype, 'routes', {
|
|
71
|
+
name: 'koaRouter.prototype',
|
|
72
|
+
patchType,
|
|
73
|
+
post: () => {
|
|
74
|
+
routeCoverage.discoveryFinished();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
core.routeCoverage.koa = {
|
|
80
|
+
install() {
|
|
81
|
+
['@koa/router', 'koa-router'].forEach((name) => {
|
|
82
|
+
depHooks.resolve(
|
|
83
|
+
{ name }, registerRouteHandler.bind(this)
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return core.routeCoverage.koa;
|
|
90
|
+
};
|
package/lib/utils/route-info.js
CHANGED
|
@@ -14,10 +14,12 @@
|
|
|
14
14
|
*/
|
|
15
15
|
'use strict';
|
|
16
16
|
|
|
17
|
+
const patchType = 'route-coverage';
|
|
18
|
+
|
|
17
19
|
/**
|
|
18
20
|
* Creates a formatted "signature" for a route
|
|
19
21
|
* @param {string} path
|
|
20
|
-
* @param {
|
|
22
|
+
* @param {string} method
|
|
21
23
|
* @return {string} formatted signature
|
|
22
24
|
*/
|
|
23
25
|
function createSignature(path, method) {
|
|
@@ -26,9 +28,9 @@ function createSignature(path, method) {
|
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
30
|
* Creates a route identifier based on the method name and the url
|
|
29
|
-
* @param {
|
|
31
|
+
* @param {Pick<import('../index').RouteInfo, 'method' | 'url'>} info
|
|
30
32
|
* @return {string}
|
|
31
33
|
*/
|
|
32
34
|
const routeIdentifier = (info) => `${info.method}.${info.url}`;
|
|
33
35
|
|
|
34
|
-
module.exports = { createSignature, routeIdentifier };
|
|
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.4.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.8.0"
|
|
18
18
|
}
|
|
19
19
|
}
|