@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,144 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { InputType } = require('@contrast/common');
6
+ const { initAssessFixture } = require('@contrast/test/fixtures');
7
+ const init = require('./jsonBodyParser');
8
+
9
+ describe('assess dataflow sources restify jsonBodyParser', function () {
10
+ let core, simulateRequestScope, handle, jsonBodyParserStub, req, res, next;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+ handle = sinon.stub(core.assess.dataflow.sources, 'handle');
15
+
16
+ jsonBodyParserStub = sinon.stub().returns([
17
+ sinon.stub(),
18
+ sinon.stub().callsArg(2),
19
+ ]);
20
+
21
+ req = {};
22
+ res = {};
23
+ next = sinon.stub();
24
+
25
+ core.depHooks.resolve
26
+ .withArgs({ name: 'restify', file: 'lib/plugins/jsonBodyParser.js', version: '>=8' })
27
+ .callsFake((_, cb) => {
28
+ jsonBodyParserStub = cb(jsonBodyParserStub);
29
+ });
30
+
31
+ init(core).install();
32
+ });
33
+
34
+ it('does not call `handle` when not in context', function () {
35
+ const [, middleware] = jsonBodyParserStub();
36
+
37
+ simulateRequestScope(() => {
38
+ middleware(req, res, next);
39
+
40
+ expect(handle).not.to.have.been.called;
41
+ expect(core.logger.error).to.have.been.calledWith(
42
+ { funcKey: 'assess-dataflow-source:restify.plugins.jsonBodyParser.parseJson' },
43
+ 'unable to handle source. Missing `sourceContext`'
44
+ );
45
+ }, {});
46
+ });
47
+
48
+ it('does not call `handle` when body is already tracked', function () {
49
+ const [, middleware] = jsonBodyParserStub();
50
+
51
+ simulateRequestScope(() => {
52
+ middleware(req, res, next);
53
+
54
+ expect(handle).not.to.have.been.called;
55
+ expect(core.logger.trace).to.have.been.calledWith(
56
+ { funcKey: 'assess-dataflow-source:restify.plugins.jsonBodyParser.parseJson' },
57
+ 'values already tracked'
58
+ );
59
+ }, { assess: { parsedBody: true } });
60
+ });
61
+
62
+ it('calls `handle` appropriately', function () {
63
+ const [, middleware] = jsonBodyParserStub();
64
+ req.body = { foo: 'bar' };
65
+
66
+ simulateRequestScope(() => {
67
+ middleware(req, res, next);
68
+
69
+ expect(handle).to.have.been.calledOnceWith({
70
+ context: 'req.body',
71
+ data: req.body,
72
+ inputType: InputType.JSON_VALUE,
73
+ name: 'restify.plugins.jsonBodyParser.parseJson',
74
+ stacktraceOpts: {
75
+ constructorOpt: sinon.match.func,
76
+ },
77
+ sourceContext: sinon.match.object,
78
+ });
79
+ });
80
+ });
81
+
82
+ it('calls `handle` twice if `mapParams` is set', function () {
83
+ const [, middleware] = jsonBodyParserStub({ mapParams: true });
84
+ req.body = { foo: 'bar' };
85
+ req.params = req.body;
86
+
87
+ simulateRequestScope(() => {
88
+ middleware(req, res, next);
89
+
90
+ expect(handle).to.have.callCount(2);
91
+ expect(handle).to.have.been.calledWith({
92
+ context: 'req.body',
93
+ data: req.body,
94
+ inputType: InputType.JSON_VALUE,
95
+ name: 'restify.plugins.jsonBodyParser.parseJson',
96
+ stacktraceOpts: {
97
+ constructorOpt: sinon.match.func,
98
+ },
99
+ sourceContext: sinon.match.object,
100
+ });
101
+ expect(handle).to.have.been.calledWith({
102
+ context: 'req.params',
103
+ data: req.params,
104
+ inputType: InputType.JSON_VALUE,
105
+ name: 'restify.plugins.jsonBodyParser.parseJson',
106
+ stacktraceOpts: {
107
+ constructorOpt: sinon.match.func,
108
+ },
109
+ sourceContext: sinon.match.object,
110
+ });
111
+ });
112
+ });
113
+
114
+
115
+ it('logs errors in the `handle` method', function () {
116
+ const [, middleware] = jsonBodyParserStub();
117
+ const err = new Error('yikes!');
118
+ handle.throws(err);
119
+
120
+ simulateRequestScope(() => {
121
+ middleware(req, res, next);
122
+
123
+ expect(core.logger.error).to.have.been.calledWith(
124
+ { err, funcKey: 'assess-dataflow-source:restify.plugins.jsonBodyParser.parseJson' },
125
+ 'unable to handle Restify source',
126
+ );
127
+ });
128
+ });
129
+
130
+ it('logs errors in the `handle` method 2 ', function () {
131
+ const [, middleware] = jsonBodyParserStub({ mapParams: true });
132
+ const err = new Error('yikes!');
133
+ handle.onCall(1).throws(err);
134
+
135
+ simulateRequestScope(() => {
136
+ middleware(req, res, next);
137
+
138
+ expect(core.logger.error).to.have.been.calledWith(
139
+ { err, funcKey: 'assess-dataflow-source:restify.plugins.jsonBodyParser.parseJson' },
140
+ 'unable to handle Restify source',
141
+ );
142
+ });
143
+ });
144
+ });
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { InputType } = require('@contrast/common');
6
+ const { initAssessFixture } = require('@contrast/test/fixtures');
7
+ const init = require('./router');
8
+
9
+ describe('assess dataflow sources restify router', function () {
10
+ let core, simulateRequestScope, handle, Router, req;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+ handle = sinon.stub(core.assess.dataflow.sources, 'handle');
15
+
16
+ Router = { prototype: { lookup: sinon.stub() } };
17
+ req = {};
18
+
19
+ core.depHooks.resolve
20
+ .withArgs({ name: 'restify', file: 'lib/router.js', version: '>=8' })
21
+ .yields(Router);
22
+
23
+ init(core).install();
24
+ });
25
+
26
+ it('does not call `handle` when not in context', function () {
27
+ simulateRequestScope(() => {
28
+ Router.prototype.lookup(req);
29
+
30
+ expect(handle).not.to.have.been.called;
31
+ expect(core.logger.error).to.have.been.calledWith(
32
+ { funcKey: 'assess-dataflow-source:restify.Router.prototype.lookup' },
33
+ 'unable to handle source. Missing `sourceContext`'
34
+ );
35
+ }, {});
36
+ });
37
+
38
+ it('does not call `handle` when body is already tracked', function () {
39
+ simulateRequestScope(() => {
40
+ Router.prototype.lookup(req);
41
+
42
+ expect(handle).not.to.have.been.called;
43
+ expect(core.logger.trace).to.have.been.calledWith(
44
+ { funcKey: 'assess-dataflow-source:restify.Router.prototype.lookup' },
45
+ 'values already tracked'
46
+ );
47
+ }, { assess: { parsedParams: true } });
48
+ });
49
+
50
+ it('calls `handle` appropriately', function () {
51
+ req.params = { foo: 'bar' };
52
+
53
+ simulateRequestScope(() => {
54
+ Router.prototype.lookup(req);
55
+
56
+ expect(handle).to.have.been.calledWith({
57
+ context: 'req.params',
58
+ data: req.params,
59
+ inputType: InputType.URL_PARAMETER,
60
+ name: 'restify.Router.prototype.lookup',
61
+ stacktraceOpts: {
62
+ constructorOpt: sinon.match.func,
63
+ prependFrames: [sinon.match.func],
64
+ },
65
+ sourceContext: sinon.match.object,
66
+ });
67
+ });
68
+ });
69
+
70
+ it('logs errors in the `handle` method', function () {
71
+ const err = new Error('yikes!');
72
+ handle.throws(err);
73
+
74
+ simulateRequestScope(() => {
75
+ Router.prototype.lookup(req);
76
+
77
+ expect(core.logger.error).to.have.been.calledWith(
78
+ { err, funcKey: 'assess-dataflow-source:restify.Router.prototype.lookup' },
79
+ 'unable to handle Restify source',
80
+ );
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,27 @@
1
+ /* eslint-disable */
2
+
3
+ 'use strict';
4
+
5
+ const { expect } = require('chai');
6
+ const utils = require('./tag-utils');
7
+
8
+ const parsed = require('@contrast/test/data/tag-util-args-and-results');
9
+
10
+ describe('assess dataflow tag-utils - verify all existing usage', function() {
11
+ for (const test in parsed) {
12
+ const fnName = test.slice(0, -4);
13
+ const { lines, tests } = parsed[test];
14
+ describe(fnName, function() {
15
+ if (!utils[fnName]) {
16
+ throw new Error(`No test for ${fnName}`);
17
+ }
18
+ for (let i = 0; i < tests.length; i++) {
19
+ it(lines[i], function() {
20
+ const { args, ret } = tests[i];
21
+ const r = utils[fnName](...args);
22
+ expect(r).to.deep.equal(ret);
23
+ });
24
+ }
25
+ });
26
+ }
27
+ });
@@ -0,0 +1,192 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const utils = require('./tag-utils');
5
+
6
+ describe('assess dataflow tag-utils', function() {
7
+ describe('createSubsetTags', function() {
8
+ it('creates subset tags for multiple tags', function() {
9
+ const ret = utils.createSubsetTags({
10
+ lowSpan: [0, 3],
11
+ span: [0, 7],
12
+ highSpan: [3, 7],
13
+ below: [0, 1],
14
+ above: [7, 10],
15
+ }, 2, 4);
16
+
17
+ expect(ret).to.deep.equal({
18
+ lowSpan: [0, 1],
19
+ span: [0, 3],
20
+ highSpan: [1, 3],
21
+ });
22
+ });
23
+
24
+ it('creates subset tags for multiple tag ranges', function() {
25
+ const ret = utils.createSubsetTags({
26
+ multiples: [1, 2, 4, 4],
27
+ }, 2, 10);
28
+
29
+ expect(ret).to.deep.equal({
30
+ multiples: [0, 0, 2, 2]
31
+ });
32
+ });
33
+
34
+ it('will not merge results since input ranges are expected to be merged', function() {
35
+ const ret = utils.createSubsetTags({
36
+ // correctly merged these would be [1, 3]
37
+ multiples: [1, 2, 3, 3],
38
+ }, 2, 10);
39
+
40
+ expect(ret).to.deep.equal({
41
+ multiples: [0, 0, 1, 1]
42
+ });
43
+ });
44
+ });
45
+
46
+ describe('createAppendTags', function() {
47
+ it('creates appended ranges from 2 tags #1', function() {
48
+ const ret = utils.createAppendTags(
49
+ { untrusted: [0, 3] },
50
+ { untrusted: [0, 3] },
51
+ 3
52
+ );
53
+ expect(ret).to.deep.equal({
54
+ untrusted: [0, 6]
55
+ });
56
+ });
57
+ it('creates appended ranges from 2 tags #2', function() {
58
+ const ret = utils.createAppendTags(
59
+ { untrusted: [0, 3] },
60
+ { untrusted: [3, 6] },
61
+ 3
62
+ );
63
+ expect(ret).to.deep.equal({
64
+ untrusted: [0, 3, 6, 9]
65
+ });
66
+ });
67
+ });
68
+
69
+ describe('createFullLengthCopyTags', function() {
70
+ it('creates tags with the full length of the result', function() {
71
+ const ret = utils.createFullLengthCopyTags(
72
+ {
73
+ untrusted: [0, 10],
74
+ 'sql-encoded': [0, 20],
75
+ 'custom-encoded': [0, 12]
76
+ },
77
+ 13
78
+ );
79
+ expect(ret).to.deep.equal({
80
+ untrusted: [0, 12],
81
+ 'sql-encoded': [0, 12],
82
+ 'custom-encoded': [0, 12]
83
+ });
84
+ });
85
+
86
+ it('returns null in case of bad arguments', function() {
87
+ expect(utils.createFullLengthCopyTags({}, 1)).to.be.null;
88
+ expect(utils.createFullLengthCopyTags({ untrusted: [0, 2] }, 0)).to.be.null;
89
+ expect(utils.createFullLengthCopyTags({ untrusted: [0, 2] }, null)).to.be.null;
90
+ expect(utils.createFullLengthCopyTags({ untrusted: [0, 2] })).to.be.null;
91
+ });
92
+ });
93
+
94
+ describe('createMergedTags', function() {
95
+ it('creates merged ranges from 2 tags #1', function() {
96
+ const ret = utils.createMergedTags(
97
+ { untrusted: [0, 3] },
98
+ { untrusted: [3, 6] },
99
+ );
100
+ expect(ret).to.deep.equal({
101
+ untrusted: [0, 6]
102
+ });
103
+ });
104
+
105
+ it('creates merged ranges from 2 tags #2', function() {
106
+ const ret = utils.createMergedTags(
107
+ { untrusted: [0, 3] },
108
+ { untrusted: [5, 8] },
109
+ );
110
+ expect(ret).to.deep.equal({
111
+ untrusted: [0, 3, 5, 8]
112
+ });
113
+ });
114
+
115
+ it('creates merged ranges from 2 tags #3', function() {
116
+ const ret = utils.createMergedTags(
117
+ { untrusted: [0, 3, 9, 12, 21, 23] },
118
+ { untrusted: [5, 8, 14, 18] },
119
+ );
120
+ expect(ret).to.deep.equal({
121
+ untrusted: [0, 3, 5, 12, 14, 18, 21, 23]
122
+ });
123
+ });
124
+
125
+ it('returns null when trying to merge empty tag ranges', function() {
126
+ const ret = utils.createMergedTags(
127
+ { untrusted: [] },
128
+ { untrusted: [] },
129
+ );
130
+ expect(ret).to.equal(null);
131
+ });
132
+
133
+ });
134
+
135
+ describe('createOverlappingTags', function() {
136
+ it('should return overlapping tags', function() {
137
+ const result = utils.createOverlappingTags({
138
+ untrusted: [0, 20],
139
+ tag2: [2, 5],
140
+ tag3: [4, 9],
141
+ tag4: [6, 6]
142
+ }, 1, 5);
143
+
144
+ expect(result).to.eql({
145
+ untrusted: [[0, 20]],
146
+ tag2: [[2, 5]],
147
+ tag3: [[4, 9]],
148
+ });
149
+ });
150
+ });
151
+
152
+ describe('createEscapeTagRanges', function() {
153
+ it('should return empty object when no overlap exists', function() {
154
+ const result = utils.createEscapeTagRanges('foo', 'bar', { untrusted: [0, 2] });
155
+ expect(result).to.be.deep.equal({});
156
+ });
157
+
158
+ it('should carry over tag ranges when overlap exists', function() {
159
+ const result = utils.createEscapeTagRanges('<foo>', '&lt;foo&gt;', { untrusted: [1, 3] });
160
+ expect(result).to.be.deep.equal({ untrusted: [4, 6] });
161
+ });
162
+
163
+ it('should handle duplicate strings', function() {
164
+ const result = utils.createEscapeTagRanges('<foo><foo>', '&lt;foo&gt;&lt;foo&gt;', { untrusted: [1, 3, 6, 8] });
165
+ expect(result).to.be.deep.equal({ untrusted: [4, 6, 15, 17] });
166
+ });
167
+ });
168
+
169
+ describe('createTagsWithExclusion', function() {
170
+ const tests = [
171
+ { args: [{ untrusted: [0, 0] }, [0, 0]], expected: null },
172
+ { args: [{ untrusted: [0, 1] }, [0, 0]], expected: { untrusted: [1, 1] } },
173
+ { args: [{ untrusted: [0, 1] }, [1, 1]], expected: { untrusted: [0, 0] } },
174
+ { args: [{ untrusted: [0, 1] }, [0, 1]], expected: null },
175
+ { args: [{ untrusted: [0, 2] }, [0, 0]], expected: { untrusted: [1, 2] } },
176
+ { args: [{ untrusted: [0, 2] }, [1, 1]], expected: { untrusted: [0, 0, 2, 2] } },
177
+ { args: [{ untrusted: [0, 2] }, [2, 2]], expected: { untrusted: [0, 1] } },
178
+
179
+ ];
180
+
181
+ for (const test of tests) {
182
+ const { args, expected } = test;
183
+ const [tags, exclusion] = args;
184
+ const tagRange = tags.untrusted;
185
+
186
+ it(`should return ${expected} for ${tagRange} and ${exclusion}`, function() {
187
+ const result = utils.createTagsWithExclusion(tags, exclusion);
188
+ expect(result).to.deep.equal(expected);
189
+ });
190
+ }
191
+ });
192
+ });
@@ -0,0 +1,216 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { inspect } = require('util');
6
+ const mocks = require('@contrast/test/mocks');
7
+ const patcher = require('@contrast/patcher');
8
+ const distringuish = require('@contrast/distringuish');
9
+
10
+ describe('assess dataflow tracker', function () {
11
+ let core, tracker;
12
+
13
+ beforeEach(function () {
14
+ core = mocks.core();
15
+ core.logger = mocks.logger();
16
+ core.depHooks = mocks.depHooks();
17
+ core.patcher = patcher;
18
+ core.assess = mocks.assess();
19
+ tracker = require('./tracker')(core);
20
+ });
21
+
22
+ it('tracks strings', function () {
23
+ const str = 'foo';
24
+ const meta = {
25
+ source: 'query',
26
+ tags: {
27
+ userControlled: [0, 0, 2, 2],
28
+ urlEncoded: [[0, 2]],
29
+ }
30
+ };
31
+
32
+ tracker.track(str, meta);
33
+
34
+ const str2 = 'bar';
35
+
36
+ const strInfo = tracker.track(str2, {
37
+ parents: [meta],
38
+ tags: {
39
+ userControlled: [0, 2]
40
+ },
41
+ });
42
+
43
+ expect(tracker.getData(strInfo.extern)).to.deep.equal({
44
+ parents: [{
45
+ source: 'query',
46
+ tags: {
47
+ userControlled: [0, 0, 2, 2],
48
+ urlEncoded: [[0, 2]],
49
+ },
50
+ value: 'foo',
51
+ }],
52
+ tags: {
53
+ userControlled: [0, 2]
54
+ },
55
+ value: 'bar',
56
+ });
57
+ });
58
+
59
+ it('logs an error when the first argument is falsey', function () {
60
+ const result = tracker.track(null, { test: 'meta' });
61
+
62
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: null }, 'tracker.track called with invalid argument: value is falsy');
63
+ expect(result).to.deep.equal({ extern: null });
64
+ });
65
+
66
+ it('logs an error when the second argument is falsey', function () {
67
+ const result = tracker.track('testString', null);
68
+
69
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, metadata: null }, 'tracker.track called with invalid argument: metadata is falsy');
70
+ expect(result).to.deep.equal({ extern: null });
71
+ });
72
+
73
+ it('logs an error when the metadata is not validated through the factory function for it', function () {
74
+ core.assess.eventFactory.createdEvents.has = sinon.stub().returns(false);
75
+ const meta = { test: 'meta' };
76
+ const result = tracker.track('testString', meta);
77
+
78
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, metadata: meta }, 'tracker.track called without validated metadata');
79
+ expect(result).to.deep.equal({ extern: null });
80
+ });
81
+
82
+ it('logs an error when the string is already tracked', function () {
83
+ const str = 'test';
84
+ const meta = { test: 'meta' };
85
+ const newMeta = { test: 'new meta' };
86
+
87
+ const trackedStr = tracker.track(str, meta).extern;
88
+ const result = tracker.track(trackedStr, newMeta);
89
+
90
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: str }, 'tracker.track called with a string value that is already tracked');
91
+ expect(result).to.deep.equal({ extern: null });
92
+ });
93
+
94
+ [
95
+ { prop: 'val' },
96
+ [1, 'two']
97
+ ].forEach((obj) => {
98
+ it(`tracks objects ${inspect(obj)}`, function () {
99
+ const metadata = { foo: 'bar' };
100
+ tracker.track(obj, metadata);
101
+ expect(tracker.getData(obj)).to.deep.equal(metadata);
102
+ });
103
+ });
104
+
105
+ it('returns null for getData() if the object is not tracked', function () {
106
+ const obj = new String('foo');
107
+
108
+ const result = tracker.getData(obj);
109
+
110
+ expect(result).to.equal(null);
111
+ });
112
+
113
+ it('returns the metadata of on object that already have been tracked', function () {
114
+ const metadata = { foo: 'bar' };
115
+ const obj = new String('foo');
116
+
117
+ const returnedMetadata = tracker.track(obj, metadata);
118
+
119
+ expect(tracker.track(obj, metadata)).to.equal(returnedMetadata);
120
+ });
121
+
122
+ it('logs an error when the value is not trackable', function () {
123
+ const metadata = { foo: 'bar' };
124
+ const value = 123;
125
+
126
+ const result = tracker.track(value, metadata);
127
+
128
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value }, 'tracker.track called with a value type that is not trackable');
129
+ expect(result).to.be.null;
130
+ });
131
+
132
+ it('untracks strings', function () {
133
+ const str = 'foo';
134
+ const meta = {
135
+ source: 'query',
136
+ tags: {
137
+ userControlled: [0, 2],
138
+ urlEncoded: [[0, 2]],
139
+ },
140
+ history: [{ some: 'history' }],
141
+ resultTracked: true,
142
+ };
143
+
144
+ const trackedStr = tracker.track(str, meta).extern;
145
+ tracker.untrack(trackedStr);
146
+
147
+ expect(tracker.getData(trackedStr)).to.be.null;
148
+ });
149
+
150
+ it('untracks objects', function () {
151
+ const obj = new String('foo');
152
+ const meta = {
153
+ source: 'query',
154
+ tags: {
155
+ userControlled: [0, 2],
156
+ urlEncoded: [[0, 2]],
157
+ }
158
+ };
159
+
160
+ tracker.track(obj, meta);
161
+ tracker.untrack(obj);
162
+
163
+ expect(tracker.getData(obj)).to.be.null;
164
+ });
165
+
166
+ it('returns null if untrack() argument is not string or object)', function () {
167
+ expect(tracker.untrack(1)).to.be.null;
168
+ });
169
+
170
+ describe('error checks with mocked `externalize` method', function () {
171
+ const originalExternalize = distringuish.externalize;
172
+
173
+ beforeEach(function () {
174
+ distringuish.externalize = sinon.stub().callsFake((...a) => Number(a[0]));
175
+ });
176
+
177
+ afterEach(function () {
178
+ distringuish.externalize = originalExternalize;
179
+ });
180
+
181
+ it('error for zero-length string', function () {
182
+ const result = tracker.track('1', {});
183
+
184
+ expect(result).to.deep.equal({ extern: null });
185
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: '1' }, 'tracker.track was unable to externalize because zero-length string was passed');
186
+ });
187
+
188
+ it('error for non-two-byte-encoded string', function () {
189
+ const result = tracker.track('2', {});
190
+
191
+ expect(result).to.deep.equal({ extern: null });
192
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: '2' }, 'tracker.track was unable to externalize because non-two-byte encoded string was passed');
193
+ });
194
+
195
+ it('error for unsucessful conversion', function () {
196
+ const result = tracker.track('3', {});
197
+
198
+ expect(result).to.deep.equal({ extern: null });
199
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: '3' }, 'tracker.track was unable to externalize because distringuish was unable to convert a MaybeLocal to Local');
200
+ });
201
+
202
+ it('error for isExternal returning false', function () {
203
+ const result = tracker.track('4', {});
204
+
205
+ expect(result).to.deep.equal({ extern: null });
206
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: '4' }, 'tracker.track was unable to externalize because distinguish\'s isExternal call returned false');
207
+ });
208
+
209
+ it('error for unknown', function () {
210
+ const result = tracker.track('5', {});
211
+
212
+ expect(result).to.deep.equal({ extern: null });
213
+ expect(core.logger.error).to.have.been.calledOnceWith({ err: sinon.match.any, value: '5' }, 'tracker.track was unable to externalize because unknown error while trying to externalize the string was encountered');
214
+ });
215
+ });
216
+ });
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const { isSafeContentType, SAFE_XSS_CONTENT_TYPES } = require('./is-safe-content-type');
4
+ const { expect } = require('chai');
5
+
6
+ describe('assess dataflow utils isSafeContentType', function() {
7
+ SAFE_XSS_CONTENT_TYPES.forEach(type => {
8
+ it(`should returns true for when content type is ${type}`, function() {
9
+ expect(isSafeContentType(type)).to.be.true;
10
+ });
11
+ });
12
+
13
+ it('should return false when content type is not part of the safe types', function() {
14
+ expect(isSafeContentType('something')).to.be.false;
15
+ });
16
+ });