@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.
- package/lib/crypto-analysis/install/crypto.test.js +146 -0
- package/lib/crypto-analysis/install/math.test.js +65 -0
- package/lib/dataflow/index.test.js +36 -0
- package/lib/dataflow/propagation/index.test.js +103 -0
- package/lib/dataflow/propagation/install/JSON/index.test.js +50 -0
- package/lib/dataflow/propagation/install/JSON/parse-fn.test.js +232 -0
- package/lib/dataflow/propagation/install/JSON/parse.test.js +968 -0
- package/lib/dataflow/propagation/install/JSON/stringify.test.js +265 -0
- package/lib/dataflow/propagation/install/array-prototype-join.test.js +106 -0
- package/lib/dataflow/propagation/install/buffer.test.js +109 -0
- package/lib/dataflow/propagation/install/contrast-methods/add.test.js +94 -0
- package/lib/dataflow/propagation/install/contrast-methods/index.test.js +49 -0
- package/lib/dataflow/propagation/install/contrast-methods/number.test.js +50 -0
- package/lib/dataflow/propagation/install/contrast-methods/string.test.js +148 -0
- package/lib/dataflow/propagation/install/contrast-methods/tag.test.js +145 -0
- package/lib/dataflow/propagation/install/decode-uri-component.test.js +78 -0
- package/lib/dataflow/propagation/install/ejs/escape-xml.test.js +69 -0
- package/lib/dataflow/propagation/install/ejs/template.test.js +62 -0
- package/lib/dataflow/propagation/install/encode-uri.test.js +83 -0
- package/lib/dataflow/propagation/install/escape-html.test.js +71 -0
- package/lib/dataflow/propagation/install/escape.test.js +73 -0
- package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.test.js +71 -0
- package/lib/dataflow/propagation/install/isnumeric-0.test.js +58 -0
- package/lib/dataflow/propagation/install/joi/any.test.js +270 -0
- package/lib/dataflow/propagation/install/joi/array.test.js +912 -0
- package/lib/dataflow/propagation/install/joi/boolean.test.js +103 -0
- package/lib/dataflow/propagation/install/joi/expression.test.js +76 -0
- package/lib/dataflow/propagation/install/joi/index.test.js +39 -0
- package/lib/dataflow/propagation/install/joi/number.test.js +103 -0
- package/lib/dataflow/propagation/install/joi/object.test.js +119 -0
- package/lib/dataflow/propagation/install/joi/ref.test.js +607 -0
- package/lib/dataflow/propagation/install/joi/string-schema.test.js +513 -0
- package/lib/dataflow/propagation/install/mongoose/index.test.js +42 -0
- package/lib/dataflow/propagation/install/mongoose/schema-map.test.js +348 -0
- package/lib/dataflow/propagation/install/mongoose/schema-mixed.test.js +512 -0
- package/lib/dataflow/propagation/install/mongoose/schema-string.test.js +160 -0
- package/lib/dataflow/propagation/install/mustache-escape.test.js +62 -0
- package/lib/dataflow/propagation/install/mysql-connection-escape.test.js +74 -0
- package/lib/dataflow/propagation/install/parse-int.test.js +48 -0
- package/lib/dataflow/propagation/install/path/basename.test.js +143 -0
- package/lib/dataflow/propagation/install/path/dirname.test.js +167 -0
- package/lib/dataflow/propagation/install/path/extname.test.js +141 -0
- package/lib/dataflow/propagation/install/path/format.test.js +250 -0
- package/lib/dataflow/propagation/install/path/index.test.js +45 -0
- package/lib/dataflow/propagation/install/path/join-and-resolve.test.js +485 -0
- package/lib/dataflow/propagation/install/path/normalize.test.js +176 -0
- package/lib/dataflow/propagation/install/path/parse.test.js +238 -0
- package/lib/dataflow/propagation/install/path/relative.test.js +239 -0
- package/lib/dataflow/propagation/install/path/toNamespacedPath.test.js +158 -0
- package/lib/dataflow/propagation/install/pug/index.test.js +55 -0
- package/lib/dataflow/propagation/install/pug-runtime-escape.test.js +69 -0
- package/lib/dataflow/propagation/install/querystring/escape.test.js +63 -0
- package/lib/dataflow/propagation/install/querystring/index.test.js +40 -0
- package/lib/dataflow/propagation/install/querystring/parse.test.js +272 -0
- package/lib/dataflow/propagation/install/querystring/stringify.test.js +301 -0
- package/lib/dataflow/propagation/install/reg-exp-prototype-exec.test.js +281 -0
- package/lib/dataflow/propagation/install/send.test.js +63 -0
- package/lib/dataflow/propagation/install/sequelize/query-generator.test.js +73 -0
- package/lib/dataflow/propagation/install/sequelize/sql-string.test.js +130 -0
- package/lib/dataflow/propagation/install/sql-template-strings.test.js +100 -0
- package/lib/dataflow/propagation/install/string/concat.test.js +132 -0
- package/lib/dataflow/propagation/install/string/format-methods.test.js +61 -0
- package/lib/dataflow/propagation/install/string/html-methods.test.js +164 -0
- package/lib/dataflow/propagation/install/string/index.test.js +103 -0
- package/lib/dataflow/propagation/install/string/match-all.test.js +399 -0
- package/lib/dataflow/propagation/install/string/match.test.js +361 -0
- package/lib/dataflow/propagation/install/string/replace.test.js +588 -0
- package/lib/dataflow/propagation/install/string/slice.test.js +265 -0
- package/lib/dataflow/propagation/install/string/split.test.js +500 -0
- package/lib/dataflow/propagation/install/string/substring.test.js +238 -0
- package/lib/dataflow/propagation/install/string/trim.test.js +122 -0
- package/lib/dataflow/propagation/install/unescape.test.js +78 -0
- package/lib/dataflow/propagation/install/url/domain-parsers.test.js +63 -0
- package/lib/dataflow/propagation/install/url/parse.test.js +391 -0
- package/lib/dataflow/propagation/install/url/searchParams.test.js +538 -0
- package/lib/dataflow/propagation/install/url/url.test.js +466 -0
- package/lib/dataflow/propagation/install/util-format.test.js +336 -0
- package/lib/dataflow/propagation/install/validator/hooks.test.js +211 -0
- package/lib/dataflow/sinks/index.test.js +78 -0
- package/lib/dataflow/sinks/install/child-process.test.js +338 -0
- package/lib/dataflow/sinks/install/eval.test.js +95 -0
- package/lib/dataflow/sinks/install/express/index.test.js +33 -0
- package/lib/dataflow/sinks/install/express/reflected-xss.js +55 -57
- package/lib/dataflow/sinks/install/express/reflected-xss.test.js +109 -0
- package/lib/dataflow/sinks/install/express/unvalidated-redirect.test.js +144 -0
- package/lib/dataflow/sinks/install/fastify/index.test.js +32 -0
- package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.test.js +130 -0
- package/lib/dataflow/sinks/install/fs.test.js +138 -0
- package/lib/dataflow/sinks/install/function.test.js +103 -0
- package/lib/dataflow/sinks/install/hapi/index.test.js +32 -0
- package/lib/dataflow/sinks/install/hapi/unvalidated-redirect.test.js +130 -0
- package/lib/dataflow/sinks/install/http/index.test.js +33 -0
- package/lib/dataflow/sinks/install/http/request.test.js +184 -0
- package/lib/dataflow/sinks/install/http/server-response.test.js +162 -0
- package/lib/dataflow/sinks/install/koa/index.test.js +32 -0
- package/lib/dataflow/sinks/install/koa/unvalidated-redirect.test.js +200 -0
- package/lib/dataflow/sinks/install/libxmljs.test.js +158 -0
- package/lib/dataflow/sinks/install/marsdb.test.js +166 -0
- package/lib/dataflow/sinks/install/mongodb.test.js +621 -0
- package/lib/dataflow/sinks/install/mssql.test.js +136 -0
- package/lib/dataflow/sinks/install/mysql.test.js +233 -0
- package/lib/dataflow/sinks/install/node-serialize.test.js +85 -0
- package/lib/dataflow/sinks/install/postgres.test.js +158 -0
- package/lib/dataflow/sinks/install/restify.test.js +142 -0
- package/lib/dataflow/sinks/install/sequelize.test.js +100 -0
- package/lib/dataflow/sinks/install/sqlite3.test.js +118 -0
- package/lib/dataflow/sinks/install/vm.test.js +326 -0
- package/lib/dataflow/sources/handler.test.js +463 -0
- package/lib/dataflow/sources/index.test.js +58 -0
- package/lib/dataflow/sources/install/body-parser1.test.js +248 -0
- package/lib/dataflow/sources/install/busboy.test.js +152 -0
- package/lib/dataflow/sources/install/cookie-parser1.test.js +143 -0
- package/lib/dataflow/sources/install/express/params.test.js +105 -0
- package/lib/dataflow/sources/install/express/parsedUrl.test.js +65 -0
- package/lib/dataflow/sources/install/fastify/fastify.test.js +210 -0
- package/lib/dataflow/sources/install/fastify/index.test.js +33 -0
- package/lib/dataflow/sources/install/formidable1.test.js +119 -0
- package/lib/dataflow/sources/install/hapi/hapi.test.js +172 -0
- package/lib/dataflow/sources/install/hapi/index.test.js +33 -0
- package/lib/dataflow/sources/install/http.test.js +155 -0
- package/lib/dataflow/sources/install/koa/index.test.js +40 -0
- package/lib/dataflow/sources/install/koa/koa-bodyparsers.test.js +161 -0
- package/lib/dataflow/sources/install/koa/koa-multer.test.js +197 -0
- package/lib/dataflow/sources/install/koa/koa-routers.test.js +146 -0
- package/lib/dataflow/sources/install/koa/koa2.test.js +145 -0
- package/lib/dataflow/sources/install/multer1.test.js +145 -0
- package/lib/dataflow/sources/install/qs6.test.js +131 -0
- package/lib/dataflow/sources/install/querystring.test.js +82 -0
- package/lib/dataflow/sources/install/restify/fieldedTextBodyParser.test.js +88 -0
- package/lib/dataflow/sources/install/restify/index.test.js +38 -0
- package/lib/dataflow/sources/install/restify/jsonBodyParser.test.js +144 -0
- package/lib/dataflow/sources/install/restify/router.test.js +83 -0
- package/lib/dataflow/tag-utils-complete.test.js +27 -0
- package/lib/dataflow/tag-utils.test.js +192 -0
- package/lib/dataflow/tracker.js +7 -2
- package/lib/dataflow/tracker.test.js +216 -0
- package/lib/dataflow/utils/is-safe-content-type.test.js +16 -0
- package/lib/dataflow/utils/is-vulnerable.test.js +115 -0
- package/lib/event-factory.test.js +321 -0
- package/lib/get-policy.test.js +194 -0
- package/lib/get-source-context.js +10 -1
- package/lib/get-source-context.test.js +108 -0
- package/lib/index.test.js +41 -0
- package/lib/make-source-context.test.js +50 -0
- package/lib/response-scanning/handlers/index.test.js +425 -0
- package/lib/response-scanning/handlers/utils.test.js +391 -0
- package/lib/response-scanning/index.test.js +41 -0
- package/lib/response-scanning/install/http.test.js +175 -0
- package/lib/rule-scopes.test.js +27 -0
- package/lib/session-configuration/handlers.test.js +84 -0
- package/lib/session-configuration/index.test.js +36 -0
- package/lib/session-configuration/install/express-session.test.js +220 -0
- package/lib/session-configuration/install/fastify-cookie.test.js +65 -0
- package/lib/session-configuration/install/hapi.test.js +269 -0
- package/lib/session-configuration/install/koa.test.js +92 -0
- package/package.json +2 -2
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { expect } = require('chai');
|
|
4
|
+
const sinon = require('sinon');
|
|
5
|
+
const {
|
|
6
|
+
DataflowTag,
|
|
7
|
+
InputType
|
|
8
|
+
} = require('@contrast/common');
|
|
9
|
+
const { initAssessFixture } = require('@contrast/test/fixtures');
|
|
10
|
+
|
|
11
|
+
describe('assess dataflow sources handler', function () {
|
|
12
|
+
let core, simulateRequestScope, sources, tracker;
|
|
13
|
+
|
|
14
|
+
beforeEach(function () {
|
|
15
|
+
({ core, simulateRequestScope } = initAssessFixture());
|
|
16
|
+
core.config.assess.max_context_source_events = 100;
|
|
17
|
+
|
|
18
|
+
// we want to remove previous calls to `logger` methods from before these tests.
|
|
19
|
+
sinon.reset();
|
|
20
|
+
|
|
21
|
+
sources = core.assess.dataflow.sources;
|
|
22
|
+
tracker = core.assess.dataflow.tracker;
|
|
23
|
+
|
|
24
|
+
sinon.spy(tracker, 'track');
|
|
25
|
+
core.assess.dataflow.propagation.bufferInstrumentation.install();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('composes methods on core correctly', function () {
|
|
29
|
+
expect(sources.createTags).to.be.a('function');
|
|
30
|
+
expect(sources.handle).to.be.a('function');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('createTags', function () {
|
|
34
|
+
[
|
|
35
|
+
{
|
|
36
|
+
arg: {
|
|
37
|
+
value: ''
|
|
38
|
+
},
|
|
39
|
+
expected: null,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
arg: {
|
|
43
|
+
value: undefined
|
|
44
|
+
},
|
|
45
|
+
expected: null,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
arg: {
|
|
49
|
+
inputType: InputType.QUERYSTRING,
|
|
50
|
+
name: 'foo',
|
|
51
|
+
value: 'a',
|
|
52
|
+
},
|
|
53
|
+
expected: {
|
|
54
|
+
[DataflowTag.UNTRUSTED]: [0, 0]
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
arg: {
|
|
59
|
+
inputType: InputType.QUERYSTRING,
|
|
60
|
+
fieldName: 'foo',
|
|
61
|
+
value: 'ab',
|
|
62
|
+
},
|
|
63
|
+
expected: {
|
|
64
|
+
[DataflowTag.UNTRUSTED]: [0, 1]
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
arg: {
|
|
69
|
+
inputType: InputType.HEADER,
|
|
70
|
+
fieldName: 'foo',
|
|
71
|
+
value: 'abcd',
|
|
72
|
+
},
|
|
73
|
+
expected: {
|
|
74
|
+
[DataflowTag.UNTRUSTED]: [0, 3]
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
arg: {
|
|
79
|
+
inputType: InputType.HEADER,
|
|
80
|
+
fieldName: 'referer',
|
|
81
|
+
value: 'abcd',
|
|
82
|
+
tagNames: ['excluded:sql-injection'],
|
|
83
|
+
},
|
|
84
|
+
expected: {
|
|
85
|
+
[DataflowTag.HEADER]: [0, 3],
|
|
86
|
+
[DataflowTag.UNTRUSTED]: [0, 3],
|
|
87
|
+
['excluded:sql-injection']: [0, 3],
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
].forEach(({ arg, expected }) => {
|
|
91
|
+
it('creates expected tags based on name, value, and input type', function () {
|
|
92
|
+
const result = sources.createTags(arg);
|
|
93
|
+
expect(result).to.deep.equal(expected);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('handle', function () {
|
|
99
|
+
it('does not handle when no assess source context is active', function () {
|
|
100
|
+
const result = sources.handle({
|
|
101
|
+
name: 'test-source-name',
|
|
102
|
+
inputType: InputType.QUERYSTRING,
|
|
103
|
+
data: {
|
|
104
|
+
prop1: 'foo',
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
expect(result).to.be.null;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('does nothing when async store\'s assess policy is null', function () {
|
|
111
|
+
simulateRequestScope(() => {
|
|
112
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
113
|
+
const trackedObj = sources.handle({
|
|
114
|
+
name: 'test-source-name',
|
|
115
|
+
inputType: InputType.QUERYSTRING,
|
|
116
|
+
sourceContext
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(trackedObj).to.be.undefined;
|
|
120
|
+
expect(core.logger.trace).not.to.have.been.called;
|
|
121
|
+
expect(core.logger.warn).not.to.have.been.called;
|
|
122
|
+
expect(tracker.track).not.to.have.been.called;
|
|
123
|
+
}, { assess: { policy: null } });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('does nothing when no data is passed', function () {
|
|
127
|
+
simulateRequestScope(() => {
|
|
128
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
129
|
+
const trackedObj = sources.handle({
|
|
130
|
+
name: 'test-source-name',
|
|
131
|
+
inputType: InputType.QUERYSTRING,
|
|
132
|
+
sourceContext
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(trackedObj).to.be.undefined;
|
|
136
|
+
expect(core.logger.trace).not.to.have.been.called;
|
|
137
|
+
expect(core.logger.warn).not.to.have.been.called;
|
|
138
|
+
expect(tracker.track).not.to.have.been.called;
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('traverses objects and tracks string values', function () {
|
|
143
|
+
simulateRequestScope(() => {
|
|
144
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
145
|
+
const trackedObj = sources.handle({
|
|
146
|
+
name: 'test-source-name',
|
|
147
|
+
inputType: InputType.QUERYSTRING,
|
|
148
|
+
data: {
|
|
149
|
+
prop1: 'foo',
|
|
150
|
+
prop2: ['bar', 'bugs'],
|
|
151
|
+
},
|
|
152
|
+
sourceContext
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
expect(tracker.getData(trackedObj.prop1)).to.be.like({
|
|
156
|
+
name: 'test-source-name',
|
|
157
|
+
pathName: 'prop1',
|
|
158
|
+
result: { tracked: true, value: 'foo' },
|
|
159
|
+
inputType: InputType.QUERYSTRING,
|
|
160
|
+
stack: [],
|
|
161
|
+
value: 'foo',
|
|
162
|
+
tags: {
|
|
163
|
+
[DataflowTag.UNTRUSTED]: [0, 2],
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
expect(tracker.getData(trackedObj.prop2[0])).to.be.like({
|
|
168
|
+
name: 'test-source-name',
|
|
169
|
+
pathName: 'prop2.0',
|
|
170
|
+
result: { tracked: true, value: 'bar' },
|
|
171
|
+
inputType: InputType.QUERYSTRING,
|
|
172
|
+
stack: [],
|
|
173
|
+
value: 'bar',
|
|
174
|
+
tags: {
|
|
175
|
+
[DataflowTag.UNTRUSTED]: [0, 2],
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
expect(tracker.getData(trackedObj.prop2[1])).to.be.like({
|
|
180
|
+
name: 'test-source-name',
|
|
181
|
+
pathName: 'prop2.1',
|
|
182
|
+
result: { tracked: true, value: 'bugs' },
|
|
183
|
+
inputType: InputType.QUERYSTRING,
|
|
184
|
+
stack: [],
|
|
185
|
+
value: 'bugs',
|
|
186
|
+
tags: {
|
|
187
|
+
[DataflowTag.UNTRUSTED]: [0, 3],
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
core.config.assess.stacktraces = 'ALL';
|
|
192
|
+
const anotherTrackedObj = sources.handle({
|
|
193
|
+
name: 'test-source-name',
|
|
194
|
+
inputType: InputType.QUERYSTRING,
|
|
195
|
+
data: { prop1: 'foo' },
|
|
196
|
+
sourceContext
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const trackData = tracker.getData(anotherTrackedObj.prop1);
|
|
200
|
+
expect(trackData).to.be.like({
|
|
201
|
+
name: 'test-source-name',
|
|
202
|
+
pathName: 'prop1',
|
|
203
|
+
result: { tracked: true, value: 'foo' },
|
|
204
|
+
inputType: InputType.QUERYSTRING,
|
|
205
|
+
value: 'foo',
|
|
206
|
+
tags: {
|
|
207
|
+
[DataflowTag.UNTRUSTED]: [0, 2],
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const pathSep = process.platform === 'win32' ? '\\' : '/';
|
|
212
|
+
|
|
213
|
+
expect(trackData.stack.some((entry) => entry.file?.includes(`core${pathSep}lib${pathSep}capture-stacktrace.js`))).to.be.true;
|
|
214
|
+
expect(trackData.stack.some((entry) => entry.method?.includes('core.createSnapshot'))).to.be.true;
|
|
215
|
+
expect(trackData.stack.some((entry) => entry.file?.includes(`assess${pathSep}lib${pathSep}dataflow${pathSep}sources${pathSep}handler.js`))).to.be.true;
|
|
216
|
+
expect(trackData.stack.some((entry) => entry.method?.includes('sources.handle'))).to.be.true;
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('traverses objects and tracks string values by specific keys', function () {
|
|
221
|
+
simulateRequestScope(() => {
|
|
222
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
223
|
+
const trackedObj = sources.handle({
|
|
224
|
+
name: 'test-source-name',
|
|
225
|
+
keys: ['prop1', 'prop3'],
|
|
226
|
+
inputType: InputType.QUERYSTRING,
|
|
227
|
+
data: {
|
|
228
|
+
prop1: 'foo',
|
|
229
|
+
prop2: ['bar', 'bugs'],
|
|
230
|
+
prop3: {
|
|
231
|
+
foo: 'yellow'
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
sourceContext
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
expect(tracker.getData(trackedObj.prop1)).to.be.like({
|
|
238
|
+
name: 'test-source-name',
|
|
239
|
+
pathName: 'prop1',
|
|
240
|
+
result: { tracked: true, value: 'foo' },
|
|
241
|
+
inputType: InputType.QUERYSTRING,
|
|
242
|
+
stack: [],
|
|
243
|
+
value: 'foo',
|
|
244
|
+
tags: {
|
|
245
|
+
[DataflowTag.UNTRUSTED]: [0, 2],
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
expect(tracker.getData(trackedObj.prop2[0])).to.be.null;
|
|
249
|
+
expect(tracker.getData(trackedObj.prop2[1])).to.be.null;
|
|
250
|
+
expect(tracker.getData(trackedObj.prop3.foo)).to.be.like({
|
|
251
|
+
name: 'test-source-name',
|
|
252
|
+
pathName: 'prop3.foo',
|
|
253
|
+
result: { tracked: true, value: 'yellow' },
|
|
254
|
+
inputType: InputType.QUERYSTRING,
|
|
255
|
+
stack: [],
|
|
256
|
+
value: 'yellow',
|
|
257
|
+
tags: {
|
|
258
|
+
[DataflowTag.UNTRUSTED]: [0, 5],
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('traverses objects and tracks Buffer values', function () {
|
|
265
|
+
simulateRequestScope(() => {
|
|
266
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
267
|
+
const event = {
|
|
268
|
+
name: 'test-source-name',
|
|
269
|
+
inputType: InputType.QUERYSTRING,
|
|
270
|
+
data: {
|
|
271
|
+
prop1: Buffer.from('foo'),
|
|
272
|
+
prop2: Buffer.from('ascii', 'ascii'),
|
|
273
|
+
prop3: Buffer.from('base64', 'base64'),
|
|
274
|
+
prop4: Buffer.from('binary', 'binary'),
|
|
275
|
+
prop5: {
|
|
276
|
+
foo: Buffer.from('yellow'),
|
|
277
|
+
},
|
|
278
|
+
prop6: 'a string value',
|
|
279
|
+
},
|
|
280
|
+
sourceContext
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
sources.handle(event);
|
|
284
|
+
|
|
285
|
+
expect(tracker.getData(event.data.prop1.toString())).to.be.ok;
|
|
286
|
+
expect(tracker.getData(event.data.prop2.toString())).to.be.ok;
|
|
287
|
+
expect(tracker.getData(event.data.prop3.toString())).to.be.ok;
|
|
288
|
+
expect(tracker.getData(event.data.prop4.toString())).to.be.ok;
|
|
289
|
+
expect(tracker.getData(event.data.prop5.foo.toString())).to.be.ok;
|
|
290
|
+
expect(tracker.getData(event.data.prop6)).to.be.ok;
|
|
291
|
+
// we don't cache metadata since buffer's content can change
|
|
292
|
+
expect(
|
|
293
|
+
tracker.getData(event.data.prop1.toString())
|
|
294
|
+
).not.to.equal(
|
|
295
|
+
tracker.getData(event.data.prop1.toString())
|
|
296
|
+
);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('appropriately tracks when data param is Buffer instance', function () {
|
|
301
|
+
simulateRequestScope(() => {
|
|
302
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
303
|
+
const event = {
|
|
304
|
+
name: 'test-source-name',
|
|
305
|
+
inputType: InputType.QUERYSTRING,
|
|
306
|
+
data: Buffer.from('foo'),
|
|
307
|
+
sourceContext
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
sources.handle(event);
|
|
311
|
+
|
|
312
|
+
expect(tracker.getData(event.data)).to.be.ok;
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('exits source handling when configured max source events is reached', function () {
|
|
317
|
+
core.config.assess.max_context_source_events = 1;
|
|
318
|
+
simulateRequestScope(() => {
|
|
319
|
+
const name = 'test-source-name';
|
|
320
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
321
|
+
const trackedObj = sources.handle({
|
|
322
|
+
name,
|
|
323
|
+
inputType: InputType.QUERYSTRING,
|
|
324
|
+
data: {
|
|
325
|
+
prop1: 'foo',
|
|
326
|
+
prop2: ['bar', 'bugs'],
|
|
327
|
+
},
|
|
328
|
+
sourceContext
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
expect(tracker.getData(trackedObj.prop1)).to.be.like({
|
|
332
|
+
name: 'test-source-name',
|
|
333
|
+
pathName: 'prop1',
|
|
334
|
+
result: { tracked: true, value: 'foo' },
|
|
335
|
+
inputType: InputType.QUERYSTRING,
|
|
336
|
+
stack: [],
|
|
337
|
+
value: 'foo',
|
|
338
|
+
tags: {
|
|
339
|
+
[DataflowTag.UNTRUSTED]: [0, 2],
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
expect(tracker.getData(trackedObj.prop2[0])).to.be.null;
|
|
344
|
+
expect(tracker.getData(trackedObj.prop2[1])).to.be.null;
|
|
345
|
+
expect(core.logger.trace).to.have.been.calledWith(
|
|
346
|
+
{ sourceName: name, inputType: InputType.QUERYSTRING },
|
|
347
|
+
'exiting assess source handling - %s max events exceeded',
|
|
348
|
+
1
|
|
349
|
+
);
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('logs an error when cannot create an event', function () {
|
|
354
|
+
simulateRequestScope(() => {
|
|
355
|
+
const name = 'test-source-name';
|
|
356
|
+
const sourceContext = core.scopes.sources.getStore()?.assess;
|
|
357
|
+
sources.handle({
|
|
358
|
+
name,
|
|
359
|
+
inputType: 'wrong-input-type',
|
|
360
|
+
data: {
|
|
361
|
+
prop1: 'foo',
|
|
362
|
+
},
|
|
363
|
+
sourceContext
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
expect(tracker.track).not.to.have.been.called;
|
|
367
|
+
expect(core.logger.trace).not.to.have.been.called;
|
|
368
|
+
expect(core.logger.warn).to.have.been.calledOnceWith(
|
|
369
|
+
{ inputType: 'wrong-input-type', sourceName: 'test-source-name', pathName: 'prop1', value: 'foo' },
|
|
370
|
+
'unable to create source event',
|
|
371
|
+
);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
describe('traversal', function () {
|
|
377
|
+
const { traverse } = require('./handler');
|
|
378
|
+
let visitorSpy;
|
|
379
|
+
|
|
380
|
+
beforeEach(function () {
|
|
381
|
+
visitorSpy = sinon.stub();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('doesn\'t revist seen references', function () {
|
|
385
|
+
const obj = {
|
|
386
|
+
prop1: 'foo',
|
|
387
|
+
prop2: { foo: 'bar' },
|
|
388
|
+
prop3: [
|
|
389
|
+
'hello',
|
|
390
|
+
['world'],
|
|
391
|
+
{ key: 'val' },
|
|
392
|
+
]
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
obj.circ = obj;
|
|
396
|
+
|
|
397
|
+
traverse(obj, (path, key, value) => {
|
|
398
|
+
// copy path, otherwise it will be empty by assertion time
|
|
399
|
+
visitorSpy(Array.from(path), key, value);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const visitorArgs = [
|
|
403
|
+
[['prop1'], 'prop1', 'foo'],
|
|
404
|
+
[['prop2', 'foo'], 'foo', 'bar'],
|
|
405
|
+
[['prop3', '0'], '0', 'hello'],
|
|
406
|
+
[['prop3', '1', '0'], '0', 'world'],
|
|
407
|
+
[['prop3', '2', 'key'], 'key', 'val'],
|
|
408
|
+
[['circ', 'prop1'], 'prop1', 'foo'],
|
|
409
|
+
];
|
|
410
|
+
|
|
411
|
+
visitorArgs.forEach((args) => {
|
|
412
|
+
expect(visitorSpy).to.have.been.calledWithMatch(...args);
|
|
413
|
+
});
|
|
414
|
+
expect(visitorSpy).to.have.callCount(visitorArgs.length);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it('will not recurse into buffers or visit numbers, null, Maps, Sets, or undefined', function () {
|
|
418
|
+
const b = Buffer.from('buffer content');
|
|
419
|
+
|
|
420
|
+
traverse({
|
|
421
|
+
b,
|
|
422
|
+
numberThree: 3,
|
|
423
|
+
numberNine: Number('9'),
|
|
424
|
+
null: null,
|
|
425
|
+
undefined,
|
|
426
|
+
set: new Set(['frog', 'beetle']),
|
|
427
|
+
map: new Map([['k', 'v']])
|
|
428
|
+
}, (path, key, value) => {
|
|
429
|
+
visitorSpy(Array.from(path), key, value);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
expect(visitorSpy).to.have.been.calledWithMatch(['b'], 'b', b).and.have.callCount(1);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('visits and traverses objects created by Object.create(null)', function () {
|
|
436
|
+
const o = Object.create(null);
|
|
437
|
+
o.foo = 'bar';
|
|
438
|
+
o.x = Object.assign(Object.create(null), { abc: 'xyz' });
|
|
439
|
+
|
|
440
|
+
traverse(o, (path, key, value) => {
|
|
441
|
+
visitorSpy(Array.from(path), key, value);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
[
|
|
445
|
+
[['foo'], 'foo', 'bar'],
|
|
446
|
+
[['x'], 'x', { abc: 'xyz' }],
|
|
447
|
+
[['x', 'abc'], 'abc', 'xyz']
|
|
448
|
+
].forEach((args) => {
|
|
449
|
+
expect(visitorSpy).to.have.been.calledWithMatch(...args);
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
it('halts when callback returns `false`', function () {
|
|
455
|
+
traverse({ values: ['one', 'two'] }, (path, key, value, obj) => {
|
|
456
|
+
visitorSpy();
|
|
457
|
+
return false;
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
expect(visitorSpy).to.have.callCount(1);
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
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[moduleName] = mock[moduleName];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
describe('assess dataflow sources', function () {
|
|
12
|
+
let core, sourcesMock, sourcesModule, modulesWithInstall;
|
|
13
|
+
|
|
14
|
+
beforeEach(function () {
|
|
15
|
+
core = mocks.core();
|
|
16
|
+
core.assess = mocks.assess();
|
|
17
|
+
core.logger = mocks.logger();
|
|
18
|
+
sourcesMock = core.assess.dataflow.sources;
|
|
19
|
+
sourcesModule = proxyquire('.', {
|
|
20
|
+
'./install/http': moduleMock('httpSourcesInstr', sourcesMock),
|
|
21
|
+
'./install/express': moduleMock('expressInstrumentation', sourcesMock),
|
|
22
|
+
'./install/fastify': moduleMock('fastifyInstrumentation', sourcesMock),
|
|
23
|
+
'./install/hapi': moduleMock('hapiInstrumentation', sourcesMock),
|
|
24
|
+
'./install/koa': moduleMock('koaInstrumentation', sourcesMock),
|
|
25
|
+
'./install/restify': moduleMock('restifyInstrumentation', sourcesMock),
|
|
26
|
+
'./install/body-parser1': moduleMock('bodyParser1Instrumentation', sourcesMock),
|
|
27
|
+
'./install/cookie-parser1': moduleMock('cookieParser1Instrumentation', sourcesMock),
|
|
28
|
+
'./install/busboy': moduleMock('busboyInstrumentation', sourcesMock),
|
|
29
|
+
'./install/multer1': moduleMock('multer1Instrumentation', sourcesMock),
|
|
30
|
+
'./install/formidable1': moduleMock('formidable1Instrumentation', sourcesMock),
|
|
31
|
+
'./install/qs6': moduleMock('qs6Instrumentation', sourcesMock),
|
|
32
|
+
'./install/querystring': moduleMock('querystringInstrumentation', sourcesMock),
|
|
33
|
+
});
|
|
34
|
+
modulesWithInstall = [
|
|
35
|
+
'httpSourcesInstr',
|
|
36
|
+
'expressInstrumentation',
|
|
37
|
+
'fastifyInstrumentation',
|
|
38
|
+
'hapiInstrumentation',
|
|
39
|
+
'koaInstrumentation',
|
|
40
|
+
'restifyInstrumentation',
|
|
41
|
+
'bodyParser1Instrumentation',
|
|
42
|
+
'busboyInstrumentation',
|
|
43
|
+
'multer1Instrumentation',
|
|
44
|
+
'cookieParser1Instrumentation',
|
|
45
|
+
'formidable1Instrumentation',
|
|
46
|
+
'qs6Instrumentation',
|
|
47
|
+
'querystringInstrumentation',
|
|
48
|
+
];
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('installs the components', function () {
|
|
52
|
+
sourcesModule(core).install();
|
|
53
|
+
modulesWithInstall.forEach((module) => {
|
|
54
|
+
expect(sourcesMock[module].install).to.have.been.calledOnce;
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|