@contrast/assess 1.31.0 → 1.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/lib/crypto-analysis/install/crypto.test.js +146 -0
  2. package/lib/crypto-analysis/install/math.test.js +65 -0
  3. package/lib/dataflow/index.test.js +36 -0
  4. package/lib/dataflow/propagation/index.test.js +103 -0
  5. package/lib/dataflow/propagation/install/JSON/index.test.js +50 -0
  6. package/lib/dataflow/propagation/install/JSON/parse-fn.test.js +232 -0
  7. package/lib/dataflow/propagation/install/JSON/parse.test.js +968 -0
  8. package/lib/dataflow/propagation/install/JSON/stringify.test.js +265 -0
  9. package/lib/dataflow/propagation/install/array-prototype-join.test.js +106 -0
  10. package/lib/dataflow/propagation/install/buffer.test.js +109 -0
  11. package/lib/dataflow/propagation/install/contrast-methods/add.test.js +94 -0
  12. package/lib/dataflow/propagation/install/contrast-methods/index.test.js +49 -0
  13. package/lib/dataflow/propagation/install/contrast-methods/number.test.js +50 -0
  14. package/lib/dataflow/propagation/install/contrast-methods/string.test.js +148 -0
  15. package/lib/dataflow/propagation/install/contrast-methods/tag.test.js +145 -0
  16. package/lib/dataflow/propagation/install/decode-uri-component.test.js +78 -0
  17. package/lib/dataflow/propagation/install/ejs/escape-xml.test.js +69 -0
  18. package/lib/dataflow/propagation/install/ejs/template.test.js +62 -0
  19. package/lib/dataflow/propagation/install/encode-uri.test.js +83 -0
  20. package/lib/dataflow/propagation/install/escape-html.test.js +71 -0
  21. package/lib/dataflow/propagation/install/escape.test.js +73 -0
  22. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.test.js +71 -0
  23. package/lib/dataflow/propagation/install/isnumeric-0.test.js +58 -0
  24. package/lib/dataflow/propagation/install/joi/any.test.js +270 -0
  25. package/lib/dataflow/propagation/install/joi/array.test.js +912 -0
  26. package/lib/dataflow/propagation/install/joi/boolean.test.js +103 -0
  27. package/lib/dataflow/propagation/install/joi/expression.test.js +76 -0
  28. package/lib/dataflow/propagation/install/joi/index.test.js +39 -0
  29. package/lib/dataflow/propagation/install/joi/number.test.js +103 -0
  30. package/lib/dataflow/propagation/install/joi/object.test.js +119 -0
  31. package/lib/dataflow/propagation/install/joi/ref.test.js +607 -0
  32. package/lib/dataflow/propagation/install/joi/string-schema.test.js +513 -0
  33. package/lib/dataflow/propagation/install/mongoose/index.test.js +42 -0
  34. package/lib/dataflow/propagation/install/mongoose/schema-map.test.js +348 -0
  35. package/lib/dataflow/propagation/install/mongoose/schema-mixed.test.js +512 -0
  36. package/lib/dataflow/propagation/install/mongoose/schema-string.test.js +160 -0
  37. package/lib/dataflow/propagation/install/mustache-escape.test.js +62 -0
  38. package/lib/dataflow/propagation/install/mysql-connection-escape.test.js +74 -0
  39. package/lib/dataflow/propagation/install/parse-int.test.js +48 -0
  40. package/lib/dataflow/propagation/install/path/basename.test.js +143 -0
  41. package/lib/dataflow/propagation/install/path/dirname.test.js +167 -0
  42. package/lib/dataflow/propagation/install/path/extname.test.js +141 -0
  43. package/lib/dataflow/propagation/install/path/format.test.js +250 -0
  44. package/lib/dataflow/propagation/install/path/index.test.js +45 -0
  45. package/lib/dataflow/propagation/install/path/join-and-resolve.test.js +485 -0
  46. package/lib/dataflow/propagation/install/path/normalize.test.js +176 -0
  47. package/lib/dataflow/propagation/install/path/parse.test.js +238 -0
  48. package/lib/dataflow/propagation/install/path/relative.test.js +239 -0
  49. package/lib/dataflow/propagation/install/path/toNamespacedPath.test.js +158 -0
  50. package/lib/dataflow/propagation/install/pug/index.test.js +55 -0
  51. package/lib/dataflow/propagation/install/pug-runtime-escape.test.js +69 -0
  52. package/lib/dataflow/propagation/install/querystring/escape.test.js +63 -0
  53. package/lib/dataflow/propagation/install/querystring/index.test.js +40 -0
  54. package/lib/dataflow/propagation/install/querystring/parse.test.js +272 -0
  55. package/lib/dataflow/propagation/install/querystring/stringify.test.js +301 -0
  56. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.test.js +281 -0
  57. package/lib/dataflow/propagation/install/send.test.js +63 -0
  58. package/lib/dataflow/propagation/install/sequelize/query-generator.test.js +73 -0
  59. package/lib/dataflow/propagation/install/sequelize/sql-string.test.js +130 -0
  60. package/lib/dataflow/propagation/install/sql-template-strings.test.js +100 -0
  61. package/lib/dataflow/propagation/install/string/concat.test.js +132 -0
  62. package/lib/dataflow/propagation/install/string/format-methods.test.js +61 -0
  63. package/lib/dataflow/propagation/install/string/html-methods.test.js +164 -0
  64. package/lib/dataflow/propagation/install/string/index.test.js +103 -0
  65. package/lib/dataflow/propagation/install/string/match-all.test.js +399 -0
  66. package/lib/dataflow/propagation/install/string/match.test.js +361 -0
  67. package/lib/dataflow/propagation/install/string/replace.test.js +588 -0
  68. package/lib/dataflow/propagation/install/string/slice.test.js +265 -0
  69. package/lib/dataflow/propagation/install/string/split.test.js +500 -0
  70. package/lib/dataflow/propagation/install/string/substring.test.js +238 -0
  71. package/lib/dataflow/propagation/install/string/trim.test.js +122 -0
  72. package/lib/dataflow/propagation/install/unescape.test.js +78 -0
  73. package/lib/dataflow/propagation/install/url/domain-parsers.test.js +63 -0
  74. package/lib/dataflow/propagation/install/url/parse.test.js +391 -0
  75. package/lib/dataflow/propagation/install/url/searchParams.test.js +538 -0
  76. package/lib/dataflow/propagation/install/url/url.test.js +466 -0
  77. package/lib/dataflow/propagation/install/util-format.test.js +336 -0
  78. package/lib/dataflow/propagation/install/validator/hooks.test.js +211 -0
  79. package/lib/dataflow/sinks/index.test.js +78 -0
  80. package/lib/dataflow/sinks/install/child-process.test.js +338 -0
  81. package/lib/dataflow/sinks/install/eval.test.js +95 -0
  82. package/lib/dataflow/sinks/install/express/index.test.js +33 -0
  83. package/lib/dataflow/sinks/install/express/reflected-xss.js +55 -57
  84. package/lib/dataflow/sinks/install/express/reflected-xss.test.js +109 -0
  85. package/lib/dataflow/sinks/install/express/unvalidated-redirect.test.js +144 -0
  86. package/lib/dataflow/sinks/install/fastify/index.test.js +32 -0
  87. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.test.js +130 -0
  88. package/lib/dataflow/sinks/install/fs.test.js +138 -0
  89. package/lib/dataflow/sinks/install/function.test.js +103 -0
  90. package/lib/dataflow/sinks/install/hapi/index.test.js +32 -0
  91. package/lib/dataflow/sinks/install/hapi/unvalidated-redirect.test.js +130 -0
  92. package/lib/dataflow/sinks/install/http/index.test.js +33 -0
  93. package/lib/dataflow/sinks/install/http/request.test.js +184 -0
  94. package/lib/dataflow/sinks/install/http/server-response.test.js +162 -0
  95. package/lib/dataflow/sinks/install/koa/index.test.js +32 -0
  96. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.test.js +200 -0
  97. package/lib/dataflow/sinks/install/libxmljs.test.js +158 -0
  98. package/lib/dataflow/sinks/install/marsdb.test.js +166 -0
  99. package/lib/dataflow/sinks/install/mongodb.test.js +621 -0
  100. package/lib/dataflow/sinks/install/mssql.test.js +136 -0
  101. package/lib/dataflow/sinks/install/mysql.test.js +233 -0
  102. package/lib/dataflow/sinks/install/node-serialize.test.js +85 -0
  103. package/lib/dataflow/sinks/install/postgres.test.js +158 -0
  104. package/lib/dataflow/sinks/install/restify.test.js +142 -0
  105. package/lib/dataflow/sinks/install/sequelize.test.js +100 -0
  106. package/lib/dataflow/sinks/install/sqlite3.test.js +118 -0
  107. package/lib/dataflow/sinks/install/vm.test.js +326 -0
  108. package/lib/dataflow/sources/handler.test.js +463 -0
  109. package/lib/dataflow/sources/index.test.js +58 -0
  110. package/lib/dataflow/sources/install/body-parser1.test.js +248 -0
  111. package/lib/dataflow/sources/install/busboy.test.js +152 -0
  112. package/lib/dataflow/sources/install/cookie-parser1.test.js +143 -0
  113. package/lib/dataflow/sources/install/express/params.test.js +105 -0
  114. package/lib/dataflow/sources/install/express/parsedUrl.test.js +65 -0
  115. package/lib/dataflow/sources/install/fastify/fastify.test.js +210 -0
  116. package/lib/dataflow/sources/install/fastify/index.test.js +33 -0
  117. package/lib/dataflow/sources/install/formidable1.test.js +119 -0
  118. package/lib/dataflow/sources/install/hapi/hapi.test.js +172 -0
  119. package/lib/dataflow/sources/install/hapi/index.test.js +33 -0
  120. package/lib/dataflow/sources/install/http.test.js +155 -0
  121. package/lib/dataflow/sources/install/koa/index.test.js +40 -0
  122. package/lib/dataflow/sources/install/koa/koa-bodyparsers.test.js +161 -0
  123. package/lib/dataflow/sources/install/koa/koa-multer.test.js +197 -0
  124. package/lib/dataflow/sources/install/koa/koa-routers.test.js +146 -0
  125. package/lib/dataflow/sources/install/koa/koa2.test.js +145 -0
  126. package/lib/dataflow/sources/install/multer1.test.js +145 -0
  127. package/lib/dataflow/sources/install/qs6.test.js +131 -0
  128. package/lib/dataflow/sources/install/querystring.test.js +82 -0
  129. package/lib/dataflow/sources/install/restify/fieldedTextBodyParser.test.js +88 -0
  130. package/lib/dataflow/sources/install/restify/index.test.js +38 -0
  131. package/lib/dataflow/sources/install/restify/jsonBodyParser.test.js +144 -0
  132. package/lib/dataflow/sources/install/restify/router.test.js +83 -0
  133. package/lib/dataflow/tag-utils-complete.test.js +27 -0
  134. package/lib/dataflow/tag-utils.test.js +192 -0
  135. package/lib/dataflow/tracker.js +7 -2
  136. package/lib/dataflow/tracker.test.js +216 -0
  137. package/lib/dataflow/utils/is-safe-content-type.test.js +16 -0
  138. package/lib/dataflow/utils/is-vulnerable.test.js +115 -0
  139. package/lib/event-factory.test.js +321 -0
  140. package/lib/get-policy.test.js +194 -0
  141. package/lib/get-source-context.js +10 -1
  142. package/lib/get-source-context.test.js +108 -0
  143. package/lib/index.test.js +41 -0
  144. package/lib/make-source-context.test.js +50 -0
  145. package/lib/response-scanning/handlers/index.test.js +425 -0
  146. package/lib/response-scanning/handlers/utils.test.js +391 -0
  147. package/lib/response-scanning/index.test.js +41 -0
  148. package/lib/response-scanning/install/http.test.js +175 -0
  149. package/lib/rule-scopes.test.js +27 -0
  150. package/lib/session-configuration/handlers.test.js +84 -0
  151. package/lib/session-configuration/index.test.js +36 -0
  152. package/lib/session-configuration/install/express-session.test.js +220 -0
  153. package/lib/session-configuration/install/fastify-cookie.test.js +65 -0
  154. package/lib/session-configuration/install/hapi.test.js +269 -0
  155. package/lib/session-configuration/install/koa.test.js +92 -0
  156. package/package.json +2 -2
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('assess dataflow sources express parsedUrl', function () {
8
+ let core, instrumentation, patcher, simulateRequestScope, middleware, req, res, tracker;
9
+
10
+ beforeEach(function () {
11
+ ({ core, simulateRequestScope } = initAssessFixture());
12
+
13
+ instrumentation = core.assess.dataflow.sources.expressInstrumentation.parsedUrl;
14
+ tracker = core.assess.dataflow.tracker;
15
+ patcher = core.patcher;
16
+
17
+ req = {};
18
+ res = {};
19
+ middleware = {
20
+ init() {
21
+ return function (req, res, next) {
22
+ req._parsedUrl = {
23
+ href: '/some/path?asdf=jkl',
24
+ path: '/some/path?asdf=jkl',
25
+ pathname: '/some/path',
26
+ query: 'asdf=jkl',
27
+ search: '?asdf=jkl;',
28
+ };
29
+ next();
30
+ };
31
+ }
32
+ };
33
+
34
+ core.depHooks
35
+ .resolve
36
+ .withArgs({ name: 'express', file: 'lib/middleware/init.js' })
37
+ .yields(middleware);
38
+ });
39
+
40
+ it('hooks init next in order to track req._parsedUrl values', function () {
41
+ instrumentation.install();
42
+ expect(patcher.isContrastHooked(middleware.init)).to.be.true;
43
+
44
+ const middlewareFn = middleware.init('text');
45
+
46
+ simulateRequestScope(() => {
47
+ const cb = sinon.spy(function () {
48
+ [
49
+ 'href',
50
+ 'path',
51
+ 'pathname',
52
+ 'query',
53
+ 'search',
54
+ ].forEach((prop) => {
55
+ const strInfo = tracker.getData(req._parsedUrl[prop]);
56
+ expect(strInfo).to.be.ok;
57
+ });
58
+ });
59
+
60
+ middlewareFn(req, res, cb);
61
+
62
+ expect(cb).to.have.been.called;
63
+ });
64
+ });
65
+ });
@@ -0,0 +1,210 @@
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 fastifyInstr = require('./fastify');
8
+
9
+ describe('assess dataflow sources fastify', function () {
10
+ let core, simulateRequestScope, fastifyServerMock, serverMock, sources, reqMock, replyMock, doneMock;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+ sources = core.assess.dataflow.sources;
15
+
16
+ sinon.spy(sources, 'handle');
17
+
18
+ reqMock = {
19
+ query: { foo: 'bar' },
20
+ params: { frogs: 'bugs' },
21
+ body: { fred: 'barney' },
22
+ };
23
+ replyMock = {};
24
+ doneMock = sinon.stub();
25
+ serverMock = {
26
+ addHook: sinon.stub().yields(reqMock, replyMock, doneMock),
27
+ };
28
+ fastifyServerMock = () => serverMock;
29
+ core.depHooks
30
+ .resolve
31
+ .withArgs({ name: 'fastify', version: '>=3.2.0' })
32
+ .callsFake((desc, cb) => {
33
+ fastifyServerMock = cb(fastifyServerMock);
34
+ });
35
+
36
+ fastifyInstr(core).install();
37
+ core.logger.trace.resetHistory();
38
+ });
39
+
40
+ afterEach(function () {
41
+ sources.handle.restore?.();
42
+ });
43
+
44
+ describe('preValidationHook', function () {
45
+ const commonData = {
46
+ name: 'fastify.preValidation',
47
+ stacktraceOpts: {
48
+ constructorOpt: sinon.match((value) => typeof value === 'function' && /handler/i.test(value.name)),
49
+ },
50
+ };
51
+
52
+ it('adds hook and tracks the body, headers and query/url params', function () {
53
+ simulateRequestScope(function () {
54
+ fastifyServerMock();
55
+ const sourceContext = core.scopes.sources.getStore()?.assess;
56
+
57
+ expect(serverMock.addHook).to.have.been.calledOnce;
58
+ expect(doneMock).to.have.been.calledOnce;
59
+
60
+ expect(sources.handle).to.have.been.calledWith({
61
+ ...commonData,
62
+ context: 'req.query',
63
+ inputType: InputType.QUERYSTRING,
64
+ data: reqMock.query,
65
+ sourceContext,
66
+ });
67
+ expect(sources.handle).to.have.been.calledWith({
68
+ ...commonData,
69
+ context: 'req.params',
70
+ inputType: InputType.URL_PARAMETER,
71
+ data: reqMock.params,
72
+ sourceContext
73
+ });
74
+ expect(sources.handle).to.have.been.calledWith({
75
+ ...commonData,
76
+ context: 'req.body',
77
+ inputType: InputType.PARAMETER_VALUE,
78
+ data: reqMock.body,
79
+ sourceContext
80
+ });
81
+ });
82
+
83
+ simulateRequestScope(function () {
84
+ reqMock.headers = {
85
+ 'content-type': 'application/json'
86
+ };
87
+ fastifyServerMock();
88
+ const sourceContext = core.scopes.sources.getStore()?.assess;
89
+
90
+ expect(serverMock.addHook).to.have.been.calledTwice;
91
+ expect(doneMock).to.have.been.calledTwice;
92
+
93
+ expect(sources.handle).to.have.been.calledWith({
94
+ ...commonData,
95
+ context: 'req.query',
96
+ inputType: InputType.QUERYSTRING,
97
+ data: reqMock.query,
98
+ sourceContext
99
+ });
100
+ expect(sources.handle).to.have.been.calledWith({
101
+ ...commonData,
102
+ context: 'req.params',
103
+ inputType: InputType.URL_PARAMETER,
104
+ data: reqMock.params,
105
+ sourceContext
106
+ });
107
+ expect(sources.handle).to.have.been.calledWith({
108
+ ...commonData,
109
+ context: 'req.body',
110
+ inputType: InputType.JSON_VALUE,
111
+ data: reqMock.body,
112
+ sourceContext
113
+ });
114
+ });
115
+
116
+ simulateRequestScope(function () {
117
+ delete reqMock.headers;
118
+ reqMock.body = 'non-object-body';
119
+ fastifyServerMock();
120
+ const sourceContext = core.scopes.sources.getStore()?.assess;
121
+
122
+ expect(serverMock.addHook).to.have.been.calledThrice;
123
+ expect(doneMock).to.have.been.calledThrice;
124
+
125
+ expect(sources.handle).to.have.been.calledWith({
126
+ ...commonData,
127
+ context: 'req.query',
128
+ inputType: InputType.QUERYSTRING,
129
+ data: reqMock.query,
130
+ sourceContext,
131
+ });
132
+ expect(sources.handle).to.have.been.calledWith({
133
+ ...commonData,
134
+ context: 'req.params',
135
+ inputType: InputType.URL_PARAMETER,
136
+ data: reqMock.params,
137
+ sourceContext
138
+ });
139
+ expect(sources.handle).to.have.been.calledWith({
140
+ ...commonData,
141
+ context: 'req.body',
142
+ inputType: InputType.BODY,
143
+ data: reqMock.body,
144
+ sourceContext
145
+ });
146
+ });
147
+ });
148
+
149
+ it('does not call `.handle` when not in context', function () {
150
+ simulateRequestScope(() => {
151
+ fastifyServerMock();
152
+ }, {});
153
+
154
+ expect(sources.handle).not.to.have.been.called;
155
+ });
156
+
157
+ it('does not call `.handle` when the values are already tracked', function () {
158
+ simulateRequestScope(() => {
159
+ const sourceContext = core.scopes.sources.getStore()?.assess;
160
+ sourceContext.parsedQuery = true;
161
+ sourceContext.parsedParams = true;
162
+ sourceContext.parsedBody = true;
163
+
164
+ fastifyServerMock();
165
+ });
166
+
167
+ expect(sources.handle).not.to.have.been.called;
168
+ expect(core.logger.trace).to.have.been.calledThrice;
169
+ expect(core.logger.trace).to.have.been.calledWith(
170
+ { inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:fastify.constructor' },
171
+ 'values already tracked',
172
+ );
173
+ expect(core.logger.trace).to.have.been.calledWith(
174
+ { inputType: InputType.URL_PARAMETER, funcKey: 'assess-dataflow-source:fastify.constructor' },
175
+ 'values already tracked',
176
+ );
177
+ expect(core.logger.trace).to.have.been.calledWith(
178
+ { inputType: InputType.PARAMETER_VALUE, funcKey: 'assess-dataflow-source:fastify.constructor' },
179
+ 'values already tracked',
180
+ );
181
+ });
182
+
183
+ it('handles a case with an error in the `.handle` method', function () {
184
+ fastifyInstr(core).install();
185
+ const err = new Error('test');
186
+
187
+ core.assess.dataflow.sources.handle = () => {
188
+ throw err;
189
+ };
190
+
191
+ simulateRequestScope(function () {
192
+ fastifyServerMock();
193
+ });
194
+
195
+ expect(core.logger.error).to.have.been.calledThrice;
196
+ expect(core.logger.error).to.have.been.calledWith(
197
+ { err, inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:fastify.constructor' },
198
+ 'unable to handle Fastify source',
199
+ );
200
+ expect(core.logger.error).to.have.been.calledWith(
201
+ { err, inputType: InputType.URL_PARAMETER, funcKey: 'assess-dataflow-source:fastify.constructor' },
202
+ 'unable to handle Fastify source',
203
+ );
204
+ expect(core.logger.error).to.have.been.calledWith(
205
+ { err, inputType: InputType.PARAMETER_VALUE, funcKey: 'assess-dataflow-source:fastify.constructor' },
206
+ 'unable to handle Fastify source',
207
+ );
208
+ });
209
+ });
210
+ });
@@ -0,0 +1,33 @@
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.fastifyInstrumentation[moduleName] = mock[moduleName];
9
+ };
10
+
11
+ describe('assess dataflow sources fastify', function () {
12
+ let core, fastifySourcesMock, fastifySourcesModule, modulesWithInstall;
13
+
14
+ beforeEach(function () {
15
+ core = mocks.core();
16
+ core.assess = mocks.assess();
17
+ fastifySourcesMock = core.assess.dataflow.sources.fastifyInstrumentation;
18
+ fastifySourcesModule = proxyquire('.', {
19
+ './fastify': moduleMock('fastify', fastifySourcesMock)
20
+ });
21
+ modulesWithInstall = [
22
+ 'fastify'
23
+ ];
24
+ });
25
+
26
+ it('installs the components', function () {
27
+ fastifySourcesModule(core).install();
28
+
29
+ modulesWithInstall.forEach((module) => {
30
+ expect(fastifySourcesMock[module].install).to.have.been.calledOnce;
31
+ });
32
+ });
33
+ });
@@ -0,0 +1,119 @@
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 formidableInstr = require('./formidable1');
8
+
9
+ describe('assess dataflow sources formidable v1.x', function () {
10
+ let core, simulateRequestScope, parseMock, patchedParse, req, cbStub, sources;
11
+
12
+ beforeEach(function () {
13
+ ({ core, simulateRequestScope } = initAssessFixture());
14
+
15
+ sources = core.assess.dataflow.sources;
16
+ sinon.spy(sources, 'handle');
17
+
18
+ req = { fields: { parameter: 'parsed' }, files: { fileField: { name: 'some-file' } } };
19
+ parseMock = (req, cb) => {
20
+ if (typeof cb === 'function') {
21
+ cb(null, req.fields, req.files);
22
+ }
23
+ };
24
+ cbStub = sinon.stub();
25
+ sinon.spy(core.patcher, 'patch');
26
+
27
+ formidableInstr(core).install();
28
+ patchedParse = core.depHooks.resolve.yield({ IncomingForm: { prototype: { parse: parseMock } } })[0].IncomingForm.prototype.parse;
29
+ core.patcher.patch.resetHistory();
30
+ });
31
+
32
+ it('calls patcher on the `.parse` method', function () {
33
+ core.depHooks.resolve.yield({ IncomingForm: { prototype: { parse: parseMock } } });
34
+
35
+ expect(core.patcher.patch).to.have.been.calledOnce;
36
+ expect(core.patcher.patch).to.have.been.calledWithMatch(sinon.match.func, {
37
+ name: 'Formidable.IncomingForm.prototype.parse',
38
+ patchType: 'assess-dataflow-source',
39
+ pre: sinon.match.func
40
+ });
41
+ });
42
+
43
+ it('calls `.handle` when parsing is successful and in right context', function () {
44
+ let sourceContext;
45
+
46
+ simulateRequestScope(() => {
47
+ patchedParse(req, cbStub);
48
+
49
+ sourceContext = core.scopes.sources.getStore()?.assess;
50
+ });
51
+
52
+ expect(cbStub).to.have.been.calledOnce;
53
+ expect(sources.handle).to.have.been.calledOnce;
54
+ expect(sources.handle).to.have.been.calledWithMatch({
55
+ context: 'req.body',
56
+ name: 'Formidable.IncomingForm.prototype.parse',
57
+ stacktraceOpts: {
58
+ constructorOpt: sinon.match.func,
59
+ prependFrames: [sinon.match.func]
60
+ },
61
+ inputType: InputType.MULTIPART_VALUE,
62
+ data: { parameter: 'parsed' },
63
+ sourceContext
64
+ });
65
+ });
66
+
67
+ it('logs an error when `handle` throws one', function () {
68
+ const err = new Error('test');
69
+
70
+ core.assess.dataflow.sources.handle = () => {
71
+ throw err;
72
+ };
73
+
74
+ simulateRequestScope(() => {
75
+ patchedParse(req, cbStub);
76
+ });
77
+
78
+ expect(cbStub).to.have.been.calledOnce;
79
+ expect(core.logger.error).to.have.been.calledWith(
80
+ { err, inputType: InputType.MULTIPART_VALUE, funcKey: 'assess-dataflow-source:Formidable.IncomingForm.prototype.parse' },
81
+ 'unable to handle source',
82
+ );
83
+ });
84
+
85
+ it('does not call `.handle` when not in context', function () {
86
+ simulateRequestScope(() => {
87
+ patchedParse(req, cbStub);
88
+ }, {});
89
+
90
+ expect(sources.handle).not.to.have.been.called;
91
+ expect(cbStub).to.have.been.calledOnce;
92
+ });
93
+
94
+ it('does not call `.handle` when the values are already tracked', function () {
95
+ simulateRequestScope(() => {
96
+ const sourceContext = core.scopes.sources.getStore()?.assess;
97
+ sourceContext.parsedBody = true;
98
+
99
+ patchedParse(req, cbStub);
100
+ });
101
+
102
+ expect(sources.handle).not.to.have.been.called;
103
+ expect(core.logger.trace).to.have.been.calledWith(
104
+ { inputType: InputType.MULTIPART_VALUE, funcKey: 'assess-dataflow-source:Formidable.IncomingForm.prototype.parse' },
105
+ 'values already tracked',
106
+ );
107
+ expect(cbStub).to.have.been.calledOnce;
108
+ });
109
+
110
+ it('does not call `.handle` when `formidable` does not return a result', function () {
111
+ simulateRequestScope(() => {
112
+ patchedParse({}, cbStub);
113
+ });
114
+
115
+ expect(sources.handle).not.to.have.been.called;
116
+ expect(core.logger.debug).not.to.have.been.called;
117
+ expect(cbStub).to.have.been.calledOnce;
118
+ });
119
+ });
@@ -0,0 +1,172 @@
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
+
8
+ describe('assess dataflow sources hapi', function () {
9
+
10
+ const hapi = {
11
+ server(server) {
12
+ return server;
13
+ },
14
+ Server(server) {
15
+ return server;
16
+ }
17
+ };
18
+
19
+ const reqMock = {
20
+ query: { q: 'value' },
21
+ path: '/uri/{id}',
22
+ state: { cookie: 'foo' },
23
+ payload: { foo: 'bar' },
24
+ params: { p: '1' },
25
+ };
26
+
27
+ const hMock = {
28
+ continue: sinon.stub()
29
+ };
30
+
31
+ let core, sources, simulateRequestScope;
32
+
33
+ beforeEach(function () {
34
+ ({ core, simulateRequestScope } = initAssessFixture());
35
+
36
+ core.depHooks.resolve
37
+ .withArgs({ name: '@hapi/hapi', version: '>=18 <22' })
38
+ .yields(hapi);
39
+
40
+ sources = core.assess.dataflow.sources;
41
+ sinon.stub(sources, 'handle');
42
+
43
+ require('./hapi')(core).install();
44
+ });
45
+
46
+ ['server', 'Server'].forEach((server) => {
47
+ describe(`${server}`, function () {
48
+
49
+ ['onRequest', 'onPostAuth'].forEach((ext) => {
50
+ it(`logs error when handle fails ${ext}`, function() {
51
+ simulateRequestScope(() => {
52
+
53
+ const err = new Error('Error');
54
+ const serverInstance = {
55
+ ext: sinon.stub(),
56
+ };
57
+
58
+ serverInstance.ext.withArgs(ext, sinon.match.func).callsFake((string, method) => {
59
+ method(reqMock, hMock);
60
+ });
61
+
62
+ sources.handle.throws(err);
63
+ hapi[server](serverInstance);
64
+
65
+ expect(core.logger.error).to.have.been.calledWith({ err, inputType: sinon.match.string, funcKey: `assess-dataflow-source:hapi.${server}` }, 'unable to handle Hapi source');
66
+ });
67
+ });
68
+ });
69
+
70
+ it('does not call handle with tracked data onRequest', function() {
71
+ simulateRequestScope(() => {
72
+ const sourceContext = core.scopes.sources.getStore()?.assess;
73
+ sourceContext.parsedQuery = true;
74
+ sourceContext.parsedPath = true;
75
+
76
+ const serverInstance = {
77
+ ext: sinon.stub(),
78
+ };
79
+
80
+ serverInstance.ext.withArgs('onRequest', sinon.match.func).callsFake((string, method) => {
81
+ method(reqMock, hMock);
82
+ });
83
+
84
+ hapi[server](serverInstance);
85
+
86
+ expect(sources.handle).to.not.have.been.called;
87
+ });
88
+ });
89
+
90
+ it('calls handle with request data onRequest', function () {
91
+ simulateRequestScope(() => {
92
+ const serverInstance = {
93
+ ext: sinon.stub(),
94
+ };
95
+
96
+ serverInstance.ext.withArgs('onRequest', sinon.match.func).callsFake((string, method) => {
97
+ method(reqMock, hMock);
98
+ });
99
+
100
+ hapi[server](serverInstance);
101
+
102
+ expect(sources.handle).to.have.been.calledWithMatch({
103
+ context: 'req.query',
104
+ data: reqMock.query,
105
+ inputType: InputType.QUERYSTRING,
106
+ name: 'hapi.onRequest',
107
+ });
108
+ expect(sources.handle).to.have.been.calledWithMatch({
109
+ context: 'req.path',
110
+ data: reqMock.path,
111
+ inputType: InputType.URI,
112
+ name: 'hapi.onRequest',
113
+ });
114
+ });
115
+ });
116
+
117
+ it('does not call handle with tracked data onPostAuth', function() {
118
+ simulateRequestScope(() => {
119
+ const sourceContext = core.scopes.sources.getStore()?.assess;
120
+ sourceContext.parsedCookies = true;
121
+ sourceContext.parsedBody = true;
122
+ sourceContext.parsedParams = true;
123
+
124
+ const serverInstance = {
125
+ ext: sinon.stub(),
126
+ };
127
+
128
+ serverInstance.ext.withArgs('onPostAuth', sinon.match.func).callsFake((string, method) => {
129
+ method(reqMock, hMock);
130
+ });
131
+
132
+ hapi[server](serverInstance);
133
+
134
+ expect(sources.handle).to.not.have.been.called;
135
+ });
136
+ });
137
+
138
+ it('calls handle with request data onPostAuth', function () {
139
+ simulateRequestScope(() => {
140
+ const serverInstance = {
141
+ ext: sinon.stub(),
142
+ };
143
+
144
+ serverInstance.ext.withArgs('onPostAuth', sinon.match.func).callsFake((string, method) => {
145
+ method(reqMock, hMock);
146
+ });
147
+
148
+ hapi[server](serverInstance);
149
+
150
+ expect(sources.handle).to.have.been.calledWithMatch({
151
+ context: 'req.state',
152
+ data: reqMock.state,
153
+ inputType: InputType.COOKIE_VALUE,
154
+ name: 'hapi.onPostAuth',
155
+ });
156
+ expect(sources.handle).to.have.been.calledWithMatch({
157
+ context: 'req.payload',
158
+ data: reqMock.payload,
159
+ inputType: InputType.BODY,
160
+ name: 'hapi.onPostAuth',
161
+ });
162
+ expect(sources.handle).to.have.been.calledWithMatch({
163
+ context: 'req.params',
164
+ data: reqMock.params,
165
+ inputType: InputType.URL_PARAMETER,
166
+ name: 'hapi.onPostAuth',
167
+ });
168
+ });
169
+ });
170
+ });
171
+ });
172
+ });
@@ -0,0 +1,33 @@
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.hapiInstrumentation[moduleName] = mock[moduleName];
9
+ };
10
+
11
+ describe('assess dataflow sources Hapi', function () {
12
+ let core, hapiSourcesMock, hapiSourcesModule, modulesWithInstall;
13
+
14
+ beforeEach(function () {
15
+ core = mocks.core();
16
+ core.assess = mocks.assess();
17
+ hapiSourcesMock = core.assess.dataflow.sources.hapiInstrumentation;
18
+ hapiSourcesModule = proxyquire('.', {
19
+ './hapi': moduleMock('hapi', hapiSourcesMock)
20
+ });
21
+ modulesWithInstall = [
22
+ 'hapi'
23
+ ];
24
+ });
25
+
26
+ it('installs the components', function () {
27
+ hapiSourcesModule(core).install();
28
+
29
+ modulesWithInstall.forEach((module) => {
30
+ expect(hapiSourcesMock[module].install).to.have.been.calledOnce;
31
+ });
32
+ });
33
+ });