@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,84 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+ const handlers = require('./handlers');
7
+ const {
8
+ Event,
9
+ SessionConfigurationRule: {
10
+ HTTPONLY,
11
+ SECURE_FLAG_MISSING,
12
+ }
13
+ } = require('@contrast/common');
14
+
15
+ describe('assess session-configuration handlers', function () {
16
+ let sessionConfiguration, core, simulateRequestScope;
17
+
18
+ beforeEach(function () {
19
+ ({ core, simulateRequestScope } = initAssessFixture());
20
+ sessionConfiguration = handlers(core);
21
+ sinon.spy(core.messages, 'emit');
22
+ });
23
+
24
+ [
25
+ {
26
+ method: 'handleHttpOnly',
27
+ cookie: 'HttpOnly',
28
+ ruleId: HTTPONLY
29
+ },
30
+ {
31
+ method: 'handleSecure',
32
+ cookie: 'Secure',
33
+ ruleId: SECURE_FLAG_MISSING
34
+ }
35
+ ].forEach(function ({ method, cookie, ruleId }) {
36
+ it(`should not report findings if cookie includes ${cookie} - ${method}`, function () {
37
+ simulateRequestScope(() => {
38
+ const sourceContext = core.assess.getSourceContext();
39
+ sessionConfiguration[method](sourceContext, `foo; cookie; ${cookie};`);
40
+ expect(core.messages.emit).to.not.have.been.called;
41
+ });
42
+ });
43
+
44
+ it(`should report when ${cookie} is not part of the cookie - ${method}`, function () {
45
+ simulateRequestScope(() => {
46
+ const sourceContext = core.assess.getSourceContext();
47
+ const sessionEvent = { mock: 'sessionEvent' };
48
+
49
+ sessionConfiguration[method](sourceContext, 'foo; cookie', sessionEvent);
50
+ expect(core.messages.emit).to.have.been.calledWith(Event.ASSESS_SESSION_CONFIGURATION_FINDING, {
51
+ ruleId,
52
+ sinkEvent: sessionEvent,
53
+ properties: {
54
+ evidence: 'foo; cookie'
55
+ }
56
+ });
57
+ });
58
+ });
59
+
60
+ it(`should report when ${cookie} (an array) is not part of the cookie - ${method} `, function () {
61
+ simulateRequestScope(() => {
62
+ const sourceContext = core.assess.getSourceContext();
63
+ const sessionEvent = { mock: 'sessionEvent' };
64
+
65
+ sessionConfiguration[method](sourceContext, ['foo; cookie', 'hello; cookie3'], sessionEvent);
66
+ expect(core.messages.emit).to.have.been.calledWith(Event.ASSESS_SESSION_CONFIGURATION_FINDING, {
67
+ ruleId,
68
+ sinkEvent: sessionEvent,
69
+ properties: {
70
+ evidence: 'foo; cookie'
71
+ }
72
+ });
73
+
74
+ expect(core.messages.emit).not.to.have.been.calledWith(Event.ASSESS_SESSION_CONFIGURATION_FINDING, {
75
+ ruleId,
76
+ sinkEvent: sessionEvent,
77
+ properties: {
78
+ evidence: 'hello; cookie3'
79
+ }
80
+ });
81
+ });
82
+ });
83
+ });
84
+ });
@@ -0,0 +1,36 @@
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) => (core) => {
8
+ core.assess.sessionConfiguration[moduleName] = mock[moduleName];
9
+ };
10
+
11
+ describe('assess session-configuration', function () {
12
+ let core, sessionConfigurationMock, sessionConfigurationModule, modulesWithInstall;
13
+
14
+ beforeEach(function () {
15
+ core = mocks.core();
16
+ core.scopes = mocks.scopes();
17
+ core.assess = mocks.assess();
18
+ sessionConfigurationMock = core.assess.sessionConfiguration;
19
+ sessionConfigurationModule = proxyquire('.', {
20
+ './install/express-session': moduleMock('expressSession', sessionConfigurationMock),
21
+ './install/hapi': moduleMock('hapiSession', sessionConfigurationMock),
22
+ './install/fastify-cookie': moduleMock('fastifyCookie', sessionConfigurationMock),
23
+ './install/koa': moduleMock('koaSession', sessionConfigurationMock),
24
+ });
25
+ modulesWithInstall = ['expressSession', 'hapiSession', 'fastifyCookie', 'koaSession'];
26
+ });
27
+
28
+ it('installs the components', function () {
29
+ sessionConfigurationModule(core).install();
30
+
31
+ modulesWithInstall.forEach((module) => {
32
+ expect(core.assess.sessionConfiguration[module].install).to.have.been.calledOnce;
33
+ });
34
+ });
35
+ });
36
+
@@ -0,0 +1,220 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+ const util = require('util');
7
+
8
+ describe('assess session-configuration http', function () {
9
+ let core,
10
+ expressSession,
11
+ simulateRequestScope;
12
+
13
+ function ExpressSession(options) {
14
+ return (req, res) => true;
15
+ }
16
+
17
+ beforeEach(function () {
18
+ ({ core, simulateRequestScope } = initAssessFixture());
19
+ core.depHooks.resolve
20
+ .withArgs({ name: 'express-session' })
21
+ .yields(ExpressSession);
22
+
23
+ sinon.spy(core.assess.sessionConfiguration, 'handleHttpOnly');
24
+ sinon.spy(core.assess.sessionConfiguration, 'handleSecure');
25
+
26
+ expressSession = require('./express-session')(core).install();
27
+ });
28
+
29
+ it('skip instrumentation if options are set correctly', function () {
30
+ const options = {
31
+ secret: 'my-secret',
32
+ cookie: { httpOnly: true, secure: true },
33
+ };
34
+
35
+ expressSession(options);
36
+
37
+ expect(core.assess.sessionConfiguration.handleSecure).to.not.have.been.called;
38
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.not.have.been.called;
39
+ });
40
+
41
+ [
42
+ {
43
+ title: 'should instrument secure',
44
+ sessionOptions: [
45
+ undefined,
46
+ {},
47
+ { cookie: { httpOnly: undefined, secure: undefined } },
48
+ { cookie: { httpOnly: true } },
49
+ ],
50
+ },
51
+ ].forEach(({ title, sessionOptions }) => {
52
+ sessionOptions.forEach((options) => {
53
+ it(`${title} - ${util.inspect(options)}`, function () {
54
+ simulateRequestScope(() => {
55
+ const httpOnly = options?.cookie?.httpOnly;
56
+ const secure = options?.cookie?.secure;
57
+
58
+ const middleware = expressSession(options);
59
+ const request = {};
60
+ const response = {
61
+ setHeader: () => true,
62
+ };
63
+
64
+ middleware(request, response);
65
+
66
+ response.setHeader('Set-Cookie', 'hello');
67
+
68
+ // for code coverage
69
+ response.setHeader('custom-header', 'value');
70
+
71
+ const sourceContext = core.scopes.sources.getStore()?.assess;
72
+ expect(core.assess.sessionConfiguration.handleSecure).to.have.been.calledWith(
73
+ sourceContext,
74
+ 'hello',
75
+ sinon.match({
76
+ args: [
77
+ {
78
+ tracked: false,
79
+ value: `{ cookie: { httpOnly: ${httpOnly}, secure: ${secure} } }`
80
+ },
81
+ ],
82
+ context: `expressSession({ cookie: { httpOnly: ${httpOnly}, secure: ${secure} } })`,
83
+ history: [],
84
+ name: 'express.hookedSessionConstructor',
85
+ moduleName: 'express-session',
86
+ methodName: '',
87
+ object: {
88
+ tracked: false,
89
+ value: 'Express.Response',
90
+ },
91
+ result: {
92
+ tracked: false,
93
+ value: undefined,
94
+ },
95
+ source: 'P0',
96
+ stack: [],
97
+ tags: {},
98
+ framework: 'express',
99
+ options: { cookie: { httpOnly, secure } },
100
+ })
101
+ );
102
+ });
103
+ });
104
+ });
105
+ });
106
+
107
+ [
108
+ {
109
+ title: 'should not instrument secure',
110
+ sessionOptions: [
111
+ { cookie: { secure: true } },
112
+ ],
113
+ },
114
+ ].forEach(({ title, sessionOptions }) => {
115
+ sessionOptions.forEach((options) => {
116
+ it(`${title} - ${util.inspect(options)}`, function () {
117
+ simulateRequestScope(() => {
118
+ const middleware = expressSession(options);
119
+ const request = {};
120
+ const response = {
121
+ setHeader: () => true,
122
+ };
123
+
124
+ middleware(request, response);
125
+
126
+ response.setHeader('Set-Cookie', 'hello');
127
+
128
+ expect(core.assess.sessionConfiguration.handleSecure).to.not.have.been.called;
129
+ });
130
+ });
131
+ });
132
+ });
133
+
134
+ [
135
+ {
136
+ title: 'should not instrument httpOnly',
137
+ sessionOptions: [
138
+ undefined,
139
+ {},
140
+ { cookie: {} },
141
+ { cookie: { httpOnly: true } },
142
+ ],
143
+ },
144
+ ].forEach(({ title, sessionOptions }) => {
145
+ sessionOptions.forEach((options) => {
146
+ it(`${title} - ${util.inspect(options)}`, function () {
147
+ simulateRequestScope(() => {
148
+ const middleware = expressSession(options);
149
+ const request = {};
150
+ const response = {
151
+ setHeader: () => true,
152
+ };
153
+
154
+ middleware(request, response);
155
+
156
+ response.setHeader('Set-Cookie', 'hello');
157
+
158
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.not.have.been.called;
159
+ });
160
+ });
161
+ });
162
+ });
163
+
164
+ [
165
+ {
166
+ title: 'should instrument httpOnly',
167
+ sessionOptions: [
168
+ { cookie: { httpOnly: false, secure: undefined } },
169
+ ],
170
+ },
171
+ ].forEach(({ title, sessionOptions }) => {
172
+ sessionOptions.forEach((options) => {
173
+ it(`${title} - ${util.inspect(options)}`, function () {
174
+ simulateRequestScope(() => {
175
+ const middleware = expressSession(options);
176
+ const request = {};
177
+ const response = {
178
+ setHeader: () => true,
179
+ };
180
+
181
+ middleware(request, response);
182
+
183
+ response.setHeader('Set-Cookie', 'hello');
184
+
185
+ const sourceContext = core.scopes.sources.getStore()?.assess;
186
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.have.been.calledWith(
187
+ sourceContext,
188
+ 'hello',
189
+ sinon.match({
190
+ args: [
191
+ {
192
+ tracked: false,
193
+ value: util.inspect(options),
194
+ },
195
+ ],
196
+ context: `expressSession(${util.inspect(options)})`,
197
+ history: [],
198
+ name: 'express.hookedSessionConstructor',
199
+ moduleName: 'express-session',
200
+ methodName: '',
201
+ object: {
202
+ tracked: false,
203
+ value: 'Express.Response',
204
+ },
205
+ result: {
206
+ tracked: false,
207
+ value: undefined,
208
+ },
209
+ source: 'P0',
210
+ stack: [],
211
+ tags: {},
212
+ framework: 'express',
213
+ options,
214
+ })
215
+ );
216
+ });
217
+ });
218
+ });
219
+ });
220
+ });
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { SessionConfigurationRule } = require('@contrast/common');
6
+ const { initAssessFixture } = require('@contrast/test/fixtures');
7
+
8
+ const { HTTPONLY, SECURE_FLAG_MISSING } = SessionConfigurationRule;
9
+
10
+ describe('assess session-configuration @fastify/cookie', function () {
11
+ let core,
12
+ simulateRequestScope,
13
+ serverMock,
14
+ mockFastifyCookie,
15
+ mockExport,
16
+ reply;
17
+
18
+ beforeEach(function () {
19
+ ({ core, simulateRequestScope } = initAssessFixture());
20
+
21
+ mockExport = function fastifyCookieMock(server, options) { };
22
+ mockExport.default = mockExport;
23
+ mockExport.fastifyCookie = mockExport;
24
+ reply = { header: sinon.stub() };
25
+ serverMock = {
26
+ addHook: sinon.stub().yields({}, reply),
27
+ };
28
+
29
+ core.depHooks.resolve
30
+ .withArgs({ name: '@fastify/cookie' })
31
+ .yields(mockExport);
32
+
33
+ sinon.stub(core.assess.sessionConfiguration, 'reportFindings');
34
+ core.assess.sessionConfiguration.fastifyCookie.install();
35
+
36
+ mockFastifyCookie = mockExport.fastifyCookie;
37
+ });
38
+
39
+ it('reports httponly and secure-flag-missing when both options are unset', function () {
40
+ mockFastifyCookie(serverMock, {});
41
+
42
+ simulateRequestScope(() => {
43
+ reply.header('Set-Cookie', 'bar');
44
+ });
45
+
46
+ expect(core.assess.sessionConfiguration.reportFindings).to.have.been.calledWithMatch({
47
+ ruleId: HTTPONLY,
48
+ sinkEvent: sinon.match({
49
+ context: 'fastifyCookie({ parseOptions: { httpOnly: undefined, secure: undefined } })',
50
+ }),
51
+ });
52
+ expect(core.assess.sessionConfiguration.reportFindings).to.have.been.calledWithMatch({
53
+ ruleId: SECURE_FLAG_MISSING,
54
+ sinkEvent: sinon.match({
55
+ context: 'fastifyCookie({ parseOptions: { httpOnly: undefined, secure: undefined } })',
56
+ }),
57
+ });
58
+ });
59
+
60
+ it('will not report if instrumentation runs outside of request scope', function () {
61
+ mockFastifyCookie(serverMock, {});
62
+ reply.header('Set-Cookie', 'bar');
63
+ expect(core.assess.sessionConfiguration.reportFindings).not.to.have.been.called;
64
+ });
65
+ });
@@ -0,0 +1,269 @@
1
+ 'use strict';
2
+
3
+ const util = require('util');
4
+ const sinon = require('sinon');
5
+ const { expect } = require('chai');
6
+ const { initAssessFixture } = require('@contrast/test/fixtures');
7
+
8
+ describe('assess session-configuration hapi', function () {
9
+ const hapi = {
10
+ server() {
11
+ return { state() { }, ext: sinon.stub() };
12
+ },
13
+ Server() {
14
+ return { state() { }, ext: sinon.stub() };
15
+ }
16
+ };
17
+
18
+ let core, hapiServer, simulateRequestScope;
19
+
20
+ beforeEach(function () {
21
+ ({ core, simulateRequestScope } = initAssessFixture());
22
+ core.depHooks.resolve
23
+ .withArgs({ name: '@hapi/hapi', version: '>=18 <22' })
24
+ .yields(hapi);
25
+
26
+ sinon.spy(core.assess.sessionConfiguration, 'handleHttpOnly');
27
+ sinon.spy(core.assess.sessionConfiguration, 'handleSecure');
28
+
29
+ require('./hapi')(core).install();
30
+ });
31
+
32
+ ['server', 'Server'].forEach((server) => {
33
+ describe(`${server}`, function () {
34
+ it('skips instrumentation if options are set correctly', function () {
35
+ simulateRequestScope(() => {
36
+ const options = {
37
+ isHttpOnly: true,
38
+ isSecure: true
39
+ };
40
+
41
+ hapiServer = hapi[server]();
42
+ hapiServer.state('data', options);
43
+
44
+ expect(core.assess.sessionConfiguration.handleSecure).to.not.have.been.called;
45
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.not.have.been.called;
46
+ });
47
+ });
48
+
49
+ it('does not call handlers when set-cookie is missing in headers', function () {
50
+ simulateRequestScope(() => {
51
+
52
+ const options = {
53
+ isHttpOnly: false,
54
+ isSecure: false
55
+ };
56
+
57
+ hapiServer = hapi[server]();
58
+ hapiServer.ext.withArgs('onPostResponse', sinon.match.func).callsFake((string, method) => {
59
+ method({
60
+ response: {
61
+ headers: {}
62
+ }
63
+ });
64
+ });
65
+ hapiServer.state('data', options);
66
+
67
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.not.have.been.called;
68
+ expect(core.assess.sessionConfiguration.handleSecure).to.not.have.been.called;
69
+ });
70
+ });
71
+
72
+ [
73
+ {
74
+ isSecure: false
75
+ },
76
+ {
77
+ isSecure: false,
78
+ isHttpOnly: true
79
+ }
80
+ ].forEach((options) => {
81
+ it('handles isSecure flag set to false', function () {
82
+ simulateRequestScope(() => {
83
+
84
+ hapiServer = hapi[server]();
85
+ hapiServer.ext.withArgs('onPostResponse', sinon.match.func).callsFake((string, method) => {
86
+ method({
87
+ response: {
88
+ headers: {
89
+ 'set-cookie': ['foo']
90
+ }
91
+ }
92
+ });
93
+ });
94
+ hapiServer.state('data', options);
95
+
96
+ const sourceContext = core.scopes.sources.getStore()?.assess;
97
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.not.have.been.called;
98
+ expect(core.assess.sessionConfiguration.handleSecure).to.have.been.calledWith(
99
+ sourceContext,
100
+ 'set-cookie: foo',
101
+ sinon.match({
102
+ args: [
103
+ {
104
+ tracked: false,
105
+ value: util.inspect(options),
106
+ },
107
+ ],
108
+ context: `state(${util.inspect(['data', options])})`,
109
+ history: [],
110
+ name: `hapi.${server}.state`,
111
+ moduleName: 'hapi',
112
+ methodName: '',
113
+ object: {
114
+ tracked: false,
115
+ value: `hapi.${server}`,
116
+ },
117
+ result: {
118
+ tracked: false,
119
+ value: undefined,
120
+ },
121
+ source: 'P1',
122
+ framework: 'hapi',
123
+ options,
124
+ })
125
+ );
126
+ });
127
+ });
128
+ });
129
+
130
+ [
131
+ {
132
+ isHttpOnly: false
133
+ },
134
+ {
135
+ isSecure: true,
136
+ isHttpOnly: false
137
+ }
138
+ ].forEach((options) => {
139
+ it('handles isHttpOnly flag set to false', function () {
140
+ simulateRequestScope(() => {
141
+
142
+ hapiServer = hapi[server]();
143
+ hapiServer.ext.withArgs('onPostResponse', sinon.match.func).callsFake((string, method) => {
144
+ method({
145
+ response: {
146
+ headers: {
147
+ 'set-cookie': ['foo']
148
+ }
149
+ }
150
+ });
151
+ });
152
+ hapiServer.state('data', options);
153
+
154
+ const sourceContext = core.scopes.sources.getStore()?.assess;
155
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.have.been.calledWith(
156
+ sourceContext,
157
+ 'set-cookie: foo',
158
+ sinon.match({
159
+ args: [
160
+ {
161
+ tracked: false,
162
+ value: util.inspect(options),
163
+ },
164
+ ],
165
+ context: `state(${util.inspect(['data', options])})`,
166
+ history: [],
167
+ name: `hapi.${server}.state`,
168
+ moduleName: 'hapi',
169
+ methodName: '',
170
+ object: {
171
+ tracked: false,
172
+ value: `hapi.${server}`,
173
+ },
174
+ result: {
175
+ tracked: false,
176
+ value: undefined,
177
+ },
178
+ source: 'P1',
179
+ framework: 'hapi',
180
+ options,
181
+ })
182
+ );
183
+ });
184
+ });
185
+ });
186
+
187
+ it('handles isHttpOnly flag and isSecure set to false', function () {
188
+ simulateRequestScope(() => {
189
+
190
+ const options = {
191
+ isSecure: false,
192
+ isHttpOnly: false
193
+ };
194
+
195
+ hapiServer = hapi[server]();
196
+ hapiServer.ext.withArgs('onPostResponse', sinon.match.func).callsFake((string, method) => {
197
+ method({
198
+ response: {
199
+ headers: {
200
+ 'set-cookie': ['foo']
201
+ }
202
+ }
203
+ });
204
+ });
205
+ hapiServer.state('data', options);
206
+
207
+ const sourceContext = core.scopes.sources.getStore()?.assess;
208
+ expect(core.assess.sessionConfiguration.handleHttpOnly).to.have.been.calledWith(
209
+ sourceContext,
210
+ 'set-cookie: foo',
211
+ sinon.match({
212
+ args: [
213
+ {
214
+ tracked: false,
215
+ value: util.inspect(options),
216
+ },
217
+ ],
218
+ context: `state(${util.inspect(['data', options])})`,
219
+ history: [],
220
+ name: `hapi.${server}.state`,
221
+ moduleName: 'hapi',
222
+ methodName: '',
223
+ object: {
224
+ tracked: false,
225
+ value: `hapi.${server}`,
226
+ },
227
+ result: {
228
+ tracked: false,
229
+ value: undefined,
230
+ },
231
+ source: 'P1',
232
+ framework: 'hapi',
233
+ options,
234
+ })
235
+ );
236
+
237
+ expect(core.assess.sessionConfiguration.handleSecure).to.have.been.calledWith(
238
+ sourceContext,
239
+ 'set-cookie: foo',
240
+ sinon.match({
241
+ args: [
242
+ {
243
+ tracked: false,
244
+ value: util.inspect(options),
245
+ },
246
+ ],
247
+ context: `state(${util.inspect(['data', options])})`,
248
+ history: [],
249
+ name: `hapi.${server}.state`,
250
+ moduleName: 'hapi',
251
+ methodName: '',
252
+ object: {
253
+ tracked: false,
254
+ value: `hapi.${server}`,
255
+ },
256
+ result: {
257
+ tracked: false,
258
+ value: undefined,
259
+ },
260
+ source: 'P1',
261
+ framework: 'hapi',
262
+ options,
263
+ })
264
+ );
265
+ });
266
+ });
267
+ });
268
+ });
269
+ });