@contrast/assess 1.5.0 → 1.6.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.
@@ -0,0 +1,122 @@
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 { patchType } = require('../common');
19
+ const { Rule, isString, DataflowTag: { CUSTOM_ENCODED_SQL_INJECTION, CUSTOM_ENCODED, CUSTOM_VALIDATED_SQL_INJECTION, CUSTOM_VALIDATED, SQL_ENCODED, LIMITED_CHARS, UNTRUSTED } } = require('@contrast/common');
20
+
21
+ const safeTags = [
22
+ CUSTOM_ENCODED_SQL_INJECTION,
23
+ CUSTOM_ENCODED,
24
+ CUSTOM_VALIDATED_SQL_INJECTION,
25
+ CUSTOM_VALIDATED,
26
+ SQL_ENCODED,
27
+ LIMITED_CHARS,
28
+ ];
29
+
30
+ module.exports = function (core) {
31
+ const {
32
+ depHooks,
33
+ patcher,
34
+ scopes: { sources },
35
+ assess: {
36
+ dataflow: {
37
+ tracker,
38
+ sinks: { isVulnerable, reportFindings },
39
+ eventFactory: { createSinkEvent },
40
+ },
41
+ },
42
+ } = core;
43
+
44
+ function getValueFromArgs([value]) {
45
+ if (isString(value)) {
46
+ return value;
47
+ }
48
+
49
+ if (isString(value.sql)) {
50
+ return value.sql;
51
+ }
52
+ }
53
+
54
+ const pre = (module, file, obj) => (data) => {
55
+ const store = sources.getStore()?.assess;
56
+ if (!store || !data.args[0]) return;
57
+
58
+ const val = getValueFromArgs(data.args);
59
+ if (!val) return;
60
+
61
+ const strInfo = tracker.getData(val);
62
+ if (!strInfo || !isVulnerable(UNTRUSTED, safeTags, strInfo.tags)) {
63
+ return;
64
+ }
65
+
66
+ const event = createSinkEvent({
67
+ name: `${module}/${file}`,
68
+ history: [strInfo],
69
+ object: {
70
+ value: `${module}.${obj}`,
71
+ tracked: false,
72
+ },
73
+ args: [
74
+ {
75
+ value: strInfo.value,
76
+ tracked: true,
77
+ },
78
+ ],
79
+ tags: strInfo.tags,
80
+ source: 'P0',
81
+ stacktraceOpts: {
82
+ contructorOpt: data.hooked,
83
+ },
84
+ });
85
+
86
+ if (event) {
87
+ reportFindings({
88
+ ruleId: Rule.SQL_INJECTION,
89
+ sinkEvent: event,
90
+ });
91
+ }
92
+ };
93
+
94
+ core.assess.dataflow.sinks.mysql = {
95
+ install() {
96
+ depHooks.resolve(
97
+ { name: 'mysql', file: 'lib/Connection' },
98
+ (Connection) => {
99
+ patcher.patch(Connection.prototype, 'query', {
100
+ name: 'Connection.prototype.query',
101
+ patchType,
102
+ pre: pre('mysql', 'lib/Connection.query', 'Connection')
103
+ });
104
+ },
105
+ );
106
+ depHooks.resolve(
107
+ { name: 'mysql2', file: 'lib/connection' },
108
+ (connection) => {
109
+ ['query', 'execute'].forEach((method) => {
110
+ patcher.patch(connection.prototype, `${method}`, {
111
+ name: `connection.prototype.${method}`,
112
+ patchType,
113
+ pre: pre('mysql2', `lib/connection.Connection.${method}`, 'connection')
114
+ });
115
+ });
116
+ },
117
+ );
118
+ },
119
+ };
120
+
121
+ return core.assess.dataflow.sinks.mysql;
122
+ };
@@ -16,7 +16,11 @@
16
16
  'use strict';
17
17
 
18
18
  const util = require('util');
19
- const { Rule, isString } = require('@contrast/common');
19
+ const {
20
+ DataflowTag: { UNTRUSTED, SQL_ENCODED, LIMITED_CHARS, CUSTOM_VALIDATED, CUSTOM_ENCODED },
21
+ Rule,
22
+ isString
23
+ } = require('@contrast/common');
20
24
  const { patchType } = require('../common');
21
25
 
22
26
  module.exports = function (core) {
@@ -34,12 +38,12 @@ module.exports = function (core) {
34
38
  } = core;
35
39
 
36
40
  const safeTags = [
37
- 'sql-encoded',
38
- 'limited-chars',
39
- 'custom-validated',
40
- 'custom-encoded',
41
+ SQL_ENCODED,
42
+ LIMITED_CHARS,
43
+ CUSTOM_VALIDATED,
44
+ CUSTOM_ENCODED,
41
45
  ];
42
- const requiredTag = 'untrusted';
46
+
43
47
  const inspect = patcher.unwrap(util.inspect);
44
48
 
45
49
  const postgres = core.assess.dataflow.sinks.postgres = {};
@@ -58,7 +62,7 @@ module.exports = function (core) {
58
62
  const objValue = methodSignature.includes('native') ? 'pg.native.Client' : 'pg.Client';
59
63
  const arg0Val = inspect(arg0);
60
64
 
61
- if (isVulnerable(requiredTag, safeTags, strInfo.tags)) {
65
+ if (isVulnerable(UNTRUSTED, safeTags, strInfo.tags)) {
62
66
  const event = createSinkEvent({
63
67
  args: [{
64
68
  tracked: true,
@@ -116,8 +120,6 @@ module.exports = function (core) {
116
120
  },
117
121
  );
118
122
 
119
- const pgClientPatchName = `${patchType}:${pgClientQueryPatchName}.query`;
120
- const pgNativeClientPatchName = `${patchType}:${pgNativeClientQueryPatchName}.query`;
121
123
  depHooks.resolve({ name: 'pg-pool' }, (pool, version) => {
122
124
  const name = 'pg-pool.Pool.prototype.query';
123
125
  patcher.patch(pool.prototype, 'query', {
@@ -129,9 +131,8 @@ module.exports = function (core) {
129
131
  )?.funcKeys;
130
132
 
131
133
  if (
132
- funcKeys &&
133
- (funcKeys.has(pgClientPatchName) ||
134
- funcKeys.has(pgNativeClientPatchName))
134
+ funcKeys?.has(`${patchType}:${pgClientQueryPatchName}`) ||
135
+ funcKeys?.has(`${patchType}:${pgNativeClientQueryPatchName}`)
135
136
  ) {
136
137
  return;
137
138
  }
@@ -14,14 +14,19 @@
14
14
  */
15
15
 
16
16
  'use strict';
17
+
17
18
  const { patchType } = require('../common');
18
- const { Rule, isString } = require('@contrast/common');
19
+ const {
20
+ DataflowTag: { UNTRUSTED, SQL_ENCODED, LIMITED_CHARS, CUSTOM_VALIDATED, CUSTOM_ENCODED },
21
+ Rule,
22
+ isString
23
+ } = require('@contrast/common');
19
24
 
20
- const SAFE_TAGS = [
21
- 'sql-encoded',
22
- 'limited-chars',
23
- 'custom-validated',
24
- 'custom-encoded',
25
+ const safeTags = [
26
+ SQL_ENCODED,
27
+ LIMITED_CHARS,
28
+ CUSTOM_VALIDATED,
29
+ CUSTOM_ENCODED,
25
30
  ];
26
31
 
27
32
  module.exports = function (core) {
@@ -43,7 +48,7 @@ module.exports = function (core) {
43
48
  if (!store || !data.args[0] || !isString(data.args[0])) return;
44
49
 
45
50
  const strInfo = tracker.getData(data.args[0]);
46
- if (!strInfo || !isVulnerable('untrusted', SAFE_TAGS, strInfo.tags)) {
51
+ if (!strInfo || !isVulnerable(UNTRUSTED, safeTags, strInfo.tags)) {
47
52
  return;
48
53
  }
49
54
 
@@ -15,7 +15,12 @@
15
15
 
16
16
  'use strict';
17
17
 
18
- const { isString, traverseValues } = require('@contrast/common');
18
+ const {
19
+ InputType,
20
+ DataflowTag,
21
+ isString,
22
+ traverseValues
23
+ } = require('@contrast/common');
19
24
 
20
25
  module.exports = function(core) {
21
26
  const {
@@ -40,15 +45,11 @@ module.exports = function(core) {
40
45
 
41
46
  const stop = value.length - 1;
42
47
  const tags = {
43
- untrusted: [0, stop]
48
+ [DataflowTag.UNTRUSTED]: [0, stop]
44
49
  };
45
50
 
46
- if (inputType === 'header' && key.toLowerCase() === 'referer') {
47
- tags.header = [0, stop];
48
- }
49
-
50
- if (inputType === 'cookie') {
51
- tags.cookie = [0, stop];
51
+ if (inputType === InputType.HEADER && key.toLowerCase() === 'referer') {
52
+ tags[DataflowTag.HEADER] = [0, stop];
52
53
  }
53
54
 
54
55
  return tags;
@@ -57,7 +58,7 @@ module.exports = function(core) {
57
58
  sources.handle = function({
58
59
  context,
59
60
  name,
60
- inputType = 'unknown',
61
+ inputType = InputType.UNKNOWN,
61
62
  stacktraceOpts,
62
63
  data,
63
64
  sourceContext
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/assess",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -15,7 +15,7 @@
15
15
  "dependencies": {
16
16
  "@contrast/distringuish": "^4.1.0",
17
17
  "@contrast/scopes": "1.3.0",
18
- "@contrast/common": "1.8.0",
18
+ "@contrast/common": "1.9.0",
19
19
  "parseurl": "^1.3.3"
20
20
  }
21
21
  }