forked from DeepSourceCorp/globstar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjango-insecure-eval-exec.go
More file actions
232 lines (187 loc) · 6.72 KB
/
django-insecure-eval-exec.go
File metadata and controls
232 lines (187 loc) · 6.72 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package python
import (
"regexp"
"strings"
sitter "github.com/smacker/go-tree-sitter"
"globstar.dev/analysis"
)
var DjangoInsecureEvalExec *analysis.Analyzer = &analysis.Analyzer{
Name: "django-insecure-eval-exec",
Language: analysis.LangPy,
Description: "Using `eval`/`exec` or `os.system` with user data creates a severe security vulnerability that allows attackers to execute arbitrary code on your system. This dangerous practice can lead to complete system compromise, data theft, or service disruption. Instead, replace `eval` with dedicated libraries or methods specifically designed for your required functionality.",
Category: analysis.CategorySecurity,
Severity: analysis.SeverityWarning,
Run: checkDjangoInsecureEvalExec,
}
func checkDjangoInsecureEvalExec(pass *analysis.Pass) (interface{}, error) {
requestVarMap := make(map[string]bool)
userFmtStrVarMap := make(map[string]bool)
// first pass: check for assignment of `request` data stored in variables
analysis.Preorder(pass, func(node *sitter.Node) {
if node.Type() != "assignment" {
return
}
leftNode := node.ChildByFieldName("left")
rightNode := node.ChildByFieldName("right")
if rightNode.Type() != "call" && rightNode.Type() != "subscript" && rightNode.Type() != "binary_operator" {
return
}
if isRequestCall(rightNode, pass.FileContext.Source) {
varName := leftNode.Content(pass.FileContext.Source)
requestVarMap[varName] = true
}
})
// second pass: get variable names for string formatting
analysis.Preorder(pass, func(node *sitter.Node) {
if node.Type() != "assignment" {
return
}
leftNode := node.ChildByFieldName("left")
rightNode := node.ChildByFieldName("right")
if isStringFormatted(rightNode, pass.FileContext.Source, requestVarMap, userFmtStrVarMap) {
userFmtStrVarMap[leftNode.Content(pass.FileContext.Source)] = true
}
})
analysis.Preorder(pass, func(node *sitter.Node) {
if node.Type() != "call" {
return
}
funcNode := node.ChildByFieldName("function")
funcPattern := `\b(eval|exec)\b`
re := regexp.MustCompile(funcPattern)
if !re.MatchString(funcNode.Content(pass.FileContext.Source)) && !strings.Contains(funcNode.Content(pass.FileContext.Source), "os.system") {
return
}
argNode := node.ChildByFieldName("arguments")
argumentList := getNamedChildren(argNode, 0)
for _, arg := range argumentList {
if arg.Type() == "identifier" {
if hasUserDataVar(arg, pass.FileContext.Source, requestVarMap, userFmtStrVarMap) {
pass.Report(pass, node, "Detected user data in code-execution call which can cause remote code execution")
}
} else if isRequestCall(arg, pass.FileContext.Source) {
pass.Report(pass, node, "Detected user data in code-execution call which can cause remote code execution")
} else if isStringFormatted(arg, pass.FileContext.Source, requestVarMap, userFmtStrVarMap) {
pass.Report(pass, node, "Detected user data in code-execution call which can cause remote code execution")
} else if isBase64Decoded(arg, pass.FileContext.Source, requestVarMap, userFmtStrVarMap) {
pass.Report(pass, node, "Detected user data in code-execution call which can cause remote code execution")
}
}
})
return nil, nil
}
func isBase64Decoded(node *sitter.Node, source []byte, reqVarMap map[string]bool, userFmtStringVarMap map[string]bool) bool {
if node.Type() != "call" {
return false
}
funcNode := node.ChildByFieldName("function")
if funcNode.Type() != "attribute" {
return false
}
funcAttribute := funcNode.Content(source)
if funcAttribute != "base64.decodestring" && funcAttribute != "decodestring" {
return false
}
argsListNode := node.ChildByFieldName("arguments")
argsList := getNamedChildren(argsListNode, 0)
for _, argsNode := range argsList {
switch argsNode.Type() {
case "call":
if isRequestCall(argsNode, source) || isStringFormatted(argsNode, source, reqVarMap, userFmtStringVarMap) {
return true
}
byteFuncName := argsNode.ChildByFieldName("function")
if byteFuncName.Type() != "identifier" && byteFuncName.Content(source) != "bytes" {
return false
}
byteArgsNode := argsNode.ChildByFieldName("arguments")
if byteArgsNode.NamedChildCount() == 0 {
return false
}
byteArgs := getNamedChildren(byteArgsNode, 0)
for _, arg := range byteArgs {
if isStringFormatted(arg, source, reqVarMap, userFmtStringVarMap) || isRequestCall(arg, source) || hasUserDataVar(arg, source, reqVarMap, userFmtStringVarMap) {
return true
}
}
return false
case "identifier":
if hasUserDataVar(argsNode, source, reqVarMap, userFmtStringVarMap) {
return true
}
return false
case "string":
if isStringFormatted(node, source, reqVarMap, userFmtStringVarMap) {
return true
}
return false
}
}
return false
}
func isStringFormatted(node *sitter.Node, source []byte, reqVarMap map[string]bool, userFmtStringVarMap map[string]bool) bool {
switch node.Type() {
case "call":
funcNode := node.ChildByFieldName("function")
if funcNode.Type() != "attribute" {
return false
}
strObjectNode := funcNode.ChildByFieldName("object")
funcAttribute := funcNode.Content(source)
if !strings.HasSuffix(funcAttribute, ".format") || strObjectNode.Type() != "string" {
return false
}
argNode := node.ChildByFieldName("arguments")
if argNode.Type() != "argument_list" {
return false
}
reqArgNode := argNode.NamedChild(0)
if !isRequestCall(reqArgNode, source) && !hasUserDataVar(reqArgNode, source, reqVarMap, userFmtStringVarMap) {
return false
}
return true
case "binary_operator":
binaryOpLeftNode := node.ChildByFieldName("left")
binaryOpRightNode := node.ChildByFieldName("right")
if binaryOpLeftNode.Type() != "string" {
return false
}
if !isRequestCall(binaryOpRightNode, source) && !hasUserDataVar(binaryOpRightNode, source, reqVarMap, userFmtStringVarMap) {
return false
}
return true
case "string":
strContent := node.Content(source)
// check if f-string
if strContent[0] != 'f' {
return false
}
allChildren := getNamedChildren(node, 0)
// check if user data is present in f-string interpolation
for _, child := range allChildren {
if child.Type() == "interpolation" {
if isRequestCall(child.NamedChild(0), source) || hasUserDataVar(child.NamedChild(0), source, reqVarMap, userFmtStringVarMap) {
return true
}
}
}
}
return false
}
func hasUserDataVar(node *sitter.Node, source []byte, reqVarMap map[string]bool, userFmtStringVarMap map[string]bool) bool {
if node.Type() != "identifier" {
return false
}
argName := node.Content(source)
for key := range reqVarMap {
if argName == key {
return true
}
}
for key := range userFmtStringVarMap {
if argName == key {
return true
}
}
return false
}