@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,142 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { initAssessFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('assess dataflow restify sinks', function () {
8
+ let core,
9
+ simulateRequestScope,
10
+ trackString,
11
+ reportFindings,
12
+ getRes,
13
+ patchMock;
14
+
15
+ function next() {}
16
+
17
+ beforeEach(function () {
18
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
19
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
20
+
21
+ // this represents mock of what restify does here: https://github.com/restify/node-restify/blob/master/lib/response.js
22
+ function patch(Orig) {
23
+ Orig.prototype.redirect = function() {
24
+ return 'canary';
25
+ };
26
+ }
27
+
28
+ function Response() {}
29
+
30
+ getRes = () => {
31
+ // this mimics running of restify's patch, and our it will trigger out instrumentation
32
+ patchMock(Response);
33
+ return new Response();
34
+ };
35
+
36
+ core.depHooks.resolve
37
+ .withArgs({ name: 'restify', file: 'lib/response.js' })
38
+ .callsFake((desc, cb) => {
39
+ patchMock = cb(patch);
40
+ });
41
+
42
+ require('./restify')(core).install();
43
+ });
44
+
45
+ describe('res.redirect(String, Function)', function() {
46
+ it('reports when string is tracked', function() {
47
+ simulateRequestScope(() => {
48
+ const res = getRes();
49
+ res.redirect(trackString('/fargs'), next);
50
+
51
+ expect(reportFindings).to.have.been.calledWithMatch({
52
+ ruleId: 'unvalidated-redirect',
53
+ sinkEvent: sinon.match({
54
+ args: [
55
+ { tracked: true, value: '\'/fargs\'' },
56
+ { tracked: false, value: 'Function' }
57
+ ],
58
+ context: 'res.redirect(\'/fargs\',Function)',
59
+ tags: { UNTRUSTED: [1, 6] },
60
+ source: 'P0',
61
+ }),
62
+ });
63
+ });
64
+ });
65
+
66
+ it('does not report if string arg is untracked or safely tagged', function() {
67
+ simulateRequestScope(() => {
68
+ const res = getRes();
69
+ const str = trackString('/fargs', { tags: { UNTRUSTED: [0, 5], URL_ENCODED: [0, 5] } });
70
+ res.redirect(str, next);
71
+ res.redirect('/fargs', next);
72
+ expect(reportFindings.getCalls()).to.have.lengthOf(0);
73
+ });
74
+ });
75
+ });
76
+
77
+ describe('res.redirect(Number, String, Function)', function() {
78
+ it('reports when string is tracked', function() {
79
+ simulateRequestScope(() => {
80
+ const res = getRes();
81
+ res.redirect(301, trackString('/fargs'), next);
82
+
83
+ expect(reportFindings).to.have.been.calledWithMatch({
84
+ ruleId: 'unvalidated-redirect',
85
+ sinkEvent: sinon.match({
86
+ args: [
87
+ { tracked: false, value: 'Number' },
88
+ { tracked: true, value: '\'/fargs\'' },
89
+ { tracked: false, value: 'Function' }
90
+ ],
91
+ context: 'res.redirect(Number,\'/fargs\',Function)',
92
+ tags: { UNTRUSTED: [1, 6] },
93
+ source: 'P1',
94
+ }),
95
+ });
96
+ });
97
+ });
98
+
99
+ it('does not report if string arg is untracked or safely tagged', function() {
100
+ simulateRequestScope(() => {
101
+ const res = getRes();
102
+ const str = trackString('/fargs', { tags: { UNTRUSTED: [0, 5], URL_ENCODED: [0, 5] } });
103
+ res.redirect(301, str, next);
104
+ res.redirect('/fargs', next);
105
+ expect(reportFindings.getCalls()).to.have.lengthOf(0);
106
+ });
107
+ });
108
+ });
109
+
110
+ describe('res.redirect(Object, Function)', function() {
111
+ it('reports when string options are tracked', function() {
112
+ simulateRequestScope(() => {
113
+ const res = getRes();
114
+
115
+ res.redirect({ pathname: trackString('/fargs') }, next);
116
+
117
+ expect(reportFindings).to.have.been.calledWithMatch({
118
+ ruleId: 'unvalidated-redirect',
119
+ sinkEvent: sinon.match({
120
+ args: [
121
+ { tracked: true, value: '{...\'pathname\':\'/fargs\'...}' },
122
+ { tracked: false, value: 'Function' }
123
+ ],
124
+ context: 'res.redirect({...\'pathname\':\'/fargs\'...},Function)',
125
+ tags: { UNTRUSTED: [16, 21] },
126
+ source: 'P0',
127
+ }),
128
+ });
129
+ });
130
+ });
131
+
132
+ it('does not report if string arg is untracked or safely tagged', function() {
133
+ simulateRequestScope(() => {
134
+ const res = getRes();
135
+ const str = trackString('/fargs', { tags: { UNTRUSTED: [0, 5], URL_ENCODED: [0, 5] } });
136
+ res.redirect({ pathname: str }, next);
137
+ res.redirect({ pathname: '/fargs' }, next);
138
+ expect(reportFindings.getCalls()).to.have.lengthOf(0);
139
+ });
140
+ });
141
+ });
142
+ });
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { inspect } = require('util');
6
+ const {
7
+ DataflowTag: { SQL_ENCODED, UNTRUSTED },
8
+ } = require('@contrast/common');
9
+ const { initAssessFixture } = require('@contrast/test/fixtures');
10
+
11
+ describe('assess dataflow sinks sequelize', function () {
12
+ let core, simulateRequestScope, trackString, tracker, reportFindings;
13
+
14
+ class Sequelize {
15
+ query() { }
16
+ }
17
+
18
+ beforeEach(function () {
19
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
20
+ tracker = core.assess.dataflow.tracker;
21
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
22
+
23
+ core.depHooks.resolve.yields(Sequelize);
24
+
25
+ require('./sequelize')(core).install();
26
+ });
27
+
28
+ it('skips instrumentation if assess store is missing', function () {
29
+ Sequelize.prototype.query();
30
+ expect(reportFindings).to.not.have.been.called;
31
+ });
32
+
33
+ it('skips instrumentation if a query is not provided ', function () {
34
+ simulateRequestScope(function () {
35
+ Sequelize.prototype.query();
36
+ expect(reportFindings).to.not.have.been.called;
37
+ });
38
+ });
39
+
40
+ it('skips instrumentation if the query is not a string', function () {
41
+ simulateRequestScope(function () {
42
+ Sequelize.prototype.query({});
43
+ expect(reportFindings).to.not.have.been.called;
44
+ });
45
+ });
46
+
47
+ it('skips instrumentation if the query is not a tracked', function () {
48
+ simulateRequestScope(function () {
49
+ Sequelize.prototype.query('hello');
50
+ expect(reportFindings).to.not.have.been.called;
51
+ });
52
+ });
53
+
54
+ it('skips instrumentation if the query is safe', function () {
55
+ simulateRequestScope(function () {
56
+ const str = trackString('drop table', {
57
+ tags: {
58
+ [SQL_ENCODED]: [0, 9],
59
+ [UNTRUSTED]: [0, 9],
60
+ },
61
+ });
62
+ const { extern } = tracker.getData(str);
63
+
64
+ Sequelize.prototype.query(extern);
65
+ expect(reportFindings).to.not.have.been.called;
66
+ });
67
+ });
68
+
69
+ it('reports an sql-injection', function () {
70
+ simulateRequestScope(function () {
71
+ const str = trackString('drop table', {
72
+ tags: {
73
+ [SQL_ENCODED]: [0, 4],
74
+ [UNTRUSTED]: [0, 9],
75
+ },
76
+ });
77
+ const { value } = tracker.getData(str);
78
+ const options = { dialect: 'mysql' };
79
+
80
+ Sequelize.prototype.query(str, options);
81
+
82
+ expect(reportFindings).to.have.been.calledWithMatch({
83
+ ruleId: 'sql-injection',
84
+ sinkEvent: {
85
+ name: 'sequelize.prototype.query',
86
+ moduleName: 'sequelize',
87
+ methodName: 'prototype.query',
88
+ context: `sequelize.prototype.query('${value}', { dialect: 'mysql' })`,
89
+ object: {
90
+ value: sinon.match('sequelize'),
91
+ tracked: false,
92
+ },
93
+ args: [{ value, tracked: true }, { value: inspect(options), tracked: false }],
94
+ tags: { [SQL_ENCODED]: [0, 4], [UNTRUSTED]: [0, 9] },
95
+ source: 'P0',
96
+ },
97
+ });
98
+ });
99
+ });
100
+ });
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const {
6
+ DataflowTag: { UNTRUSTED, SQL_ENCODED },
7
+ } = require('@contrast/common');
8
+ const { initAssessFixture } = require('@contrast/test/fixtures');
9
+
10
+ describe('assess dataflow sinks sqlite3', function () {
11
+ let core, sqlite3, simulateRequestScope, trackString, tracker, reportFindings;
12
+
13
+ beforeEach(function () {
14
+
15
+ sqlite3 = {
16
+ // eslint-disable-next-line object-shorthand
17
+ Database: function () { }
18
+ };
19
+
20
+ sqlite3.Database.prototype = {
21
+ all: sinon.stub(),
22
+ run: sinon.stub(),
23
+ get: sinon.stub(),
24
+ each: sinon.stub(),
25
+ exec: sinon.stub(),
26
+ prepare: sinon.stub()
27
+ };
28
+
29
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
30
+ tracker = core.assess.dataflow.tracker;
31
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
32
+
33
+ core.depHooks.resolve.yields(sqlite3);
34
+
35
+ require('./sqlite3')(core).install();
36
+ });
37
+
38
+ ['all', 'run', 'get', 'each', 'exec', 'prepare'].forEach((method) => {
39
+ describe(`instruments connection.${method}()`, function () {
40
+ it('skips instrumentation if assess store is missing', function () {
41
+ const db = new sqlite3.Database();
42
+ db[method]('SELECT "foo"');
43
+ expect(reportFindings).not.to.have.been.called;
44
+ });
45
+
46
+ it('skips instrumentation if a value is not provided ', function () {
47
+ simulateRequestScope(function () {
48
+ const db = new sqlite3.Database();
49
+ db[method]();
50
+ expect(reportFindings).not.to.have.been.called;
51
+ });
52
+ });
53
+
54
+ it('skips instrumentation if the value is not a string', function () {
55
+ simulateRequestScope(function () {
56
+ const db = new sqlite3.Database();
57
+ db[method]({});
58
+ expect(reportFindings).not.to.have.been.called;
59
+ });
60
+ });
61
+
62
+ it('skips instrumentation if the query is not a tracked', function () {
63
+ simulateRequestScope(function () {
64
+ const db = new sqlite3.Database();
65
+ db[method]('SELECT "foo"');
66
+ expect(reportFindings).not.to.have.been.called;
67
+ });
68
+ });
69
+
70
+ it('skips instrumentation if the query is safe', function () {
71
+ simulateRequestScope(function () {
72
+ const str = trackString('SELECT "foo"', {
73
+ tags: {
74
+ [SQL_ENCODED]: [0, 11],
75
+ [UNTRUSTED]: [0, 11],
76
+ },
77
+ });
78
+ const { extern } = tracker.getData(str);
79
+
80
+ const db = new sqlite3.Database();
81
+ db[method](extern);
82
+ expect(reportFindings).not.to.have.been.called;
83
+ });
84
+ });
85
+
86
+ it('reports an sql-injection', function() {
87
+ simulateRequestScope(function () {
88
+ const value = 'SELECT "foo"';
89
+ const extern = trackString(value, {
90
+ tags: {
91
+ [UNTRUSTED]: [0, 11],
92
+ },
93
+ });
94
+
95
+ const db = new sqlite3.Database();
96
+ db[method](extern);
97
+
98
+ expect(reportFindings).to.have.been.calledWithMatch({
99
+ ruleId: 'sql-injection',
100
+ sinkEvent: {
101
+ name: `sqlite3.Database.prototype.${method}`,
102
+ moduleName: 'sqlite3',
103
+ methodName: `Database.prototype.${method}`,
104
+ context: `db.${method}('${extern}')`,
105
+ object: {
106
+ value: '[Module<sqlite3>].Database',
107
+ tracked: false,
108
+ },
109
+ args: [{ value, tracked: true }],
110
+ tags: { [UNTRUSTED]: [0, 11] },
111
+ source: 'P0',
112
+ },
113
+ });
114
+ });
115
+ });
116
+ });
117
+ });
118
+ });
@@ -0,0 +1,326 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const {
6
+ DataflowTag: { UNTRUSTED, CUSTOM_VALIDATED },
7
+ Rule: { UNSAFE_CODE_EXECUTION },
8
+ UtilInspect,
9
+ } = require('@contrast/common');
10
+ const { initAssessFixture } = require('@contrast/test/fixtures');
11
+
12
+ describe('assess dataflow sinks vm', function () {
13
+ let vm, core, simulateRequestScope, trackString, reportFindings, reportSafePositive;
14
+
15
+ beforeEach(function () {
16
+ ({ core, simulateRequestScope, trackString } = initAssessFixture());
17
+
18
+ vm = require('vm');
19
+
20
+ reportFindings = sinon.stub(core.assess.dataflow.sinks, 'reportFindings');
21
+ reportSafePositive = sinon.stub(core.assess.dataflow.sinks, 'reportSafePositive');
22
+
23
+ core.depHooks.resolve.yields(vm);
24
+
25
+ require('./vm')(core).install();
26
+ });
27
+
28
+ [
29
+ {
30
+ method: 'Script',
31
+ vulnerableArgs: [{ type: 'string', index: 0 }],
32
+ args: () => ['val = "test"'],
33
+ sanitizedArgs: () => [trackString('val = "test"', {
34
+ tags: {
35
+ [UNTRUSTED]: [0, 11],
36
+ [CUSTOM_VALIDATED]: [0, 11]
37
+ }
38
+ })],
39
+ isConstructor: true,
40
+ expectedSafeReportInfo: [{
41
+ value: UtilInspect('val = "test"'),
42
+ strValues: ['val = "test"'],
43
+ tags: [{ [UNTRUSTED]: [0, 11], [CUSTOM_VALIDATED]: [0, 11] }]
44
+ }],
45
+ expectedSinkEvent: [{
46
+ args: [{ value: 'val = "test"', tracked: true }],
47
+ context: 'vm.Script("val = "test"")',
48
+ name: 'vm.Script',
49
+ moduleName: 'vm',
50
+ methodName: 'Script',
51
+ object: { value: 'vm', tracked: false },
52
+ source: 'P0',
53
+ tags: { [UNTRUSTED]: [0, 11] }
54
+ }]
55
+ },
56
+ {
57
+ method: 'createScript',
58
+ vulnerableArgs: [{ type: 'string', index: 0 }],
59
+ args: () => ['val = "test"'],
60
+ sanitizedArgs: () => [trackString('val = "test"', {
61
+ tags: {
62
+ [UNTRUSTED]: [0, 11],
63
+ [CUSTOM_VALIDATED]: [0, 11]
64
+ }
65
+ })],
66
+ expectedSafeReportInfo: [{
67
+ value: UtilInspect('val = "test"'),
68
+ strValues: ['val = "test"'],
69
+ tags: [{ [UNTRUSTED]: [0, 11], [CUSTOM_VALIDATED]: [0, 11] }]
70
+ }],
71
+ expectedSinkEvent: [{
72
+ args: [{ value: 'val = "test"', tracked: true }],
73
+ context: 'vm.createScript("val = "test"")',
74
+ name: 'vm.createScript',
75
+ moduleName: 'vm',
76
+ methodName: 'createScript',
77
+ object: { value: 'vm', tracked: false },
78
+ source: 'P0',
79
+ tags: { [UNTRUSTED]: [0, 11] }
80
+ }]
81
+ },
82
+ {
83
+ method: 'runInContext',
84
+ vulnerableArgs: [{ type: 'string', index: 0 }],
85
+ args: () => ['val = "test"', vm.createContext({ val: '' })],
86
+ sanitizedArgs: () => [trackString('val = "test"', {
87
+ tags: {
88
+ [UNTRUSTED]: [0, 11],
89
+ [CUSTOM_VALIDATED]: [0, 11]
90
+ }
91
+ }),
92
+ vm.createContext({ val: '' })],
93
+ missingArgs: () => ['', vm.createContext({})],
94
+ expectedSafeReportInfo: [{
95
+ value: UtilInspect('val = "test"'),
96
+ strValues: ['val = "test"'],
97
+ tags: [{ [UNTRUSTED]: [0, 11], [CUSTOM_VALIDATED]: [0, 11] }]
98
+ }],
99
+ expectedSinkEvent: [{
100
+ args: [{ value: 'val = "test"', tracked: true }, { value: UtilInspect({ val: '' }), tracked: false }],
101
+ context: 'vm.runInContext("val = "test"", { val: \'\' })',
102
+ name: 'vm.runInContext',
103
+ moduleName: 'vm',
104
+ methodName: 'runInContext',
105
+ object: { value: 'vm', tracked: false },
106
+ source: 'P0',
107
+ tags: { [UNTRUSTED]: [0, 11] }
108
+ }]
109
+ },
110
+ {
111
+ method: 'runInThisContext',
112
+ vulnerableArgs: [{ type: 'string', index: 0 }],
113
+ sanitizedArgs: () => [trackString('val = "test"', {
114
+ tags: {
115
+ [UNTRUSTED]: [0, 11],
116
+ [CUSTOM_VALIDATED]: [0, 11]
117
+ }
118
+ })],
119
+ args: () => ['val = "test"'],
120
+ expectedSafeReportInfo: [{
121
+ value: UtilInspect('val = "test"'),
122
+ strValues: ['val = "test"'],
123
+ tags: [{ [UNTRUSTED]: [0, 11], [CUSTOM_VALIDATED]: [0, 11] }]
124
+ }],
125
+ expectedSinkEvent: [{
126
+ args: [{ value: 'val = "test"', tracked: true }],
127
+ context: 'vm.runInThisContext("val = "test"")',
128
+ name: 'vm.runInThisContext',
129
+ moduleName: 'vm',
130
+ methodName: 'runInThisContext',
131
+ object: { value: 'vm', tracked: false },
132
+ source: 'P0',
133
+ tags: { [UNTRUSTED]: [0, 11] }
134
+ }]
135
+ },
136
+ {
137
+ method: 'createContext',
138
+ vulnerableArgs: [{ type: 'object', index: 0 }],
139
+ sanitizedArgs: () => [{
140
+ val: trackString('value', {
141
+ tags: {
142
+ [UNTRUSTED]: [0, 4],
143
+ [CUSTOM_VALIDATED]: [0, 4]
144
+ }
145
+ })
146
+ }],
147
+ args: () => [{ val: 'value' }],
148
+ expectedSafeReportInfo: [{
149
+ value: UtilInspect({ val: 'value' }),
150
+ strValues: ['value'],
151
+ tags: [{ [UNTRUSTED]: [8, 12], [CUSTOM_VALIDATED]: [8, 12] }]
152
+ }],
153
+ expectedSinkEvent: [{
154
+ args: [{ value: UtilInspect({ val: 'value' }), tracked: false }],
155
+ context: 'vm.createContext({ val: \'value\' })',
156
+ name: 'vm.createContext',
157
+ moduleName: 'vm',
158
+ methodName: 'createContext',
159
+ object: { value: 'vm', tracked: false },
160
+ source: 'P0',
161
+ tags: { [UNTRUSTED]: [8, 12] }
162
+ }]
163
+ },
164
+ {
165
+ method: 'runInNewContext',
166
+ vulnerableArgs: [{ type: 'string', index: 0 }, { type: 'object', index: 1 }],
167
+ sanitizedArgs: () => [trackString('val = "test"', {
168
+ tags: {
169
+ [UNTRUSTED]: [0, 11],
170
+ [CUSTOM_VALIDATED]: [0, 11]
171
+ }
172
+ }), {
173
+ val: trackString('SAFE', {
174
+ tags: {
175
+ [UNTRUSTED]: [0, 3],
176
+ [CUSTOM_VALIDATED]: [0, 3]
177
+ }
178
+ })
179
+ }],
180
+ args: () => ['val = "test"', { val: 'value' }],
181
+ expectedSafeReportInfo: [
182
+ {
183
+ value: UtilInspect('val = "test"'),
184
+ strValues: ['val = "test"'],
185
+ tags: [{ [UNTRUSTED]: [0, 11], [CUSTOM_VALIDATED]: [0, 11] }]
186
+ },
187
+ {
188
+ value: UtilInspect({ val: 'SAFE' }),
189
+ strValues: ['SAFE'],
190
+ tags: [{ [UNTRUSTED]: [8, 11], [CUSTOM_VALIDATED]: [8, 11] }]
191
+ }
192
+ ],
193
+ expectedSinkEvent: [{
194
+ args: [{ value: 'val = "test"', tracked: true }, { value: UtilInspect({ val: 'value' }), tracked: false }],
195
+ context: 'vm.runInNewContext("val = "test"", { val: \'value\' })',
196
+ name: 'vm.runInNewContext',
197
+ moduleName: 'vm',
198
+ methodName: 'runInNewContext',
199
+ object: { value: 'vm', tracked: false },
200
+ source: 'P0',
201
+ tags: { [UNTRUSTED]: [0, 11] }
202
+ },
203
+ {
204
+ args: [{ value: 'val = "test"', tracked: false }, { value: UtilInspect({ val: 'value' }), tracked: false }],
205
+ context: 'vm.runInNewContext("val = "test"", { val: \'value\' })',
206
+ name: 'vm.runInNewContext',
207
+ moduleName: 'vm',
208
+ methodName: 'runInNewContext',
209
+ object: { value: 'vm', tracked: false },
210
+ source: 'P1',
211
+ tags: { [UNTRUSTED]: [8, 12] }
212
+ }]
213
+ },
214
+ {
215
+ method: 'runInNewContext',
216
+ vulnerableArgs: [{ type: 'object', index: 0 }],
217
+ args: () => [{ val: 'value' }],
218
+ sanitizedArgs: () => [{
219
+ val: trackString('SAFE', {
220
+ tags: {
221
+ [UNTRUSTED]: [0, 3],
222
+ [CUSTOM_VALIDATED]: [0, 3]
223
+ }
224
+ })
225
+ }],
226
+ scriptEntity: true,
227
+ expectedSafeReportInfo: [
228
+ {
229
+ value: UtilInspect({ val: 'SAFE' }),
230
+ strValues: ['SAFE'],
231
+ tags: [{ [UNTRUSTED]: [8, 11], [CUSTOM_VALIDATED]: [8, 11] }]
232
+ }
233
+ ],
234
+ expectedSinkEvent: [{
235
+ args: [{ value: UtilInspect({ val: 'value' }), tracked: false }],
236
+ context: 'vm.Script.prototype.runInNewContext({ val: \'value\' })',
237
+ name: 'vm.Script.prototype.runInNewContext',
238
+ moduleName: 'vm',
239
+ methodName: 'runInNewContext',
240
+ object: { value: 'vm.Script.prototype', tracked: false },
241
+ source: 'P0',
242
+ tags: { [UNTRUSTED]: [8, 12] }
243
+ }]
244
+ }
245
+ ].forEach(({ method, args, missingArgs = () => [], vulnerableArgs, sanitizedArgs, isConstructor, scriptEntity, expectedSafeReportInfo, expectedSinkEvent }) => {
246
+ describe(`vm.${method}`, function () {
247
+ let fn;
248
+ if (scriptEntity) {
249
+ fn = () => {
250
+ const scr = new vm.Script('val = "test"');
251
+ return scr;
252
+ };
253
+ } else {
254
+ fn = () => vm[method];
255
+ }
256
+
257
+ function makeAVmMethodCall(args) {
258
+ const vmMethod = fn();
259
+
260
+ if (isConstructor) {
261
+ new vmMethod(...args());
262
+ } else if (scriptEntity) {
263
+ vmMethod.runInNewContext(...args());
264
+ } else {
265
+ vmMethod(...args());
266
+ }
267
+ }
268
+
269
+ it('skips instrumentation if assess store is missing', function () {
270
+ makeAVmMethodCall(args);
271
+
272
+ expect(reportFindings).not.to.have.been.called;
273
+ });
274
+
275
+ it('skips instrumentation if a value is not provided ', function () {
276
+ simulateRequestScope(function () {
277
+ makeAVmMethodCall(missingArgs);
278
+
279
+ expect(reportFindings).not.to.have.been.called;
280
+ });
281
+ });
282
+
283
+ it('skips instrumentation if the argument is not a tracked', function () {
284
+ simulateRequestScope(function () {
285
+ makeAVmMethodCall(args);
286
+
287
+ expect(reportFindings).not.to.have.been.called;
288
+ });
289
+ });
290
+
291
+ it('skips instrumentation if the argument is sanitized', function () {
292
+ simulateRequestScope(function () {
293
+ makeAVmMethodCall(sanitizedArgs);
294
+
295
+ expect(reportFindings).not.to.have.been.called;
296
+ expect(reportSafePositive).to.have.been.calledOnce;
297
+ expect(reportSafePositive).to.have.been.calledWith({
298
+ name: scriptEntity ? `vm.Script.prototype.${method}` : `vm.${method}`,
299
+ ruleId: UNSAFE_CODE_EXECUTION,
300
+ safeTags: [CUSTOM_VALIDATED],
301
+ strInfo: expectedSafeReportInfo
302
+ });
303
+ });
304
+ });
305
+
306
+ it('reports an unsafe-code-execution', function () {
307
+ simulateRequestScope(function () {
308
+ vulnerableArgs.forEach((arg, idx) => {
309
+ const argsCopy = [...args()];
310
+ const extern = arg.type === 'string' ? trackString(argsCopy[arg.index]) : trackString(argsCopy[arg.index].val);
311
+ argsCopy[arg.index] = arg.type === 'string' ? extern : { val: extern };
312
+
313
+ makeAVmMethodCall(() => argsCopy);
314
+
315
+ expect(reportFindings).to.have.been.calledOnce;
316
+ expect(reportFindings).to.have.been.calledWithMatch({
317
+ ruleId: UNSAFE_CODE_EXECUTION,
318
+ sinkEvent: expectedSinkEvent[idx],
319
+ });
320
+ reportFindings.resetHistory();
321
+ });
322
+ });
323
+ });
324
+ });
325
+ });
326
+ });