forked from DeepSourceCorp/globstar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjango-insecure-pickle-deserialize.go
More file actions
202 lines (163 loc) · 5.49 KB
/
django-insecure-pickle-deserialize.go
File metadata and controls
202 lines (163 loc) · 5.49 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
package python
import (
// "fmt"
"slices"
"strings"
sitter "github.com/smacker/go-tree-sitter"
"globstar.dev/analysis"
)
var DjangoInsecurePickleDeserialize *analysis.Analyzer = &analysis.Analyzer{
Name: "django-insecure-pickle-deserialize",
Language: analysis.LangPy,
Description: "Insecure deserialization with pickle, _pickle, cpickle, dill, shelve, or yaml can lead to remote code execution. These libraries execute arbitrary code when loading untrusted data",
Category: analysis.CategorySecurity,
Severity: analysis.SeverityWarning,
Run: checkDjangoInsecurePickleDeserialize,
}
func checkDjangoInsecurePickleDeserialize(pass *analysis.Pass) (interface{}, error) {
reqvarmap := make(map[string]bool)
analysis.Preorder(pass, func(node *sitter.Node) {
if node.Type() != "assignment" {
return
}
left := node.ChildByFieldName("left")
right := node.ChildByFieldName("right")
if right == nil {
return
}
if isRequestSource(right, pass.FileContext.Source, reqvarmap) {
reqvarmap[left.Content(pass.FileContext.Source)] = true
}
})
// detect pickle and dill calls
analysis.Preorder(pass, func(node *sitter.Node) {
if node.Type() != "call" {
return
}
libraryNames := []string{"pickle", "_pickle", "cPickle", "shelve", "dill"}
methodNames := []string{"dump", "dumps", "load", "loads", "dump_session", "load_session"}
funcNode := node.ChildByFieldName("function")
if funcNode.Type() != "attribute" {
return
}
funcObj := funcNode.ChildByFieldName("object").Content(pass.FileContext.Source)
funcAttr := funcNode.ChildByFieldName("attribute").Content(pass.FileContext.Source)
if !slices.Contains(libraryNames, funcObj) || !slices.Contains(methodNames, funcAttr) {
return
}
argListNode := node.ChildByFieldName("arguments")
if argListNode.Type() != "argument_list" {
return
}
argNodes := getNamedChildren(argListNode, 0)
for _, arg := range argNodes {
if arg.Type() == "identifier" {
if reqvarmap[arg.Content(pass.FileContext.Source)] {
pass.Report(pass, node, "Detected insecure deserialization which may lead to remote code execution—use safer alternatives or validate input")
}
} else if arg.Type() == "call" {
if isRequestSource(arg, pass.FileContext.Source, reqvarmap) {
pass.Report(pass, node, "Detected insecure deserialization which may lead to remote code execution—use safer alternatives or validate input")
}
}
}
})
analysis.Preorder(pass, func(node *sitter.Node) {
if node.Type() != "call" {
return
}
yamlMethodNames := []string{"dump", "dump_all", "load", "load_all"}
funcNode := node.ChildByFieldName("function")
if funcNode.Type() != "attribute" {
return
}
funcObj := funcNode.ChildByFieldName("object").Content(pass.FileContext.Source)
funcAttr := funcNode.ChildByFieldName("attribute").Content(pass.FileContext.Source)
if funcObj != "yaml" || !slices.Contains(yamlMethodNames, funcAttr) {
return
}
argListNode := node.ChildByFieldName("arguments")
if argListNode.Type() != "argument_list" {
return
}
argNodes := getNamedChildren(argListNode, 0)
containsSafeParam := false
for _, arg := range argNodes {
if arg.Type() == "keyword_argument" {
key := arg.ChildByFieldName("name").Content(pass.FileContext.Source)
val := arg.ChildByFieldName("value").Content(pass.FileContext.Source)
if (key == "Dumper" || key == "Loader") && (strings.Contains(val, "SafeDumper") || strings.Contains(val, "SafeLoader")) {
containsSafeParam = true
}
}
}
if containsSafeParam {
return
}
for _, arg := range argNodes {
if arg.Type() == "identifier" {
if reqvarmap[arg.Content(pass.FileContext.Source)] {
pass.Report(pass, node, "Detected insecure deserialization which may lead to remote code execution—use safer alternatives or validate input")
}
} else if arg.Type() == "call" {
if isRequestSource(arg, pass.FileContext.Source, reqvarmap) {
pass.Report(pass, node, "Detected insecure deserialization which may lead to remote code execution—use safer alternatives or validate input")
}
}
}
})
return nil, nil
}
func isRequestSource(node *sitter.Node, source []byte, reqVarMap map[string]bool) bool {
switch node.Type() {
case "call":
if isEncoded(node, source, reqVarMap) {
return true
}
funcNode := node.ChildByFieldName("function")
if funcNode.Type() != "attribute" {
return false
}
objectNode := funcNode.ChildByFieldName("object")
if !strings.Contains(objectNode.Content(source), "request") {
return false
}
attributeNode := funcNode.ChildByFieldName("attribute")
if attributeNode.Type() != "identifier" {
return false
}
return true
case "subscript":
valueNode := node.ChildByFieldName("value")
if valueNode.Type() != "attribute" {
return false
}
objNode := valueNode.ChildByFieldName("object")
if objNode.Type() != "identifier" && objNode.Content(source) != "request" {
return false
}
return true
case "identifier":
return reqVarMap[node.Content(source)]
}
return false
}
func isEncoded(node *sitter.Node, source []byte, reqVarMap map[string]bool) bool {
if node.Type() != "call" {
return false
}
if strings.HasPrefix(node.Content(source), "request") {
return false
}
argListNode := node.ChildByFieldName("arguments")
if argListNode.Type() != "argument_list" {
return false
}
argNodes := getNamedChildren(argListNode, 0)
for _, arg := range argNodes {
if isRequestSource(arg, source, reqVarMap) {
return true
}
}
return false
}