@contrast/assess 1.46.0 → 1.46.2

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 (157) hide show
  1. package/package.json +14 -11
  2. package/lib/crypto-analysis/install/crypto.test.js +0 -146
  3. package/lib/crypto-analysis/install/math.test.js +0 -65
  4. package/lib/dataflow/index.test.js +0 -36
  5. package/lib/dataflow/propagation/index.test.js +0 -103
  6. package/lib/dataflow/propagation/install/JSON/index.test.js +0 -50
  7. package/lib/dataflow/propagation/install/JSON/parse-fn.test.js +0 -232
  8. package/lib/dataflow/propagation/install/JSON/parse.test.js +0 -968
  9. package/lib/dataflow/propagation/install/JSON/stringify.test.js +0 -265
  10. package/lib/dataflow/propagation/install/array-prototype-join.test.js +0 -106
  11. package/lib/dataflow/propagation/install/buffer.test.js +0 -112
  12. package/lib/dataflow/propagation/install/contrast-methods/add.test.js +0 -94
  13. package/lib/dataflow/propagation/install/contrast-methods/index.test.js +0 -49
  14. package/lib/dataflow/propagation/install/contrast-methods/number.test.js +0 -50
  15. package/lib/dataflow/propagation/install/contrast-methods/string.test.js +0 -148
  16. package/lib/dataflow/propagation/install/contrast-methods/tag.test.js +0 -145
  17. package/lib/dataflow/propagation/install/decode-uri-component.test.js +0 -78
  18. package/lib/dataflow/propagation/install/ejs/escape-xml.test.js +0 -69
  19. package/lib/dataflow/propagation/install/ejs/template.test.js +0 -62
  20. package/lib/dataflow/propagation/install/encode-uri.test.js +0 -83
  21. package/lib/dataflow/propagation/install/escape-html.test.js +0 -71
  22. package/lib/dataflow/propagation/install/escape.test.js +0 -73
  23. package/lib/dataflow/propagation/install/fastify-send.test.js +0 -42
  24. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.test.js +0 -71
  25. package/lib/dataflow/propagation/install/isnumeric-0.test.js +0 -58
  26. package/lib/dataflow/propagation/install/joi/any.test.js +0 -270
  27. package/lib/dataflow/propagation/install/joi/array.test.js +0 -912
  28. package/lib/dataflow/propagation/install/joi/boolean.test.js +0 -103
  29. package/lib/dataflow/propagation/install/joi/expression.test.js +0 -76
  30. package/lib/dataflow/propagation/install/joi/index.test.js +0 -39
  31. package/lib/dataflow/propagation/install/joi/number.test.js +0 -103
  32. package/lib/dataflow/propagation/install/joi/object.test.js +0 -119
  33. package/lib/dataflow/propagation/install/joi/ref.test.js +0 -607
  34. package/lib/dataflow/propagation/install/joi/string-schema.test.js +0 -513
  35. package/lib/dataflow/propagation/install/mongoose/index.test.js +0 -42
  36. package/lib/dataflow/propagation/install/mongoose/schema-map.test.js +0 -348
  37. package/lib/dataflow/propagation/install/mongoose/schema-mixed.test.js +0 -511
  38. package/lib/dataflow/propagation/install/mongoose/schema-string.test.js +0 -199
  39. package/lib/dataflow/propagation/install/mustache-escape.test.js +0 -62
  40. package/lib/dataflow/propagation/install/mysql-connection-escape.test.js +0 -74
  41. package/lib/dataflow/propagation/install/parse-int.test.js +0 -48
  42. package/lib/dataflow/propagation/install/path/basename.test.js +0 -143
  43. package/lib/dataflow/propagation/install/path/dirname.test.js +0 -167
  44. package/lib/dataflow/propagation/install/path/extname.test.js +0 -141
  45. package/lib/dataflow/propagation/install/path/format.test.js +0 -250
  46. package/lib/dataflow/propagation/install/path/index.test.js +0 -45
  47. package/lib/dataflow/propagation/install/path/join-and-resolve.test.js +0 -485
  48. package/lib/dataflow/propagation/install/path/normalize.test.js +0 -176
  49. package/lib/dataflow/propagation/install/path/parse.test.js +0 -238
  50. package/lib/dataflow/propagation/install/path/relative.test.js +0 -239
  51. package/lib/dataflow/propagation/install/path/toNamespacedPath.test.js +0 -158
  52. package/lib/dataflow/propagation/install/pug/index.test.js +0 -55
  53. package/lib/dataflow/propagation/install/pug-runtime-escape.test.js +0 -69
  54. package/lib/dataflow/propagation/install/querystring/escape.test.js +0 -63
  55. package/lib/dataflow/propagation/install/querystring/index.test.js +0 -40
  56. package/lib/dataflow/propagation/install/querystring/parse.test.js +0 -272
  57. package/lib/dataflow/propagation/install/querystring/stringify.test.js +0 -301
  58. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.test.js +0 -283
  59. package/lib/dataflow/propagation/install/send.test.js +0 -63
  60. package/lib/dataflow/propagation/install/sequelize/query-generator.test.js +0 -74
  61. package/lib/dataflow/propagation/install/sequelize/sql-string.test.js +0 -119
  62. package/lib/dataflow/propagation/install/sql-template-strings.test.js +0 -100
  63. package/lib/dataflow/propagation/install/string/concat.test.js +0 -145
  64. package/lib/dataflow/propagation/install/string/format-methods.test.js +0 -74
  65. package/lib/dataflow/propagation/install/string/html-methods.test.js +0 -177
  66. package/lib/dataflow/propagation/install/string/index.test.js +0 -103
  67. package/lib/dataflow/propagation/install/string/match-all.test.js +0 -412
  68. package/lib/dataflow/propagation/install/string/match.test.js +0 -374
  69. package/lib/dataflow/propagation/install/string/replace.test.js +0 -601
  70. package/lib/dataflow/propagation/install/string/slice.test.js +0 -278
  71. package/lib/dataflow/propagation/install/string/split.test.js +0 -513
  72. package/lib/dataflow/propagation/install/string/substring.test.js +0 -251
  73. package/lib/dataflow/propagation/install/string/trim.test.js +0 -135
  74. package/lib/dataflow/propagation/install/unescape.test.js +0 -78
  75. package/lib/dataflow/propagation/install/url/domain-parsers.test.js +0 -63
  76. package/lib/dataflow/propagation/install/url/parse.test.js +0 -391
  77. package/lib/dataflow/propagation/install/url/searchParams.test.js +0 -538
  78. package/lib/dataflow/propagation/install/url/url.test.js +0 -466
  79. package/lib/dataflow/propagation/install/util-format.test.js +0 -336
  80. package/lib/dataflow/propagation/install/validator/hooks.test.js +0 -211
  81. package/lib/dataflow/sinks/index.test.js +0 -78
  82. package/lib/dataflow/sinks/install/child-process.test.js +0 -338
  83. package/lib/dataflow/sinks/install/eval.test.js +0 -95
  84. package/lib/dataflow/sinks/install/express/index.test.js +0 -33
  85. package/lib/dataflow/sinks/install/express/reflected-xss.test.js +0 -109
  86. package/lib/dataflow/sinks/install/express/unvalidated-redirect.test.js +0 -144
  87. package/lib/dataflow/sinks/install/fastify/index.test.js +0 -32
  88. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.test.js +0 -130
  89. package/lib/dataflow/sinks/install/fs.test.js +0 -138
  90. package/lib/dataflow/sinks/install/function.test.js +0 -103
  91. package/lib/dataflow/sinks/install/hapi/index.test.js +0 -32
  92. package/lib/dataflow/sinks/install/hapi/unvalidated-redirect.test.js +0 -130
  93. package/lib/dataflow/sinks/install/http/index.test.js +0 -33
  94. package/lib/dataflow/sinks/install/http/request.test.js +0 -184
  95. package/lib/dataflow/sinks/install/http/server-response.test.js +0 -160
  96. package/lib/dataflow/sinks/install/koa/index.test.js +0 -32
  97. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.test.js +0 -200
  98. package/lib/dataflow/sinks/install/libxmljs.test.js +0 -158
  99. package/lib/dataflow/sinks/install/marsdb.test.js +0 -166
  100. package/lib/dataflow/sinks/install/mongodb.test.js +0 -617
  101. package/lib/dataflow/sinks/install/mssql.test.js +0 -134
  102. package/lib/dataflow/sinks/install/mysql.test.js +0 -224
  103. package/lib/dataflow/sinks/install/node-serialize.test.js +0 -83
  104. package/lib/dataflow/sinks/install/postgres.test.js +0 -152
  105. package/lib/dataflow/sinks/install/restify.test.js +0 -140
  106. package/lib/dataflow/sinks/install/sequelize.test.js +0 -100
  107. package/lib/dataflow/sinks/install/sqlite3.test.js +0 -118
  108. package/lib/dataflow/sinks/install/vm.test.js +0 -326
  109. package/lib/dataflow/sources/handler.test.js +0 -501
  110. package/lib/dataflow/sources/index.test.js +0 -60
  111. package/lib/dataflow/sources/install/body-parser1.test.js +0 -244
  112. package/lib/dataflow/sources/install/busboy.test.js +0 -152
  113. package/lib/dataflow/sources/install/cookie-parser1.test.js +0 -141
  114. package/lib/dataflow/sources/install/express/params.test.js +0 -110
  115. package/lib/dataflow/sources/install/express/parsedUrl.test.js +0 -107
  116. package/lib/dataflow/sources/install/fastify/fastify.test.js +0 -207
  117. package/lib/dataflow/sources/install/fastify/index.test.js +0 -33
  118. package/lib/dataflow/sources/install/formidable1.test.js +0 -119
  119. package/lib/dataflow/sources/install/graphql-http.test.js +0 -133
  120. package/lib/dataflow/sources/install/hapi/hapi.test.js +0 -171
  121. package/lib/dataflow/sources/install/hapi/index.test.js +0 -33
  122. package/lib/dataflow/sources/install/http.test.js +0 -149
  123. package/lib/dataflow/sources/install/koa/index.test.js +0 -40
  124. package/lib/dataflow/sources/install/koa/koa-bodyparsers.test.js +0 -160
  125. package/lib/dataflow/sources/install/koa/koa-multer.test.js +0 -197
  126. package/lib/dataflow/sources/install/koa/koa-routers.test.js +0 -146
  127. package/lib/dataflow/sources/install/koa/koa2.test.js +0 -145
  128. package/lib/dataflow/sources/install/multer1.test.js +0 -143
  129. package/lib/dataflow/sources/install/qs6.test.js +0 -131
  130. package/lib/dataflow/sources/install/querystring.test.js +0 -82
  131. package/lib/dataflow/sources/install/restify/fieldedTextBodyParser.test.js +0 -86
  132. package/lib/dataflow/sources/install/restify/index.test.js +0 -38
  133. package/lib/dataflow/sources/install/restify/jsonBodyParser.test.js +0 -140
  134. package/lib/dataflow/sources/install/restify/router.test.js +0 -81
  135. package/lib/dataflow/tag-utils-complete.test.js +0 -27
  136. package/lib/dataflow/tag-utils.test.js +0 -192
  137. package/lib/dataflow/tracker.test.js +0 -216
  138. package/lib/dataflow/utils/is-safe-content-type.test.js +0 -16
  139. package/lib/dataflow/utils/is-vulnerable.test.js +0 -115
  140. package/lib/event-factory.test.js +0 -326
  141. package/lib/get-policy.test.js +0 -194
  142. package/lib/get-source-context.test.js +0 -161
  143. package/lib/index.test.js +0 -45
  144. package/lib/make-source-context.test.js +0 -50
  145. package/lib/response-scanning/handlers/index.test.js +0 -419
  146. package/lib/response-scanning/handlers/utils.test.js +0 -380
  147. package/lib/response-scanning/index.test.js +0 -41
  148. package/lib/response-scanning/install/http.test.js +0 -175
  149. package/lib/rule-scopes.test.js +0 -27
  150. package/lib/sampler/common.test.js +0 -101
  151. package/lib/sampler/index.test.js +0 -313
  152. package/lib/session-configuration/handlers.test.js +0 -84
  153. package/lib/session-configuration/index.test.js +0 -36
  154. package/lib/session-configuration/install/express-session.test.js +0 -218
  155. package/lib/session-configuration/install/fastify-cookie.test.js +0 -63
  156. package/lib/session-configuration/install/hapi.test.js +0 -269
  157. package/lib/session-configuration/install/koa.test.js +0 -92
@@ -1,107 +0,0 @@
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, 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
- });
20
-
21
- describe('express4', function() {
22
- let middleware;
23
-
24
- beforeEach(function() {
25
- middleware = {
26
- init() {
27
- return function (req, res, next) {
28
- req._parsedUrl = {
29
- href: '/some/path?asdf=jkl',
30
- path: '/some/path?asdf=jkl',
31
- pathname: '/some/path',
32
- query: 'asdf=jkl',
33
- search: '?asdf=jkl;',
34
- };
35
- next();
36
- };
37
- }
38
- };
39
-
40
- core.depHooks.resolve.withArgs(sinon.match({ name: 'express' })).yields(middleware);
41
- });
42
-
43
- it('hooks init next in order to track req._parsedUrl values', function () {
44
- instrumentation.install();
45
- expect(patcher.isContrastHooked(middleware.init)).to.be.true;
46
-
47
- const middlewareFn = middleware.init('text');
48
-
49
- simulateRequestScope(() => {
50
- const cb = sinon.spy(function () {
51
- [
52
- 'href',
53
- 'path',
54
- 'pathname',
55
- 'query',
56
- 'search',
57
- ].forEach((prop) => {
58
- const strInfo = tracker.getData(req._parsedUrl[prop]);
59
- expect(strInfo).to.be.ok;
60
- });
61
- });
62
-
63
- middlewareFn(req, res, cb);
64
-
65
- expect(cb).to.have.been.called;
66
- });
67
- });
68
- });
69
-
70
- describe('express5', function() {
71
- let LayerMock;
72
-
73
- beforeEach(function() {
74
- req = {
75
- _parsedUrl: {
76
- href: '/some/path?asdf=jkl',
77
- path: '/some/path?asdf=jkl',
78
- pathname: '/some/path',
79
- query: 'asdf=jkl',
80
- search: '?asdf=jkl;',
81
- }
82
- };
83
-
84
- LayerMock = sinon.stub();
85
- LayerMock.prototype.handleRequest = sinon.stub();
86
-
87
- core.depHooks.resolve.withArgs(sinon.match({ name: 'router' })).yields(LayerMock);
88
- });
89
-
90
- it('hooks handleRequest in order to track req._parsedUrl values', function () {
91
- instrumentation.install();
92
- simulateRequestScope(() => {
93
- LayerMock.prototype.handleRequest(req);
94
- [
95
- 'href',
96
- 'path',
97
- 'pathname',
98
- 'query',
99
- 'search',
100
- ].forEach((prop) => {
101
- const strInfo = tracker.getData(req._parsedUrl[prop]);
102
- expect(strInfo).to.be.ok;
103
- });
104
- });
105
- });
106
- });
107
- });
@@ -1,207 +0,0 @@
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.resolve.callsFake((desc, cb) => {
30
- fastifyServerMock = cb(fastifyServerMock);
31
- });
32
-
33
- fastifyInstr(core).install();
34
- core.logger.trace.resetHistory();
35
- });
36
-
37
- afterEach(function () {
38
- sources.handle.restore?.();
39
- });
40
-
41
- describe('preValidationHook', function () {
42
- const commonData = {
43
- name: 'fastify.preValidation',
44
- stacktraceOpts: {
45
- constructorOpt: sinon.match((value) => typeof value === 'function' && /handler/i.test(value.name)),
46
- },
47
- };
48
-
49
- it('adds hook and tracks the body, headers and query/url params', function () {
50
- simulateRequestScope(function () {
51
- fastifyServerMock();
52
- const sourceContext = core.scopes.sources.getStore()?.assess;
53
-
54
- expect(serverMock.addHook).to.have.been.calledOnce;
55
- expect(doneMock).to.have.been.calledOnce;
56
-
57
- expect(sources.handle).to.have.been.calledWith({
58
- ...commonData,
59
- context: 'req.query',
60
- inputType: InputType.QUERYSTRING,
61
- data: reqMock.query,
62
- sourceContext,
63
- });
64
- expect(sources.handle).to.have.been.calledWith({
65
- ...commonData,
66
- context: 'req.params',
67
- inputType: InputType.URL_PARAMETER,
68
- data: reqMock.params,
69
- sourceContext
70
- });
71
- expect(sources.handle).to.have.been.calledWith({
72
- ...commonData,
73
- context: 'req.body',
74
- inputType: InputType.PARAMETER_VALUE,
75
- data: reqMock.body,
76
- sourceContext
77
- });
78
- });
79
-
80
- simulateRequestScope(function () {
81
- reqMock.headers = {
82
- 'content-type': 'application/json'
83
- };
84
- fastifyServerMock();
85
- const sourceContext = core.scopes.sources.getStore()?.assess;
86
-
87
- expect(serverMock.addHook).to.have.been.calledTwice;
88
- expect(doneMock).to.have.been.calledTwice;
89
-
90
- expect(sources.handle).to.have.been.calledWith({
91
- ...commonData,
92
- context: 'req.query',
93
- inputType: InputType.QUERYSTRING,
94
- data: reqMock.query,
95
- sourceContext
96
- });
97
- expect(sources.handle).to.have.been.calledWith({
98
- ...commonData,
99
- context: 'req.params',
100
- inputType: InputType.URL_PARAMETER,
101
- data: reqMock.params,
102
- sourceContext
103
- });
104
- expect(sources.handle).to.have.been.calledWith({
105
- ...commonData,
106
- context: 'req.body',
107
- inputType: InputType.JSON_VALUE,
108
- data: reqMock.body,
109
- sourceContext
110
- });
111
- });
112
-
113
- simulateRequestScope(function () {
114
- delete reqMock.headers;
115
- reqMock.body = 'non-object-body';
116
- fastifyServerMock();
117
- const sourceContext = core.scopes.sources.getStore()?.assess;
118
-
119
- expect(serverMock.addHook).to.have.been.calledThrice;
120
- expect(doneMock).to.have.been.calledThrice;
121
-
122
- expect(sources.handle).to.have.been.calledWith({
123
- ...commonData,
124
- context: 'req.query',
125
- inputType: InputType.QUERYSTRING,
126
- data: reqMock.query,
127
- sourceContext,
128
- });
129
- expect(sources.handle).to.have.been.calledWith({
130
- ...commonData,
131
- context: 'req.params',
132
- inputType: InputType.URL_PARAMETER,
133
- data: reqMock.params,
134
- sourceContext
135
- });
136
- expect(sources.handle).to.have.been.calledWith({
137
- ...commonData,
138
- context: 'req.body',
139
- inputType: InputType.BODY,
140
- data: reqMock.body,
141
- sourceContext
142
- });
143
- });
144
- });
145
-
146
- it('does not handle source when there is no assess policy in request context', function () {
147
- simulateRequestScope(() => {
148
- fastifyServerMock();
149
- }, { assess: { policy: null } });
150
-
151
- expect(sources.handle).not.to.have.been.called;
152
- });
153
-
154
- it('does not call `.handle` when the values are already tracked', function () {
155
- simulateRequestScope(() => {
156
- const sourceContext = core.scopes.sources.getStore()?.assess;
157
- sourceContext.parsedQuery = true;
158
- sourceContext.parsedParams = true;
159
- sourceContext.parsedBody = true;
160
-
161
- fastifyServerMock();
162
- });
163
-
164
- expect(sources.handle).not.to.have.been.called;
165
- expect(core.logger.trace).to.have.been.calledThrice;
166
- expect(core.logger.trace).to.have.been.calledWith(
167
- { inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:fastify.constructor' },
168
- 'values already tracked',
169
- );
170
- expect(core.logger.trace).to.have.been.calledWith(
171
- { inputType: InputType.URL_PARAMETER, funcKey: 'assess-dataflow-source:fastify.constructor' },
172
- 'values already tracked',
173
- );
174
- expect(core.logger.trace).to.have.been.calledWith(
175
- { inputType: InputType.PARAMETER_VALUE, funcKey: 'assess-dataflow-source:fastify.constructor' },
176
- 'values already tracked',
177
- );
178
- });
179
-
180
- it('handles a case with an error in the `.handle` method', function () {
181
- fastifyInstr(core).install();
182
- const err = new Error('test');
183
-
184
- core.assess.dataflow.sources.handle = () => {
185
- throw err;
186
- };
187
-
188
- simulateRequestScope(function () {
189
- fastifyServerMock();
190
- });
191
-
192
- expect(core.logger.error).to.have.been.calledThrice;
193
- expect(core.logger.error).to.have.been.calledWith(
194
- { err, inputType: InputType.QUERYSTRING, funcKey: 'assess-dataflow-source:fastify.constructor' },
195
- 'unable to handle Fastify source',
196
- );
197
- expect(core.logger.error).to.have.been.calledWith(
198
- { err, inputType: InputType.URL_PARAMETER, funcKey: 'assess-dataflow-source:fastify.constructor' },
199
- 'unable to handle Fastify source',
200
- );
201
- expect(core.logger.error).to.have.been.calledWith(
202
- { err, inputType: InputType.PARAMETER_VALUE, funcKey: 'assess-dataflow-source:fastify.constructor' },
203
- 'unable to handle Fastify source',
204
- );
205
- });
206
- });
207
- });
@@ -1,33 +0,0 @@
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
- });
@@ -1,119 +0,0 @@
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 there is no assess policy in request context', function () {
86
- simulateRequestScope(() => {
87
- patchedParse(req, cbStub);
88
- }, { assess: { policy: null } });
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
- });
@@ -1,133 +0,0 @@
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 graphql-http v1', function () {
9
- let core, simulateRequestScope, createHandler, parseRequestParams, _exports;
10
-
11
- this.beforeEach(function () {
12
- ({ core, simulateRequestScope } = initAssessFixture());
13
- sinon.stub(core.assess.dataflow.sources, 'handle');
14
-
15
- createHandler = sinon.stub().callsFake(({ parseRequestParams }) =>
16
- sinon.stub().callsFake((req) => parseRequestParams(req)),
17
- );
18
- parseRequestParams = sinon.stub().resolves({ query: 'query { foo }' });
19
-
20
- require('./graphql-http')(core).install();
21
- _exports = core.depHooks.resolve.yield({ createHandler, parseRequestParams })[0];
22
- });
23
-
24
- describe('parseRequestParams', function () {
25
- it('does not emit an event when the parser does not return a promise', function () {
26
- parseRequestParams.returns(undefined);
27
-
28
- return simulateRequestScope(async () => {
29
- await _exports.parseRequestParams();
30
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
31
- });
32
- });
33
-
34
- it('does not emit an event when the parser does not resolve a result', function () {
35
- parseRequestParams.resolves(undefined);
36
-
37
- return simulateRequestScope(async () => {
38
- await _exports.parseRequestParams();
39
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
40
- });
41
- });
42
-
43
- it('does not emit an event when outside of a source context', async function () {
44
- await _exports.parseRequestParams();
45
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
46
- });
47
-
48
- it('does not emit an event when already parsed', function () {
49
- return simulateRequestScope(async () => {
50
- await _exports.parseRequestParams();
51
- expect(core.assess.dataflow.sources.handle).not.to.have.been.called;
52
- expect(core.logger.trace).to.have.been.calledWith(sinon.match.object, 'values already tracked');
53
- }, { assess: { parsedBody: true } });
54
- });
55
-
56
-
57
- it('emits an event when handling a GET request', function () {
58
- return simulateRequestScope(async () => {
59
- await _exports.parseRequestParams({ method: 'GET' });
60
-
61
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
62
- name: 'graphql-http.parseRequestParams',
63
- context: 'req.url',
64
- inputType: InputType.PARAMETER_VALUE,
65
- data: { query: 'query { foo }' },
66
- keys: ['query'],
67
- sourceContext: core.scopes.sources.getStore().assess,
68
- stacktraceOpts: {
69
- constructorOpt: sinon.match.func,
70
- prependFrames: [sinon.match.func],
71
- },
72
- });
73
- });
74
- });
75
-
76
- it('emits an event when handling a POST request', function () {
77
- return simulateRequestScope(async () => {
78
- await _exports.parseRequestParams({ method: 'POST' });
79
-
80
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
81
- name: 'graphql-http.parseRequestParams',
82
- context: 'req.body',
83
- inputType: InputType.JSON_VALUE,
84
- data: { query: 'query { foo }' },
85
- keys: ['query'],
86
- sourceContext: core.scopes.sources.getStore().assess,
87
- stacktraceOpts: {
88
- constructorOpt: sinon.match.func,
89
- prependFrames: [sinon.match.func],
90
- },
91
- });
92
- });
93
- });
94
-
95
- it('handles errors when emitting', function () {
96
- const err = new Error('test');
97
- core.assess.dataflow.sources.handle.throws(err);
98
-
99
- return simulateRequestScope(async () => {
100
- await _exports.parseRequestParams({ method: 'POST' });
101
- expect(core.logger.error).to.have.been.calledWith(sinon.match.object, 'unable to handle source');
102
- });
103
- });
104
- });
105
-
106
- describe('createHandler', function () {
107
- it('provides the patched default when no parseRequestParams is provided', function () {
108
- const handler = _exports.createHandler({ schema: {} });
109
- const req = { method: 'POST' };
110
-
111
- return simulateRequestScope(async () => {
112
- await handler(req);
113
- expect(parseRequestParams).to.have.been.calledWith(req);
114
- expect(core.assess.dataflow.sources.handle).to.have.been.called;
115
- });
116
- });
117
-
118
- it('patches a provided parseRequestParams function', function () {
119
- const newParseRequestParams = sinon.stub().resolves({ query: 'query SomethingElse { bar } ' });
120
- const handler = _exports.createHandler({ schema: {}, parseRequestParams: newParseRequestParams });
121
- const req = { method: 'GET' };
122
-
123
- return simulateRequestScope(async () => {
124
- await handler(req);
125
- expect(parseRequestParams).not.to.have.been.called;
126
- expect(newParseRequestParams).to.have.been.calledWith(req);
127
- expect(core.assess.dataflow.sources.handle).to.have.been.calledWithMatch({
128
- data: { query: 'query SomethingElse { bar } ' }
129
- });
130
- });
131
- });
132
- });
133
- });