-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathdetect-object-injection.js
81 lines (75 loc) · 2.21 KB
/
detect-object-injection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Tries to detect instances of var[var]
* @author Jon Lamendola
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const getPath = (value, seen, keys) => {
let index = seen.indexOf(value);
const path = [keys[index]];
for (index--; index >= 0; index--) {
if (seen[index][path[0]] === value) {
value = seen[index];
path.unshift(keys[index]);
}
}
return `~${path.join('.')}`;
};
const getSerialize = (fn, decycle) => {
const seen = [];
const keys = [];
decycle =
decycle ||
function (key, value) {
return `[Circular ${getPath(value, seen, keys)}]`;
};
return function (key, value) {
let ret = value;
if (typeof value === 'object' && value) {
if (seen.indexOf(value) !== -1) {
ret = decycle(key, value);
} else {
seen.push(value);
keys.push(key);
}
}
if (fn) {
ret = fn(key, ret);
}
return ret;
};
};
const stringify = (obj, fn, spaces, decycle) => {
return JSON.stringify(obj, getSerialize(fn, decycle), spaces);
};
stringify.getSerialize = getSerialize;
module.exports = {
meta: {
type: 'error',
docs: {
description: 'Detects "variable[key]" as a left- or right-hand assignment operand.',
category: 'Possible Security Vulnerability',
recommended: true,
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-object-injection.md',
},
},
create(context) {
return {
MemberExpression: function (node) {
if (node.computed === true) {
if (node.property.type === 'Identifier') {
if (node.parent.type === 'VariableDeclarator') {
context.report({ node: node, message: 'Variable Assigned to Object Injection Sink' });
} else if (node.parent.type === 'CallExpression') {
context.report({ node: node, message: 'Function Call Object Injection Sink' });
} else {
context.report({ node: node, message: 'Generic Object Injection Sink' });
}
}
}
},
};
},
};