@contrast/assess 1.46.1 → 1.46.2

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.
Files changed (157) hide show
  1. package/package.json +14 -11
  2. package/lib/crypto-analysis/install/crypto.test.js +0 -146
  3. package/lib/crypto-analysis/install/math.test.js +0 -65
  4. package/lib/dataflow/index.test.js +0 -36
  5. package/lib/dataflow/propagation/index.test.js +0 -103
  6. package/lib/dataflow/propagation/install/JSON/index.test.js +0 -50
  7. package/lib/dataflow/propagation/install/JSON/parse-fn.test.js +0 -232
  8. package/lib/dataflow/propagation/install/JSON/parse.test.js +0 -968
  9. package/lib/dataflow/propagation/install/JSON/stringify.test.js +0 -265
  10. package/lib/dataflow/propagation/install/array-prototype-join.test.js +0 -106
  11. package/lib/dataflow/propagation/install/buffer.test.js +0 -112
  12. package/lib/dataflow/propagation/install/contrast-methods/add.test.js +0 -94
  13. package/lib/dataflow/propagation/install/contrast-methods/index.test.js +0 -49
  14. package/lib/dataflow/propagation/install/contrast-methods/number.test.js +0 -50
  15. package/lib/dataflow/propagation/install/contrast-methods/string.test.js +0 -148
  16. package/lib/dataflow/propagation/install/contrast-methods/tag.test.js +0 -145
  17. package/lib/dataflow/propagation/install/decode-uri-component.test.js +0 -78
  18. package/lib/dataflow/propagation/install/ejs/escape-xml.test.js +0 -69
  19. package/lib/dataflow/propagation/install/ejs/template.test.js +0 -62
  20. package/lib/dataflow/propagation/install/encode-uri.test.js +0 -83
  21. package/lib/dataflow/propagation/install/escape-html.test.js +0 -71
  22. package/lib/dataflow/propagation/install/escape.test.js +0 -73
  23. package/lib/dataflow/propagation/install/fastify-send.test.js +0 -42
  24. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.test.js +0 -71
  25. package/lib/dataflow/propagation/install/isnumeric-0.test.js +0 -58
  26. package/lib/dataflow/propagation/install/joi/any.test.js +0 -270
  27. package/lib/dataflow/propagation/install/joi/array.test.js +0 -912
  28. package/lib/dataflow/propagation/install/joi/boolean.test.js +0 -103
  29. package/lib/dataflow/propagation/install/joi/expression.test.js +0 -76
  30. package/lib/dataflow/propagation/install/joi/index.test.js +0 -39
  31. package/lib/dataflow/propagation/install/joi/number.test.js +0 -103
  32. package/lib/dataflow/propagation/install/joi/object.test.js +0 -119
  33. package/lib/dataflow/propagation/install/joi/ref.test.js +0 -607
  34. package/lib/dataflow/propagation/install/joi/string-schema.test.js +0 -513
  35. package/lib/dataflow/propagation/install/mongoose/index.test.js +0 -42
  36. package/lib/dataflow/propagation/install/mongoose/schema-map.test.js +0 -348
  37. package/lib/dataflow/propagation/install/mongoose/schema-mixed.test.js +0 -511
  38. package/lib/dataflow/propagation/install/mongoose/schema-string.test.js +0 -199
  39. package/lib/dataflow/propagation/install/mustache-escape.test.js +0 -62
  40. package/lib/dataflow/propagation/install/mysql-connection-escape.test.js +0 -74
  41. package/lib/dataflow/propagation/install/parse-int.test.js +0 -48
  42. package/lib/dataflow/propagation/install/path/basename.test.js +0 -143
  43. package/lib/dataflow/propagation/install/path/dirname.test.js +0 -167
  44. package/lib/dataflow/propagation/install/path/extname.test.js +0 -141
  45. package/lib/dataflow/propagation/install/path/format.test.js +0 -250
  46. package/lib/dataflow/propagation/install/path/index.test.js +0 -45
  47. package/lib/dataflow/propagation/install/path/join-and-resolve.test.js +0 -485
  48. package/lib/dataflow/propagation/install/path/normalize.test.js +0 -176
  49. package/lib/dataflow/propagation/install/path/parse.test.js +0 -238
  50. package/lib/dataflow/propagation/install/path/relative.test.js +0 -239
  51. package/lib/dataflow/propagation/install/path/toNamespacedPath.test.js +0 -158
  52. package/lib/dataflow/propagation/install/pug/index.test.js +0 -55
  53. package/lib/dataflow/propagation/install/pug-runtime-escape.test.js +0 -69
  54. package/lib/dataflow/propagation/install/querystring/escape.test.js +0 -63
  55. package/lib/dataflow/propagation/install/querystring/index.test.js +0 -40
  56. package/lib/dataflow/propagation/install/querystring/parse.test.js +0 -272
  57. package/lib/dataflow/propagation/install/querystring/stringify.test.js +0 -301
  58. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.test.js +0 -283
  59. package/lib/dataflow/propagation/install/send.test.js +0 -63
  60. package/lib/dataflow/propagation/install/sequelize/query-generator.test.js +0 -74
  61. package/lib/dataflow/propagation/install/sequelize/sql-string.test.js +0 -119
  62. package/lib/dataflow/propagation/install/sql-template-strings.test.js +0 -100
  63. package/lib/dataflow/propagation/install/string/concat.test.js +0 -145
  64. package/lib/dataflow/propagation/install/string/format-methods.test.js +0 -74
  65. package/lib/dataflow/propagation/install/string/html-methods.test.js +0 -177
  66. package/lib/dataflow/propagation/install/string/index.test.js +0 -103
  67. package/lib/dataflow/propagation/install/string/match-all.test.js +0 -412
  68. package/lib/dataflow/propagation/install/string/match.test.js +0 -374
  69. package/lib/dataflow/propagation/install/string/replace.test.js +0 -601
  70. package/lib/dataflow/propagation/install/string/slice.test.js +0 -278
  71. package/lib/dataflow/propagation/install/string/split.test.js +0 -513
  72. package/lib/dataflow/propagation/install/string/substring.test.js +0 -251
  73. package/lib/dataflow/propagation/install/string/trim.test.js +0 -135
  74. package/lib/dataflow/propagation/install/unescape.test.js +0 -78
  75. package/lib/dataflow/propagation/install/url/domain-parsers.test.js +0 -63
  76. package/lib/dataflow/propagation/install/url/parse.test.js +0 -391
  77. package/lib/dataflow/propagation/install/url/searchParams.test.js +0 -538
  78. package/lib/dataflow/propagation/install/url/url.test.js +0 -466
  79. package/lib/dataflow/propagation/install/util-format.test.js +0 -336
  80. package/lib/dataflow/propagation/install/validator/hooks.test.js +0 -211
  81. package/lib/dataflow/sinks/index.test.js +0 -78
  82. package/lib/dataflow/sinks/install/child-process.test.js +0 -338
  83. package/lib/dataflow/sinks/install/eval.test.js +0 -95
  84. package/lib/dataflow/sinks/install/express/index.test.js +0 -33
  85. package/lib/dataflow/sinks/install/express/reflected-xss.test.js +0 -109
  86. package/lib/dataflow/sinks/install/express/unvalidated-redirect.test.js +0 -144
  87. package/lib/dataflow/sinks/install/fastify/index.test.js +0 -32
  88. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.test.js +0 -130
  89. package/lib/dataflow/sinks/install/fs.test.js +0 -138
  90. package/lib/dataflow/sinks/install/function.test.js +0 -103
  91. package/lib/dataflow/sinks/install/hapi/index.test.js +0 -32
  92. package/lib/dataflow/sinks/install/hapi/unvalidated-redirect.test.js +0 -130
  93. package/lib/dataflow/sinks/install/http/index.test.js +0 -33
  94. package/lib/dataflow/sinks/install/http/request.test.js +0 -184
  95. package/lib/dataflow/sinks/install/http/server-response.test.js +0 -160
  96. package/lib/dataflow/sinks/install/koa/index.test.js +0 -32
  97. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.test.js +0 -200
  98. package/lib/dataflow/sinks/install/libxmljs.test.js +0 -158
  99. package/lib/dataflow/sinks/install/marsdb.test.js +0 -166
  100. package/lib/dataflow/sinks/install/mongodb.test.js +0 -617
  101. package/lib/dataflow/sinks/install/mssql.test.js +0 -134
  102. package/lib/dataflow/sinks/install/mysql.test.js +0 -224
  103. package/lib/dataflow/sinks/install/node-serialize.test.js +0 -83
  104. package/lib/dataflow/sinks/install/postgres.test.js +0 -152
  105. package/lib/dataflow/sinks/install/restify.test.js +0 -140
  106. package/lib/dataflow/sinks/install/sequelize.test.js +0 -100
  107. package/lib/dataflow/sinks/install/sqlite3.test.js +0 -118
  108. package/lib/dataflow/sinks/install/vm.test.js +0 -326
  109. package/lib/dataflow/sources/handler.test.js +0 -501
  110. package/lib/dataflow/sources/index.test.js +0 -60
  111. package/lib/dataflow/sources/install/body-parser1.test.js +0 -244
  112. package/lib/dataflow/sources/install/busboy.test.js +0 -152
  113. package/lib/dataflow/sources/install/cookie-parser1.test.js +0 -141
  114. package/lib/dataflow/sources/install/express/params.test.js +0 -110
  115. package/lib/dataflow/sources/install/express/parsedUrl.test.js +0 -107
  116. package/lib/dataflow/sources/install/fastify/fastify.test.js +0 -207
  117. package/lib/dataflow/sources/install/fastify/index.test.js +0 -33
  118. package/lib/dataflow/sources/install/formidable1.test.js +0 -119
  119. package/lib/dataflow/sources/install/graphql-http.test.js +0 -133
  120. package/lib/dataflow/sources/install/hapi/hapi.test.js +0 -171
  121. package/lib/dataflow/sources/install/hapi/index.test.js +0 -33
  122. package/lib/dataflow/sources/install/http.test.js +0 -149
  123. package/lib/dataflow/sources/install/koa/index.test.js +0 -40
  124. package/lib/dataflow/sources/install/koa/koa-bodyparsers.test.js +0 -160
  125. package/lib/dataflow/sources/install/koa/koa-multer.test.js +0 -197
  126. package/lib/dataflow/sources/install/koa/koa-routers.test.js +0 -146
  127. package/lib/dataflow/sources/install/koa/koa2.test.js +0 -145
  128. package/lib/dataflow/sources/install/multer1.test.js +0 -143
  129. package/lib/dataflow/sources/install/qs6.test.js +0 -131
  130. package/lib/dataflow/sources/install/querystring.test.js +0 -82
  131. package/lib/dataflow/sources/install/restify/fieldedTextBodyParser.test.js +0 -86
  132. package/lib/dataflow/sources/install/restify/index.test.js +0 -38
  133. package/lib/dataflow/sources/install/restify/jsonBodyParser.test.js +0 -140
  134. package/lib/dataflow/sources/install/restify/router.test.js +0 -81
  135. package/lib/dataflow/tag-utils-complete.test.js +0 -27
  136. package/lib/dataflow/tag-utils.test.js +0 -192
  137. package/lib/dataflow/tracker.test.js +0 -216
  138. package/lib/dataflow/utils/is-safe-content-type.test.js +0 -16
  139. package/lib/dataflow/utils/is-vulnerable.test.js +0 -115
  140. package/lib/event-factory.test.js +0 -326
  141. package/lib/get-policy.test.js +0 -194
  142. package/lib/get-source-context.test.js +0 -161
  143. package/lib/index.test.js +0 -45
  144. package/lib/make-source-context.test.js +0 -50
  145. package/lib/response-scanning/handlers/index.test.js +0 -419
  146. package/lib/response-scanning/handlers/utils.test.js +0 -380
  147. package/lib/response-scanning/index.test.js +0 -41
  148. package/lib/response-scanning/install/http.test.js +0 -175
  149. package/lib/rule-scopes.test.js +0 -27
  150. package/lib/sampler/common.test.js +0 -101
  151. package/lib/sampler/index.test.js +0 -313
  152. package/lib/session-configuration/handlers.test.js +0 -84
  153. package/lib/session-configuration/index.test.js +0 -36
  154. package/lib/session-configuration/install/express-session.test.js +0 -218
  155. package/lib/session-configuration/install/fastify-cookie.test.js +0 -63
  156. package/lib/session-configuration/install/hapi.test.js +0 -269
  157. package/lib/session-configuration/install/koa.test.js +0 -92
@@ -1,197 +0,0 @@
1
- 'use strict';
2
-
3
- const sinon = require('sinon');
4
- const { expect } = require('chai');
5
- const koaMulter = require('./koa-multer');
6
- const { InputType } = require('@contrast/common');
7
- const { initAssessFixture } = require('@contrast/test/fixtures');
8
-
9
- describe('assess dataflow sources Koa multer', function () {
10
- let core,
11
- koaMulterMock,
12
- patchedKoaMulter,
13
- simulateRequestScope;
14
-
15
- ['koa-multer', '@koa/multer'].forEach((name) => {
16
- describe(name, function() {
17
- beforeEach(function () {
18
- ({ core, simulateRequestScope } = initAssessFixture());
19
-
20
- sinon.spy(core.patcher, 'patch');
21
-
22
- core.assess.dataflow.sources.handle = sinon.stub();
23
-
24
- koaMulterMock = function() {
25
- const multer = {
26
- _makeMiddleware() {
27
- return function(req, res, next) {
28
- next();
29
- };
30
- }
31
- };
32
-
33
- multer.any = function() {
34
- const middleware = multer._makeMiddleware();
35
-
36
- return (ctx, next) =>
37
- new Promise((resolve, reject) => {
38
- middleware(ctx.req, ctx.res, (err) => {
39
- err ? reject(err) : resolve(ctx);
40
- });
41
- }).then(next);
42
- };
43
-
44
- return multer;
45
- };
46
-
47
- koaMulter(core).install();
48
-
49
- [patchedKoaMulter] = core.depHooks.resolve
50
- .withArgs(sinon.match({ name }))
51
- .yield(koaMulterMock);
52
-
53
- });
54
-
55
- it('skip instrumentation if it is our of scope', async function () {
56
- const ctx = {
57
- req: {},
58
- res: {}
59
- };
60
- const next = sinon.stub();
61
- const middleware = patchedKoaMulter({ dest: '/uploads' }).any('file');
62
-
63
- await middleware(ctx, next);
64
- expect(next).to.have.been.called;
65
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
66
- });
67
-
68
- it('skip instrumentation if there are no files ', function () {
69
- simulateRequestScope(async () => {
70
- const ctx = {
71
- req: {},
72
- res: {}
73
- };
74
- const next = sinon.stub();
75
- const middleware = patchedKoaMulter({ dest: '/uploads' }).any('file');
76
-
77
- await middleware(ctx, next);
78
- expect(next).to.have.been.called;
79
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
80
- });
81
- });
82
-
83
- it('handle sources (file)', function () {
84
- simulateRequestScope(async () => {
85
- const ctx = {
86
- req: {
87
- file: {
88
- property: 'file'
89
- }
90
- },
91
- res: {}
92
- };
93
- const next = sinon.stub();
94
- const middleware = patchedKoaMulter({ dest: '/uploads' }).any('file');
95
-
96
- await middleware(ctx, next);
97
- expect(next).to.have.been.called;
98
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
99
- context: 'req',
100
- data: ctx.req,
101
- keys: ['file'],
102
- name: 'multer',
103
- inputType: InputType.BODY,
104
- });
105
- });
106
- });
107
-
108
- it('handle sources (files)', function () {
109
- simulateRequestScope(async () => {
110
- const ctx = {
111
- req: {
112
- files: [
113
- {
114
- property: 'file0'
115
- },
116
- {
117
- property: 'file1'
118
- }
119
- ]
120
- },
121
- res: {}
122
- };
123
-
124
- const next = sinon.stub();
125
- const middleware = patchedKoaMulter({ dest: '/uploads' }).any('file');
126
-
127
- await middleware(ctx, next);
128
- expect(next).to.have.been.called;
129
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
130
- context: 'req.files',
131
- data: ctx.req.files[0],
132
- keys: [0],
133
- name: 'multer',
134
- inputType: InputType.BODY,
135
- });
136
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
137
- context: 'req.files',
138
- data: ctx.req.files[1],
139
- keys: [1],
140
- name: 'multer',
141
- inputType: InputType.BODY,
142
- });
143
- });
144
- });
145
-
146
- it('handle sources (body)', function () {
147
- simulateRequestScope(async () => {
148
- const ctx = {
149
- req: {
150
- body: {
151
- name: 'John'
152
- }
153
- },
154
- res: {}
155
- };
156
-
157
- const next = sinon.stub();
158
- const middleware = patchedKoaMulter({ dest: '/uploads' }).any('file');
159
-
160
- await middleware(ctx, next);
161
- expect(next).to.have.been.called;
162
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
163
- context: 'req',
164
- data: ctx.req,
165
- keys: ['body'],
166
- name: 'multer',
167
- inputType: InputType.BODY,
168
- });
169
- });
170
- });
171
-
172
- it('should log error if there is an issue handling sources', function () {
173
- simulateRequestScope(async () => {
174
- const err = new Error();
175
- const ctx = {
176
- req: {
177
- file: {
178
- property: 'file'
179
- }
180
- },
181
- res: {}
182
- };
183
- core.assess.dataflow.sources.handle.callsFake(() => {
184
- throw err;
185
- });
186
-
187
- const next = sinon.stub();
188
- const middleware = patchedKoaMulter({ dest: '/uploads' }).any('file');
189
-
190
- await middleware(ctx, next);
191
- expect(next).to.have.been.called;
192
- expect(core.logger.error).to.have.been.calledWith({ err }, 'error handling Koa multer Assess dataflow %s.%s source', 'req', 'file');
193
- });
194
- });
195
- });
196
- });
197
- });
@@ -1,146 +0,0 @@
1
- 'use strict';
2
-
3
- const sinon = require('sinon');
4
- const { expect } = require('chai');
5
- const { initAssessFixture } = require('@contrast/test/fixtures');
6
- const { InputType } = require('@contrast/common');
7
- const koaRouters = require('./koa-routers');
8
-
9
- describe('assess dataflow sources koa-router and @koa/router', function () {
10
- let core,
11
- simulateRequestScope,
12
- routerMock,
13
- legacyRouterMock,
14
- params,
15
- sources;
16
-
17
-
18
- beforeEach(function () {
19
- ({ core, simulateRequestScope } = initAssessFixture());
20
-
21
- sources = core.assess.dataflow.sources;
22
- sinon.spy(sources, 'handle');
23
- sinon.spy(core.patcher, 'patch');
24
-
25
- params = { parameter: 'parsed-param' };
26
- routerMock = {
27
- prototype: {
28
- params: (parameters) => parameters || {}
29
- }
30
- };
31
-
32
- legacyRouterMock = {
33
- prototype: {
34
- params: (parameters) => parameters || {}
35
- }
36
- };
37
- const mockDict = {
38
- ['koa-router']: legacyRouterMock,
39
- ['@koa/router']: routerMock,
40
- };
41
-
42
- core.depHooks.resolve.callsFake(function (desc, cb) {
43
- const cbArg = mockDict[desc.name];
44
- if (cbArg) {
45
- cb(cbArg);
46
- }
47
- });
48
-
49
- koaRouters(core).install();
50
- core.patcher.patch.resetHistory();
51
- });
52
-
53
- it('calls patcher for every necessary instrumentation', function () {
54
- koaRouters(core).install();
55
- expect(core.patcher.patch).to.have.been.calledTwice;
56
- expect(core.patcher.patch).to.have.been.calledWithMatch(legacyRouterMock.prototype, 'params', {
57
- name: '[koa-router].layer.prototype',
58
- patchType: 'assess-dataflow-source',
59
- post: sinon.match.func
60
- });
61
- expect(core.patcher.patch).to.have.been.calledWithMatch(routerMock.prototype, 'params', {
62
- name: '[@koa/router].layer.prototype',
63
- patchType: 'assess-dataflow-source',
64
- post: sinon.match.func
65
- });
66
- });
67
-
68
- describe('koa routers post hook', function () {
69
- let getParamsFn;
70
- this.beforeEach(function () {
71
- getParamsFn = function (routerName) {
72
- return routerName == 'koa-router' ? legacyRouterMock.prototype.params : routerMock.prototype.params;
73
- };
74
- });
75
- ['koa-router', '@koa/router'].forEach((router) => {
76
- it(`[${router}] calls \`.handle\` when parsing is successful and in the right context`, function () {
77
- const paramsFn = getParamsFn(router);
78
- let sourceContext;
79
-
80
- simulateRequestScope(() => {
81
- paramsFn(params);
82
-
83
- sourceContext = core.scopes.sources.getStore()?.assess;
84
- });
85
-
86
- expect(sources.handle).to.have.been.calledOnce;
87
- expect(sources.handle).to.have.been.calledWithMatch({
88
- context: 'ctx.params',
89
- name: `[${router}].layer.prototype`,
90
- stacktraceOpts: {
91
- constructorOpt: sinon.match.func,
92
- prependFrames: [sinon.match.func]
93
- },
94
- inputType: InputType.URL_PARAMETER,
95
- data: { parameter: 'parsed-param' },
96
- sourceContext
97
- });
98
- });
99
-
100
- it(`[${router}] does not call \`.handle\` when there is no assess policy in request context`, function () {
101
- const paramsFn = getParamsFn(router);
102
-
103
- simulateRequestScope(() => {
104
- paramsFn(params);
105
- }, { assess: { policy: null } });
106
-
107
- expect(sources.handle).not.to.have.been.called;
108
- });
109
-
110
- it('does not call `.handle` when the values are already tracked', function () {
111
- const paramsFn = getParamsFn(router);
112
-
113
- simulateRequestScope(() => {
114
- const sourceContext = core.scopes.sources.getStore()?.assess;
115
- sourceContext.parsedParams = true;
116
-
117
- paramsFn(params);
118
- });
119
-
120
- expect(sources.handle).not.to.have.been.called;
121
- expect(core.logger.trace).to.have.been.calledWith(
122
- { inputType: InputType.URL_PARAMETER, funcKey: `assess-dataflow-source:[${router}].layer.prototype` },
123
- 'values already tracked',
124
- );
125
- });
126
-
127
- it('handles a case with an error in the `.handle` method', function () {
128
- const paramsFn = getParamsFn(router);
129
- const err = new Error('test');
130
-
131
- core.assess.dataflow.sources.handle = () => {
132
- throw err;
133
- };
134
-
135
- simulateRequestScope(() => {
136
- paramsFn(params);
137
- });
138
-
139
- expect(core.logger.error).to.have.been.calledWith(
140
- { err, inputType: InputType.URL_PARAMETER, funcKey: `assess-dataflow-source:[${router}].layer.prototype` },
141
- 'unable to handle Koa source',
142
- );
143
- });
144
- });
145
- });
146
- });
@@ -1,145 +0,0 @@
1
- 'use strict';
2
-
3
- const sinon = require('sinon');
4
- const { expect } = require('chai');
5
- const { initAssessFixture } = require('@contrast/test/fixtures');
6
- const { InputType } = require('@contrast/common');
7
- const koaInstr = require('./koa2');
8
-
9
- describe('assess dataflow koa v2.x', function () {
10
- let core,
11
- simulateRequestScope,
12
- koaMock,
13
- nextStub,
14
- query,
15
- sources;
16
-
17
-
18
- beforeEach(function () {
19
- ({ core, simulateRequestScope } = initAssessFixture());
20
-
21
- sources = core.assess.dataflow.sources;
22
- sinon.spy(sources, 'handle');
23
- sinon.spy(core.patcher, 'patch');
24
-
25
- query = { parameter: 'parsed-query' };
26
- koaMock = {
27
- prototype: {
28
- middleware: [],
29
- use: (newMiddleware) => {
30
- newMiddleware && koaMock.prototype.middleware && koaMock.prototype.middleware.push(newMiddleware);
31
- },
32
- }
33
- };
34
-
35
- nextStub = sinon.stub();
36
-
37
- koaInstr(core).install();
38
- core.depHooks.resolve.yield(koaMock);
39
- core.patcher.patch.resetHistory();
40
- });
41
-
42
- it('calls patcher for every necessary instrumentation', function () {
43
- core.depHooks.resolve.yield(koaMock);
44
-
45
- expect(core.patcher.patch).to.have.been.calledOnce;
46
- expect(core.patcher.patch).to.have.been.calledWithMatch(koaMock.prototype, 'use', {
47
- name: 'Koa.Application',
48
- patchType: 'assess-dataflow-source',
49
- pre: sinon.match.func
50
- });
51
- });
52
-
53
- describe('contrastStartMiddleware', function () {
54
- it('successfully injects contrastMiddleware', function () {
55
- koaMock.prototype.use('test');
56
-
57
- expect(koaMock.prototype.middleware.length).to.equal(2);
58
- expect(koaMock.prototype.middleware[0]._isContrastStartMiddleware).to.be.true;
59
- });
60
-
61
- it('does not inject contrastMiddleware if there is one already injected', function () {
62
- koaMock.prototype.middleware.push({ _isContrastStartMiddleware: true });
63
- koaMock.prototype.use();
64
-
65
- expect(koaMock.prototype.middleware.length).to.equal(1);
66
- });
67
-
68
- it('calls `.handle` if there is query on the ctx for `contrastStartMiddleware`', function () {
69
- koaMock.prototype.use();
70
-
71
- const contrastStartMiddleware = koaMock.prototype.middleware[0];
72
- let sourceContext;
73
-
74
- simulateRequestScope(() => {
75
- contrastStartMiddleware({ query }, nextStub);
76
-
77
- sourceContext = core.scopes.sources.getStore()?.assess;
78
- });
79
-
80
- expect(nextStub).to.have.been.calledOnce;
81
- expect(sources.handle).to.have.been.calledOnce;
82
- expect(sources.handle).to.have.been.calledWithMatch({
83
- context: 'ctx.query',
84
- name: 'Koa.Application',
85
- stacktraceOpts: {
86
- constructorOpt: sinon.match.func,
87
- },
88
- inputType: InputType.QUERYSTRING,
89
- data: { parameter: 'parsed-query' },
90
- sourceContext
91
- });
92
- });
93
-
94
- it('does not call `.handle` if there is query on the ctx for `contrastStartMiddleware`, but we have already analyzed it', function () {
95
- koaMock.prototype.use();
96
-
97
- const contrastStartMiddleware = koaMock.prototype.middleware[0];
98
-
99
- simulateRequestScope(() => {
100
- const sourceContext = core.scopes.sources.getStore()?.assess;
101
- sourceContext.parsedQuery = 'analyzed-query';
102
- contrastStartMiddleware({ query }, nextStub);
103
- });
104
-
105
- expect(nextStub).to.have.been.calledOnce;
106
- expect(sources.handle).not.to.have.been.called;
107
- });
108
-
109
- it('does not call `.handle` if there is no context', function () {
110
- koaMock.prototype.use();
111
-
112
- const contrastStartMiddleware = koaMock.prototype.middleware[0];
113
-
114
- simulateRequestScope(() => {
115
- contrastStartMiddleware({ query }, nextStub);
116
- }, { assess: { policy: null } });
117
-
118
- expect(nextStub).to.have.been.calledOnce;
119
- expect(sources.handle).not.to.have.been.called;
120
- });
121
-
122
- it('handles a case with an error in the `.handle` method', function () {
123
- koaMock.prototype.use();
124
-
125
- const err = new Error('test');
126
- const contrastStartMiddleware = koaMock.prototype.middleware[0];
127
-
128
- core.assess.dataflow.sources.handle = () => {
129
- throw err;
130
- };
131
-
132
- simulateRequestScope(() => {
133
- contrastStartMiddleware({ query }, nextStub);
134
- });
135
-
136
- expect(core.logger.error).to.have.been.called;
137
- expect(core.logger.error).to.have.been.calledWith(
138
- { err, inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:Koa.Application' },
139
- 'unable to handle Koa source',
140
- );
141
- expect(nextStub).to.have.been.calledOnce;
142
- expect(nextStub).not.to.have.been.calledWith(err);
143
- });
144
- });
145
- });
@@ -1,143 +0,0 @@
1
- 'use strict';
2
-
3
- const sinon = require('sinon');
4
- const { expect } = require('chai');
5
- const { initAssessFixture } = require('@contrast/test/fixtures');
6
- const { InputType } = require('@contrast/common');
7
- const multer = require('./multer1');
8
-
9
- describe('assess dataflow sources multer', function () {
10
- let core,
11
- simulateRequestScope,
12
- multerMakeMiddlewareMock,
13
- multerMiddlewareMock;
14
-
15
- beforeEach(function () {
16
- ({ core, simulateRequestScope } = initAssessFixture());
17
-
18
- sinon.spy(core.patcher, 'patch');
19
-
20
- core.assess.dataflow.sources.handle = sinon.stub();
21
-
22
- multerMakeMiddlewareMock = function () {
23
- return multerMiddlewareMock;
24
- };
25
-
26
- multerMiddlewareMock = function (req, res, next) {
27
- next();
28
- };
29
-
30
- multer(core).install();
31
-
32
- const [patchedFunction] = core.depHooks.resolve.yield(multerMakeMiddlewareMock);
33
- multerMakeMiddlewareMock = patchedFunction;
34
- });
35
-
36
- it('skip instrumentation if it is out of scope', function () {
37
- multerMakeMiddlewareMock()({}, {}, () => ({}));
38
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
39
- });
40
-
41
- it('skip instrumentation if there are no files ', function () {
42
- simulateRequestScope(() => {
43
- const nextFunction = sinon.stub();
44
- const req = {};
45
- const res = {};
46
-
47
- multerMakeMiddlewareMock()(req, res, nextFunction);
48
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
49
- expect(nextFunction).to.have.been.called;
50
- });
51
- });
52
-
53
- it('handle sources', function () {
54
- simulateRequestScope(() => {
55
- const nextFunction = sinon.stub();
56
- const file = {
57
- property: 'file',
58
- };
59
- const files = [
60
- {
61
- property: 'files.0',
62
- },
63
- {
64
- property: 'files.1',
65
- },
66
- ];
67
-
68
- const body = {
69
- name: 'John'
70
- };
71
-
72
- const req = {
73
- file,
74
- files,
75
- body,
76
- };
77
- const res = {};
78
-
79
- multerMakeMiddlewareMock()(req, res, nextFunction);
80
-
81
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
82
- context: 'req',
83
- data: req,
84
- keys: ['file'],
85
- name: 'multer',
86
- inputType: InputType.BODY,
87
- });
88
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
89
- context: 'req.files',
90
- data: files[0],
91
- keys: [0],
92
- name: 'multer',
93
- inputType: InputType.BODY,
94
- });
95
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
96
- context: 'req.files',
97
- data: files[1],
98
- keys: [1],
99
- name: 'multer',
100
- inputType: InputType.BODY,
101
- });
102
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
103
- context: 'req',
104
- data: req,
105
- keys: ['body'],
106
- name: 'multer',
107
- inputType: InputType.BODY,
108
- });
109
- expect(nextFunction).to.have.been.called;
110
- });
111
- });
112
-
113
- it('should log error if there is an issue handling sources', function () {
114
- simulateRequestScope(() => {
115
- const nextFunction = sinon.stub();
116
- const file = {
117
- property: 'foo',
118
- };
119
- const req = { file };
120
- const res = {};
121
- const err = new Error();
122
-
123
- core.assess.dataflow.sources.handle.callsFake(() => {
124
- throw err;
125
- });
126
-
127
- multerMakeMiddlewareMock()(req, res, nextFunction);
128
-
129
- expect(
130
- core.assess.dataflow.sources.handle,
131
- ).to.have.been.calledWithMatch({
132
- context: 'req',
133
- data: { file },
134
- keys: ['file'],
135
- name: 'multer',
136
- inputType: InputType.BODY,
137
- });
138
-
139
- expect(core.logger.error).to.have.been.calledWith({ err }, 'error handling multer Assess dataflow %s.%s source', 'req', 'file');
140
- expect(nextFunction).to.have.been.called;
141
- });
142
- });
143
- });