@contrast/assess 1.31.0 → 1.33.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.test.js +210 -0
  116. package/lib/dataflow/sources/install/fastify/index.test.js +33 -0
  117. package/lib/dataflow/sources/install/formidable1.test.js +119 -0
  118. package/lib/dataflow/sources/install/hapi/hapi.test.js +172 -0
  119. package/lib/dataflow/sources/install/hapi/index.test.js +33 -0
  120. package/lib/dataflow/sources/install/http.test.js +155 -0
  121. package/lib/dataflow/sources/install/koa/index.test.js +40 -0
  122. package/lib/dataflow/sources/install/koa/koa-bodyparsers.test.js +161 -0
  123. package/lib/dataflow/sources/install/koa/koa-multer.test.js +197 -0
  124. package/lib/dataflow/sources/install/koa/koa-routers.test.js +146 -0
  125. package/lib/dataflow/sources/install/koa/koa2.test.js +145 -0
  126. package/lib/dataflow/sources/install/multer1.test.js +145 -0
  127. package/lib/dataflow/sources/install/qs6.test.js +131 -0
  128. package/lib/dataflow/sources/install/querystring.test.js +82 -0
  129. package/lib/dataflow/sources/install/restify/fieldedTextBodyParser.test.js +88 -0
  130. package/lib/dataflow/sources/install/restify/index.test.js +38 -0
  131. package/lib/dataflow/sources/install/restify/jsonBodyParser.test.js +144 -0
  132. package/lib/dataflow/sources/install/restify/router.test.js +83 -0
  133. package/lib/dataflow/tag-utils-complete.test.js +27 -0
  134. package/lib/dataflow/tag-utils.test.js +192 -0
  135. package/lib/dataflow/tracker.js +7 -2
  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.js +10 -1
  142. package/lib/get-source-context.test.js +108 -0
  143. package/lib/index.test.js +41 -0
  144. package/lib/make-source-context.test.js +50 -0
  145. package/lib/response-scanning/handlers/index.test.js +425 -0
  146. package/lib/response-scanning/handlers/utils.test.js +391 -0
  147. package/lib/response-scanning/index.test.js +41 -0
  148. package/lib/response-scanning/install/http.test.js +175 -0
  149. package/lib/rule-scopes.test.js +27 -0
  150. package/lib/session-configuration/handlers.test.js +84 -0
  151. package/lib/session-configuration/index.test.js +36 -0
  152. package/lib/session-configuration/install/express-session.test.js +220 -0
  153. package/lib/session-configuration/install/fastify-cookie.test.js +65 -0
  154. package/lib/session-configuration/install/hapi.test.js +269 -0
  155. package/lib/session-configuration/install/koa.test.js +92 -0
  156. package/package.json +2 -2
@@ -0,0 +1,103 @@
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 string', function () {
8
+ let core, trackString, simulateRequestScope, instr;
9
+
10
+ beforeEach(function () {
11
+ ({ core, trackString, simulateRequestScope } = initAssessFixture());
12
+ instr = core.assess.dataflow.propagation.stringInstrumentation;
13
+ });
14
+
15
+ const propagatorList = ['concat', 'replace', 'substring', 'trim'];
16
+
17
+ it('composes different installers', function () {
18
+ propagatorList.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
+ propagatorList.forEach((name) => {
29
+ const instr = core.assess.dataflow.propagation.stringInstrumentation;
30
+ installs.push(sinon.stub(instr[name], 'install'));
31
+ uninstalls.push(sinon.stub(instr[name], 'uninstall'));
32
+ });
33
+
34
+ instr.install();
35
+ instr.uninstall();
36
+
37
+ expect(installs).to.have.lengthOf(4);
38
+ installs.forEach((installer) => {
39
+ expect(installer).to.have.been.called;
40
+ });
41
+ expect(uninstalls).to.have.lengthOf(4);
42
+ uninstalls.forEach((installer) => {
43
+ expect(installer).to.have.been.called;
44
+ });
45
+ });
46
+
47
+ describe('utils patchCustomMatcher', function () {
48
+ let tracker, arg, patchedFn, argInfo;
49
+ const customFn = (str) => str;
50
+ const methodCustomArg = {
51
+ [Symbol.match]: customFn,
52
+ };
53
+
54
+ beforeEach(function () {
55
+ tracker = core.assess.dataflow.tracker;
56
+ simulateRequestScope(function () {
57
+ arg = trackString('foo');
58
+ argInfo = tracker.getData(arg);
59
+ patchedFn = instr.utils.patchCustomMatcher(
60
+ methodCustomArg[Symbol.match],
61
+ argInfo,
62
+ methodCustomArg,
63
+ 'String.prototype.match',
64
+ 'testPatch'
65
+ );
66
+ });
67
+ });
68
+
69
+ it('places a propagation event on the function\'s argument', function () {
70
+ simulateRequestScope(function () {
71
+ const res = patchedFn(arg);
72
+ const resInfo = tracker.getData(res);
73
+
74
+ expect(resInfo).to.not.equal(argInfo);
75
+ expect(resInfo.context).to.equal('\'foo\'.match(Object)');
76
+ expect(resInfo.history).to.deep.equal([argInfo]);
77
+ expect(resInfo.name).to.equal('String.prototype.match');
78
+ expect(resInfo.methodName).to.equal('prototype.match');
79
+ expect(resInfo.object).to.deep.equal({
80
+ value: argInfo.value,
81
+ tracked: true,
82
+ });
83
+ expect(resInfo.tags).to.deep.equal(argInfo.tags);
84
+ });
85
+ });
86
+
87
+ it('does not create a propagation event if the function is called out of request scope', function () {
88
+ const res = patchedFn(arg);
89
+
90
+ expect(tracker.getData(res)).to.equal(argInfo);
91
+ });
92
+
93
+ it('does not create a propagation event if the function is called with locked instrumentation', function () {
94
+ simulateRequestScope(function () {
95
+ core.scopes.instrumentation.run({ lock: true }, () => {
96
+ const res = patchedFn(arg);
97
+
98
+ expect(tracker.getData(res)).to.equal(argInfo);
99
+ });
100
+ });
101
+ });
102
+ });
103
+ });
@@ -0,0 +1,399 @@
1
+ 'use strict';
2
+
3
+ const { DataflowTag: { UNTRUSTED } } = require('@contrast/common');
4
+ const { expect } = require('chai');
5
+ const { inspect } = require('util');
6
+ const { initAssessFixture } = require('@contrast/test/fixtures');
7
+
8
+
9
+ function createItem(idx, itemValue = [''], input = 'foo', groups = undefined) {
10
+ const item = [...itemValue];
11
+ item.index = idx;
12
+ item.input = input;
13
+ item.groups = groups;
14
+
15
+ return item;
16
+ }
17
+
18
+ describe('assess dataflow propagation string matchAll', function () {
19
+ let core, simulateRequestScope, trackString, tracker;
20
+
21
+ beforeEach(function () {
22
+ ({
23
+ core,
24
+ trackString,
25
+ simulateRequestScope
26
+ } = initAssessFixture());
27
+
28
+ tracker = core.assess.dataflow.tracker;
29
+
30
+ core.assess.dataflow.propagation.regExpExec.install();
31
+ core.assess.dataflow.propagation.stringInstrumentation.matchAll.install();
32
+ core.assess.dataflow.propagation.stringInstrumentation.substring.install();
33
+ });
34
+
35
+ afterEach(function () {
36
+ core.assess.dataflow.propagation.regExpExec.uninstall();
37
+ core.assess.dataflow.propagation.stringInstrumentation.matchAll.uninstall();
38
+ core.assess.dataflow.propagation.stringInstrumentation.substring.uninstall();
39
+ });
40
+
41
+ describe('base cases', function () {
42
+ [
43
+ [],
44
+ [undefined],
45
+ ['']
46
+ ].forEach((val) => {
47
+ it(`returns empty strings from matchAll for ${inspect(val[0])}`, function () {
48
+ simulateRequestScope(() => {
49
+
50
+ const ret = 'foo'.matchAll(val[0]);
51
+
52
+ expect([...ret]).to.deep.equal([
53
+ createItem(0),
54
+ createItem(1),
55
+ createItem(2),
56
+ createItem(4)
57
+ ]);
58
+ expect(tracker.getData(ret[0])).to.be.null;
59
+ });
60
+ });
61
+ });
62
+
63
+ it('returns null when no matchAll is found', function () {
64
+ simulateRequestScope(() => {
65
+ const ret = ''.matchAll('foo');
66
+
67
+ expect([...ret]).to.deep.equal([]);
68
+ });
69
+ });
70
+
71
+ it('returns first match string', function () {
72
+ simulateRequestScope(() => {
73
+ const ret = 'foo'.matchAll(/f/g);
74
+ const consumed = [...ret];
75
+
76
+ expect(consumed).to.deep.equal([createItem(0, 'f')]);
77
+ expect(tracker.getData(consumed[0[0]])).to.be.null;
78
+ });
79
+ });
80
+
81
+ it('returns all match strings', function () {
82
+ simulateRequestScope(() => {
83
+ const ret = 'foo'.matchAll(/o/g);
84
+ const consumed = [...ret];
85
+
86
+ expect(consumed).to.deep.equal([
87
+ createItem(0, 'o'),
88
+ createItem(1, 'o'),
89
+ ]);
90
+ consumed.forEach((val) => {
91
+ expect(tracker.getData(val[0])).to.be.null;
92
+ });
93
+ });
94
+ });
95
+
96
+ it('converts string to regex and returns matchAll result', function () {
97
+ simulateRequestScope(() => {
98
+ const ret = 'foo'.matchAll('f');
99
+ const consumed = [...ret];
100
+
101
+ expect(consumed).to.deep.equal([createItem(0, 'f')]);
102
+ expect(tracker.getData(consumed[0[0]])).to.be.null;
103
+ });
104
+ });
105
+
106
+ it('returns match from custom matcher', function () {
107
+ simulateRequestScope(() => {
108
+ const ret = 'foo'.matchAll({
109
+ [Symbol.matchAll](_str) {
110
+ return ['foo'];
111
+ },
112
+ });
113
+
114
+ expect(ret).to.deep.equal(['foo']);
115
+ expect(tracker.getData(ret[0])).to.be.null;
116
+ });
117
+ });
118
+ });
119
+
120
+ describe('propagation', function () {
121
+ it('keeps track of tracked of all globally matched strings', function () {
122
+ simulateRequestScope(() => {
123
+ const extern = trackString('foobarfoo');
124
+
125
+ const ret = extern.matchAll(/foo/g);
126
+ const consumed = [...ret];
127
+
128
+ expect(consumed).to.deep.equal([
129
+ createItem(0, ['foo'], 'foobarfoo'),
130
+ createItem(0, ['foo'], 'foobarfoo')
131
+ ]);
132
+ consumed.forEach((val) => {
133
+ const valInfo = tracker.getData(val[0]);
134
+
135
+ expect(valInfo.tags).to.deep.equal({
136
+ [UNTRUSTED]: [0, 2]
137
+ });
138
+ expect(valInfo.name).to.equal('String.prototype.matchAll');
139
+ });
140
+ });
141
+ });
142
+
143
+ it('keeps track of partially tracked matched strings', function () {
144
+ simulateRequestScope(() => {
145
+ const extern = trackString('foobarfoo', {
146
+ tags: {
147
+ [UNTRUSTED]: [0, 5]
148
+ }
149
+ });
150
+
151
+ const ret = extern.matchAll(/foo/g);
152
+ const consumed = [...ret];
153
+ const firstArgInfo = tracker.getData(consumed[0][0]);
154
+
155
+ expect(consumed).to.deep.equal([
156
+ createItem(0, ['foo'], 'foobarfoo'),
157
+ createItem(0, ['foo'], 'foobarfoo')
158
+ ]);
159
+ expect(firstArgInfo.tags).to.deep.equal({
160
+ [UNTRUSTED]: [0, 2]
161
+ });
162
+ expect(firstArgInfo.name).to.equal('String.prototype.matchAll');
163
+ expect(tracker.getData(consumed[1][0])).to.be.null;
164
+ });
165
+ });
166
+
167
+ it('keeps track of tracked strings with multiple ranges', function () {
168
+ simulateRequestScope(() => {
169
+ const extern = trackString('foobarfoo', {
170
+ tags: {
171
+ foo: [0, 2, 6, 8],
172
+ bar: [3, 5],
173
+ foobar: [0, 5],
174
+ barfoo: [3, 8]
175
+ }
176
+ });
177
+
178
+ const ret = extern.matchAll(/foo/g);
179
+ const consumed = [...ret];
180
+ const retFirstArgInfo = tracker.getData(consumed[0][0]);
181
+ const retSecondArgInfo = tracker.getData(consumed[1][0]);
182
+
183
+ expect(consumed).to.deep.equal([
184
+ createItem(0, ['foo'], 'foobarfoo'),
185
+ createItem(0, ['foo'], 'foobarfoo')
186
+ ]);
187
+ expect(retFirstArgInfo.tags).to.deep.equal({
188
+ foo: [0, 2],
189
+ foobar: [0, 2]
190
+ });
191
+ expect(retFirstArgInfo.name).to.equal('String.prototype.matchAll');
192
+ expect(retSecondArgInfo.tags).to.deep.equal({
193
+ foo: [0, 2],
194
+ barfoo: [0, 2]
195
+ });
196
+ expect(retSecondArgInfo.name).to.equal('String.prototype.matchAll');
197
+ });
198
+ });
199
+
200
+ describe('empty or full matches', function () {
201
+ it('handles full string matches', function () {
202
+ simulateRequestScope(() => {
203
+ const extern = trackString('foobar');
204
+
205
+ const ret = extern.matchAll(/(?<full>foobar)/g);
206
+ const consumed = [...ret];
207
+ const matchInfo = tracker.getData(consumed[0][0]);
208
+ const captrueMatchInfo = tracker.getData(consumed[0][1]);
209
+ const fullGroupInfo = tracker.getData(consumed[0].groups.full);
210
+
211
+ expect(consumed[0]).to.deep.equal([
212
+ 'foobar',
213
+ 'foobar',
214
+ ]);
215
+ expect(matchInfo.tags).to.deep.equal({
216
+ [UNTRUSTED]: [0, 5]
217
+ });
218
+ expect(matchInfo.name).to.equal('String.prototype.matchAll');
219
+ expect(captrueMatchInfo.tags).to.deep.equal({
220
+ [UNTRUSTED]: [0, 5]
221
+ });
222
+ expect(captrueMatchInfo.name).to.equal('String.prototype.matchAll');
223
+ expect(consumed[0].groups.full).to.equal('foobar');
224
+ expect(fullGroupInfo.tags).to.deep.equal({
225
+ [UNTRUSTED]: [0, 5]
226
+ });
227
+ expect(fullGroupInfo.name).to.equal('String.prototype.matchAll');
228
+ });
229
+ });
230
+
231
+ it('handles empty string matches', function () {
232
+ simulateRequestScope(() => {
233
+ const extern = trackString('foobar');
234
+
235
+ const ret = extern.matchAll(/(?<empty>)/g);
236
+ const consumed = [...ret];
237
+ const matchInfo = tracker.getData(consumed[0][0]);
238
+ const captrueMatchInfo = tracker.getData(consumed[0][1]);
239
+ const emptyGroupInfo = tracker.getData(consumed[0].groups.empty);
240
+
241
+ expect(consumed[0]).to.deep.equal([
242
+ '',
243
+ '',
244
+ ]);
245
+ expect(matchInfo).to.be.null;
246
+ expect(captrueMatchInfo).to.be.null;
247
+ expect(consumed[0].groups.empty).to.equal('');
248
+ expect(emptyGroupInfo).to.be.null;
249
+ });
250
+ });
251
+ });
252
+
253
+ describe('capture groups', function () {
254
+ it('keeps track of tracked strings within matched capture groups', function () {
255
+ simulateRequestScope(() => {
256
+ const extern = trackString('foobar', {
257
+ tags: {
258
+ [UNTRUSTED]: [0, 5]
259
+ }
260
+ });
261
+
262
+ const ret = extern.matchAll(/(foo)|(bar)/g);
263
+ const consumed = [...ret];
264
+ const retFirstArgInfo = tracker.getData(consumed[0][0]);
265
+ const retSecondArgInfo = tracker.getData(consumed[1][0]);
266
+
267
+ expect(consumed).to.deep.equal([
268
+ createItem(0, ['foo', 'foo', undefined], 'foobarfoo'),
269
+ createItem(3, ['bar', undefined, 'bar'], 'foobarfoo'),
270
+ ]);
271
+ expect(ret.groups).to.be.undefined;
272
+ expect(retFirstArgInfo.tags).to.deep.equal({
273
+ [UNTRUSTED]: [0, 2]
274
+ });
275
+ expect(retFirstArgInfo.name).to.equal('String.prototype.matchAll');
276
+ expect(retSecondArgInfo.tags).to.deep.equal({
277
+ [UNTRUSTED]: [0, 2]
278
+ });
279
+ expect(retSecondArgInfo.name).to.equal('String.prototype.matchAll');
280
+ });
281
+ });
282
+
283
+ it('keeps track of tracked strings within matched named capture groups', function () {
284
+ simulateRequestScope(() => {
285
+ const extern = trackString('foobar', {
286
+ tags: {
287
+ [UNTRUSTED]: [0, 5]
288
+ }
289
+ });
290
+
291
+ const ret = extern.matchAll(/(?<fooOrBar>foo|bar)/g);
292
+ const consumed = [...ret];
293
+
294
+ const infos = [
295
+ tracker.getData(consumed[0][0]),
296
+ tracker.getData(consumed[0][1]),
297
+ tracker.getData(consumed[1][0]),
298
+ tracker.getData(consumed[1][1]),
299
+ tracker.getData(consumed[0].groups.fooOrBar),
300
+ tracker.getData(consumed[1].groups.fooOrBar)
301
+ ];
302
+
303
+ expect(consumed).to.deep.equal([
304
+ createItem(0, ['foo', 'foo'], 'foobar', { fooOrBar: 'foo' }),
305
+ createItem(0, ['bar', 'bar'], 'foobar', { fooOrBar: 'bar' })
306
+ ]);
307
+ expect(consumed[0].groups.fooOrBar).to.equal('foo');
308
+ expect(consumed[1].groups.fooOrBar).to.equal('bar');
309
+ infos.forEach((info) => {
310
+ expect(info.tags).to.deep.equal({
311
+ [UNTRUSTED]: [0, 2]
312
+ });
313
+ expect(info.name).to.equal('String.prototype.matchAll');
314
+ });
315
+ });
316
+ });
317
+
318
+ it('keeps track of tracked strings within multiple matched named capture groups', function () {
319
+ simulateRequestScope(() => {
320
+ const extern = trackString('foobar', {
321
+ tags: {
322
+ [UNTRUSTED]: [0, 5]
323
+ }
324
+ });
325
+
326
+ const ret = extern.matchAll(/(?<fizzOrBuzz>fizz|buzz)|(?<fooOrBar>foo|bar)/g);
327
+ const consumed = [...ret];
328
+ const infos = [
329
+ tracker.getData(consumed[0][0]),
330
+ tracker.getData(consumed[0][2]),
331
+ tracker.getData(consumed[1][0]),
332
+ tracker.getData(consumed[1][2]),
333
+ tracker.getData(consumed[0].groups.fooOrBar),
334
+ tracker.getData(consumed[1].groups.fooOrBar)
335
+ ];
336
+
337
+ expect(consumed).to.deep.equal([
338
+ createItem(0, ['foo', undefined, 'foo'], 'foobar', { fizzOrBuzz: undefined, fooOrBar: 'foo' }),
339
+ createItem(3, ['bar', undefined, 'bar'], 'foobar', { fizzOrBuzz: undefined, fooOrBar: 'bar' }),
340
+ ]);
341
+ expect(consumed[0].groups).to.deep.equal({ fizzOrBuzz: undefined, fooOrBar: 'foo' });
342
+ expect(consumed[1].groups).to.deep.equal({ fizzOrBuzz: undefined, fooOrBar: 'bar' });
343
+ infos.forEach((info) => {
344
+ expect(info.tags).to.deep.equal({
345
+ [UNTRUSTED]: [0, 2]
346
+ });
347
+ expect(info.name).to.equal('String.prototype.matchAll');
348
+ });
349
+ });
350
+ });
351
+ });
352
+
353
+ describe('custom matcher', function () {
354
+ it('keeps track of tracked string returned by custom matcher', function () {
355
+ simulateRequestScope(() => {
356
+ const extern = trackString('foo', {
357
+ tags: {
358
+ untrusted: [0, 2]
359
+ }
360
+ });
361
+
362
+ const ret = 'foo'.matchAll({
363
+ [Symbol.matchAll](_str) {
364
+ return [extern];
365
+ },
366
+ });
367
+ expect(ret).to.deep.equal(['foo']);
368
+ expect(tracker.getData(ret[0]).tags).to.deep.equal({
369
+ untrusted: [0, 2]
370
+ });
371
+ });
372
+ });
373
+
374
+ it('keeps track of tracked string copied by custom matcher', function () {
375
+ simulateRequestScope(() => {
376
+ const extern = trackString('foobar', {
377
+ tags: {
378
+ untrusted: [0, 5]
379
+ }
380
+ });
381
+
382
+ const ret = extern.matchAll({
383
+ [Symbol.matchAll](str) {
384
+ return [str.substring(0, 3)];
385
+ },
386
+ });
387
+ const retInfo = tracker.getData(ret[0]);
388
+
389
+ expect(ret).to.deep.equal(['foo']);
390
+ expect(retInfo.tags).to.deep.equal({
391
+ untrusted: [0, 2]
392
+ });
393
+ expect(retInfo.history[0].name).to.equal('String.prototype.matchAll');
394
+ });
395
+ });
396
+ });
397
+ });
398
+ });
399
+