@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,73 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const {
5
+ DataflowTag: { UNTRUSTED, SQL_ENCODED }
6
+ } = require('@contrast/common');
7
+ const { initAssessFixture } = require('@contrast/test/fixtures');
8
+
9
+ describe('assess dataflow propagation sequelize QueryGenerators', function() {
10
+ let core, component, trackString, simulateRequestScope, tracker;
11
+
12
+ class QueryGeneratorMock {
13
+ quoteIdentifier(identifier) {
14
+ return '`'.concat(identifier).concat('`');
15
+ }
16
+ }
17
+
18
+ before(function() {
19
+ ({ core, trackString, simulateRequestScope } = initAssessFixture());
20
+ tracker = core.assess.dataflow.tracker;
21
+ core.assess.dataflow.propagation.stringInstrumentation.concat.install();
22
+ component = require('./query-generator')(core);
23
+ });
24
+
25
+ after(function() {
26
+ core.assess.dataflow.propagation.stringInstrumentation.concat.uninstall();
27
+ });
28
+
29
+ [
30
+ {
31
+ Ctor: class MSSQLQueryGenerator extends QueryGeneratorMock {},
32
+ file: 'lib/dialects/mssql/query-generator.js',
33
+ },
34
+ {
35
+ Ctor: class MySqlQueryGenerator extends QueryGeneratorMock {},
36
+ file: 'lib/dialects/mysql/query-generator.js',
37
+ },
38
+ {
39
+ Ctor: class PostgresQueryGenerator extends QueryGeneratorMock {},
40
+ file: 'lib/dialects/postgres/query-generator.js',
41
+ },
42
+ {
43
+ Ctor: class SQLiteQueryGenerator extends QueryGeneratorMock {},
44
+ file: 'lib/dialects/sqlite/query-generator.js',
45
+ },
46
+ ].forEach(({ Ctor, file }) => {
47
+ it(`${Ctor.name}.prototype.quoteIdentifier adds sql-encoded tag`, function() {
48
+ core.depHooks.resolve.withArgs({ name: 'sequelize', file }).yields(Ctor);
49
+ component.install();
50
+
51
+ simulateRequestScope(() => {
52
+ const tracked = trackString('foo');
53
+ const result = new Ctor().quoteIdentifier(tracked);
54
+ const strInfo = tracker.getData(result);
55
+
56
+ expect(strInfo).to.deep.include({
57
+ args: [{ tracked: true, value: 'foo' }],
58
+ moduleName: 'sequelize',
59
+ methodName: `${Ctor.name}.prototype.quoteIdentifier`,
60
+ object: { tracked: false, value: Ctor.name },
61
+ result: { tracked: true, value: '`foo`' },
62
+ source: 'P',
63
+ tags: {
64
+ [UNTRUSTED]: [1, 3],
65
+ [SQL_ENCODED]: [0, 4],
66
+ },
67
+ target: 'R',
68
+ });
69
+ expect(strInfo.history).to.have.lengthOf(1);
70
+ });
71
+ });
72
+ });
73
+ });
@@ -0,0 +1,130 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const {
6
+ DataflowTag: { SQL_ENCODED, UNTRUSTED },
7
+ } = require('@contrast/common');
8
+ const { initAssessFixture } = require('@contrast/test/fixtures');
9
+
10
+ describe('assess dataflow propagation sequelize sql-string', function () {
11
+ let core, trackString, simulateRequestScope, tracker, mockSequelizeSqlString;
12
+
13
+ beforeEach(function () {
14
+ ({
15
+ core,
16
+ simulateRequestScope,
17
+ trackString
18
+ } = initAssessFixture());
19
+
20
+ mockSequelizeSqlString = {
21
+ escape: (str) => str,
22
+ format(str, repl) {
23
+ repl.forEach((r) => {
24
+ str = str.replace(/\?/, r);
25
+ });
26
+
27
+ return str;
28
+ },
29
+ formatNamedParameters(str, repl) {
30
+ Object.keys(repl).forEach((key) => {
31
+ str = str.replace(new RegExp(`:${key}`), repl[key]);
32
+ });
33
+
34
+ return str;
35
+ }
36
+ };
37
+ tracker = core.assess.dataflow.tracker;
38
+ core.assess.dataflow.propagation.stringInstrumentation.install();
39
+ core.assess.dataflow.propagation.sequelizeInstrumentation.install();
40
+ core.depHooks.resolve.yield(mockSequelizeSqlString);
41
+ });
42
+
43
+
44
+ afterEach(function () {
45
+ core.assess.dataflow.propagation.stringInstrumentation.uninstall();
46
+ sinon.resetHistory();
47
+ });
48
+
49
+ [
50
+ {
51
+ method: 'escape',
52
+ args: [() => trackString('inserted-value')],
53
+ notTrackedArgs: ['safe-value'],
54
+ expectedTags: {
55
+ [UNTRUSTED]: [0, 13],
56
+ [SQL_ENCODED]: [0, 13]
57
+ }
58
+ },
59
+ {
60
+ method: 'format',
61
+ args: [() => 'SELECT ? FROM ?', () => ['id', trackString('users')]],
62
+ notTrackedArgs: ['SELECT ? FROM ?', ['id', 'users']],
63
+ expectedTags: {
64
+ [UNTRUSTED]: [15, 19],
65
+ [SQL_ENCODED]: [15, 19]
66
+ }
67
+ },
68
+ {
69
+ onlySanitizeCheck: true,
70
+ method: 'format',
71
+ args: [() => trackString('SELECT password FROM users'), () => ['id', trackString('users')]],
72
+ notTrackedArgs: ['SELECT ? FROM ?', ['id', 'users']],
73
+ expectedTags: {
74
+ [UNTRUSTED]: [0, 25],
75
+ }
76
+ },
77
+ {
78
+ method: 'formatNamedParameters',
79
+ args: [() => 'SELECT :columns FROM :table', () => ({ columns: 'id', table: trackString('users') })],
80
+ notTrackedArgs: ['SELECT :columns FROM :table', { columns: 'id', table: 'users' }],
81
+ expectedTags: {
82
+ [UNTRUSTED]: [15, 19],
83
+ [SQL_ENCODED]: [15, 19]
84
+ }
85
+ },
86
+ {
87
+ onlySanitizeCheck: true,
88
+ method: 'formatNamedParameters',
89
+ args: [() => trackString('SELECT password FROM users'), () => ({ columns: 'id', table: trackString('users') })],
90
+ notTrackedArgs: ['SELECT ? FROM ?', ['id', 'users']],
91
+ expectedTags: {
92
+ [UNTRUSTED]: [0, 25],
93
+ }
94
+ },
95
+ ].forEach(({ onlySanitizeCheck, method, args, notTrackedArgs, expectedTags }) => {
96
+ describe(method, function () {
97
+ it('sanitizes correctly', function () {
98
+ simulateRequestScope(function () {
99
+
100
+ const notTrackedResult = mockSequelizeSqlString[method](...notTrackedArgs);
101
+ const notTrackedStrInfo = tracker.getData(notTrackedResult);
102
+
103
+ const trackedResult = mockSequelizeSqlString[method](...args.map(a => a()));
104
+ const trackedStrInfo = tracker.getData(trackedResult);
105
+
106
+ expect(notTrackedStrInfo).to.be.null;
107
+ expect(trackedStrInfo?.tags).to.deep.equal(expectedTags);
108
+ });
109
+ });
110
+
111
+ if (!onlySanitizeCheck) {
112
+ it('will not sanitize if there is no assess context', function () {
113
+ simulateRequestScope(function () {
114
+ const result = mockSequelizeSqlString[method](...args.map(a => a()));
115
+ expect(tracker.getData(result)).to.be.null;
116
+ }, {});
117
+ });
118
+
119
+ it('will not sanitize if there instrumentation is locked', function () {
120
+ simulateRequestScope(function () {
121
+ core.scopes.instrumentation.run({ lock: true }, function () {
122
+ const result = mockSequelizeSqlString[method](...args.map(a => a()));
123
+ expect(tracker.getData(result)?.tags || {}).to.not.haveOwnProperty(SQL_ENCODED);
124
+ });
125
+ });
126
+ });
127
+ }
128
+ });
129
+ });
130
+ });
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const {
6
+ DataflowTag: { UNTRUSTED, SQL_ENCODED }
7
+ } = require('@contrast/common');
8
+ const { initAssessFixture } = require('@contrast/test/fixtures');
9
+
10
+ // TODO: The proper usage of the method is with a template string so
11
+ // meaningful tests can be written after we add support for them (NODE-2826);
12
+ describe('assess dataflow propagation sql-template-strings', function () {
13
+ let core, trackString, simulateRequestScope, tracker, mockSqlTemplateStrings;
14
+
15
+ beforeEach(function () {
16
+ ({
17
+ core,
18
+ simulateRequestScope,
19
+ trackString
20
+ } = initAssessFixture());
21
+
22
+ mockSqlTemplateStrings = {
23
+ // SQL: (str) => ['mock-template-strings', str, 'mock-template-strings'].join('')
24
+ SQL: (str) => {
25
+ const ret = {
26
+ strings: [],
27
+ values: []
28
+ };
29
+ const valueLength = 'inserted-value'.length;
30
+ let idx = str.indexOf('inserted-value');
31
+ let newStr = str;
32
+ while (idx >= 0) {
33
+ ret.strings.push(newStr.substring(0, idx));
34
+ ret.values.push(newStr.substring(idx, idx + valueLength));
35
+
36
+ newStr = newStr.substring(idx + valueLength);
37
+ idx = newStr.indexOf('inserted-value');
38
+ }
39
+
40
+ if (newStr) ret.strings.push(newStr);
41
+
42
+ return ret;
43
+ }
44
+ };
45
+
46
+ tracker = core.assess.dataflow.tracker;
47
+ core.assess.dataflow.propagation.stringInstrumentation.install();
48
+ core.assess.dataflow.propagation.arrayJoin.install();
49
+ core.assess.dataflow.propagation.sqlTemplateStrings.install();
50
+ core.depHooks.resolve.yield(mockSqlTemplateStrings);
51
+ });
52
+
53
+ afterEach(function () {
54
+ sinon.resetHistory();
55
+ });
56
+
57
+ it('propagates correctly', function () {
58
+ simulateRequestScope(function () {
59
+ const trackedValue = trackString('inserted-value', { tags: { [UNTRUSTED]: [9, 13] } });
60
+ const trackedContext = trackString('tracked-context', { tags: { [UNTRUSTED]: [0, 13] } });
61
+
62
+ const notTrackedResult = mockSqlTemplateStrings.SQL('foo-inserted-value-bar-baz-inserted-value');
63
+ const notTrackedStrInfoStrings = tracker.getData(notTrackedResult.strings.toString());
64
+ const notTrackedStrInfoValues = tracker.getData(notTrackedResult.values.toString());
65
+
66
+ const trackedResult = mockSqlTemplateStrings.SQL([trackedContext, 'foo', trackedValue, 'bar', trackedValue].join('-'));
67
+ const trackedStrInfoStrings = tracker.getData(trackedResult.strings.toString());
68
+ const trackedStrInfoValues = tracker.getData(trackedResult.values.toString());
69
+
70
+ expect(notTrackedStrInfoStrings).to.be.null;
71
+ expect(notTrackedStrInfoValues).to.be.null;
72
+ expect(trackedStrInfoStrings?.tags).to.deep.equal({
73
+ [UNTRUSTED]: [0, 13],
74
+ [SQL_ENCODED]: [0, 19],
75
+ }); // the string is 'tracked-context-foo-,-bar-'
76
+ expect(trackedStrInfoValues?.tags).to.deep.equal({
77
+ [UNTRUSTED]: [9, 13, 24, 28],
78
+ [SQL_ENCODED]: [0, 13, 15, 28]
79
+ }); // the string is 'inserted-value,inserted-value'
80
+ });
81
+ });
82
+
83
+ it('will not propagate if there is no assess context', function () {
84
+ simulateRequestScope(function () {
85
+ const value = trackString('foo');
86
+ const result = mockSqlTemplateStrings.SQL(['foo', value].join());
87
+ expect(tracker.getData(result.strings.toString())).to.be.null;
88
+ }, {});
89
+ });
90
+
91
+ it('will not propagate if there instrumentation is locked', function () {
92
+ simulateRequestScope(function () {
93
+ core.scopes.instrumentation.run({ lock: true }, function () {
94
+ const value = trackString('foo');
95
+ const result = mockSqlTemplateStrings.SQL(['foo', value].join());
96
+ expect(tracker.getData(result.strings.toString())).to.be.null;
97
+ });
98
+ });
99
+ });
100
+ });
@@ -0,0 +1,132 @@
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 string concat', 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.stringInstrumentation.concat.install();
19
+ });
20
+
21
+ afterEach(function () {
22
+ core.assess.dataflow.propagation.stringInstrumentation.concat.uninstall();
23
+ sinon.resetHistory();
24
+ });
25
+
26
+ [
27
+ {
28
+ str: 'foo',
29
+ tags: {
30
+ untrusted: [0, 2]
31
+ },
32
+ args: [() => 'bar', () => 'baz'],
33
+ expected: {
34
+ untrusted: [0, 2]
35
+ }
36
+ },
37
+ {
38
+ str: 'foo',
39
+ tags: {
40
+ untrusted: [0, 2]
41
+ },
42
+ args: [
43
+ () => trackString('bar', { tags: { untrusted: [0, 2] } }),
44
+ () => trackString('baz', { tags: { untrusted: [0, 2] } }),
45
+ ],
46
+ expected: {
47
+ untrusted: [0, 8]
48
+ }
49
+ },
50
+ {
51
+ str: 'boo',
52
+ tags: {
53
+ untrusted: [0, 2]
54
+ },
55
+ args: [
56
+ () => trackString('bar', { tags: { untrusted: [0, 2] } }),
57
+ () => null,
58
+ () => undefined,
59
+ () => trackString('baz', { tags: { untrusted: [0, 2] } }),
60
+ ],
61
+ expected: {
62
+ untrusted: [0, 5, 19, 21]
63
+ }
64
+ },
65
+ {
66
+ str: 'foo',
67
+ tags: {
68
+ untrusted: [0, 2]
69
+ },
70
+ args: [
71
+ () => trackString('bar', { other: 'metadata', tags: undefined }),
72
+ () => trackString('baz', { tags: { untrusted: [0, 2] } }),
73
+ ],
74
+ expected: {
75
+ untrusted: [0, 2, 6, 8]
76
+ }
77
+ },
78
+ {
79
+ str: 'not-tracked',
80
+ args: [
81
+ () => trackString('bar', { tags: { untrusted: [0, 2] } }),
82
+ () => trackString('baz', { tags: { untrusted: [0, 2] } }),
83
+ ],
84
+ expected: {
85
+ untrusted: [11, 16]
86
+ }
87
+ },
88
+ ].forEach(({ str, tags, args, expected = null }) => {
89
+ it('concat', function () {
90
+ simulateRequestScope(function () {
91
+ let result;
92
+ const value = str !== 'not-tracked' ? trackString(str, { tags }) : str;
93
+
94
+ if (str !== 'boo') {
95
+ result = value.concat(...args.map((a) => a()));
96
+ } else {
97
+ result = value.concat.bind('', value, ...args.map((a) => a()))();
98
+ }
99
+
100
+ const strInfo = tracker.getData(result);
101
+ expect(strInfo?.tags).to.deep.equal(expected);
102
+ });
103
+ });
104
+ });
105
+
106
+ it('will nto "retrack" if result is unchanged', function () {
107
+ simulateRequestScope(function () {
108
+ const value = trackString('foo');
109
+ const result = value.concat('');
110
+
111
+ expect(tracker.getData(value)).to.equal(tracker.getData(result));
112
+ });
113
+ });
114
+
115
+ it('will not propagate if there is no assess context', function () {
116
+ simulateRequestScope(function () {
117
+ const value = trackString('foo');
118
+ const result = value.concat('bar');
119
+ expect(tracker.getData(result)).to.be.null;
120
+ }, {});
121
+ });
122
+
123
+ it('will not propagate if there instrumentation is locked', function () {
124
+ simulateRequestScope(function () {
125
+ core.scopes.instrumentation.run({ lock: true }, function () {
126
+ const value = trackString('foo');
127
+ const result = value.concat('bar');
128
+ expect(tracker.getData(result)).to.be.null;
129
+ });
130
+ });
131
+ });
132
+ });
@@ -0,0 +1,61 @@
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 string format-methods', 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.stringInstrumentation.formatMethods.install();
19
+ });
20
+
21
+ afterEach(function () {
22
+ core.assess.dataflow.propagation.stringInstrumentation.formatMethods.uninstall();
23
+ sinon.resetHistory();
24
+ });
25
+
26
+ ['toLowerCase', 'toUpperCase', 'toLocaleLowerCase', 'toLocaleUpperCase'].forEach((method) => {
27
+ it(method, function () {
28
+ simulateRequestScope(function () {
29
+ const notTrackedValue = 'foo';
30
+ const trackedValue = trackString('foo', { tags: { untrusted: [0, 2] } });
31
+
32
+ const notTrackedResult = notTrackedValue[method]();
33
+ const notTrackedStrInfo = tracker.getData(notTrackedResult);
34
+
35
+ const trackedResult = trackedValue[method]();
36
+ const trackedStrInfo = tracker.getData(trackedResult);
37
+
38
+ expect(notTrackedStrInfo).to.be.null;
39
+ expect(trackedStrInfo?.tags).to.deep.equal({ untrusted: [0, 2] });
40
+ });
41
+ });
42
+ });
43
+
44
+ it('will not propagate if there is no assess context', function () {
45
+ simulateRequestScope(function () {
46
+ const value = trackString('foo');
47
+ const result = value.toLowerCase('bar');
48
+ expect(tracker.getData(result)).to.be.null;
49
+ }, {});
50
+ });
51
+
52
+ it('will not propagate if there instrumentation is locked', function () {
53
+ simulateRequestScope(function () {
54
+ core.scopes.instrumentation.run({ lock: true }, function () {
55
+ const value = trackString('foo');
56
+ const result = value.toLowerCase('bar');
57
+ expect(tracker.getData(result)).to.be.null;
58
+ });
59
+ });
60
+ });
61
+ });
@@ -0,0 +1,164 @@
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 string html-methods', 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.stringInstrumentation.htmlMethods.install();
19
+ });
20
+
21
+ afterEach(function () {
22
+ core.assess.dataflow.propagation.stringInstrumentation.htmlMethods.uninstall();
23
+ sinon.resetHistory();
24
+ });
25
+
26
+ [
27
+ {
28
+ method: 'anchor',
29
+ str: 'foo',
30
+ args: [() => undefined, () => 'id', () => trackString('tracked-id', { tags: { untrusted: [0, 9] } })],
31
+ tags: {
32
+ untrusted: [0, 2]
33
+ },
34
+ expected: [{ untrusted: [20, 22] }, { untrusted: [13, 15] }, { untrusted: [9, 18, 21, 23] }],
35
+ },
36
+ {
37
+ method: 'anchor',
38
+ str: 'not-tracked',
39
+ args: [() => 'id', () => trackString('tracked-id', { tags: { untrusted: [0, 9] } })],
40
+ expected: [undefined, { untrusted: [9, 18] }]
41
+ },
42
+ {
43
+ method: 'big',
44
+ str: 'foo',
45
+ tags: {
46
+ untrusted: [0, 2]
47
+ },
48
+ expected: { untrusted: [5, 7] }
49
+ },
50
+ {
51
+ method: 'big',
52
+ str: 'not-tracked',
53
+ },
54
+ {
55
+ method: 'blink',
56
+ str: 'foo',
57
+ tags: {
58
+ untrusted: [0, 2]
59
+ },
60
+ expected: { untrusted: [7, 9] }
61
+ },
62
+ {
63
+ method: 'blink',
64
+ str: 'not-tracked',
65
+ },
66
+ {
67
+ method: 'italics',
68
+ str: 'foo',
69
+ tags: {
70
+ untrusted: [0, 2]
71
+ },
72
+ expected: { untrusted: [3, 5] }
73
+ },
74
+ {
75
+ method: 'italics',
76
+ str: 'not-tracked',
77
+ },
78
+ {
79
+ method: 'small',
80
+ str: 'foo',
81
+ tags: {
82
+ untrusted: [0, 2]
83
+ },
84
+ expected: { untrusted: [7, 9] }
85
+ },
86
+ {
87
+ method: 'small',
88
+ str: 'not-tracked',
89
+ },
90
+ {
91
+ method: 'strike',
92
+ str: 'foo',
93
+ tags: {
94
+ untrusted: [0, 2]
95
+ },
96
+ expected: { untrusted: [8, 10] }
97
+ },
98
+ {
99
+ method: 'strike',
100
+ str: 'not-tracked',
101
+ },
102
+ {
103
+ method: 'sub',
104
+ str: 'foo',
105
+ tags: {
106
+ untrusted: [0, 2]
107
+ },
108
+ expected: { untrusted: [5, 7] }
109
+ },
110
+ {
111
+ method: 'sub',
112
+ str: 'not-tracked',
113
+ },
114
+ {
115
+ method: 'fixed',
116
+ str: 'foo',
117
+ tags: {
118
+ untrusted: [0, 2]
119
+ },
120
+ expected: { untrusted: [4, 6] }
121
+ },
122
+ {
123
+ method: 'fixed',
124
+ str: 'not-tracked',
125
+ },
126
+ ].forEach(({ str, tags, args, method, expected }) => {
127
+ it(method, function () {
128
+ simulateRequestScope(function () {
129
+ const value = str !== 'not-tracked' ? trackString(str, { tags }) : str;
130
+
131
+ if (args?.length) {
132
+ for (let i = 0; i < args.length; i++) {
133
+ const result = value[method](args[i]());
134
+ const strInfo = tracker.getData(result);
135
+ expect(strInfo?.tags).to.deep.equal(expected[i]);
136
+ }
137
+ } else {
138
+ const result = value[method]();
139
+ const strInfo = tracker.getData(result);
140
+ expect(strInfo?.tags).to.deep.equal(expected);
141
+ }
142
+
143
+ });
144
+ });
145
+ });
146
+
147
+ it('will not propagate if there is no assess context', function () {
148
+ simulateRequestScope(function () {
149
+ const value = trackString('foo');
150
+ const result = value.anchor('bar');
151
+ expect(tracker.getData(result)).to.be.null;
152
+ }, {});
153
+ });
154
+
155
+ it('will not propagate if there instrumentation is locked', function () {
156
+ simulateRequestScope(function () {
157
+ core.scopes.instrumentation.run({ lock: true }, function () {
158
+ const value = trackString('foo');
159
+ const result = value.anchor('bar');
160
+ expect(tracker.getData(result)).to.be.null;
161
+ });
162
+ });
163
+ });
164
+ });