@contrast/assess 1.30.0 → 1.32.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.
Files changed (156) hide show
  1. package/lib/crypto-analysis/install/crypto.test.js +146 -0
  2. package/lib/crypto-analysis/install/math.test.js +65 -0
  3. package/lib/dataflow/index.test.js +36 -0
  4. package/lib/dataflow/propagation/index.test.js +103 -0
  5. package/lib/dataflow/propagation/install/JSON/index.test.js +50 -0
  6. package/lib/dataflow/propagation/install/JSON/parse-fn.test.js +232 -0
  7. package/lib/dataflow/propagation/install/JSON/parse.test.js +968 -0
  8. package/lib/dataflow/propagation/install/JSON/stringify.test.js +265 -0
  9. package/lib/dataflow/propagation/install/array-prototype-join.test.js +106 -0
  10. package/lib/dataflow/propagation/install/buffer.test.js +109 -0
  11. package/lib/dataflow/propagation/install/contrast-methods/add.test.js +94 -0
  12. package/lib/dataflow/propagation/install/contrast-methods/index.test.js +49 -0
  13. package/lib/dataflow/propagation/install/contrast-methods/number.test.js +50 -0
  14. package/lib/dataflow/propagation/install/contrast-methods/string.test.js +148 -0
  15. package/lib/dataflow/propagation/install/contrast-methods/tag.test.js +145 -0
  16. package/lib/dataflow/propagation/install/decode-uri-component.test.js +78 -0
  17. package/lib/dataflow/propagation/install/ejs/escape-xml.test.js +69 -0
  18. package/lib/dataflow/propagation/install/ejs/template.test.js +62 -0
  19. package/lib/dataflow/propagation/install/encode-uri.test.js +83 -0
  20. package/lib/dataflow/propagation/install/escape-html.test.js +71 -0
  21. package/lib/dataflow/propagation/install/escape.test.js +73 -0
  22. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.test.js +71 -0
  23. package/lib/dataflow/propagation/install/isnumeric-0.test.js +58 -0
  24. package/lib/dataflow/propagation/install/joi/any.test.js +270 -0
  25. package/lib/dataflow/propagation/install/joi/array.test.js +912 -0
  26. package/lib/dataflow/propagation/install/joi/boolean.test.js +103 -0
  27. package/lib/dataflow/propagation/install/joi/expression.test.js +76 -0
  28. package/lib/dataflow/propagation/install/joi/index.test.js +39 -0
  29. package/lib/dataflow/propagation/install/joi/number.test.js +103 -0
  30. package/lib/dataflow/propagation/install/joi/object.test.js +119 -0
  31. package/lib/dataflow/propagation/install/joi/ref.test.js +607 -0
  32. package/lib/dataflow/propagation/install/joi/string-schema.test.js +513 -0
  33. package/lib/dataflow/propagation/install/mongoose/index.test.js +42 -0
  34. package/lib/dataflow/propagation/install/mongoose/schema-map.test.js +348 -0
  35. package/lib/dataflow/propagation/install/mongoose/schema-mixed.test.js +512 -0
  36. package/lib/dataflow/propagation/install/mongoose/schema-string.test.js +160 -0
  37. package/lib/dataflow/propagation/install/mustache-escape.test.js +62 -0
  38. package/lib/dataflow/propagation/install/mysql-connection-escape.test.js +74 -0
  39. package/lib/dataflow/propagation/install/parse-int.test.js +48 -0
  40. package/lib/dataflow/propagation/install/path/basename.test.js +143 -0
  41. package/lib/dataflow/propagation/install/path/dirname.test.js +167 -0
  42. package/lib/dataflow/propagation/install/path/extname.test.js +141 -0
  43. package/lib/dataflow/propagation/install/path/format.test.js +250 -0
  44. package/lib/dataflow/propagation/install/path/index.test.js +45 -0
  45. package/lib/dataflow/propagation/install/path/join-and-resolve.test.js +485 -0
  46. package/lib/dataflow/propagation/install/path/normalize.test.js +176 -0
  47. package/lib/dataflow/propagation/install/path/parse.test.js +238 -0
  48. package/lib/dataflow/propagation/install/path/relative.test.js +239 -0
  49. package/lib/dataflow/propagation/install/path/toNamespacedPath.test.js +158 -0
  50. package/lib/dataflow/propagation/install/pug/index.test.js +55 -0
  51. package/lib/dataflow/propagation/install/pug-runtime-escape.test.js +69 -0
  52. package/lib/dataflow/propagation/install/querystring/escape.test.js +63 -0
  53. package/lib/dataflow/propagation/install/querystring/index.test.js +40 -0
  54. package/lib/dataflow/propagation/install/querystring/parse.test.js +272 -0
  55. package/lib/dataflow/propagation/install/querystring/stringify.test.js +301 -0
  56. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.test.js +281 -0
  57. package/lib/dataflow/propagation/install/send.test.js +63 -0
  58. package/lib/dataflow/propagation/install/sequelize/query-generator.test.js +73 -0
  59. package/lib/dataflow/propagation/install/sequelize/sql-string.test.js +130 -0
  60. package/lib/dataflow/propagation/install/sql-template-strings.test.js +100 -0
  61. package/lib/dataflow/propagation/install/string/concat.test.js +132 -0
  62. package/lib/dataflow/propagation/install/string/format-methods.test.js +61 -0
  63. package/lib/dataflow/propagation/install/string/html-methods.test.js +164 -0
  64. package/lib/dataflow/propagation/install/string/index.test.js +103 -0
  65. package/lib/dataflow/propagation/install/string/match-all.test.js +399 -0
  66. package/lib/dataflow/propagation/install/string/match.test.js +361 -0
  67. package/lib/dataflow/propagation/install/string/replace.test.js +588 -0
  68. package/lib/dataflow/propagation/install/string/slice.test.js +265 -0
  69. package/lib/dataflow/propagation/install/string/split.test.js +500 -0
  70. package/lib/dataflow/propagation/install/string/substring.test.js +238 -0
  71. package/lib/dataflow/propagation/install/string/trim.test.js +122 -0
  72. package/lib/dataflow/propagation/install/unescape.test.js +78 -0
  73. package/lib/dataflow/propagation/install/url/domain-parsers.test.js +63 -0
  74. package/lib/dataflow/propagation/install/url/parse.test.js +391 -0
  75. package/lib/dataflow/propagation/install/url/searchParams.test.js +538 -0
  76. package/lib/dataflow/propagation/install/url/url.test.js +466 -0
  77. package/lib/dataflow/propagation/install/util-format.test.js +336 -0
  78. package/lib/dataflow/propagation/install/validator/hooks.test.js +211 -0
  79. package/lib/dataflow/sinks/index.test.js +78 -0
  80. package/lib/dataflow/sinks/install/child-process.test.js +338 -0
  81. package/lib/dataflow/sinks/install/eval.test.js +95 -0
  82. package/lib/dataflow/sinks/install/express/index.test.js +33 -0
  83. package/lib/dataflow/sinks/install/express/reflected-xss.js +55 -57
  84. package/lib/dataflow/sinks/install/express/reflected-xss.test.js +109 -0
  85. package/lib/dataflow/sinks/install/express/unvalidated-redirect.test.js +144 -0
  86. package/lib/dataflow/sinks/install/fastify/index.test.js +32 -0
  87. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.test.js +130 -0
  88. package/lib/dataflow/sinks/install/fs.test.js +138 -0
  89. package/lib/dataflow/sinks/install/function.test.js +103 -0
  90. package/lib/dataflow/sinks/install/hapi/index.test.js +32 -0
  91. package/lib/dataflow/sinks/install/hapi/unvalidated-redirect.test.js +130 -0
  92. package/lib/dataflow/sinks/install/http/index.test.js +33 -0
  93. package/lib/dataflow/sinks/install/http/request.test.js +184 -0
  94. package/lib/dataflow/sinks/install/http/server-response.test.js +162 -0
  95. package/lib/dataflow/sinks/install/koa/index.test.js +32 -0
  96. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.test.js +200 -0
  97. package/lib/dataflow/sinks/install/libxmljs.test.js +158 -0
  98. package/lib/dataflow/sinks/install/marsdb.test.js +166 -0
  99. package/lib/dataflow/sinks/install/mongodb.test.js +621 -0
  100. package/lib/dataflow/sinks/install/mssql.test.js +136 -0
  101. package/lib/dataflow/sinks/install/mysql.test.js +233 -0
  102. package/lib/dataflow/sinks/install/node-serialize.test.js +85 -0
  103. package/lib/dataflow/sinks/install/postgres.test.js +158 -0
  104. package/lib/dataflow/sinks/install/restify.test.js +142 -0
  105. package/lib/dataflow/sinks/install/sequelize.test.js +100 -0
  106. package/lib/dataflow/sinks/install/sqlite3.test.js +118 -0
  107. package/lib/dataflow/sinks/install/vm.test.js +326 -0
  108. package/lib/dataflow/sources/handler.test.js +463 -0
  109. package/lib/dataflow/sources/index.test.js +58 -0
  110. package/lib/dataflow/sources/install/body-parser1.test.js +248 -0
  111. package/lib/dataflow/sources/install/busboy.test.js +152 -0
  112. package/lib/dataflow/sources/install/cookie-parser1.test.js +143 -0
  113. package/lib/dataflow/sources/install/express/params.test.js +105 -0
  114. package/lib/dataflow/sources/install/express/parsedUrl.test.js +65 -0
  115. package/lib/dataflow/sources/install/fastify/fastify.js +1 -1
  116. package/lib/dataflow/sources/install/fastify/fastify.test.js +210 -0
  117. package/lib/dataflow/sources/install/fastify/index.test.js +33 -0
  118. package/lib/dataflow/sources/install/formidable1.test.js +119 -0
  119. package/lib/dataflow/sources/install/hapi/hapi.test.js +172 -0
  120. package/lib/dataflow/sources/install/hapi/index.test.js +33 -0
  121. package/lib/dataflow/sources/install/http.test.js +155 -0
  122. package/lib/dataflow/sources/install/koa/index.test.js +40 -0
  123. package/lib/dataflow/sources/install/koa/koa-bodyparsers.test.js +161 -0
  124. package/lib/dataflow/sources/install/koa/koa-multer.test.js +197 -0
  125. package/lib/dataflow/sources/install/koa/koa-routers.test.js +146 -0
  126. package/lib/dataflow/sources/install/koa/koa2.test.js +145 -0
  127. package/lib/dataflow/sources/install/multer1.test.js +145 -0
  128. package/lib/dataflow/sources/install/qs6.test.js +131 -0
  129. package/lib/dataflow/sources/install/querystring.test.js +82 -0
  130. package/lib/dataflow/sources/install/restify/fieldedTextBodyParser.test.js +88 -0
  131. package/lib/dataflow/sources/install/restify/index.test.js +38 -0
  132. package/lib/dataflow/sources/install/restify/jsonBodyParser.test.js +144 -0
  133. package/lib/dataflow/sources/install/restify/router.test.js +83 -0
  134. package/lib/dataflow/tag-utils-complete.test.js +27 -0
  135. package/lib/dataflow/tag-utils.test.js +192 -0
  136. package/lib/dataflow/tracker.test.js +216 -0
  137. package/lib/dataflow/utils/is-safe-content-type.test.js +16 -0
  138. package/lib/dataflow/utils/is-vulnerable.test.js +115 -0
  139. package/lib/event-factory.test.js +321 -0
  140. package/lib/get-policy.test.js +194 -0
  141. package/lib/get-source-context.test.js +108 -0
  142. package/lib/index.test.js +41 -0
  143. package/lib/make-source-context.test.js +50 -0
  144. package/lib/response-scanning/handlers/index.test.js +425 -0
  145. package/lib/response-scanning/handlers/utils.test.js +391 -0
  146. package/lib/response-scanning/index.test.js +41 -0
  147. package/lib/response-scanning/install/http.test.js +175 -0
  148. package/lib/rule-scopes.test.js +27 -0
  149. package/lib/session-configuration/handlers.test.js +84 -0
  150. package/lib/session-configuration/index.test.js +36 -0
  151. package/lib/session-configuration/install/express-session.test.js +220 -0
  152. package/lib/session-configuration/install/fastify-cookie.test.js +65 -0
  153. package/lib/session-configuration/install/hapi.test.js +269 -0
  154. package/lib/session-configuration/install/koa.js +50 -44
  155. package/lib/session-configuration/install/koa.test.js +92 -0
  156. package/package.json +2 -2
@@ -0,0 +1,248 @@
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
+
8
+ describe('assess dataflow sources body-parser v1', function () {
9
+ let core, simulateRequestScope, req, res, next, bodyParser;
10
+
11
+ beforeEach(function () {
12
+ ({ core, simulateRequestScope } = initAssessFixture());
13
+ req = {};
14
+ res = {};
15
+ next = sinon.stub();
16
+
17
+ sinon.stub(core.assess.dataflow.sources, 'handle');
18
+
19
+ const bodyParserMock = (body) => (req, res, next) => {
20
+ req.body = body;
21
+ next();
22
+ };
23
+ bodyParserMock.json = (body) => (req, res, next) => {
24
+ req.body = body;
25
+ next();
26
+ };
27
+ bodyParserMock.raw = (body) => (req, res, next) => {
28
+ req.body = body;
29
+ next();
30
+ };
31
+ bodyParserMock.text = (body) => (req, res, next) => {
32
+ req.body = body;
33
+ next();
34
+ };
35
+ bodyParserMock.urlencoded = (body) => (req, res, next) => {
36
+ req.body = body;
37
+ next();
38
+ };
39
+
40
+ require('./body-parser1')(core).install();
41
+ bodyParser = core.depHooks.resolve.yield(bodyParserMock)[0];
42
+ });
43
+
44
+ // tests are not properly patching the default function
45
+ describe('bodyParser', function () {
46
+ it('handles a json request body as expected', function () {
47
+ const body = { foo: 'bar' };
48
+ const middleware = bodyParser(body);
49
+ req.headers = { 'content-type': 'application/json' };
50
+
51
+ simulateRequestScope(() => {
52
+ middleware(req, res, next);
53
+
54
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
55
+ context: 'req.body',
56
+ name: 'body-parser.bodyParser',
57
+ inputType: InputType.JSON_VALUE,
58
+ stacktraceOpts: {
59
+ constructorOpt: sinon.match.func,
60
+ },
61
+ data: body,
62
+ sourceContext: core.scopes.sources.getStore().assess
63
+ });
64
+ });
65
+ });
66
+
67
+ it('handles a urlencoded request body as expected', function () {
68
+ const body = { foo: 'bar' };
69
+ const middleware = bodyParser(body);
70
+ req.headers = { 'content-type': 'application/x-www-form-urlencoded' };
71
+
72
+ simulateRequestScope(() => {
73
+ middleware(req, res, next);
74
+
75
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
76
+ context: 'req.body',
77
+ name: 'body-parser.bodyParser',
78
+ inputType: InputType.PARAMETER_VALUE,
79
+ stacktraceOpts: {
80
+ constructorOpt: sinon.match.func,
81
+ },
82
+ data: body,
83
+ sourceContext: core.scopes.sources.getStore().assess
84
+ });
85
+ });
86
+ });
87
+
88
+ it('handles an unknown format request body as expected', function () {
89
+ const body = 'text';
90
+ const middleware = bodyParser(body);
91
+
92
+ simulateRequestScope(() => {
93
+ middleware(req, res, next);
94
+
95
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
96
+ context: 'req',
97
+ name: 'body-parser.bodyParser',
98
+ inputType: InputType.BODY,
99
+ stacktraceOpts: {
100
+ constructorOpt: sinon.match.func,
101
+ },
102
+ data: { body },
103
+ sourceContext: core.scopes.sources.getStore().assess
104
+ });
105
+ });
106
+ });
107
+
108
+ it('does not handle the request when not in context', function () {
109
+ const middleware = bodyParser('text');
110
+
111
+ simulateRequestScope(() => {
112
+ middleware(req, res, next);
113
+
114
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
115
+ expect(core.logger.error).to.have.been.calledWith(
116
+ { funcKey: 'assess-dataflow-source:body-parser.bodyParser' },
117
+ 'unable to handle source. Missing `sourceContext`'
118
+ );
119
+ }, {});
120
+ });
121
+
122
+ it('does not handle the request when already tracked', function () {
123
+ const middleware = bodyParser('text');
124
+
125
+ simulateRequestScope(() => {
126
+ // run twice
127
+ middleware(req, res, next);
128
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledOnce;
129
+ middleware(req, res, next);
130
+
131
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledOnce;
132
+ expect(core.logger.trace).to.have.been.calledWith(
133
+ { funcKey: 'assess-dataflow-source:body-parser.bodyParser' },
134
+ 'values already tracked',
135
+ );
136
+ });
137
+ });
138
+
139
+ it('logs errors in the handle method', function () {
140
+ const middleware = bodyParser('text');
141
+ const err = new Error('yikes!');
142
+ core.assess.dataflow.sources.handle.throws(err);
143
+ simulateRequestScope(() => {
144
+ middleware(req, res, next);
145
+
146
+ expect(core.logger.error).to.have.been.calledWith(
147
+ { err, funcKey: 'assess-dataflow-source:body-parser.bodyParser' },
148
+ 'unable to handle source',
149
+ );
150
+ });
151
+ });
152
+ });
153
+
154
+ [
155
+ {
156
+ method: 'json',
157
+ body: { json: 'body' },
158
+ inputType: InputType.JSON_VALUE,
159
+ context: 'req.body',
160
+ data: { json: 'body' },
161
+ },
162
+ {
163
+ method: 'raw',
164
+ body: Buffer.from('raw'),
165
+ inputType: InputType.BODY,
166
+ context: 'req',
167
+ data: { body: Buffer.from('raw') }
168
+ },
169
+ {
170
+ method: 'text',
171
+ body: 'text',
172
+ inputType: InputType.BODY,
173
+ context: 'req',
174
+ data: { body: 'text' }
175
+ },
176
+ {
177
+ method: 'urlencoded',
178
+ body: { urlencoded: 'yes' },
179
+ inputType: InputType.PARAMETER_VALUE,
180
+ context: 'req.body',
181
+ data: { urlencoded: 'yes' }
182
+ },
183
+ ].forEach(({ method, body, inputType, context, data }) => {
184
+ describe(`bodyParser.${method}`, function () {
185
+ let middleware;
186
+
187
+ beforeEach(function () {
188
+ middleware = bodyParser[method](body);
189
+ });
190
+
191
+ it('handles the request body as expected', function () {
192
+ simulateRequestScope(() => {
193
+ middleware(req, res, next);
194
+
195
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
196
+ context,
197
+ name: `body-parser.${method}.${method}Parser`,
198
+ inputType,
199
+ stacktraceOpts: {
200
+ constructorOpt: sinon.match.func,
201
+ },
202
+ data,
203
+ sourceContext: core.scopes.sources.getStore().assess
204
+ });
205
+ });
206
+ });
207
+
208
+ it('does not handle the request when not in context', function () {
209
+ simulateRequestScope(() => {
210
+ middleware(req, res, next);
211
+
212
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
213
+ expect(core.logger.error).to.have.been.calledWith(
214
+ { funcKey: `assess-dataflow-source:body-parser.${method}.${method}Parser` },
215
+ 'unable to handle source. Missing `sourceContext`',
216
+ );
217
+ }, {});
218
+ });
219
+
220
+ it('does not handle the request when already tracked', function () {
221
+ simulateRequestScope(() => {
222
+ core.scopes.sources.getStore().assess.parsedBody = true;
223
+ middleware(req, res, next);
224
+
225
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
226
+ expect(core.logger.trace).to.have.been.calledWith(
227
+ { funcKey: `assess-dataflow-source:body-parser.${method}.${method}Parser` },
228
+ 'values already tracked',
229
+ );
230
+ });
231
+ });
232
+
233
+ it('logs errors in the handle method', function () {
234
+ const err = new Error('yikes!');
235
+ core.assess.dataflow.sources.handle.throws(err);
236
+
237
+ simulateRequestScope(() => {
238
+ middleware(req, res, next);
239
+
240
+ expect(core.logger.error).to.have.been.calledWith(
241
+ { err, funcKey: `assess-dataflow-source:body-parser.${method}.${method}Parser` },
242
+ 'unable to handle source',
243
+ );
244
+ });
245
+ });
246
+ });
247
+ });
248
+ });
@@ -0,0 +1,152 @@
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 busboyInstr = require('./busboy');
8
+
9
+ describe('assess dataflow sources busboy', function () {
10
+ let core, simulateRequestScope, mockBusboy0, mockBusboy1, sources, emitMethodVersions;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+
15
+ mockBusboy0 = function () { };
16
+ mockBusboy0.prototype.emit = function () { };
17
+
18
+ mockBusboy1 = function () {
19
+ return {
20
+ emit() { }
21
+ };
22
+ };
23
+ sinon.spy(mockBusboy0.prototype, 'emit');
24
+ sinon.spy(core.patcher, 'patch');
25
+
26
+ sources = core.assess.dataflow.sources;
27
+ sinon.spy(sources, 'handle');
28
+
29
+ busboyInstr(core).install();
30
+ core.depHooks.resolve.withArgs({ name: 'busboy', version: '<1.0.0' }).yield(mockBusboy0);
31
+ core.depHooks.resolve.withArgs({ name: 'busboy', version: '>=1.0.0' }).yield(mockBusboy1);
32
+ const patchedBusboy1 = core.patcher.patch.getCall(1).returnValue;
33
+
34
+ emitMethodVersions = {
35
+ 'v0': mockBusboy0.prototype.emit,
36
+ 'v1': patchedBusboy1().emit
37
+ };
38
+ });
39
+
40
+ it('calls patcher on the `.emit` method', function () {
41
+ expect(core.patcher.patch).to.have.been.calledWithMatch(mockBusboy0.prototype, 'emit', {
42
+ name: 'busboy.prototype.emit',
43
+ patchType: 'assess-dataflow-source',
44
+ pre: sinon.match.func
45
+ });
46
+
47
+ expect(core.patcher.patch).to.have.been.calledWithMatch(mockBusboy1, {
48
+ name: 'busboy',
49
+ patchType: 'assess-dataflow-source',
50
+ post: sinon.match.func
51
+ });
52
+ });
53
+
54
+ [
55
+ { version: 'v0', finalEvent: 'finish' },
56
+ { version: 'v1', finalEvent: 'close' }
57
+ ].forEach(({ version, finalEvent }) => {
58
+ describe(version, function () {
59
+ it('calls `.handle` when there are fields of data to be tracked and it is in right context', function () {
60
+ let sourceContext;
61
+
62
+ simulateRequestScope(() => {
63
+ emitMethodVersions[version]('field', 'field1', 'value1');
64
+ emitMethodVersions[version]('field', 'field2', 'value2');
65
+ emitMethodVersions[version](finalEvent);
66
+
67
+ sourceContext = core.scopes.sources.getStore()?.assess;
68
+ });
69
+
70
+ expect(sources.handle).to.have.been.calledTwice;
71
+ expect(sources.handle).to.have.been.calledWithMatch({
72
+ context: 'req.body',
73
+ name: 'busboy',
74
+ stacktraceOpts: {
75
+ constructorOpt: sinon.match.func,
76
+ prependFrames: [sinon.match.func],
77
+ },
78
+ inputType: InputType.MULTIPART_VALUE,
79
+ data: { field1: 'value1' },
80
+ sourceContext
81
+ });
82
+ expect(sources.handle).to.have.been.calledWithMatch({
83
+ context: 'req.body',
84
+ name: 'busboy',
85
+ stacktraceOpts: {
86
+ constructorOpt: sinon.match.func,
87
+ prependFrames: [sinon.match.func],
88
+ },
89
+ inputType: InputType.MULTIPART_VALUE,
90
+ data: { field2: 'value2' },
91
+ sourceContext
92
+ });
93
+ });
94
+
95
+ it('logs an error when `.handle` throws one', function () {
96
+ const err = new Error('test');
97
+
98
+ core.assess.dataflow.sources.handle = () => {
99
+ throw err;
100
+ };
101
+
102
+ simulateRequestScope(() => {
103
+ emitMethodVersions[version]('field', 'field1', 'value1');
104
+ emitMethodVersions[version](finalEvent);
105
+ });
106
+
107
+ expect(core.logger.error).to.have.been.calledOnceWith(
108
+ { err, inputType: InputType.MULTIPART_VALUE, funcKey: sinon.match.string },
109
+ 'unable to handle source'
110
+ );
111
+ });
112
+
113
+ it('does not call `.handle` when not in context', function () {
114
+ simulateRequestScope(() => {
115
+ emitMethodVersions[version]('field', 'field1', 'value1');
116
+ emitMethodVersions[version]('field', 'field2', 'value2');
117
+ emitMethodVersions[version](finalEvent);
118
+ }, {});
119
+
120
+ expect(sources.handle).not.to.have.been.called;
121
+ });
122
+
123
+ it('does not call `.handle` when the values are already tracked', function () {
124
+ simulateRequestScope(() => {
125
+ const sourceContext = core.scopes.sources.getStore()?.assess;
126
+ sourceContext.parsedBody = true;
127
+
128
+ emitMethodVersions[version]('field', 'field1', 'value1');
129
+ emitMethodVersions[version]('field', 'field2', 'value2');
130
+ emitMethodVersions[version](finalEvent);
131
+ });
132
+
133
+ expect(sources.handle).not.to.have.been.called;
134
+ expect(core.logger.trace).to.have.been.calledWith(
135
+ { inputType: InputType.MULTIPART_VALUE, funcKey: sinon.match.string },
136
+ 'values already tracked');
137
+ });
138
+
139
+ it('does not call `.handle` when `.emit` call does not contain field name or data', function () {
140
+ simulateRequestScope(() => {
141
+ emitMethodVersions[version]('field', null, 'value1');
142
+ emitMethodVersions[version]('field', 'field2', null);
143
+ emitMethodVersions[version](finalEvent);
144
+ }, {});
145
+
146
+ expect(sources.handle).not.to.have.been.called;
147
+ expect(core.logger.debug).not.to.have.been.called;
148
+ });
149
+ });
150
+ });
151
+ });
152
+
@@ -0,0 +1,143 @@
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
+
8
+ describe('assess dataflow sources cookie-parser v1', function () {
9
+ let core, simulateRequestScope, req, res, next, cookieParser;
10
+
11
+ beforeEach(function () {
12
+ ({ core, simulateRequestScope } = initAssessFixture());
13
+ req = {};
14
+ res = {};
15
+ next = sinon.stub();
16
+
17
+ sinon.stub(core.assess.dataflow.sources, 'handle');
18
+
19
+ require('./cookie-parser1')(core).install();
20
+ cookieParser = core.depHooks.resolve.yield(
21
+ function cookieParser(props = {}) {
22
+ return function cookieParser(req, res, next) {
23
+ Object.assign(req, props);
24
+ next();
25
+ };
26
+ }
27
+ )[0];
28
+ });
29
+
30
+ it('handles cookies as expected', function () {
31
+ const cookies = { foo: 'bar' };
32
+ const middleware = cookieParser({ cookies });
33
+
34
+ simulateRequestScope(() => {
35
+ middleware(req, res, next);
36
+
37
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWith({
38
+ context: 'req.cookies',
39
+ name: 'cookie-parser.cookieParser',
40
+ inputType: InputType.COOKIE_VALUE,
41
+ stacktraceOpts: {
42
+ constructorOpt: sinon.match.func,
43
+ },
44
+ data: cookies,
45
+ sourceContext: core.scopes.sources.getStore().assess
46
+ });
47
+ });
48
+ });
49
+
50
+ it('handles signed cookies as expected', function () {
51
+ const cookies = { foo: 'bar' };
52
+ const middleware = cookieParser({ signedCookies: cookies });
53
+
54
+ simulateRequestScope(() => {
55
+ middleware(req, res, next);
56
+
57
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWith({
58
+ context: 'req.signedCookies',
59
+ name: 'cookie-parser.cookieParser',
60
+ inputType: InputType.COOKIE_VALUE,
61
+ stacktraceOpts: {
62
+ constructorOpt: sinon.match.func,
63
+ },
64
+ data: cookies,
65
+ sourceContext: core.scopes.sources.getStore().assess
66
+ });
67
+ });
68
+ });
69
+
70
+ it('does not handle the request when not in context', function () {
71
+ const middleware = cookieParser();
72
+
73
+ simulateRequestScope(() => {
74
+ middleware(req, res, next);
75
+
76
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
77
+ expect(core.logger.error).to.have.been.calledWith(
78
+ { funcKey: 'assess-dataflow-source:cookie-parser.cookieParser' },
79
+ 'unable to handle source. Missing `sourceContext`',
80
+ );
81
+ }, {});
82
+ });
83
+
84
+ it('does not handle the request when cookies already tracked', function () {
85
+ const middleware = cookieParser();
86
+
87
+ simulateRequestScope(() => {
88
+ core.scopes.sources.getStore().assess.parsedCookies = true;
89
+ middleware(req, res, next);
90
+
91
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
92
+ expect(core.logger.trace).to.have.been.calledWith(
93
+ { funcKey: 'assess-dataflow-source:cookie-parser.cookieParser' },
94
+ 'cookies already tracked',
95
+ );
96
+ });
97
+ });
98
+
99
+ it('does not handle the request when signed cookies already tracked', function () {
100
+ const middleware = cookieParser();
101
+
102
+ simulateRequestScope(() => {
103
+ core.scopes.sources.getStore().assess.parsedSignedCookies = true;
104
+ middleware(req, res, next);
105
+
106
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
107
+ expect(core.logger.trace).to.have.been.calledWith(
108
+ { funcKey: 'assess-dataflow-source:cookie-parser.cookieParser' },
109
+ 'signedCookies already tracked',
110
+ );
111
+ });
112
+ });
113
+
114
+ it('logs errors when handling cookies', function () {
115
+ const middleware = cookieParser({ cookies: {} });
116
+ const err = new Error('yikes!');
117
+ core.assess.dataflow.sources.handle.throws(err);
118
+
119
+ simulateRequestScope(() => {
120
+ middleware(req, res, next);
121
+
122
+ expect(core.logger.error).to.have.been.calledWith(
123
+ { err, funcKey: 'assess-dataflow-source:cookie-parser.cookieParser' },
124
+ 'unable to handle source',
125
+ );
126
+ });
127
+ });
128
+
129
+ it('logs errors when handling signed cookies', function () {
130
+ const middleware = cookieParser({ signedCookies: {} });
131
+ const err = new Error('yikes!');
132
+ core.assess.dataflow.sources.handle.throws(err);
133
+
134
+ simulateRequestScope(() => {
135
+ middleware(req, res, next);
136
+
137
+ expect(core.logger.error).to.have.been.calledWith(
138
+ { err, funcKey: 'assess-dataflow-source:cookie-parser.cookieParser' },
139
+ 'unable to handle source',
140
+ );
141
+ });
142
+ });
143
+ });
@@ -0,0 +1,105 @@
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
+
8
+ describe('assess dataflow sources express params', function () {
9
+ let core, simulateRequestScope, Layer, layer;
10
+
11
+ beforeEach(function () {
12
+ ({ core, simulateRequestScope } = initAssessFixture());
13
+
14
+ sinon.stub(core.assess.dataflow.sources, 'handle');
15
+
16
+ const LayerMock = sinon.stub();
17
+ LayerMock.prototype.match = sinon.stub().returns(true);
18
+
19
+ require('./params')(core).install();
20
+ Layer = core.depHooks.resolve.yield(LayerMock)[0];
21
+ layer = { keys: ['foo'], params: { foo: 'bar' } };
22
+ });
23
+
24
+ it('calls `.handle` when `match` adds params (and keys) to the Layer instance', function () {
25
+ simulateRequestScope(() => {
26
+ Reflect.apply(Layer.prototype.match, layer, ['/bar']);
27
+
28
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWith({
29
+ context: 'req.params',
30
+ name: 'Layer.prototype.match',
31
+ inputType: InputType.PARAMETER_VALUE,
32
+ stacktraceOpts: {
33
+ constructorOpt: sinon.match.func,
34
+ prependFrames: sinon.match.array,
35
+ },
36
+ data: layer.params,
37
+ sourceContext: core.scopes.sources.getStore().assess
38
+ });
39
+ });
40
+ });
41
+
42
+ it('does not call `.handle` when `match` returns false', function () {
43
+ Layer.prototype.match.returns(false);
44
+
45
+ simulateRequestScope(() => {
46
+ Reflect.apply(Layer.prototype.match, layer, ['/bar']);
47
+
48
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
49
+ });
50
+ });
51
+
52
+ it('does not call `.handle` when the layer is missing the expected properties', function () {
53
+ simulateRequestScope(() => {
54
+ Reflect.apply(Layer.prototype.match, {}, ['/bar']);
55
+
56
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
57
+ });
58
+ });
59
+
60
+ it('does not call `.handle` when `match` adds no params or keys', function () {
61
+ simulateRequestScope(() => {
62
+ Reflect.apply(Layer.prototype.match, { keys: [], params: {} }, ['/bar']);
63
+
64
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
65
+ });
66
+ });
67
+
68
+ it('does not call `.handle` when not in context', function () {
69
+ simulateRequestScope(() => {
70
+ Reflect.apply(Layer.prototype.match, layer, ['/bar']);
71
+
72
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
73
+ expect(core.logger.error).to.have.been.calledWith(
74
+ { funcKey: 'assess-dataflow-source:Layer.prototype.match' },
75
+ 'unable to handle source. Missing `sourceContext`'
76
+ );
77
+ }, {});
78
+ });
79
+
80
+ it('does not call `.handle` when the values are already tracked', function () {
81
+ simulateRequestScope(() => {
82
+ Reflect.apply(Layer.prototype.match, layer, ['/bar']);
83
+
84
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
85
+ expect(core.logger.trace).to.have.been.calledWith(
86
+ { funcKey: 'assess-dataflow-source:Layer.prototype.match' },
87
+ 'values already tracked'
88
+ );
89
+ }, { assess: { parsedParams: true } });
90
+ });
91
+
92
+ it('handles a case with an error in the `.handle` method', function () {
93
+ const err = new Error('test');
94
+ core.assess.dataflow.sources.handle.throws(err);
95
+
96
+ simulateRequestScope(() => {
97
+ Reflect.apply(Layer.prototype.match, layer, ['/bar']);
98
+
99
+ expect(core.logger.error).to.have.been.calledWith(
100
+ { err, funcKey: 'assess-dataflow-source:Layer.prototype.match' },
101
+ 'unable to handle source'
102
+ );
103
+ });
104
+ });
105
+ });