@contrast/assess 1.8.0 → 1.10.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 (75) hide show
  1. package/lib/dataflow/event-factory.js +17 -13
  2. package/lib/dataflow/propagation/index.js +4 -0
  3. package/lib/dataflow/propagation/install/JSON/index.js +1 -0
  4. package/lib/dataflow/propagation/install/JSON/parse-fn.js +248 -0
  5. package/lib/dataflow/propagation/install/JSON/parse.js +196 -0
  6. package/lib/dataflow/propagation/install/JSON/stringify.js +5 -3
  7. package/lib/dataflow/propagation/install/array-prototype-join.js +21 -14
  8. package/lib/dataflow/propagation/install/buffer.js +2 -0
  9. package/lib/dataflow/propagation/install/contrast-methods/add.js +2 -0
  10. package/lib/dataflow/propagation/install/contrast-methods/index.js +1 -0
  11. package/lib/dataflow/propagation/install/contrast-methods/number.js +58 -0
  12. package/lib/dataflow/propagation/install/contrast-methods/string.js +53 -6
  13. package/lib/dataflow/propagation/install/contrast-methods/tag.js +3 -1
  14. package/lib/dataflow/propagation/install/decode-uri-component.js +9 -2
  15. package/lib/dataflow/propagation/install/ejs/escape-xml.js +8 -2
  16. package/lib/dataflow/propagation/install/encode-uri-component.js +9 -2
  17. package/lib/dataflow/propagation/install/escape-html.js +13 -5
  18. package/lib/dataflow/propagation/install/escape.js +9 -2
  19. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.js +11 -4
  20. package/lib/dataflow/propagation/install/isnumeric-0.js +59 -0
  21. package/lib/dataflow/propagation/install/mongoose/index.js +36 -0
  22. package/lib/dataflow/propagation/install/mongoose/schema-string.js +156 -0
  23. package/lib/dataflow/propagation/install/mysql-connection-escape.js +5 -0
  24. package/lib/dataflow/propagation/install/parse-int.js +60 -0
  25. package/lib/dataflow/propagation/install/path/basename.js +124 -0
  26. package/lib/dataflow/propagation/install/path/common.js +176 -0
  27. package/lib/dataflow/propagation/install/path/index.js +32 -0
  28. package/lib/dataflow/propagation/install/path/join-and-resolve.js +141 -0
  29. package/lib/dataflow/propagation/install/path/normalize.js +123 -0
  30. package/lib/dataflow/propagation/install/pug-runtime-escape.js +9 -2
  31. package/lib/dataflow/propagation/install/querystring/parse.js +12 -10
  32. package/lib/dataflow/propagation/install/sequelize.js +6 -3
  33. package/lib/dataflow/propagation/install/sql-template-strings.js +9 -2
  34. package/lib/dataflow/propagation/install/string/concat.js +8 -2
  35. package/lib/dataflow/propagation/install/string/format-methods.js +7 -2
  36. package/lib/dataflow/propagation/install/string/html-methods.js +15 -5
  37. package/lib/dataflow/propagation/install/string/match.js +16 -11
  38. package/lib/dataflow/propagation/install/string/replace.js +23 -15
  39. package/lib/dataflow/propagation/install/string/slice.js +14 -6
  40. package/lib/dataflow/propagation/install/string/split.js +16 -12
  41. package/lib/dataflow/propagation/install/string/substring.js +18 -8
  42. package/lib/dataflow/propagation/install/string/trim.js +4 -1
  43. package/lib/dataflow/propagation/install/unescape.js +9 -2
  44. package/lib/dataflow/propagation/install/url/domain-parsers.js +7 -2
  45. package/lib/dataflow/propagation/install/url/index.js +1 -0
  46. package/lib/dataflow/propagation/install/url/url.js +228 -0
  47. package/lib/dataflow/propagation/install/validator/hooks.js +6 -2
  48. package/lib/dataflow/sinks/index.js +8 -4
  49. package/lib/dataflow/sinks/install/child-process.js +116 -50
  50. package/lib/dataflow/sinks/install/eval.js +138 -0
  51. package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +7 -4
  52. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +9 -5
  53. package/lib/dataflow/sinks/install/fs.js +45 -13
  54. package/lib/dataflow/sinks/install/function.js +160 -0
  55. package/lib/dataflow/sinks/install/http/index.js +31 -0
  56. package/lib/dataflow/sinks/install/http/request.js +152 -0
  57. package/lib/dataflow/sinks/install/{http.js → http/server-response.js} +7 -4
  58. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +8 -5
  59. package/lib/dataflow/sinks/install/marsdb.js +3 -0
  60. package/lib/dataflow/sinks/install/mongodb.js +7 -24
  61. package/lib/dataflow/sinks/install/mssql.js +49 -29
  62. package/lib/dataflow/sinks/install/mysql.js +9 -4
  63. package/lib/dataflow/sinks/install/postgres.js +6 -3
  64. package/lib/dataflow/sinks/install/sequelize.js +7 -5
  65. package/lib/dataflow/sinks/install/sqlite3.js +7 -3
  66. package/lib/dataflow/sinks/install/vm.js +276 -0
  67. package/lib/dataflow/sources/handler.js +2 -1
  68. package/lib/dataflow/sources/install/http.js +1 -1
  69. package/lib/dataflow/tag-utils.js +95 -2
  70. package/lib/dataflow/tracker.js +6 -6
  71. package/lib/index.js +2 -0
  72. package/lib/response-scanning/handlers/utils.js +2 -2
  73. package/lib/session-configuration/index.js +34 -0
  74. package/lib/session-configuration/install/http.js +79 -0
  75. package/package.json +2 -2
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const { isString } = require('@contrast/common');
19
+ const { patchType } = require('../../common');
20
+
21
+ module.exports = function(core) {
22
+ const {
23
+ logger,
24
+ scopes: { instrumentation, sources },
25
+ patcher,
26
+ assess: {
27
+ dataflow: { tracker }
28
+ }
29
+ } = core;
30
+
31
+ const name = 'ContrastMethods.Number';
32
+
33
+ return core.assess.dataflow.propagation.contrastMethodsInstrumentation.number = {
34
+ install() {
35
+ patcher.patch(global.ContrastMethods, 'Number', {
36
+ name,
37
+ patchType,
38
+ post(data) {
39
+ const { args: [value], result } = data;
40
+ if (
41
+ isNaN(result) ||
42
+ !value ||
43
+ !isString(value) ||
44
+ !sources.getStore()?.assess ||
45
+ instrumentation.isLocked() ||
46
+ !tracker.getData(value)
47
+ ) return;
48
+
49
+ tracker.untrack(value);
50
+ logger.trace({ sanitizer: name, value }, 'untracked a string value');
51
+ }
52
+ });
53
+ },
54
+ uninstall() {
55
+ global.ContrastMethods.Number = patcher.unwrap(global.Number);
56
+ },
57
+ };
58
+ };
@@ -15,30 +15,42 @@
15
15
 
16
16
  'use strict';
17
17
 
18
+ const { DataflowTag } = require('@contrast/common');
18
19
  const { patchType } = require('../../common');
19
20
 
21
+ function metadataUpdate(strInfo, event) {
22
+ for (const key of Object.keys(strInfo)) {
23
+ if (key === 'value' || key === 'extern') continue;
24
+ delete strInfo[key];
25
+ }
26
+
27
+ Object.assign(strInfo, event);
28
+ }
29
+
20
30
  module.exports = function(core) {
21
31
  const {
22
32
  scopes: { sources, instrumentation },
23
33
  patcher,
24
34
  assess: {
25
35
  dataflow: {
26
- tracker
36
+ tracker,
37
+ eventFactory: { createPropagationEvent },
27
38
  },
28
39
  }
29
40
  } = core;
30
41
 
31
42
  return core.assess.dataflow.propagation.contrastMethodsInstrumentation.string = {
32
43
  install() {
44
+ const name = 'ContrastMethods.String';
33
45
  patcher.patch(global.ContrastMethods, 'String', {
34
- name: 'ContrastMethods.String',
46
+ name,
35
47
  patchType,
36
48
  post(data) {
37
49
  if (!data.result || !sources.getStore() || instrumentation.isLocked()) return;
38
50
 
39
51
  const arg = data.args[0];
40
52
  let argInfo = tracker.getData(arg);
41
- if (tracker.getData(data.result)) return;
53
+ const objInfo = tracker.getData(data.obj);
42
54
 
43
55
  if (data.obj && !argInfo) {
44
56
  argInfo = tracker.getData(data.result.toString());
@@ -48,10 +60,45 @@ module.exports = function(core) {
48
60
  return;
49
61
  }
50
62
 
51
- const { extern } = tracker.track(data.result, argInfo);
63
+ const history = [{ ...argInfo }];
64
+ const strLength = typeof arg === 'string' ? arg.length : arg.toString().length;
65
+ const event = createPropagationEvent({
66
+ addedTags: [DataflowTag.STRING_TYPE_CHECKED],
67
+ name,
68
+ moduleName: 'global',
69
+ methodName: 'ContrastMethods.String',
70
+ context: `${name}('${argInfo.value}')`,
71
+ history,
72
+ object: {
73
+ tracked: !!objInfo,
74
+ value: objInfo ? objInfo.value : data.obj,
75
+ },
76
+ args: [{ tracked: true, value: argInfo.value }],
77
+ source: 'P',
78
+ tags: {
79
+ ...argInfo.tags,
80
+ [DataflowTag.STRING_TYPE_CHECKED]: [0, strLength - 1],
81
+ },
82
+ target: 'R',
83
+ stacktraceOpts: {
84
+ prependFrames: [data.orig],
85
+ },
86
+ result: {
87
+ value: data.result,
88
+ tracked: true
89
+ },
90
+ });
91
+
92
+ if (!event) {
93
+ return;
94
+ }
95
+
96
+ const resultInfo = tracker.getData(data.result);
52
97
 
53
- if (extern) {
54
- data.result = extern;
98
+ if (resultInfo) {
99
+ metadataUpdate(resultInfo, event);
100
+ } else {
101
+ metadataUpdate(argInfo, event);
55
102
  }
56
103
  }
57
104
  });
@@ -17,7 +17,7 @@
17
17
 
18
18
  const { patchType } = require('../../common');
19
19
 
20
- module.exports = function (core) {
20
+ module.exports = function(core) {
21
21
  const {
22
22
  assess: {
23
23
  dataflow: {
@@ -79,6 +79,8 @@ module.exports = function (core) {
79
79
  createPropagationEvent({
80
80
  args,
81
81
  context: `\`${context}\``,
82
+ moduleName: 'global',
83
+ methodName: 'ContrastMethods.tag',
82
84
  history: Array.from(history),
83
85
  object: {
84
86
  tracked: false,
@@ -34,8 +34,10 @@ module.exports = function(core) {
34
34
 
35
35
  return core.assess.dataflow.propagation.decodeURIComponent = {
36
36
  install() {
37
+ const name = 'global.decodeURIComponent';
38
+
37
39
  patcher.patch(global, 'decodeURIComponent', {
38
- name: 'global.decodeURIComponent',
40
+ name,
39
41
  patchType,
40
42
  post(data) {
41
43
  const { args, result, hooked, orig } = data;
@@ -55,7 +57,10 @@ module.exports = function(core) {
55
57
  if (!Object.keys(newTags).length) return;
56
58
 
57
59
  const event = createPropagationEvent({
58
- name: 'global.decodeURIComponent',
60
+ name,
61
+ moduleName: 'global',
62
+ methodName: 'decodeURIComponent',
63
+ context: `decodeURIComponent('${argInfo.value}')`,
59
64
  object: {
60
65
  value: createObjectLabel('global'),
61
66
  tracked: false
@@ -67,6 +72,8 @@ module.exports = function(core) {
67
72
  args: [{ value: argInfo.value, tracked: true }],
68
73
  tags: newTags,
69
74
  history,
75
+ source: 'P',
76
+ target: 'R',
70
77
  removedTags: [URL_ENCODED],
71
78
  stacktraceOpts: {
72
79
  constructorOpt: hooked,
@@ -36,8 +36,9 @@ module.exports = function(core) {
36
36
  return core.assess.dataflow.propagation.ejsInstrumentation.escapeXML = {
37
37
  install() {
38
38
  depHooks.resolve({ name: 'ejs', file: 'lib/utils.js', version: '>=2.6.2' }, (ejsUtils, version) => {
39
+ const name = 'ejs.utils.escapeXML';
39
40
  patcher.patch(ejsUtils, 'escapeXML', {
40
- name: 'ejs.utils.escapeXML',
41
+ name,
41
42
  patchType,
42
43
  post(data) {
43
44
  const { args, result, hooked, orig } = data;
@@ -54,7 +55,10 @@ module.exports = function(core) {
54
55
  newTags[WEAK_URL_ENCODED] = [0, result.length - 1];
55
56
 
56
57
  const event = createPropagationEvent({
57
- name: 'ejs.utils.escapeXML',
58
+ name,
59
+ context: `ejs.utils.escapeXML('${argInfo.value}')`,
60
+ moduleName: 'ejs',
61
+ methodName: 'escapeXML',
58
62
  object: {
59
63
  value: `[${createModuleLabel('ejs', version)}].utils`,
60
64
  tracked: false
@@ -71,6 +75,8 @@ module.exports = function(core) {
71
75
  constructorOpt: hooked,
72
76
  prependFrames: [orig]
73
77
  },
78
+ source: 'P',
79
+ target: 'R',
74
80
  });
75
81
 
76
82
  if (!event) return;
@@ -34,8 +34,10 @@ module.exports = function(core) {
34
34
 
35
35
  return core.assess.dataflow.propagation.encodeURIComponent = {
36
36
  install() {
37
+ const name = 'global.encodeURIComponent';
38
+
37
39
  patcher.patch(global, 'encodeURIComponent', {
38
- name: 'global.encodeURIComponent',
40
+ name,
39
41
  patchType,
40
42
  post(data) {
41
43
  const { args, result, hooked, orig } = data;
@@ -54,7 +56,10 @@ module.exports = function(core) {
54
56
  newTags[URL_ENCODED] = [0, result.length - 1];
55
57
 
56
58
  const event = createPropagationEvent({
57
- name: 'global.encodeURIComponent',
59
+ name,
60
+ moduleName: 'global',
61
+ methodName: 'encodeURIComponent',
62
+ context: `encodeURIComponent('${argInfo.value}')`,
58
63
  object: {
59
64
  value: createObjectLabel('global'),
60
65
  tracked: false
@@ -66,6 +71,8 @@ module.exports = function(core) {
66
71
  args: [{ value: argInfo.value, tracked: true }],
67
72
  tags: newTags,
68
73
  history,
74
+ source: 'P',
75
+ target: 'R',
69
76
  addedTags: [URL_ENCODED],
70
77
  stacktraceOpts: {
71
78
  constructorOpt: hooked,
@@ -35,9 +35,11 @@ module.exports = function(core) {
35
35
 
36
36
  return core.assess.dataflow.propagation.escapeHtml = {
37
37
  install() {
38
- depHooks.resolve({ name: 'escape-html' }, (escapeHtml, version) =>
39
- patcher.patch(escapeHtml, {
40
- name: 'escape-html',
38
+ depHooks.resolve({ name: 'escape-html' }, (escapeHtml) => {
39
+ const name = 'escape-html';
40
+
41
+ return patcher.patch(escapeHtml, {
42
+ name,
41
43
  patchType,
42
44
  post(data) {
43
45
  const { args, result, hooked, orig } = data;
@@ -54,7 +56,10 @@ module.exports = function(core) {
54
56
  newTags[HTML_ENCODED] = [0, result.length - 1];
55
57
 
56
58
  const event = createPropagationEvent({
57
- name: 'escape-html',
59
+ name,
60
+ moduleName: 'escape-html',
61
+ methodName: '',
62
+ context: `escapeHtml(${argInfo.value})`,
58
63
  object: {
59
64
  value: 'escape-html',
60
65
  tracked: false
@@ -66,6 +71,8 @@ module.exports = function(core) {
66
71
  args: [{ value: argInfo.value, tracked: true }],
67
72
  tags: newTags,
68
73
  history,
74
+ source: 'P',
75
+ target: 'R',
69
76
  addedTags: [HTML_ENCODED],
70
77
  stacktraceOpts: {
71
78
  constructorOpt: hooked,
@@ -85,7 +92,8 @@ module.exports = function(core) {
85
92
  data.result = extern;
86
93
  }
87
94
  }
88
- })
95
+ });
96
+ }
89
97
  );
90
98
  },
91
99
  };
@@ -34,8 +34,10 @@ module.exports = function(core) {
34
34
 
35
35
  return core.assess.dataflow.propagation.escape = {
36
36
  install() {
37
+ const name = 'global.escape';
38
+
37
39
  patcher.patch(global, 'escape', {
38
- name: 'global.escape',
40
+ name,
39
41
  patchType,
40
42
  post(data) {
41
43
  const { args, result, hooked, orig } = data;
@@ -52,7 +54,10 @@ module.exports = function(core) {
52
54
  newTags[WEAK_URL_ENCODED] = [0, result.length - 1];
53
55
 
54
56
  const event = createPropagationEvent({
55
- name: 'global.escape',
57
+ name,
58
+ moduleName: 'global',
59
+ methodName: 'escape',
60
+ context: `escape('${argInfo.value}')`,
56
61
  object: {
57
62
  value: createObjectLabel('global'),
58
63
  tracked: false
@@ -64,6 +69,8 @@ module.exports = function(core) {
64
69
  args: [{ value: argInfo.value, tracked: true }],
65
70
  tags: newTags,
66
71
  history,
72
+ source: 'P',
73
+ target: 'R',
67
74
  addedTags: [WEAK_URL_ENCODED],
68
75
  stacktraceOpts: {
69
76
  constructorOpt: hooked,
@@ -21,7 +21,7 @@ const {
21
21
  const {
22
22
  createFullLengthCopyTags
23
23
  } = require('../../tag-utils');
24
- const { patchType, createModuleLabel } = require('../common');
24
+ const { patchType } = require('../common');
25
25
 
26
26
  module.exports = function(core) {
27
27
  const {
@@ -36,8 +36,10 @@ module.exports = function(core) {
36
36
  return core.assess.dataflow.propagation.handlebarsEscapeExpression = {
37
37
  install() {
38
38
  depHooks.resolve({ name: 'handlebars', version: '>=4.0.0' }, (handlebars, version) => {
39
+ const name = 'handlebars.Utils.escapeExpression';
40
+
39
41
  patcher.patch(handlebars.Utils, 'escapeExpression', {
40
- name: 'handlebars.Utils.escapeExpression',
42
+ name,
41
43
  patchType,
42
44
  post(data) {
43
45
  const { args, result, hooked, orig } = data;
@@ -54,9 +56,12 @@ module.exports = function(core) {
54
56
  newTags[HTML_ENCODED] = [0, result.length - 1];
55
57
 
56
58
  const event = createPropagationEvent({
57
- name: 'handlebars.Utils.escapeExpression',
59
+ name,
60
+ moduleName: 'handlebars',
61
+ methodName: 'Utils.escapeExpression',
62
+ context: `${name}('${argInfo.value}')`,
58
63
  object: {
59
- value: `[${createModuleLabel('handlebars', version)}].Utils`,
64
+ value: 'handlebars.Utils',
60
65
  tracked: false
61
66
  },
62
67
  result: {
@@ -67,6 +72,8 @@ module.exports = function(core) {
67
72
  tags: newTags,
68
73
  addedTags: [HTML_ENCODED],
69
74
  history,
75
+ source: 'P',
76
+ target: 'R',
70
77
  stacktraceOpts: {
71
78
  constructorOpt: hooked,
72
79
  prependFrames: [orig]
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const { isString } = require('@contrast/common');
19
+ const { patchType } = require('../common');
20
+
21
+ module.exports = function(core) {
22
+ const {
23
+ logger,
24
+ scopes: { sources, instrumentation },
25
+ patcher,
26
+ depHooks,
27
+ assess: {
28
+ dataflow: { tracker }
29
+ }
30
+ } = core;
31
+
32
+ return core.assess.dataflow.propagation.isnumericInstrumentation = {
33
+ install() {
34
+ const name = 'isnumeric';
35
+
36
+ depHooks.resolve({ name, version: '<1.0.0' }, (_export, { version }) => {
37
+ const fullName = `${name}@${version}`;
38
+ return patcher.patch(_export, {
39
+ name,
40
+ patchType,
41
+ post(data) {
42
+ const { args: [value], result } = data;
43
+ if (
44
+ !result ||
45
+ !value ||
46
+ !isString(value) ||
47
+ !sources.getStore()?.assess ||
48
+ instrumentation.isLocked() ||
49
+ !tracker.getData(value)
50
+ ) return;
51
+
52
+ tracker.untrack(value);
53
+ logger.trace({ sanitizer: fullName, value }, 'untracked a string value');
54
+ }
55
+ });
56
+ });
57
+ },
58
+ };
59
+ };
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const c = require('@contrast/common');
19
+
20
+ module.exports = function(core) {
21
+ const component = core.assess.dataflow.propagation.mongooseInstrumentation = {
22
+ install() {
23
+ c.callChildComponentMethodsSync(component, 'install');
24
+ }
25
+ };
26
+
27
+ // todo NODE-3103
28
+ // require('./schema-map')(core);
29
+
30
+ // todo NODE-3102
31
+ // require('./schema-mixed')(core);
32
+
33
+ require('./schema-string')(core);
34
+
35
+ return component;
36
+ };
@@ -0,0 +1,156 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const { DataflowTag, substring } = require('@contrast/common');
19
+ const { patchType } = require('../../common');
20
+
21
+ module.exports = function(core) {
22
+ const {
23
+ scopes: { sources },
24
+ patcher,
25
+ depHooks,
26
+ assess: {
27
+ dataflow: {
28
+ tracker,
29
+ eventFactory: { createPropagationEvent }
30
+ }
31
+ }
32
+ } = core;
33
+
34
+ return core.assess.dataflow.propagation.mongooseInstrumentation.schemaString = {
35
+ install() {
36
+ depHooks.resolve(
37
+ { name: 'mongoose', file: 'lib/schema/string.js', version: '>=6.0.0' },
38
+ (SchemaString) => {
39
+ patchEnum(SchemaString);
40
+ patchDoValidate(SchemaString);
41
+ patchDoValidateSync(SchemaString);
42
+ }
43
+ );
44
+ },
45
+ };
46
+
47
+ function patchEnum(SchemaString) {
48
+ patcher.patch(SchemaString.prototype, 'enum', {
49
+ name: 'mongoose.SchemaString.prototype.enum',
50
+ patchType,
51
+ post(data) {
52
+ if (!data.result) return;
53
+
54
+ const enumValidator = data.result.validators.find(
55
+ (validator) => validator.type === 'enum'
56
+ );
57
+
58
+ if (!enumValidator) return;
59
+
60
+ patcher.patch(enumValidator, 'validator', {
61
+ name: 'mongoose.SchemaString.prototype.enum.validator',
62
+ patchType,
63
+ post(data) {
64
+ const sourceContext = sources.getStore()?.assess;
65
+ if (!sourceContext) return;
66
+
67
+ if (data.result) {
68
+ tracker.untrack(data.args[0]);
69
+ }
70
+ }
71
+ });
72
+ }
73
+ });
74
+ }
75
+
76
+ function patchDoValidate(SchemaString) {
77
+ // called when `Model.validate(data)` static method is used, as well as `model.validate()` instance method
78
+ const name = 'mongoose.SchemaString.prototype.doValidate';
79
+ patcher.patch(SchemaString.prototype, 'doValidate', {
80
+ name,
81
+ patchType,
82
+ pre(data) {
83
+ const [value, cb] = data.args;
84
+ if (value && typeof cb === 'function' && sources.getStore()?.assess) {
85
+ data.args[1] = patcher.patch(cb, {
86
+ name,
87
+ patchType,
88
+ pre({ args: [err] }) {
89
+ if (!err) propagate(name, data.orig, value);
90
+ }
91
+ });
92
+ }
93
+ }
94
+ });
95
+ }
96
+
97
+ function patchDoValidateSync(SchemaString) {
98
+ const name = 'mongoose.SchemaString.prototype.doValidateSync';
99
+ patcher.patch(SchemaString.prototype, 'doValidateSync', {
100
+ name,
101
+ patchType,
102
+ post({ orig, args: [value] }) {
103
+ if (value?.length) {
104
+ propagate(name, orig, value);
105
+ }
106
+ }
107
+ });
108
+
109
+ }
110
+
111
+ function propagate(name, orig, value) {
112
+ const strInfo = tracker.getData(value);
113
+ if (!strInfo) return;
114
+
115
+ const methodName = substring(name, name.indexOf('.') + 1);
116
+ // copy because we mutate the metadata value inline
117
+ const history = [{ ...strInfo }];
118
+ const event = createPropagationEvent({
119
+ addedTags: [DataflowTag.STRING_TYPE_CHECKED],
120
+ name,
121
+ moduleName: 'mongoose',
122
+ methodName,
123
+ history,
124
+ object: {
125
+ tracked: false,
126
+ value: 'mongoose.SchemaString',
127
+ },
128
+ args: [{ tracked: true, value: strInfo.value }],
129
+ result: {
130
+ tracked: false,
131
+ value: undefined,
132
+ },
133
+ source: 'P0',
134
+ tags: {
135
+ ...strInfo.tags,
136
+ [DataflowTag.STRING_TYPE_CHECKED]: [0, value.length - 1],
137
+ },
138
+ target: 'P0',
139
+ stacktraceOpts: {
140
+ prependFrames: [orig],
141
+ }
142
+ });
143
+
144
+ if (!event) {
145
+ return;
146
+ }
147
+
148
+ // in case the event type changed e.g. Source->Propagation
149
+ for (const key of Object.keys(strInfo)) {
150
+ if (key === 'value' || key === 'extern') continue;
151
+ delete strInfo[key];
152
+ }
153
+
154
+ Object.assign(strInfo, event);
155
+ }
156
+ };
@@ -50,6 +50,9 @@ module.exports = function(core) {
50
50
 
51
51
  const event = createPropagationEvent({
52
52
  name: eventName,
53
+ moduleName: 'mysql',
54
+ methodName: 'escape',
55
+ context: `mysql.escape('${argInfo.value}')`,
53
56
  object: {
54
57
  value: objectValue,
55
58
  tracked: false
@@ -62,6 +65,8 @@ module.exports = function(core) {
62
65
  tags: newTags,
63
66
  addedTags: [SQL_ENCODED],
64
67
  history,
68
+ source: 'P',
69
+ target: 'R',
65
70
  stacktraceOpts: {
66
71
  constructorOpt: hooked,
67
72
  prependFrames: [orig]