@contrast/assess 1.31.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 (154) 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.test.js +216 -0
  136. package/lib/dataflow/utils/is-safe-content-type.test.js +16 -0
  137. package/lib/dataflow/utils/is-vulnerable.test.js +115 -0
  138. package/lib/event-factory.test.js +321 -0
  139. package/lib/get-policy.test.js +194 -0
  140. package/lib/get-source-context.test.js +108 -0
  141. package/lib/index.test.js +41 -0
  142. package/lib/make-source-context.test.js +50 -0
  143. package/lib/response-scanning/handlers/index.test.js +425 -0
  144. package/lib/response-scanning/handlers/utils.test.js +391 -0
  145. package/lib/response-scanning/index.test.js +41 -0
  146. package/lib/response-scanning/install/http.test.js +175 -0
  147. package/lib/rule-scopes.test.js +27 -0
  148. package/lib/session-configuration/handlers.test.js +84 -0
  149. package/lib/session-configuration/index.test.js +36 -0
  150. package/lib/session-configuration/install/express-session.test.js +220 -0
  151. package/lib/session-configuration/install/fastify-cookie.test.js +65 -0
  152. package/lib/session-configuration/install/hapi.test.js +269 -0
  153. package/lib/session-configuration/install/koa.test.js +92 -0
  154. package/package.json +2 -2
@@ -0,0 +1,136 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ DataflowTag: { UNTRUSTED, SQL_ENCODED }
5
+ } = require('@contrast/common');
6
+ const { expect } = require('chai');
7
+ const sinon = require('sinon');
8
+ const { initAssessFixture } = require('@contrast/test/fixtures');
9
+
10
+ describe('assess dataflow sinks mssql', function () {
11
+ let core, simulateRequestScope, trackString, tracker, reportFindings, reportSafePositive;
12
+
13
+ class PreparedStatement {
14
+ prepare() { }
15
+ }
16
+
17
+ class Request {
18
+ batch() { }
19
+ query() { }
20
+ }
21
+
22
+ beforeEach(function () {
23
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
24
+ tracker = core.assess.dataflow.tracker;
25
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
26
+ reportSafePositive = sinon.stub(core.assess.dataflow.sinks, 'reportSafePositive');
27
+
28
+ core.depHooks.resolve
29
+ .withArgs({ name: 'mssql', file: 'lib/base/prepared-statement.js' })
30
+ .yields(PreparedStatement);
31
+
32
+ core.depHooks.resolve
33
+ .withArgs({ name: 'mssql', file: 'lib/base/request.js' })
34
+ .yields(Request);
35
+
36
+ require('./mssql')(core).install();
37
+ });
38
+
39
+ [
40
+ { subject: PreparedStatement, method: 'prepare', name: 'mssql/lib/base/prepared-statement.prototype.prepare' },
41
+ { subject: Request, method: 'batch', name: 'mssql/lib/base/request.prototype.batch' },
42
+ { subject: Request, method: 'query', name: 'mssql/lib/base/request.prototype.query' },
43
+ ].forEach(({ subject, method, name }) => {
44
+ describe(`${subject.name}.prototype.${method}()`, function () {
45
+ it('skips instrumentation if assess store is missing', function () {
46
+ subject.prototype[method]();
47
+ expect(reportFindings).not.to.have.been.called;
48
+ });
49
+
50
+ it('skips instrumentation if no argument is provided', function () {
51
+ simulateRequestScope(function () {
52
+ subject.prototype[method]();
53
+ expect(reportFindings).not.to.have.been.called;
54
+ });
55
+ });
56
+
57
+ it('skips instrumentation if the argument is not a string', function () {
58
+ simulateRequestScope(function () {
59
+ subject.prototype[method]({});
60
+ expect(reportFindings).not.to.have.been.called;
61
+ });
62
+ });
63
+
64
+ it('skips instrumentation if the argument is not tracked', function () {
65
+ simulateRequestScope(function () {
66
+ subject.prototype[method]('hello');
67
+ expect(reportFindings).not.to.have.been.called;
68
+ });
69
+ });
70
+
71
+ it('skips instrumentation if the argument is safe', function () {
72
+ simulateRequestScope(function () {
73
+ const extern = trackString(';drop table Foo--', {
74
+ tags: {
75
+ [SQL_ENCODED]: [0, 16],
76
+ [UNTRUSTED]: [0, 16],
77
+ },
78
+ });
79
+
80
+ subject.prototype[method](extern);
81
+ expect(reportFindings).not.to.have.been.called;
82
+ });
83
+ });
84
+
85
+ it('reports an sql-injection', function () {
86
+ simulateRequestScope(function () {
87
+ const value = ';drop table Foo--';
88
+ const extern = trackString(value);
89
+
90
+ subject.prototype[method](extern);
91
+ expect(reportFindings).to.have.been.calledWithMatch({
92
+ ruleId: 'sql-injection',
93
+ sinkEvent: {
94
+ name,
95
+ moduleName: 'mssql',
96
+ methodName: `PreparedStatement.prototype.${method}`,
97
+ context: `mssql.PreparedStatement.${method}('${value}')`,
98
+ object: {
99
+ value: sinon.match(subject.name),
100
+ tracked: false,
101
+ },
102
+ args: [{ value, tracked: true }],
103
+ tags: { [UNTRUSTED]: [0, 16] },
104
+ source: 'P0',
105
+ },
106
+ });
107
+ });
108
+ });
109
+
110
+ it('reports safe positive', function () {
111
+ simulateRequestScope(function () {
112
+ const extern = trackString(';drop table Foo--', {
113
+ tags: {
114
+ [SQL_ENCODED]: [0, 16],
115
+ [UNTRUSTED]: [0, 16],
116
+ },
117
+ });
118
+ const strInfo = tracker.getData(extern);
119
+
120
+ subject.prototype[method](extern);
121
+ expect(reportSafePositive).to.have.been.calledWithMatch({
122
+ name,
123
+ ruleId: 'sql-injection',
124
+ safeTags: [
125
+ SQL_ENCODED,
126
+ ],
127
+ strInfo: {
128
+ tags: strInfo.tags,
129
+ value: strInfo.value,
130
+ }
131
+ });
132
+ });
133
+ });
134
+ });
135
+ });
136
+ });
@@ -0,0 +1,233 @@
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 sinks mysql', function () {
8
+ let Connection, connection, core, simulateRequestScope, trackString, tracker, reportFindings;
9
+
10
+ beforeEach(function () {
11
+ Connection = function () { };
12
+ Connection.prototype.query = sinon.stub();
13
+
14
+ connection = function () { };
15
+ connection.prototype.query = sinon.stub();
16
+ connection.prototype.execute = sinon.stub();
17
+
18
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
19
+ tracker = core.assess.dataflow.tracker;
20
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
21
+
22
+ core.depHooks.resolve
23
+ .withArgs({ name: 'mysql', file: 'lib/Connection' })
24
+ .yields(Connection);
25
+
26
+ core.depHooks.resolve
27
+ .withArgs({ name: 'mysql2', file: 'lib/connection' })
28
+ .yields(connection);
29
+
30
+ core.depHooks.resolve
31
+ .withArgs({ name: 'mysql2', file: 'lib/connection' })
32
+ .yields(connection);
33
+
34
+ require('./mysql')(core).install();
35
+ });
36
+
37
+ describe('mysql', function () {
38
+ it('skips instrumentation if assess store is missing', function () {
39
+ const conn = new Connection();
40
+ conn.query('SELECT "foo"');
41
+ expect(reportFindings).not.to.have.been.called;
42
+ });
43
+
44
+ it('skips instrumentation if no argument is provided', function () {
45
+ simulateRequestScope(function () {
46
+ const conn = new Connection();
47
+ conn.query();
48
+ expect(reportFindings).not.to.have.been.called;
49
+ });
50
+ });
51
+
52
+ it('skips instrumentation if the argument is not a string', function () {
53
+ simulateRequestScope(function () {
54
+ const conn = new Connection();
55
+ conn.query({});
56
+ expect(reportFindings).not.to.have.been.called;
57
+ });
58
+ });
59
+
60
+ it('skips instrumentation if the argument is not tracked', function () {
61
+ simulateRequestScope(function () {
62
+ const conn = new Connection();
63
+ conn.query('SELECT "foo"');
64
+ expect(reportFindings).not.to.have.been.called;
65
+ });
66
+ });
67
+
68
+ it('skips instrumentation if the argument is safe', function () {
69
+ simulateRequestScope(function () {
70
+ const extern = trackString(';drop table Foo--', {
71
+ tags: {
72
+ SQL_ENCODED: [0, 16],
73
+ UNTRUSTED: [0, 16],
74
+ },
75
+ });
76
+
77
+ const conn = new Connection();
78
+ conn.query(extern);
79
+ expect(reportFindings).not.to.have.been.called;
80
+ });
81
+ });
82
+
83
+ it('reports an sql-injection (arg0 string)', function () {
84
+ simulateRequestScope(function () {
85
+ const value = ';drop table Foo--';
86
+ const extern = trackString(value);
87
+
88
+ const conn = new Connection();
89
+ conn.query(extern);
90
+ expect(reportFindings).to.have.been.calledWithMatch({
91
+ ruleId: 'sql-injection',
92
+ sinkEvent: {
93
+ name: 'mysql/lib/Connection.query',
94
+ moduleName: 'mysql',
95
+ methodName: 'prototype.query',
96
+ context: `mysql.query('${value}')`,
97
+ object: {
98
+ value: 'mysql.Connection',
99
+ tracked: false,
100
+ },
101
+ args: [{ value, tracked: true }],
102
+ tags: { UNTRUSTED: [0, 16] },
103
+ source: 'P0',
104
+ },
105
+ });
106
+ });
107
+ });
108
+
109
+ it('reports an sql-injection (arg0 obj)', function () {
110
+ simulateRequestScope(function () {
111
+ const value = ';drop table Foo--';
112
+ const extern = trackString(value);
113
+
114
+ const conn = new Connection();
115
+ conn.query({ sql: extern });
116
+ expect(reportFindings).to.have.been.calledWithMatch({
117
+ ruleId: 'sql-injection',
118
+ sinkEvent: {
119
+ name: 'mysql/lib/Connection.query',
120
+ moduleName: 'mysql',
121
+ methodName: 'prototype.query',
122
+ context: `mysql.query({ sql: '${value}' })`,
123
+ object: {
124
+ value: 'mysql.Connection',
125
+ tracked: false,
126
+ },
127
+ args: [{ value, tracked: true }],
128
+ tags: { UNTRUSTED: [0, 16] },
129
+ source: 'P0',
130
+ },
131
+ });
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('mysql2', function () {
137
+ ['query', 'execute'].forEach((method) => {
138
+ describe(`${method}`, function () {
139
+ it('skips instrumentation if assess store is missing', function () {
140
+ const conn = new connection();
141
+ conn[method]('SELECT "foo"');
142
+ expect(reportFindings).not.to.have.been.called;
143
+ });
144
+
145
+ it('skips instrumentation if no argument is provided', function () {
146
+ simulateRequestScope(function () {
147
+ const conn = new connection();
148
+ conn[method]();
149
+ expect(reportFindings).not.to.have.been.called;
150
+ });
151
+ });
152
+
153
+ it('skips instrumentation if the argument is not a string', function () {
154
+ simulateRequestScope(function () {
155
+ const conn = new connection();
156
+ conn[method]({});
157
+ expect(reportFindings).not.to.have.been.called;
158
+ });
159
+ });
160
+
161
+ it('skips instrumentation if the argument is not tracked', function () {
162
+ simulateRequestScope(function () {
163
+ const conn = new connection();
164
+ conn[method]('SELECT "foo"');
165
+ expect(reportFindings).not.to.have.been.called;
166
+ });
167
+ });
168
+
169
+ it('skips instrumentation if the argument is safe', function () {
170
+ simulateRequestScope(function () {
171
+ const tracked = trackString(';drop table Foo--', {
172
+ tags: {
173
+ SQL_ENCODED: [0, 16],
174
+ UNTRUSTED: [0, 16],
175
+ },
176
+ });
177
+ const { extern } = tracker.getData(tracked);
178
+
179
+ const conn = new connection();
180
+ conn[method](extern);
181
+ expect(reportFindings).not.to.have.been.called;
182
+ });
183
+ });
184
+
185
+ it('reports an sql-injection (arg0 string)', function () {
186
+ simulateRequestScope(function () {
187
+ const value = ';drop table Foo--';
188
+ const extern = trackString(value);
189
+
190
+ const conn = new connection();
191
+ conn[method](extern);
192
+ expect(reportFindings).to.have.been.calledWithMatch({
193
+ ruleId: 'sql-injection',
194
+ sinkEvent: {
195
+ name: `mysql2/lib/connection.Connection.${method}`,
196
+ object: {
197
+ value: 'mysql2.connection',
198
+ tracked: false,
199
+ },
200
+ args: [{ value, tracked: true }],
201
+ tags: { UNTRUSTED: [0, 16] },
202
+ source: 'P0',
203
+ },
204
+ });
205
+ });
206
+ });
207
+
208
+ it('reports an sql-injection (arg0 obj)', function () {
209
+ simulateRequestScope(function () {
210
+ const value = ';drop table Foo--';
211
+ const extern = trackString(value);
212
+
213
+ const conn = new connection();
214
+ conn[method]({ sql: extern });
215
+ expect(reportFindings).to.have.been.calledWithMatch({
216
+ ruleId: 'sql-injection',
217
+ sinkEvent: {
218
+ name: `mysql2/lib/connection.Connection.${method}`,
219
+ object: {
220
+ value: 'mysql2.connection',
221
+ tracked: false,
222
+ },
223
+ args: [{ value, tracked: true }],
224
+ tags: { UNTRUSTED: [0, 16] },
225
+ source: 'P0',
226
+ },
227
+ });
228
+ });
229
+ });
230
+ });
231
+ });
232
+ });
233
+ });
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const serialize = require('node-serialize');
6
+ const { initAssessFixture } = require('@contrast/test/fixtures');
7
+
8
+ describe('assess dataflow sinks node-serialize', function () {
9
+ let core, simulateRequestScope, trackString, reportFindings, serializedObj;
10
+
11
+ beforeEach(function () {
12
+
13
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
14
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
15
+
16
+ core.depHooks.resolve
17
+ .withArgs({ name: 'node-serialize' })
18
+ .yields(serialize);
19
+
20
+ require('./node-serialize')(core).install();
21
+
22
+ serializedObj = '{"rce":"_$$ND_FUNC$$_function (){}()"}';
23
+ });
24
+
25
+ describe('node-serialize.unserialize', function() {
26
+ it('skips instrumentation if assess store is missing', function () {
27
+ serialize.unserialize(serializedObj);
28
+ expect(reportFindings).not.to.have.been.called;
29
+ });
30
+
31
+ it('skips instrumentation if no argument is provided', function () {
32
+ simulateRequestScope(function () {
33
+ serialize.unserialize(serializedObj);
34
+ expect(reportFindings).not.to.have.been.called;
35
+ });
36
+ });
37
+
38
+ it('skips instrumentation if instrumentation is locked', function () {
39
+ simulateRequestScope(function () {
40
+ core.scopes.instrumentation.run({ lock: true }, function () {
41
+ serialize.unserialize(serializedObj);
42
+ expect(reportFindings).not.to.have.been.called;
43
+ });
44
+ });
45
+ });
46
+
47
+ it('skips instrumentation if the argument is not a string', function () {
48
+ simulateRequestScope(function () {
49
+ serialize.unserialize(true);
50
+ expect(reportFindings).not.to.have.been.called;
51
+ });
52
+ });
53
+
54
+ it('skips instrumentation if the argument is not tracked', function () {
55
+ simulateRequestScope(function () {
56
+ serialize.unserialize(serializedObj);
57
+ expect(reportFindings).not.to.have.been.called;
58
+ });
59
+ });
60
+
61
+ it('reports untrusted-deserialization', function () {
62
+ simulateRequestScope(function () {
63
+ const extern = trackString(serializedObj);
64
+
65
+ serialize.unserialize(extern);
66
+ expect(reportFindings).to.have.been.calledWithMatch({
67
+ ruleId: 'untrusted-deserialization',
68
+ sinkEvent: {
69
+ name: 'node-serialize.unserialize',
70
+ moduleName: 'node-serialize',
71
+ methodName: 'unserialize',
72
+ context: 'node-serialize.unserialize({"rce":"_$$ND_FUNC$$_function (){}()"})',
73
+ object: {
74
+ value: 'node-serialize',
75
+ tracked: false,
76
+ },
77
+ args: [{ value: serializedObj, tracked: true }],
78
+ tags: { UNTRUSTED: [0, 37] },
79
+ source: 'P0',
80
+ },
81
+ });
82
+ });
83
+ });
84
+ });
85
+ });
@@ -0,0 +1,158 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ DataflowTag: { UNTRUSTED, SQL_ENCODED },
5
+ Rule: { SQL_INJECTION: ruleId },
6
+ } = require('@contrast/common');
7
+ const { expect } = require('chai');
8
+ const sinon = require('sinon');
9
+ const { initAssessFixture } = require('@contrast/test/fixtures');
10
+
11
+ describe('assess dataflow sinks postgres', function () {
12
+ let core, simulateRequestScope, trackString, reportFindings, reportSafePositive;
13
+
14
+ class Client {
15
+ query() { }
16
+ }
17
+
18
+ class NativeClient {
19
+ query() { }
20
+ }
21
+
22
+ class Pool {
23
+ query() { }
24
+ }
25
+
26
+ beforeEach(function () {
27
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
28
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
29
+ reportSafePositive = sinon.stub(core.assess.dataflow.sinks, 'reportSafePositive');
30
+
31
+ core.depHooks.resolve
32
+ .withArgs({ name: 'pg', file: 'lib/client.js' })
33
+ .yields(Client);
34
+
35
+ core.depHooks.resolve
36
+ .withArgs({ name: 'pg', file: 'lib/native/client.js' })
37
+ .yields(NativeClient);
38
+
39
+ core.depHooks.resolve.withArgs({ name: 'pg-pool' }).yields(Pool);
40
+
41
+ require('./postgres')(core).install();
42
+ });
43
+
44
+ [Client, NativeClient, Pool].forEach((client) => {
45
+ describe(`${client.name}.prototype.query()`, function () {
46
+ it('skips instrumentation if assess store is missing', function () {
47
+ client.prototype.query();
48
+ expect(reportFindings).to.not.have.been.called;
49
+ });
50
+
51
+ it('skips instrumentation if a query is not provided ', function () {
52
+ simulateRequestScope(function () {
53
+ client.prototype.query();
54
+ expect(reportFindings).to.not.have.been.called;
55
+ });
56
+ });
57
+
58
+ it('skips instrumentation if the query is not a string', function () {
59
+ simulateRequestScope(function () {
60
+ client.prototype.query({});
61
+ expect(reportFindings).to.not.have.been.called;
62
+ });
63
+ });
64
+
65
+ it('skips instrumentation if the query is not a tracked', function () {
66
+ simulateRequestScope(function () {
67
+ client.prototype.query('hello');
68
+ expect(reportFindings).to.not.have.been.called;
69
+ });
70
+ });
71
+
72
+ it('skips instrumentation if the query is safe', function() {
73
+ simulateRequestScope(function () {
74
+ const value = 'drop table';
75
+ const extern = trackString(value, {
76
+ tags: {
77
+ [SQL_ENCODED]: [0, 9],
78
+ [UNTRUSTED]: [0, 9],
79
+ },
80
+ });
81
+
82
+ client.prototype.query(extern);
83
+ expect(reportFindings).to.not.have.been.called;
84
+ expect(reportSafePositive).to.have.been.calledWithMatch({
85
+ name: sinon.match(/query$/),
86
+ ruleId,
87
+ safeTags: [SQL_ENCODED],
88
+ strInfo: {
89
+ tags: {
90
+ [SQL_ENCODED]: [0, 9],
91
+ [UNTRUSTED]: [0, 9],
92
+ },
93
+ value,
94
+ }
95
+ });
96
+ });
97
+ });
98
+
99
+ it('reports an sql-injection', function () {
100
+ simulateRequestScope(function () {
101
+ const value = 'drop table';
102
+ const extern = trackString(value, {
103
+ tags: {
104
+ [SQL_ENCODED]: [0, 4],
105
+ [UNTRUSTED]: [0, 9],
106
+ },
107
+ });
108
+
109
+ client.prototype.query(extern);
110
+
111
+ expect(reportFindings).to.have.been.calledWithMatch({
112
+ ruleId,
113
+ sinkEvent: {
114
+ name: sinon.match('query'),
115
+ moduleName: sinon.match('pg'),
116
+ methodName: 'Connection.prototype.query',
117
+ object: {
118
+ value: sinon.match('pg'),
119
+ tracked: false,
120
+ },
121
+ args: [{ value, tracked: true }],
122
+ tags: { [SQL_ENCODED]: [0, 4], [UNTRUSTED]: [0, 9] },
123
+ source: 'P0',
124
+ },
125
+ });
126
+ });
127
+ });
128
+ });
129
+ });
130
+
131
+ ['pg.native.Client.prototype.query', 'pg.Client.prototype.query'].forEach((clientType) => {
132
+ it(`skips calling the hook if the ${clientType} is already patched`, function () {
133
+ simulateRequestScope(function () {
134
+ const extern = trackString('drop table', {
135
+ tags: {
136
+ [SQL_ENCODED]: [0, 4],
137
+ [UNTRUSTED]: [0, 9],
138
+ },
139
+ });
140
+
141
+ Pool.prototype.Client = {
142
+ prototype: {
143
+ query: () => { }
144
+ }
145
+ };
146
+
147
+ core.patcher.patch(Pool.prototype.Client.prototype, 'query', {
148
+ name: clientType,
149
+ patchType: 'assess-dataflow-sink',
150
+ pre: () => { }
151
+ });
152
+
153
+ Pool.prototype.query(extern);
154
+ expect(reportFindings).to.not.have.been.called;
155
+ });
156
+ });
157
+ });
158
+ });