@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,265 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const { initAssessFixture } = require('@contrast/test/fixtures');
5
+ const {
6
+ UtilInspect,
7
+ DataflowTag: { UNTRUSTED },
8
+ } = require('@contrast/common');
9
+ const origStringify = JSON.stringify;
10
+
11
+ describe('assess dataflow propagation JSON stringify', function () {
12
+ let core,
13
+ simulateRequestScope,
14
+ trackString,
15
+ tracker,
16
+ objWithoutTrackedStr,
17
+ objWithTrackedStr,
18
+ standardEvent;
19
+
20
+ beforeEach(function () {
21
+ ({ core, trackString, simulateRequestScope } = initAssessFixture());
22
+
23
+ tracker = core.assess.dataflow.tracker;
24
+
25
+ core.assess.dataflow.propagation.jsonInstrumentation.stringify.install();
26
+ core.assess.dataflow.propagation.stringInstrumentation.concat.install();
27
+
28
+ objWithoutTrackedStr = {
29
+ prop1: 'test',
30
+ prop2: 'test2',
31
+ arr: [1, 2, 'test3'],
32
+ nested: {
33
+ obj: 'prop',
34
+ },
35
+ };
36
+
37
+ simulateRequestScope(() => {
38
+ objWithTrackedStr = {
39
+ prop1: trackString('test'),
40
+ prop2: trackString('test2'),
41
+ arr: [1, 2, trackString('test3')],
42
+ nested: {
43
+ obj: trackString('prop'),
44
+ },
45
+ };
46
+ });
47
+
48
+ standardEvent = {
49
+ object: {
50
+ tracked: false,
51
+ value: 'JSON',
52
+ },
53
+ args: [{ tracked: false, value: 'Object' }],
54
+ context: `JSON.stringify(${origStringify(objWithTrackedStr)})`,
55
+ name: 'JSON.stringify',
56
+ moduleName: 'JSON',
57
+ methodName: 'stringify',
58
+ source: 'P0',
59
+ target: 'R',
60
+ result: {
61
+ tracked: true,
62
+ value: origStringify(objWithTrackedStr),
63
+ },
64
+ value: origStringify(objWithTrackedStr),
65
+ tags: {
66
+ [UNTRUSTED]: [10, 13, 25, 29, 44, 48, 69, 72],
67
+ },
68
+ };
69
+ });
70
+
71
+ afterEach(function () {
72
+ core.assess.dataflow.propagation.jsonInstrumentation.stringify.uninstall();
73
+ });
74
+
75
+ it('results in an untracked string when the object is with all strings untracked ', function () {
76
+ simulateRequestScope(() => {
77
+ const ret = JSON.stringify(objWithoutTrackedStr);
78
+ expect(ret).to.equal(origStringify(objWithoutTrackedStr));
79
+ expect(tracker.getData(ret)).to.be.null;
80
+ });
81
+ });
82
+
83
+ it('results in an untracked string when there is no Assess scope', function () {
84
+ simulateRequestScope(() => {
85
+ const ret = JSON.stringify(objWithTrackedStr);
86
+ expect(ret).to.equal(origStringify(objWithTrackedStr));
87
+ expect(tracker.getData(ret)).to.be.null;
88
+ }, {});
89
+ });
90
+
91
+ it('results in an untracked string when instrumentation is locked', function () {
92
+ simulateRequestScope(() => {
93
+ core.scopes.instrumentation.run({ lock: true }, () => {
94
+ const ret = JSON.stringify(objWithTrackedStr);
95
+ expect(ret).to.equal(origStringify(objWithTrackedStr));
96
+ expect(tracker.getData(ret)).to.be.null;
97
+ });
98
+ });
99
+ });
100
+
101
+ it('propagates correctly all kinds of string properties in a generic JSON.stringify call', function () {
102
+ simulateRequestScope(() => {
103
+ const ret = JSON.stringify(objWithTrackedStr);
104
+ expect(ret).to.equal(standardEvent.value);
105
+ expect(tracker.getData(ret)).to.be.like(standardEvent);
106
+ });
107
+ });
108
+
109
+ it('propagates correctly all kinds of string properties in a JSON.stringify call with predefined spaces', function () {
110
+ simulateRequestScope(() => {
111
+ const stringifyArgs = [objWithTrackedStr, (_k, v) => v, 's'];
112
+ const ret = JSON.stringify(...stringifyArgs);
113
+ expect(ret).to.equal(origStringify(...stringifyArgs));
114
+ expect(tracker.getData(ret)).to.be.like({
115
+ ...standardEvent,
116
+ context: `JSON.stringify(${origStringify(...stringifyArgs)})`,
117
+ args: [
118
+ {
119
+ tracked: false,
120
+ value: 'Object'
121
+ },
122
+ {
123
+ tracked: false,
124
+ value: 'Function',
125
+ },
126
+ {
127
+ tracked: false,
128
+ value: 'String',
129
+ }
130
+ ],
131
+ value: origStringify(...stringifyArgs),
132
+ result: {
133
+ tracked: true,
134
+ value: origStringify(...stringifyArgs),
135
+ },
136
+ tags: {
137
+ [UNTRUSTED]: [13, 16, 31, 35, 62, 66, 96, 99],
138
+ },
139
+ });
140
+ });
141
+ });
142
+
143
+ it('propagates correctly a JSON.stringify call with untrusted specified spaces', function () {
144
+ simulateRequestScope(() => {
145
+ const stringifyArgs = [
146
+ objWithoutTrackedStr,
147
+ (_k, v) => v,
148
+ trackString('user'),
149
+ ];
150
+ const ret = JSON.stringify(...stringifyArgs);
151
+
152
+ expect(ret).to.equal(origStringify(...stringifyArgs));
153
+ expect(tracker.getData(ret)).to.be.like({
154
+ ...standardEvent,
155
+ context: `JSON.stringify(${origStringify(...stringifyArgs)})`,
156
+ args: [
157
+ {
158
+ tracked: false,
159
+ value: 'Object',
160
+ },
161
+ {
162
+ tracked: false,
163
+ value: 'Function',
164
+ },
165
+ {
166
+ tracked: true,
167
+ value: UtilInspect(stringifyArgs[2]),
168
+ },
169
+ ],
170
+ value: origStringify(...stringifyArgs),
171
+ source: 'P2',
172
+ result: {
173
+ tracked: true,
174
+ value: origStringify(...stringifyArgs),
175
+ },
176
+ tags: {
177
+ [UNTRUSTED]: [
178
+ 2, 5, 23, 26, 45, 48, 58, 65, 69, 76, 80, 87, 96, 99, 103, 106, 119,
179
+ 126, 141, 144,
180
+ ],
181
+ },
182
+ });
183
+ });
184
+ });
185
+
186
+ it('propagates correctly all kind of string properties in a JSON.stringify call with untrusted specified spaces', function () {
187
+ simulateRequestScope(() => {
188
+ const stringifyArgs = [
189
+ objWithTrackedStr,
190
+ (_k, v) => v,
191
+ trackString('user'),
192
+ ];
193
+ const ret = JSON.stringify(...stringifyArgs);
194
+ expect(ret).to.equal(origStringify(...stringifyArgs));
195
+ expect(tracker.getData(ret)).to.be.like({
196
+ ...standardEvent,
197
+ context: `JSON.stringify(${origStringify(...stringifyArgs)})`,
198
+ args: [
199
+ {
200
+ tracked: false,
201
+ value: 'Object',
202
+ },
203
+ {
204
+ tracked: false,
205
+ value: 'Function',
206
+ },
207
+ {
208
+ tracked: true,
209
+ value: UtilInspect(stringifyArgs[2]),
210
+ },
211
+ ],
212
+ value: origStringify(...stringifyArgs),
213
+ source: 'P',
214
+ result: {
215
+ tracked: true,
216
+ value: origStringify(...stringifyArgs),
217
+ },
218
+ tags: {
219
+ [UNTRUSTED]: [
220
+ 2, 5, 16, 19, 23, 26, 37, 41, 45, 48, 58, 65, 69, 76, 80, 87, 89,
221
+ 93, 96, 99, 103, 106, 119, 126, 135, 138, 141, 144,
222
+ ],
223
+ },
224
+ });
225
+ });
226
+ });
227
+
228
+ describe('some edge cases', function () {
229
+ it('handles cases when the second argument is an array', function () {
230
+ simulateRequestScope(() => {
231
+ const stringifyArgs = [objWithTrackedStr, ['arr', 'nested', 'obj']];
232
+ const ret = JSON.stringify(...stringifyArgs);
233
+ expect(ret).to.equal(origStringify(...stringifyArgs));
234
+ expect(tracker.getData(ret).tags).to.deep.equal({
235
+ [UNTRUSTED]: [13, 17, 38, 41],
236
+ });
237
+ });
238
+ });
239
+
240
+ it('handles cases when the third argument is not entirely tagged', function () {
241
+ simulateRequestScope(() => {
242
+ const concatSpaces = 'd'.concat(
243
+ trackString('us'),
244
+ 'd',
245
+ trackString('us')
246
+ );
247
+ const stringifyArgs = [
248
+ objWithoutTrackedStr,
249
+ (_k, v) => v,
250
+ concatSpaces,
251
+ ];
252
+ const ret = JSON.stringify(...stringifyArgs);
253
+ expect(ret).to.equal(origStringify(...stringifyArgs));
254
+ expect(tracker.getData(ret).tags).to.deep.equal({
255
+ [UNTRUSTED]: [
256
+ 3, 4, 6, 7, 26, 27, 29, 30, 50, 51, 53, 54, 65, 66, 68, 69, 71, 72,
257
+ 74, 75, 80, 81, 83, 84, 86, 87, 89, 90, 95, 96, 98, 99, 101, 102,
258
+ 104, 105, 115, 116, 118, 119, 124, 125, 127, 128, 142, 143, 145,
259
+ 146, 148, 149, 151, 152, 168, 169, 171, 172,
260
+ ],
261
+ });
262
+ });
263
+ });
264
+ });
265
+ });
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('assess dataflow propagation array join', function () {
8
+ let core, trackString, simulateRequestScope, tracker;
9
+
10
+ beforeEach(function () {
11
+ ({
12
+ core,
13
+ simulateRequestScope,
14
+ trackString
15
+ } = initAssessFixture());
16
+
17
+ tracker = core.assess.dataflow.tracker;
18
+ core.assess.dataflow.propagation.arrayJoin.install();
19
+ });
20
+
21
+ afterEach(function () {
22
+ core.assess.dataflow.propagation.arrayJoin.uninstall();
23
+ sinon.resetHistory();
24
+ });
25
+
26
+ [
27
+ {
28
+ expected: { untrusted: [12, 18, 38, 51] }
29
+ },
30
+ {
31
+ separator: 'not-tracked',
32
+ expected: { untrusted: [22, 28, 68, 81] }
33
+ },
34
+ {
35
+ separator: 'tracked-sep',
36
+ tags: {
37
+ untrusted: [0, 10]
38
+ },
39
+ expected: { untrusted: [11, 39, 57, 81] }
40
+ }
41
+ ].forEach(({ separator, tags, expected }) => {
42
+ it('propagates correctly', function () {
43
+ simulateRequestScope(function () {
44
+ const arr = [
45
+ 'not-tracked',
46
+ trackString('tracked', { tags: { untrusted: [0, 6] } }),
47
+ 'another-untracked',
48
+ trackString('another-tracked', { tags: { untrusted: [0, 13] } })
49
+ ];
50
+ let source = 'O';
51
+
52
+ if (tags) {
53
+ separator = trackString(separator, { tags });
54
+ source = 'A';
55
+ }
56
+
57
+ const result = arr.join(separator);
58
+ const strInfo = tracker.getData(result);
59
+ expect(strInfo?.tags).to.deep.equal(expected);
60
+ expect(strInfo?.source).to.equal(source);
61
+ });
62
+ });
63
+ });
64
+
65
+ it('propagates correclty nested arrays', function () {
66
+ simulateRequestScope(function () {
67
+ const str = trackString('tracked', { tags: { untrusted: [0, 6] } });
68
+ const arr = [
69
+ 'not-tracked',
70
+ [
71
+ 'not-tracked',
72
+ str
73
+ ],
74
+ [
75
+ 'not-tracked',
76
+ [
77
+ 'not-tracked',
78
+ str
79
+ ]
80
+ ]
81
+ ];
82
+
83
+ const result = arr.join();
84
+ const resultInfo = tracker.getData(result);
85
+ expect(resultInfo?.tags).to.deep.equal({ untrusted: [24, 30, 56, 62] });
86
+ });
87
+ });
88
+
89
+ it('will not propagate if there is no assess context', function () {
90
+ simulateRequestScope(function () {
91
+ const value = trackString('foo');
92
+ const result = ['not-tracked', value].join();
93
+ expect(tracker.getData(result)).to.be.null;
94
+ }, {});
95
+ });
96
+
97
+ it('will not propagate if there instrumentation is locked', function () {
98
+ simulateRequestScope(function () {
99
+ core.scopes.instrumentation.run({ lock: true }, function () {
100
+ const value = trackString('foo');
101
+ const result = ['not-tracked', value].join();
102
+ expect(tracker.getData(result)).to.be.null;
103
+ });
104
+ });
105
+ });
106
+ });
@@ -0,0 +1,109 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('assess dataflow propagation Buffer', function () {
8
+ let core, simulateRequestScope, tracker, trackString, createSourceEvent;
9
+
10
+ beforeEach(function () {
11
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
12
+
13
+ tracker = core.assess.dataflow.tracker;
14
+ createSourceEvent = core.assess.eventFactory.createSourceEvent;
15
+ core.assess.dataflow.propagation.bufferInstrumentation.install();
16
+ });
17
+
18
+ afterEach(function () {
19
+ core.assess.dataflow.propagation.arrayJoin.uninstall();
20
+ sinon.resetHistory();
21
+ });
22
+
23
+ describe('Buffer.prototype.toString', function () {
24
+ [
25
+ 'utf8',
26
+ 'latin1',
27
+ 'ascii',
28
+ // todo
29
+ ].forEach((encoding) => {
30
+ const value = 'foo';
31
+
32
+ it(`value when buffer is tracked (${encoding})`, function () {
33
+ simulateRequestScope(() => {
34
+ const b = Buffer.from(value, encoding);
35
+ const event = createSourceEvent({
36
+ name: 'body-parser-mock',
37
+ result: { tracked: true, value: b },
38
+ inputType: 'BODY',
39
+ tags: {
40
+ UNTRUSTED: [0, 2],
41
+ },
42
+ stack: [],
43
+ });
44
+
45
+ tracker.track(b, event);
46
+ expect(tracker.getData(b.toString(encoding))).to.deep.include({
47
+ addedTags: [],
48
+ args: [{ tracked: false, value: encoding }],
49
+ moduleName: 'Buffer',
50
+ methodName: 'prototype.toString',
51
+ context: 'buffer.toString()',
52
+ name: 'global.Buffer.prototype.toString',
53
+ object: { tracked: true, value: 'Buffer' },
54
+ removedTags: [],
55
+ result: { tracked: true, value },
56
+ source: 'O',
57
+ stack: [],
58
+ tags: {
59
+ UNTRUSTED: [0, 2]
60
+ },
61
+ target: 'R',
62
+ value,
63
+ });
64
+ });
65
+ });
66
+ });
67
+ });
68
+
69
+ describe('Buffer.from', function () {
70
+ // todo: implementation for other types. see https://nodejs.org/api/buffer.html
71
+
72
+ const trkInfoCommon = {
73
+ object: { tracked: true, value: 'Buffer' },
74
+ source: 'P0',
75
+ target: 'R',
76
+ tags: { UNTRUSTED: [0, 7] },
77
+ };
78
+
79
+ [
80
+ {
81
+ getArgs: () => trackString('a string'),
82
+ argType: 'string',
83
+ expected: {
84
+ ...trkInfoCommon,
85
+ context: 'Buffer.from(a string)',
86
+ args: [{ tracked: true, value: 'a string' }],
87
+ }
88
+ },
89
+ {
90
+ getArgs: () => Buffer.from(trackString('a buffer')),
91
+ argType: 'buffer',
92
+ expected: {
93
+ ...trkInfoCommon,
94
+ context: 'Buffer.from(a buffer)',
95
+ args: [{ tracked: true, value: 'a buffer' }],
96
+ }
97
+ },
98
+
99
+ ].forEach(({ argType, getArgs, expected }) => {
100
+ it(`tracks result when argument is tracked (arg type ${argType})`, function () {
101
+ simulateRequestScope(() => {
102
+ const b = Buffer.from(getArgs());
103
+ const trkInfo = tracker.getData(b);
104
+ expect(trkInfo).to.deep.include(expected);
105
+ });
106
+ });
107
+ });
108
+ });
109
+ });
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ DataflowTag: {
5
+ UNTRUSTED,
6
+ }
7
+ } = require('@contrast/common');
8
+ const { expect } = require('chai');
9
+ const sinon = require('sinon');
10
+ const { initAssessFixture } = require('@contrast/test/fixtures');
11
+
12
+ describe('assess dataflow propagation contrast-methods add', function () {
13
+ let core, simulateRequestScope, trackString, tracker;
14
+
15
+ beforeEach(function () {
16
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
17
+ tracker = core.assess.dataflow.tracker;
18
+
19
+ core.contrastMethods.install();
20
+ core.assess.dataflow.propagation.contrastMethodsInstrumentation.add.install();
21
+ });
22
+
23
+ afterEach(function () {
24
+ core.assess.dataflow.propagation.contrastMethodsInstrumentation.add.uninstall();
25
+ });
26
+
27
+ const foo = () => trackString('foo');
28
+ const bar = () => 'bar';
29
+
30
+ [
31
+ {
32
+ name: 'tracked A, untracked B',
33
+ args: [foo, bar],
34
+ expected: {
35
+ [UNTRUSTED]: [0, 2],
36
+ },
37
+ },
38
+ {
39
+ name: 'tracked A, tracked B',
40
+ args: [foo, foo],
41
+ expected: {
42
+ [UNTRUSTED]: [0, 5],
43
+ },
44
+ },
45
+ {
46
+ name: 'untracked A, tracked B',
47
+ args: [bar, foo],
48
+ expected: {
49
+ [UNTRUSTED]: [3, 5],
50
+ },
51
+ },
52
+ {
53
+ name: 'empty A, tracked B',
54
+ args: [() => '', foo],
55
+ expected: {
56
+ [UNTRUSTED]: [0, 2],
57
+ },
58
+ },
59
+ ].forEach(({ name, args, expected }) => {
60
+ it(`propagates ${name} correctly`, function () {
61
+ simulateRequestScope(function () {
62
+ const result = global.ContrastMethods.add(...args.map(Reflect.apply));
63
+
64
+ expect(tracker.getData(result))
65
+ .to.have.property('tags')
66
+ .that.deep.equals(expected);
67
+ });
68
+ });
69
+ });
70
+
71
+ it('will not propagate if there is no result or not in proper source scope', function () {
72
+ const { extern } = tracker.track('foo', { tags: { UNTRUSTED: [0, 2] } });
73
+
74
+ sinon.spy(tracker, 'track');
75
+ sinon.spy(tracker, 'getData');
76
+
77
+ core.scopes.sources.run({}, function () {
78
+ global.ContrastMethods.add('', '');
79
+ });
80
+
81
+ core.scopes.sources.run(null, function () {
82
+ global.ContrastMethods.add(extern, 'bar');
83
+ });
84
+
85
+ core.scopes.instrumentation.run({ lock: true }, function () {
86
+ core.scopes.sources.run({}, function () {
87
+ global.ContrastMethods.add(extern, 'bar');
88
+ });
89
+ });
90
+
91
+ expect(tracker.track).not.to.have.been.called;
92
+ expect(tracker.getData).not.to.have.been.called;
93
+ });
94
+ });
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('assess dataflow propagation contrast-methods', function () {
8
+ let core, instr;
9
+
10
+ const propagators = ['add', 'tag', 'number'];
11
+
12
+ beforeEach(function () {
13
+ ({ core } = initAssessFixture());
14
+ instr = core.assess.dataflow.propagation.contrastMethodsInstrumentation;
15
+ });
16
+
17
+ it('composes different installers', function () {
18
+ propagators.forEach((name) => {
19
+ expect(instr[name]).to.have.property('install').and.be.a('function');
20
+ expect(instr[name]).to.have.property('uninstall').and.be.a('function');
21
+ });
22
+ });
23
+
24
+ it('dispatches installation to sub-components', function () {
25
+ const installs = [];
26
+ const uninstalls = [];
27
+ // stub these to prevent side effects
28
+ propagators.forEach((name) => {
29
+ const instr =
30
+ core.assess.dataflow.propagation.contrastMethodsInstrumentation;
31
+ installs.push(sinon.stub(instr[name], 'install'));
32
+ uninstalls.push(sinon.stub(instr[name], 'uninstall'));
33
+ });
34
+
35
+ instr.install();
36
+
37
+ expect(installs).to.have.lengthOf(propagators.length);
38
+ installs.forEach((installer) => {
39
+ expect(installer).to.have.been.called;
40
+ });
41
+
42
+ instr.uninstall();
43
+
44
+ expect(uninstalls).to.have.lengthOf(propagators.length);
45
+ uninstalls.forEach((installer) => {
46
+ expect(installer).to.have.been.called;
47
+ });
48
+ });
49
+ });
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const { initAssessFixture } = require('@contrast/test/fixtures');
5
+
6
+ describe('assess dataflow propagation contrast-methods number', function () {
7
+ let core, simulateRequestScope, trackString, tracker;
8
+
9
+ beforeEach(function () {
10
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
11
+ tracker = core.assess.dataflow.tracker;
12
+ core.contrastMethods.install();
13
+ core.assess.dataflow.propagation.contrastMethodsInstrumentation.number.install();
14
+ });
15
+
16
+ afterEach(function () {
17
+ core.assess.dataflow.propagation.contrastMethodsInstrumentation.number.uninstall();
18
+ });
19
+
20
+ it('will not sanitize if Number coercion fails', function () {
21
+ simulateRequestScope(() => {
22
+ const str = trackString('foo');
23
+ global.ContrastMethods.Number(str);
24
+ expect(tracker.getData(str)).to.deep.include({
25
+ context: 'UNKNOWN.val',
26
+ name: 'assess-dataflow-fixture',
27
+ fieldName: 'val',
28
+ pathName: 'val',
29
+ stack: [],
30
+ inputType: 'UNKNOWN',
31
+ tags: { UNTRUSTED: [0, 2] },
32
+ result: { tracked: true, value: 'foo' },
33
+ });
34
+ });
35
+ });
36
+
37
+ it('will untrack if Number coercion succeeds', function () {
38
+ simulateRequestScope(() => {
39
+ const str = trackString('30000');
40
+ global.ContrastMethods.Number(str);
41
+ expect(tracker.getData(str)).to.be.null;
42
+ expect(core.logger.trace).to.have.been.calledWithMatch({
43
+ funcKey: 'assess-dataflow-propagator:ContrastMethods.Number',
44
+ sanitizer: 'ContrastMethods.Number',
45
+ value: '30000',
46
+ }, 'untracked a string value');
47
+
48
+ });
49
+ });
50
+ });