@contrast/assess 1.46.0 → 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,617 +0,0 @@
1
- 'use strict';
2
-
3
- const { expect } = require('chai');
4
- const sinon = require('sinon');
5
- const {
6
- DataflowTag: { UNTRUSTED, LIMITED_CHARS },
7
- Rule,
8
- } = require('@contrast/common');
9
- const { initAssessFixture } = require('@contrast/test/fixtures');
10
-
11
- describe('assess dataflow sinks mongodb-v4', function () {
12
- let core, simulateRequestScope, trackString, reportFindings, instr, getQueryVulnInfo, getAggregateVulnInfo, getMapReduceVulnInfo, getGroupVulnInfo;
13
-
14
- class Db {
15
- command() { }
16
- eval() { }
17
- aggregate() { }
18
- }
19
-
20
- class Collection {
21
- find() { }
22
- findOne() { }
23
- findAndModify() { }
24
- findOneAndDelete() { }
25
- findOneAndReplace() { }
26
- findOneAndUpdate() { }
27
- remove() { }
28
- removeOne() { }
29
- replaceOne() { }
30
- removeMany() { }
31
- rename() { }
32
- update() { }
33
- updateOne() { }
34
- updateMany() { }
35
- deleteOne() { }
36
- deleteMany() { }
37
- aggregate() { }
38
- mapReduce() { }
39
- group() { }
40
- }
41
-
42
- beforeEach(function () {
43
- ({ core, simulateRequestScope, trackString } = initAssessFixture());
44
- reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
45
- core.depHooks.resolve.yields({ Collection, Db });
46
-
47
- // while the Assess fixture will have already composed the module, we re-require (which
48
- // just reassigns the component) so we're able to inject the spied-upon functions above
49
- instr = require('./mongodb')(core);
50
- getQueryVulnInfo = sinon.spy(instr, 'getQueryVulnerabilityInfo');
51
- getAggregateVulnInfo = sinon.spy(instr, 'getAggregateVulnerabilityInfo');
52
- getMapReduceVulnInfo = sinon.spy(instr, 'getMapReduceVulnerabilityInfo');
53
- getGroupVulnInfo = sinon.spy(instr, 'getGroupVulnerabilityInfo');
54
- instr.install();
55
- });
56
-
57
- [
58
- { subject: Collection, method: 'find' },
59
- { subject: Collection, method: 'findOne' },
60
- { subject: Collection, method: 'findAndModify' },
61
- { subject: Collection, method: 'findOneAndDelete' },
62
- { subject: Collection, method: 'findOneAndReplace' },
63
- { subject: Collection, method: 'findOneAndUpdate' },
64
- { subject: Collection, method: 'remove' },
65
- { subject: Collection, method: 'replaceOne' },
66
- { subject: Collection, method: 'update' },
67
- { subject: Collection, method: 'updateOne' },
68
- { subject: Collection, method: 'updateMany' },
69
- { subject: Collection, method: 'deleteOne' },
70
- { subject: Collection, method: 'deleteMany' },
71
- { subject: Db, method: 'command' },
72
- ].forEach(({ subject, method }) => {
73
- describe(`${subject.name}.prototype.${method}()`, function () {
74
- it('skips instrumentation if assess store is missing', function () {
75
- const tracked = trackString('foo');
76
- (new subject())[method]({ tracked });
77
- expect(getQueryVulnInfo).not.to.have.been.called;
78
- });
79
-
80
- it('skips instrumentation if the argument is not a string', function () {
81
- simulateRequestScope(function () {
82
- (new subject())[method]({});
83
- expect(reportFindings).not.to.have.been.called;
84
- });
85
- });
86
-
87
- it('does not report when query argument has no tracked values', function () {
88
- simulateRequestScope(function () {
89
- (new subject())[method]({ foo: 'bar' });
90
- expect(reportFindings).not.to.have.been.called;
91
- });
92
- });
93
-
94
- it('reports safe event when the input is sanitized', function () {
95
- simulateRequestScope(function () {
96
- const tracked = trackString('foo', {
97
- tags: {
98
- [LIMITED_CHARS]: [0, 2],
99
- [UNTRUSTED]: [0, 2],
100
- },
101
- });
102
- const strInfo = core.assess.dataflow.tracker.getData(tracked);
103
- (new subject())[method]({ tracked });
104
- expect(getQueryVulnInfo.getCall(0).returnValue).to.deep.equal({
105
- vulnInfo: null,
106
- reportSafe: [{ path: ['tracked'], strInfo }]
107
- });
108
- expect(reportFindings).not.to.have.been.called;
109
- });
110
- });
111
-
112
- it('reports a nosql-injection', function () {
113
- simulateRequestScope(function () {
114
- const tracked = trackString('foo');
115
- (new subject())[method]({ tracked });
116
-
117
- expect(reportFindings).to.have.been.calledWithMatch({
118
- ruleId: Rule.NOSQL_INJECTION_MONGO,
119
- sinkEvent: {
120
- args: [{ value: "{...'tracked':'foo'...}", tracked: true }],
121
- name: sinon.match(method),
122
- context: `${subject.name === 'Collection' ? 'db.collection' : 'db'}.${method}({...'tracked':'foo'...})`,
123
- moduleName: 'mongodb',
124
- methodName: `${subject.name}.prototype.${method}`,
125
- object: {
126
- value: sinon.match(subject.name),
127
- tracked: false,
128
- },
129
- result: { value: 'Promise', tracked: false },
130
- source: 'P0',
131
- tags: { [UNTRUSTED]: [15, 17] },
132
- },
133
- });
134
- });
135
- });
136
- });
137
- });
138
-
139
- describe('Db.prototype.aggregate and Collection.prototype.aggregate', function () {
140
- it('report nosql-injection from user input in the $function operator', function () {
141
- simulateRequestScope(function () {
142
- const tracked = trackString('foo');
143
- (new Collection()).aggregate([
144
- {
145
- $function: {
146
- body: tracked
147
- }
148
- }
149
- ]);
150
- (new Db()).aggregate([
151
- {
152
- $function: {
153
- body: tracked
154
- }
155
- }
156
- ]);
157
-
158
- expect(reportFindings).to.have.been.calledTwice;
159
- expect(reportFindings).to.have.been.calledWithMatch({
160
- ruleId: Rule.NOSQL_INJECTION_MONGO,
161
- sinkEvent: {
162
- args: [{ value: "[...'body':'foo'...]", tracked: true }],
163
- name: 'mongodb.Collection.prototype.aggregate',
164
- object: {
165
- value: sinon.match('Collection'),
166
- tracked: false,
167
- },
168
- result: { value: 'Promise', tracked: false },
169
- source: 'P0',
170
- tags: { [UNTRUSTED]: [12, 14] },
171
- },
172
- });
173
-
174
- expect(reportFindings).to.have.been.calledWithMatch({
175
- ruleId: Rule.NOSQL_INJECTION_MONGO,
176
- sinkEvent: {
177
- args: [{ value: "[...'body':'foo'...]", tracked: true }],
178
- name: sinon.match('aggregate'),
179
- object: {
180
- value: sinon.match('Db'),
181
- tracked: false,
182
- },
183
- result: { value: 'Promise', tracked: false },
184
- source: 'P0',
185
- tags: { [UNTRUSTED]: [12, 14] },
186
- },
187
- });
188
- });
189
- });
190
-
191
- [
192
- { prop: 'init', tagRange: [12, 14] },
193
- { prop: 'merge', tagRange: [13, 15] },
194
- { prop: 'accumulate', tagRange: [18, 20] },
195
- { prop: 'finalize', tagRange: [16, 18] }
196
- ].forEach(({ prop, tagRange }) => {
197
- it(`report nosql-injection from user input in the $accumulator operator and ${prop} property`, function () {
198
- simulateRequestScope(function () {
199
- const tracked = trackString('foo');
200
- (new Collection()).aggregate([
201
- {
202
- $accumulator: {
203
- [prop]: tracked
204
- }
205
- }
206
- ]);
207
- expect(reportFindings).to.have.been.calledWithMatch({
208
- ruleId: Rule.NOSQL_INJECTION_MONGO,
209
- sinkEvent: {
210
- args: [
211
- { value: `[...'${prop}':'foo'...]`, tracked: true }
212
- ],
213
- name: 'mongodb.Collection.prototype.aggregate',
214
- object: {
215
- value: sinon.match('Collection'),
216
- tracked: false,
217
- },
218
- result: { value: 'Promise', tracked: false },
219
- source: 'P0',
220
- tags: { [UNTRUSTED]: tagRange },
221
- },
222
- });
223
- });
224
- });
225
- });
226
-
227
- it('reports safe event when the input is sanitized', function () {
228
- simulateRequestScope(function () {
229
- const tracked = trackString('foo', {
230
- tags: {
231
- [LIMITED_CHARS]: [0, 2],
232
- [UNTRUSTED]: [0, 2],
233
- },
234
- });
235
- const strInfo = core.assess.dataflow.tracker.getData(tracked);
236
- (new Collection()).aggregate({
237
- $function: {
238
- body: tracked
239
- }
240
- });
241
- expect(getAggregateVulnInfo.getCall(0).returnValue).to.deep.equal({
242
- vulnInfo: null,
243
- reportSafe: [{ path: ['$function', 'body'], strInfo }]
244
- });
245
- expect(reportFindings).not.to.have.been.called;
246
- });
247
- });
248
- });
249
-
250
- describe('Collection.prototype.mapReduce', function () {
251
- it('report nosql-injection from user input in the first 2 arguments of `mapReduce` method', function () {
252
- const options = { options: 'untracked' };
253
- simulateRequestScope(function () {
254
- const tracked = trackString('foo');
255
- (new Collection()).mapReduce(
256
- tracked,
257
- 'untracked',
258
- options
259
- );
260
- (new Collection()).mapReduce(
261
- 'untracked',
262
- tracked,
263
- options
264
- );
265
-
266
- expect(reportFindings).to.have.been.calledTwice;
267
- expect(reportFindings).to.have.been.calledWithMatch({
268
- ruleId: Rule.NOSQL_INJECTION_MONGO,
269
- sinkEvent: {
270
- args: [
271
- { value: 'foo', tracked: true },
272
- { value: 'String', tracked: false },
273
- { value: 'Object', tracked: false },
274
-
275
- ],
276
- name: 'mongodb.Collection.prototype.mapReduce',
277
- object: {
278
- value: sinon.match('Collection'),
279
- tracked: false,
280
- },
281
- result: { value: 'Promise', tracked: false },
282
- source: 'P0',
283
- tags: { [UNTRUSTED]: [0, 2] },
284
- },
285
- });
286
- expect(reportFindings).to.have.been.calledWithMatch({
287
- ruleId: Rule.NOSQL_INJECTION_MONGO,
288
- sinkEvent: {
289
- args: [
290
- { value: 'String', tracked: false },
291
- { value: 'foo', tracked: true },
292
- { value: 'Object', tracked: false },
293
-
294
- ],
295
- name: 'mongodb.Collection.prototype.mapReduce',
296
- object: {
297
- value: sinon.match('Collection'),
298
- tracked: false,
299
- },
300
- result: { value: 'Promise', tracked: false },
301
- source: 'P1',
302
- tags: { [UNTRUSTED]: [0, 2] },
303
- },
304
- });
305
- });
306
- });
307
-
308
- it('report nosql-injection from user input in the third argument of `mapReduce` method', function () {
309
- simulateRequestScope(function () {
310
- const tracked = trackString('foo');
311
- const options1 = { query: tracked };
312
- const options2 = { finalize: tracked };
313
- (new Collection()).mapReduce(
314
- 'untracked',
315
- 'untracked',
316
- options1
317
- );
318
- (new Collection()).mapReduce(
319
- 'untracked',
320
- 'untracked',
321
- options2
322
- );
323
-
324
- expect(reportFindings).to.have.been.calledTwice;
325
- expect(reportFindings).to.have.been.calledWithMatch({
326
- ruleId: Rule.NOSQL_INJECTION_MONGO,
327
- sinkEvent: {
328
- args: [
329
- { value: 'String', tracked: false },
330
- { value: 'String', tracked: false },
331
- { value: '{...\'query\':\'foo\'...}', tracked: true },
332
- ],
333
- name: 'mongodb.Collection.prototype.mapReduce',
334
- object: {
335
- value: sinon.match('Collection'),
336
- tracked: false,
337
- },
338
- result: { value: 'Promise', tracked: false },
339
- source: 'P2',
340
- tags: { [UNTRUSTED]: [13, 15] },
341
- },
342
- });
343
- expect(reportFindings).to.have.been.calledWithMatch({
344
- ruleId: Rule.NOSQL_INJECTION_MONGO,
345
- sinkEvent: {
346
- args: [
347
- { value: 'String', tracked: false },
348
- { value: 'String', tracked: false },
349
- { value: '{...\'query\':\'foo\'...}', tracked: true },
350
-
351
- ],
352
- name: 'mongodb.Collection.prototype.mapReduce',
353
- object: {
354
- value: sinon.match('Collection'),
355
- tracked: false,
356
- },
357
- result: { value: 'Promise', tracked: false },
358
- source: 'P2',
359
- tags: { [UNTRUSTED]: [13, 15] },
360
- },
361
- });
362
- });
363
- });
364
-
365
- it('reports safe event when the input is sanitized', function () {
366
- simulateRequestScope(function () {
367
- const tracked = trackString('foo', {
368
- tags: {
369
- [LIMITED_CHARS]: [0, 2],
370
- [UNTRUSTED]: [0, 2],
371
- },
372
- });
373
- const strInfo = core.assess.dataflow.tracker.getData(tracked);
374
- (new Collection()).mapReduce(
375
- tracked, // call 0
376
- 'untracked', // call 1
377
- { some: 'options' } // call 2
378
- );
379
- (new Collection()).mapReduce(
380
- tracked, // call 3
381
- 'untracked', // call 4
382
- { query: tracked } // call 5
383
- );
384
-
385
- expect(getMapReduceVulnInfo.getCall(0).returnValue).to.deep.equal({
386
- vulnInfo: null,
387
- reportSafe: [{ strInfo }]
388
- });
389
- expect(getMapReduceVulnInfo.getCall(5).returnValue).to.deep.equal({
390
- vulnInfo: null,
391
- reportSafe: [{ path: ['query'], strInfo }]
392
- });
393
- expect(reportFindings).not.to.have.been.called;
394
- });
395
- });
396
- });
397
-
398
- describe('Collection.prototype.group', function () {
399
- it('report nosql-injection from user input in the first and the fourth argument of `group` method', function () {
400
- simulateRequestScope(function () {
401
- const tracked = trackString('foo');
402
- const condition = { some: 'condition' };
403
- const initState = { init: 'state' };
404
- (new Collection()).group(
405
- tracked,
406
- condition,
407
- initState,
408
- 'reduceFn'
409
- );
410
- (new Collection()).group(
411
- 'someKeys',
412
- condition,
413
- initState,
414
- tracked
415
- );
416
-
417
- expect(reportFindings).to.have.been.calledTwice;
418
- expect(reportFindings).to.have.been.calledWithMatch({
419
- ruleId: Rule.NOSQL_INJECTION_MONGO,
420
- sinkEvent: {
421
- args: [
422
- { value: 'foo', tracked: true },
423
- { value: 'Object', tracked: false },
424
- { value: 'Object', tracked: false },
425
- { value: 'String', tracked: false },
426
-
427
- ],
428
- name: 'mongodb.Collection.prototype.group',
429
- object: {
430
- value: sinon.match('Collection'),
431
- tracked: false,
432
- },
433
- result: { value: 'Promise', tracked: false },
434
- source: 'P0',
435
- tags: { [UNTRUSTED]: [0, 2] },
436
- },
437
- });
438
- expect(reportFindings).to.have.been.calledWithMatch({
439
- ruleId: Rule.NOSQL_INJECTION_MONGO,
440
- sinkEvent: {
441
- args: [
442
- { value: 'String', tracked: false },
443
- { value: 'Object', tracked: false },
444
- { value: 'Object', tracked: false },
445
- { value: 'foo', tracked: true },
446
- ],
447
- name: 'mongodb.Collection.prototype.group',
448
- object: {
449
- value: sinon.match('Collection'),
450
- tracked: false,
451
- },
452
- result: { value: 'Promise', tracked: false },
453
- source: 'P3',
454
- tags: { [UNTRUSTED]: [0, 2] },
455
- },
456
- });
457
- });
458
- });
459
-
460
- it('report nosql-injection from user input in the second argument of `group` method', function () {
461
- simulateRequestScope(function () {
462
- const tracked = trackString('foo');
463
- const condition = { condition: tracked };
464
- const initState = { init: 'state' };
465
- (new Collection()).group(
466
- 'someKeys',
467
- condition,
468
- initState,
469
- 'reduceFn'
470
- );
471
-
472
- expect(reportFindings).to.have.been.calledWithMatch({
473
- ruleId: Rule.NOSQL_INJECTION_MONGO,
474
- sinkEvent: {
475
- args: [
476
- { value: 'String', tracked: false },
477
- { value: '{...\'condition\':\'foo\'...}', tracked: true },
478
- { value: 'Object', tracked: false },
479
- { value: 'String', tracked: false },
480
- ],
481
- name: 'mongodb.Collection.prototype.group',
482
- object: {
483
- value: sinon.match('Collection'),
484
- tracked: false,
485
- },
486
- result: { value: 'Promise', tracked: false },
487
- source: 'P1',
488
- tags: { [UNTRUSTED]: [17, 19] },
489
- },
490
- });
491
- });
492
- });
493
-
494
- it('reports safe event when the input is sanitized', function () {
495
- simulateRequestScope(function () {
496
- const tracked = trackString('foo', {
497
- tags: {
498
- [LIMITED_CHARS]: [0, 2],
499
- [UNTRUSTED]: [0, 2],
500
- },
501
- });
502
- const strInfo = core.assess.dataflow.tracker.getData(tracked);
503
- (new Collection()).group(
504
- tracked, // call 0
505
- { some: 'condition' }, // call 1
506
- { init: 'state' },
507
- 'reduceFn' // call 2
508
- );
509
- (new Collection()).group(
510
- 'untracked', // call 3
511
- { field: tracked }, // call 4
512
- { init: 'state' },
513
- 'reduceFn'
514
- );
515
-
516
- expect(getGroupVulnInfo.getCall(0).returnValue).to.deep.equal({
517
- vulnInfo: null,
518
- reportSafe: [{ strInfo }]
519
- });
520
- expect(getGroupVulnInfo.getCall(4).returnValue).to.deep.equal({
521
- vulnInfo: null,
522
- reportSafe: [{ path: ['field'], strInfo }]
523
- });
524
- expect(reportFindings).not.to.have.been.called;
525
- });
526
- });
527
- });
528
-
529
- describe('Db.prototype.eval() and other edge cases', function () {
530
- it('skips patching if the method is missing', function () {
531
- class Collection {
532
- findOne() { }
533
- }
534
- class Db {
535
- command() { }
536
- }
537
- core.depHooks.resolve.yields({ Collection, Db }, 'v5.x.x');
538
-
539
- const instr = require('./mongodb')(core);
540
- instr.install();
541
- expect(core.logger.trace).to.have.been.calledWith(
542
- { version: 'v5.x.x' },
543
- 'method %s not found - skipping instrumentation',
544
- 'mongodb.Db.prototype.eval',
545
- );
546
- expect(core.logger.trace).to.have.been.calledWith(
547
- { version: 'v5.x.x' },
548
- 'method %s not found - skipping instrumentation',
549
- 'mongodb.Collection.prototype.findOneAndDelete',
550
- );
551
- });
552
-
553
- it('logs an error if such occurs during the evaluation', function () {
554
- const err = new Error('test');
555
-
556
- reportFindings.throws(err);
557
-
558
- simulateRequestScope(function () {
559
- const tracked = trackString('foo');
560
- (new Collection()).find({ tracked });
561
- (new Db()).command({ tracked });
562
-
563
- expect(core.logger.error).to.have.been.calledWith(
564
- { err, funcKey: 'assess-dataflow-sink:mongodb.Collection.prototype.find' },
565
- 'assess sink analysis failed',
566
- );
567
- expect(core.logger.error).to.have.been.calledWith(
568
- { err, funcKey: 'assess-dataflow-sink:mongodb.Db.prototype.command' },
569
- 'assess sink analysis failed',
570
- );
571
- });
572
- });
573
-
574
- it('reports a nosql-injection from the eval methhod', function () {
575
- simulateRequestScope(function () {
576
- const tracked = trackString('foo');
577
- (new Db()).eval(tracked);
578
-
579
- expect(reportFindings).to.have.been.calledWithMatch({
580
- ruleId: Rule.NOSQL_INJECTION_MONGO,
581
- sinkEvent: {
582
- args: [{ value: 'foo', tracked: true }],
583
- name: 'mongodb.Db.prototype.eval',
584
- moduleName: 'mongodb',
585
- methodName: 'Db.prototype.eval',
586
- context: 'db.eval(\'foo\')',
587
- object: {
588
- value: 'mongodb.Db',
589
- tracked: false,
590
- },
591
- result: { value: 'Promise', tracked: false },
592
- source: 'P0',
593
- tags: { [UNTRUSTED]: [0, 2] },
594
- },
595
- });
596
- });
597
- });
598
-
599
- it('reports safe event when the input is sanitized', function () {
600
- simulateRequestScope(function () {
601
- const tracked = trackString('foo', {
602
- tags: {
603
- [LIMITED_CHARS]: [0, 2],
604
- [UNTRUSTED]: [0, 2],
605
- },
606
- });
607
- const strInfo = core.assess.dataflow.tracker.getData(tracked);
608
- (new Db()).eval(tracked);
609
- expect(getQueryVulnInfo.getCall(0).returnValue).to.deep.equal({
610
- vulnInfo: null,
611
- reportSafe: [{ strInfo }]
612
- });
613
- expect(reportFindings).not.to.have.been.called;
614
- });
615
- });
616
- });
617
- });