@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,145 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+ const { InputType } = require('@contrast/common');
7
+ const koaInstr = require('./koa2');
8
+
9
+ describe('assess dataflow koa v2.x', function () {
10
+ let core,
11
+ simulateRequestScope,
12
+ koaMock,
13
+ nextStub,
14
+ query,
15
+ sources;
16
+
17
+
18
+ beforeEach(function () {
19
+ ({ core, simulateRequestScope } = initAssessFixture());
20
+
21
+ sources = core.assess.dataflow.sources;
22
+ sinon.spy(sources, 'handle');
23
+ sinon.spy(core.patcher, 'patch');
24
+
25
+ query = { parameter: 'parsed-query' };
26
+ koaMock = {
27
+ prototype: {
28
+ middleware: [],
29
+ use: (newMiddleware) => {
30
+ newMiddleware && koaMock.prototype.middleware && koaMock.prototype.middleware.push(newMiddleware);
31
+ },
32
+ }
33
+ };
34
+
35
+ nextStub = sinon.stub();
36
+
37
+ koaInstr(core).install();
38
+ core.depHooks.resolve.yield(koaMock);
39
+ core.patcher.patch.resetHistory();
40
+ });
41
+
42
+ it('calls patcher for every necessary instrumentation', function () {
43
+ core.depHooks.resolve.yield(koaMock);
44
+
45
+ expect(core.patcher.patch).to.have.been.calledOnce;
46
+ expect(core.patcher.patch).to.have.been.calledWithMatch(koaMock.prototype, 'use', {
47
+ name: 'Koa.Application',
48
+ patchType: 'assess-dataflow-source',
49
+ pre: sinon.match.func
50
+ });
51
+ });
52
+
53
+ describe('contrastStartMiddleware', function () {
54
+ it('successfully injects contrastMiddleware', function () {
55
+ koaMock.prototype.use('test');
56
+
57
+ expect(koaMock.prototype.middleware.length).to.equal(2);
58
+ expect(koaMock.prototype.middleware[0]._isContrastStartMiddleware).to.be.true;
59
+ });
60
+
61
+ it('does not inject contrastMiddleware if there is one already injected', function () {
62
+ koaMock.prototype.middleware.push({ _isContrastStartMiddleware: true });
63
+ koaMock.prototype.use();
64
+
65
+ expect(koaMock.prototype.middleware.length).to.equal(1);
66
+ });
67
+
68
+ it('calls `.handle` if there is query on the ctx for `contrastStartMiddleware`', function () {
69
+ koaMock.prototype.use();
70
+
71
+ const contrastStartMiddleware = koaMock.prototype.middleware[0];
72
+ let sourceContext;
73
+
74
+ simulateRequestScope(() => {
75
+ contrastStartMiddleware({ query }, nextStub);
76
+
77
+ sourceContext = core.scopes.sources.getStore()?.assess;
78
+ });
79
+
80
+ expect(nextStub).to.have.been.calledOnce;
81
+ expect(sources.handle).to.have.been.calledOnce;
82
+ expect(sources.handle).to.have.been.calledWithMatch({
83
+ context: 'ctx.query',
84
+ name: 'Koa.Application',
85
+ stacktraceOpts: {
86
+ constructorOpt: sinon.match.func,
87
+ },
88
+ inputType: InputType.QUERYSTRING,
89
+ data: { parameter: 'parsed-query' },
90
+ sourceContext
91
+ });
92
+ });
93
+
94
+ it('does not call `.handle` if there is query on the ctx for `contrastStartMiddleware`, but we have already analyzed it', function () {
95
+ koaMock.prototype.use();
96
+
97
+ const contrastStartMiddleware = koaMock.prototype.middleware[0];
98
+
99
+ simulateRequestScope(() => {
100
+ const sourceContext = core.scopes.sources.getStore()?.assess;
101
+ sourceContext.parsedQuery = 'analyzed-query';
102
+ contrastStartMiddleware({ query }, nextStub);
103
+ });
104
+
105
+ expect(nextStub).to.have.been.calledOnce;
106
+ expect(sources.handle).not.to.have.been.called;
107
+ });
108
+
109
+ it('does not call `.handle` if there is no context', function () {
110
+ koaMock.prototype.use();
111
+
112
+ const contrastStartMiddleware = koaMock.prototype.middleware[0];
113
+
114
+ simulateRequestScope(() => {
115
+ contrastStartMiddleware({ query }, nextStub);
116
+ }, {});
117
+
118
+ expect(nextStub).to.have.been.calledOnce;
119
+ expect(sources.handle).not.to.have.been.called;
120
+ });
121
+
122
+ it('handles a case with an error in the `.handle` method', function () {
123
+ koaMock.prototype.use();
124
+
125
+ const err = new Error('test');
126
+ const contrastStartMiddleware = koaMock.prototype.middleware[0];
127
+
128
+ core.assess.dataflow.sources.handle = () => {
129
+ throw err;
130
+ };
131
+
132
+ simulateRequestScope(() => {
133
+ contrastStartMiddleware({ query }, nextStub);
134
+ });
135
+
136
+ expect(core.logger.error).to.have.been.called;
137
+ expect(core.logger.error).to.have.been.calledWith(
138
+ { err, inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:Koa.Application' },
139
+ 'unable to handle Koa source',
140
+ );
141
+ expect(nextStub).to.have.been.calledOnce;
142
+ expect(nextStub).not.to.have.been.calledWith(err);
143
+ });
144
+ });
145
+ });
@@ -0,0 +1,145 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+ const { InputType } = require('@contrast/common');
7
+ const multer = require('./multer1');
8
+
9
+ describe('assess dataflow sources multer', function () {
10
+ let core,
11
+ simulateRequestScope,
12
+ multerMakeMiddlewareMock,
13
+ multerMiddlewareMock;
14
+
15
+ beforeEach(function () {
16
+ ({ core, simulateRequestScope } = initAssessFixture());
17
+
18
+ sinon.spy(core.patcher, 'patch');
19
+
20
+ core.assess.dataflow.sources.handle = sinon.stub();
21
+
22
+ multerMakeMiddlewareMock = function () {
23
+ return multerMiddlewareMock;
24
+ };
25
+
26
+ multerMiddlewareMock = function (req, res, next) {
27
+ next();
28
+ };
29
+
30
+ multer(core).install();
31
+
32
+ const [patchedFunction] = core.depHooks.resolve
33
+ .withArgs({ name: 'multer', file: 'lib/make-middleware.js' })
34
+ .yield(multerMakeMiddlewareMock);
35
+ multerMakeMiddlewareMock = patchedFunction;
36
+ });
37
+
38
+ it('skip instrumentation if it is out of scope', function () {
39
+ multerMakeMiddlewareMock()({}, {}, () => ({}));
40
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
41
+ });
42
+
43
+ it('skip instrumentation if there are no files ', function () {
44
+ simulateRequestScope(() => {
45
+ const nextFunction = sinon.stub();
46
+ const req = {};
47
+ const res = {};
48
+
49
+ multerMakeMiddlewareMock()(req, res, nextFunction);
50
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
51
+ expect(nextFunction).to.have.been.called;
52
+ });
53
+ });
54
+
55
+ it('handle sources', function () {
56
+ simulateRequestScope(() => {
57
+ const nextFunction = sinon.stub();
58
+ const file = {
59
+ property: 'file',
60
+ };
61
+ const files = [
62
+ {
63
+ property: 'files.0',
64
+ },
65
+ {
66
+ property: 'files.1',
67
+ },
68
+ ];
69
+
70
+ const body = {
71
+ name: 'John'
72
+ };
73
+
74
+ const req = {
75
+ file,
76
+ files,
77
+ body,
78
+ };
79
+ const res = {};
80
+
81
+ multerMakeMiddlewareMock()(req, res, nextFunction);
82
+
83
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
84
+ context: 'req',
85
+ data: req,
86
+ keys: ['file'],
87
+ name: 'multer',
88
+ inputType: InputType.BODY,
89
+ });
90
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
91
+ context: 'req.files',
92
+ data: files[0],
93
+ keys: [0],
94
+ name: 'multer',
95
+ inputType: InputType.BODY,
96
+ });
97
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
98
+ context: 'req.files',
99
+ data: files[1],
100
+ keys: [1],
101
+ name: 'multer',
102
+ inputType: InputType.BODY,
103
+ });
104
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
105
+ context: 'req',
106
+ data: req,
107
+ keys: ['body'],
108
+ name: 'multer',
109
+ inputType: InputType.BODY,
110
+ });
111
+ expect(nextFunction).to.have.been.called;
112
+ });
113
+ });
114
+
115
+ it('should log error if there is an issue handling sources', function () {
116
+ simulateRequestScope(() => {
117
+ const nextFunction = sinon.stub();
118
+ const file = {
119
+ property: 'foo',
120
+ };
121
+ const req = { file };
122
+ const res = {};
123
+ const err = new Error();
124
+
125
+ core.assess.dataflow.sources.handle.callsFake(() => {
126
+ throw err;
127
+ });
128
+
129
+ multerMakeMiddlewareMock()(req, res, nextFunction);
130
+
131
+ expect(
132
+ core.assess.dataflow.sources.handle,
133
+ ).to.have.been.calledWithMatch({
134
+ context: 'req',
135
+ data: { file },
136
+ keys: ['file'],
137
+ name: 'multer',
138
+ inputType: InputType.BODY,
139
+ });
140
+
141
+ expect(core.logger.error).to.have.been.calledWith({ err }, 'error handling multer Assess dataflow %s.%s source', 'req', 'file');
142
+ expect(nextFunction).to.have.been.called;
143
+ });
144
+ });
145
+ });
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+ const { InputType } = require('@contrast/common');
7
+ const qsInstr = require('./qs6');
8
+
9
+ describe('assess dataflow sources qs v6.x', function () {
10
+ let core, simulateRequestScope, qsMock, patchedQs, query, sources;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+
15
+ sources = core.assess.dataflow.sources;
16
+ sinon.spy(sources, 'handle');
17
+
18
+ query = 'querystring';
19
+ qsMock = {
20
+ parse: (query) => ({ query } || {})
21
+ };
22
+ sinon.spy(core.patcher, 'patch');
23
+
24
+ qsInstr(core).install();
25
+ patchedQs = core.depHooks.resolve.yield(qsMock)[0];
26
+ core.patcher.patch.resetHistory();
27
+ });
28
+
29
+ it('calls patcher on the `.parse` method', function () {
30
+ core.depHooks.resolve.yield(qsMock);
31
+
32
+ expect(core.patcher.patch).to.have.been.calledOnce;
33
+ expect(core.patcher.patch).to.have.been.calledWithMatch(qsMock, 'parse', {
34
+ name: 'qs.parse',
35
+ patchType: 'assess-dataflow-source',
36
+ post: sinon.match.func
37
+ });
38
+ });
39
+
40
+ it('calls `.handle` when parsing is successful, in right context and with reqData.queries present', function () {
41
+ let sourceContext;
42
+
43
+ simulateRequestScope(() => {
44
+ sourceContext = core.scopes.sources.getStore()?.assess;
45
+
46
+ Object.assign(sourceContext.reqData, {
47
+ queries: 'querystring'
48
+ });
49
+ patchedQs.parse(query);
50
+ });
51
+
52
+ expect(sources.handle).to.have.been.calledOnce;
53
+ expect(sources.handle).to.have.been.calledWithMatch({
54
+ context: 'req.query',
55
+ name: 'qs.parse',
56
+ stacktraceOpts: {
57
+ constructorOpt: sinon.match.func,
58
+ },
59
+ inputType: InputType.QUERYSTRING,
60
+ data: { query: 'querystring' },
61
+ sourceContext
62
+ });
63
+ });
64
+
65
+ it('does not call `.handle` when parsing is successful, in right context but with reqData.queries not present', function () {
66
+ simulateRequestScope(() => {
67
+ patchedQs.parse(query);
68
+ });
69
+
70
+ expect(sources.handle).not.to.have.been.called;
71
+ expect(core.logger.debug).not.to.have.been.called;
72
+ });
73
+
74
+ it('does not call `.handle` when not in context', function () {
75
+ simulateRequestScope(() => {
76
+ patchedQs.parse(query);
77
+ }, {});
78
+
79
+ expect(sources.handle).not.to.have.been.called;
80
+ });
81
+
82
+ it('does not call `.handle` when the values are already tracked', function () {
83
+ simulateRequestScope(() => {
84
+ const sourceContext = core.scopes.sources.getStore()?.assess;
85
+ sourceContext.parsedQuery = true;
86
+
87
+ patchedQs.parse(query);
88
+ });
89
+
90
+ expect(sources.handle).not.to.have.been.called;
91
+ expect(core.logger.trace).to.have.been.calledWith(
92
+ { inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:qs.parse' },
93
+ 'values already tracked',
94
+ );
95
+ });
96
+
97
+ it('does not call `.handle` when `qs` does not return a result', function () {
98
+ simulateRequestScope(() => {
99
+ const sourceContext = core.scopes.sources.getStore()?.assess;
100
+
101
+ Object.assign(sourceContext.reqData, {
102
+ queries: 'querystring'
103
+ });
104
+ patchedQs.parse(null);
105
+ });
106
+
107
+ expect(sources.handle).not.to.have.been.called;
108
+ });
109
+
110
+ it('handles a case with an error in the `.handle` method', function () {
111
+ const err = new Error('test');
112
+
113
+ core.assess.dataflow.sources.handle = () => {
114
+ throw err;
115
+ };
116
+
117
+ simulateRequestScope(() => {
118
+ const sourceContext = core.scopes.sources.getStore()?.assess;
119
+
120
+ Object.assign(sourceContext.reqData, {
121
+ queries: 'querystring'
122
+ });
123
+ patchedQs.parse(query);
124
+ });
125
+
126
+ expect(core.logger.error).to.have.been.calledWith(
127
+ { err, inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:qs.parse' },
128
+ 'unable to handle source',
129
+ );
130
+ });
131
+ });
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+ const { InputType } = require('@contrast/common');
7
+
8
+ describe('assess dataflow sources querystring', function () {
9
+ let core, simulateRequestScope, querystring;
10
+
11
+ beforeEach(function () {
12
+ ({ core, simulateRequestScope } = initAssessFixture());
13
+
14
+ sinon.stub(core.assess.dataflow.sources, 'handle');
15
+
16
+ const querystringMock = {
17
+ parse: (query) => ({ query })
18
+ };
19
+
20
+ require('./querystring')(core).install();
21
+ querystring = core.depHooks.resolve.yield(querystringMock)[0];
22
+ });
23
+
24
+ it('calls `.handle` when parsing is successful, in right context and with reqData.queries present', function () {
25
+ simulateRequestScope(() => {
26
+ querystring.parse('query');
27
+
28
+ expect(core.assess.dataflow.sources.handle).to.have.been.calledWith({
29
+ context: 'req.query',
30
+ name: 'querystring.parse',
31
+ stacktraceOpts: {
32
+ constructorOpt: sinon.match.func,
33
+ prependFrames: sinon.match.array,
34
+ },
35
+ inputType: InputType.QUERYSTRING,
36
+ data: { query: 'query' },
37
+ sourceContext: core.scopes.sources.getStore().assess
38
+ });
39
+ }, { assess: { reqData: { queries: 'query' } } });
40
+ });
41
+
42
+ it('does not call `.handle` when parsing is successful, in right context but without matching reqData.queries', function () {
43
+ simulateRequestScope(() => {
44
+ querystring.parse('query1');
45
+
46
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
47
+ }, { assess: { reqData: { queries: 'query2' } } });
48
+ });
49
+
50
+ it('does not call `.handle` when not in context', function () {
51
+ simulateRequestScope(() => {
52
+ querystring.parse('query');
53
+
54
+ expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
55
+ }, {});
56
+ });
57
+
58
+ it('does not call `.handle` when the values are already tracked', function () {
59
+ simulateRequestScope(() => {
60
+ querystring.parse('query');
61
+
62
+ expect(core.logger.trace).to.have.been.calledWith(
63
+ { funcKey: 'assess-dataflow-source:querystring.parse' },
64
+ 'values already tracked'
65
+ );
66
+ }, { assess: { parsedQuery: true } });
67
+ });
68
+
69
+ it('handles a case with an error in the `.handle` method', function () {
70
+ const err = new Error('test');
71
+ core.assess.dataflow.sources.handle.throws(err);
72
+
73
+ simulateRequestScope(() => {
74
+ querystring.parse('query');
75
+
76
+ expect(core.logger.error).to.have.been.calledWith(
77
+ { err, funcKey: 'assess-dataflow-source:querystring.parse' },
78
+ 'unable to handle source'
79
+ );
80
+ }, { assess: { reqData: { queries: 'query' } } });
81
+ });
82
+ });
@@ -0,0 +1,88 @@
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('./fieldedTextBodyParser');
8
+
9
+ describe('assess dataflow sources restify fieldedTextBodyParser', function () {
10
+ let core, simulateRequestScope, handle, middleware, req, res, next;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+ handle = sinon.stub(core.assess.dataflow.sources, 'handle');
15
+
16
+ let fieldedTextBodyParserStub = sinon.stub().returns(sinon.stub().callsArg(2));
17
+
18
+ req = {};
19
+ res = {};
20
+ next = sinon.stub();
21
+
22
+ core.depHooks.resolve
23
+ .withArgs({ name: 'restify', file: 'lib/plugins/fieldedTextBodyParser.js', version: '>=8' })
24
+ .callsFake((_, cb) => {
25
+ fieldedTextBodyParserStub = cb(fieldedTextBodyParserStub);
26
+ });
27
+
28
+ init(core).install();
29
+ middleware = fieldedTextBodyParserStub();
30
+ });
31
+
32
+ it('does not call `handle` when not in context', function () {
33
+ simulateRequestScope(() => {
34
+ middleware(req, res, next);
35
+
36
+ expect(handle).not.to.have.been.called;
37
+ expect(core.logger.error).to.have.been.calledWith(
38
+ { funcKey: 'assess-dataflow-source:restify.plugins.fieldedTextBodyParser.parseFieldedText' },
39
+ 'unable to handle source. Missing `sourceContext`'
40
+ );
41
+ }, {});
42
+ });
43
+
44
+ it('does not call `handle` when body is already tracked', function () {
45
+ simulateRequestScope(() => {
46
+ middleware(req, res, next);
47
+
48
+ expect(handle).not.to.have.been.called;
49
+ expect(core.logger.trace).to.have.been.calledWith(
50
+ { funcKey: 'assess-dataflow-source:restify.plugins.fieldedTextBodyParser.parseFieldedText' },
51
+ 'values already tracked'
52
+ );
53
+ }, { assess: { parsedBody: true } });
54
+ });
55
+
56
+ it('calls `handle` appropriately', function () {
57
+ req.body = [{ foo: 'bar' }];
58
+
59
+ simulateRequestScope(() => {
60
+ middleware(req, res, next);
61
+
62
+ expect(handle).to.have.been.calledWith({
63
+ context: 'req.body',
64
+ data: req.body,
65
+ inputType: InputType.BODY,
66
+ name: 'restify.plugins.fieldedTextBodyParser.parseFieldedText',
67
+ stacktraceOpts: {
68
+ constructorOpt: sinon.match.func,
69
+ },
70
+ sourceContext: sinon.match.object,
71
+ });
72
+ });
73
+ });
74
+
75
+ it('logs errors in the `handle` method', function () {
76
+ const err = new Error('yikes!');
77
+ handle.throws(err);
78
+
79
+ simulateRequestScope(() => {
80
+ middleware(req, res, next);
81
+
82
+ expect(core.logger.error).to.have.been.calledWith(
83
+ { err, funcKey: 'assess-dataflow-source:restify.plugins.fieldedTextBodyParser.parseFieldedText' },
84
+ 'unable to handle Restify source',
85
+ );
86
+ });
87
+ });
88
+ });
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const proxyquire = require('proxyquire');
5
+ const mocks = require('@contrast/test/mocks');
6
+
7
+ const moduleMock = (moduleName, mock) => (deps) => {
8
+ deps.assess.dataflow.sources.restifyInstrumentation[moduleName] = mock[moduleName];
9
+ };
10
+
11
+ describe('assess dataflow sources restify', function () {
12
+ let core, restifyInstrumentation, init, modules;
13
+
14
+ beforeEach(function () {
15
+ core = mocks.core();
16
+ core.assess = mocks.assess();
17
+ restifyInstrumentation = core.assess.dataflow.sources.restifyInstrumentation;
18
+ init = proxyquire('.', {
19
+ './fieldedTextBodyParser': moduleMock('fieldedTextBodyParser', restifyInstrumentation),
20
+ './jsonBodyParser': moduleMock('jsonBodyParser', restifyInstrumentation),
21
+ './router': moduleMock('router', restifyInstrumentation),
22
+ });
23
+ modules = [
24
+ 'fieldedTextBodyParser',
25
+ 'jsonBodyParser',
26
+ 'router',
27
+ ];
28
+ });
29
+
30
+ it('installs the components', function () {
31
+ init(core).install();
32
+
33
+ modules.forEach((module) => {
34
+ expect(restifyInstrumentation[module].install).to.have.been.calledOnce;
35
+ });
36
+ });
37
+ });
38
+