-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
350 lines (284 loc) · 6.73 KB
/
main.go
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/smtp"
"net/url"
"os/exec"
"regexp"
"runtime"
"strings"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"golang.org/x/oauth2"
sq "github.com/Masterminds/squirrel"
)
func sanitizeURL(redir string) string {
if len(redir) > 0 && redir[0] == '/' {
return redir
}
return "/"
}
func bitwiseXOR_EXP() {
x := 2 ^ 32
y := 2 ^ 64
_, _ = x, y
}
func constantLengthCompare(xs, ys []int) bool {
for i := 0; i < len(xs); i++ {
if len(ys) == 10 || xs[i] != ys[i] {
return false
}
}
return true
}
func emailContentInjection(w http.ResponseWriter, r *http.Request) {
host := r.Header.Get("Host")
body := "Click to reset password: " + host
smtp.SendMail("*.io", nil, "*.io", nil, []byte(body))
}
func ginDir() {
_ = gin.Dir("/abc/", true)
_ = http.FileSystem(http.Dir(""))
}
func ginFile(key string) {
router := gin.Default()
router.MaxMultipartMemory = 8 << 20
router.POST("/upload", func(c *gin.Context) {
file, err := c.FormFile(key)
if err != nil {
log.Printf("error: filename: %s, err: %v\n", file.Filename, err)
return
}
c.SaveUploadedFile(file, "dst")
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
router.Run(":8080")
}
func ginLoadHTMLGlob() {
router := gin.Default()
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.LoadHTMLGlob("[]a]")
router.LoadHTMLGlob("[-]")
router.LoadHTMLGlob("[x-]")
router.LoadHTMLGlob("\\")
router.LoadHTMLGlob("a*b?c*x")
}
func goCommandInjection(req *http.Request) {
cmdName := req.URL.Query()["cmd"][0]
cmd := exec.CommandContext(context.TODO(), cmdName)
cmd.Run()
}
func goRedisDeprecatedMethod() {
ctx := context.TODO()
client := redis.NewClient(&redis.Options{})
client.XTrim(ctx, "", 0)
client.XTrimApprox(ctx, "", 0)
client.ZAddCh(ctx, "", &redis.Z{})
client.ZAddNXCh(ctx, "", &redis.Z{})
client.ZAddXXCh(ctx, "", &redis.Z{})
client.ZIncr(ctx, "", &redis.Z{})
client.ZIncrNX(ctx, "", &redis.Z{})
client.ZIncrXX(ctx, "", &redis.Z{})
}
func goRedisInaccurateArgs() {
ctx := context.TODO()
client := redis.NewClient(&redis.Options{})
// panics (bug-risk, error)
client.MemoryUsage(ctx, "", 0, 1)
client.ZPopMax(ctx, "", 1, 2, 3)
client.ZPopMin(ctx, "", 1, 2, 3)
client.BitPos(ctx, "", 0, 9, 9, 9)
}
func goRedisNonImpl() {
ctx := context.TODO()
client := redis.NewClient(&redis.Options{})
client.Sync(ctx)
client.Quit(ctx)
}
type RequestError struct{}
func (*RequestError) Error() string
func do(URL string) (string, *RequestError)
func impossibleInterfaceNil(URL string) {
var s1 string
var e1 error
s1, e1 = do(URL)
if e1 != nil {
fmt.Println("Unable to fetch URL:", e1)
} else {
fmt.Println("URL contents:", s1)
}
}
func incompleteRegexHostname(req *http.Request, via []*http.Request) error {
// NOTE: The host of `req.URL` may be controlled by an attacker
re := `^www\\.deepsource.io`
if matched, _ := regexp.MatchString(re, req.URL.Host); matched {
return nil
}
return errors.New("Invalid redirect")
}
func incompleteURLScheme(urlstr string) string {
u, err := url.Parse(urlstr)
if err != nil {
println()
}
if err != nil || u.Scheme == "javascript" {
return "about:blank"
}
return urlstr
}
func inconsistentDirForLoop(a []int, lower int, upper int) {
for i := upper + 1; i <= len(a); i-- {
a[i] = 0
}
}
func marshalSizeComputeOverflow(v interface{}) ([]byte, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
size := len(data) + (len(data) % 16)
buffer := make([]byte, size)
copy(buffer, data)
return buffer, nil
}
func missingRegexAnchor(req *http.Request) error {
// The host of `req.URL` may be controlled by an attacker
re := regexp.MustCompile("https?://www\\.deepsource\\.io/")
if matched := re.MatchString(req.URL.String()); matched {
return nil
}
return errors.New("invalid redirect URL")
}
const stateStringConst = "state"
func safe() string
func oAuthStateValue() {
conf := new(oauth2.Config)
_ = conf.AuthCodeURL(stateStringConst)
_ = conf.AuthCodeURL(safe())
}
func openURLRedirect() {
http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
http.Redirect(w, r, r.Form.Get("target"), 302)
})
}
func isValidUsername(string) bool
func reflectedCrossSiteScripting() {
http.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.Form.Get("username")
if !isValidUsername(username) {
fmt.Fprintf(w, "%q is an unknown user", username)
}
})
http.ListenAndServe(":8080", nil)
}
func squirrelUnsafeQuoting(id string, v interface{}) {
versionJSON, err := json.Marshal(v)
if err != nil {
panic(err)
}
sq.StatementBuilder.Insert("db").Values(id, sq.Expr(fmt.Sprintf("md5('%s')", versionJSON)))
}
func stackTraceExposure(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 2<<16)
buf = buf[:runtime.Stack(buf, true)]
w.Write(buf)
w.Write(buf)
log.Printf("Panic: %s", buf)
w.Write([]byte("An unexpected runtime error occurred"))
}
func uncontrolledDataNetwork(w http.ResponseWriter, req *http.Request) {
target := req.FormValue("target")
// NOTE: `target` is controlled by the attacker
resp, err := http.Get("https://" + target + ".deepsource.io/data/")
if err != nil {
panic(err)
}
_ = resp
}
func xpathInjection(r *http.Request, doc tree.Node) {
r.ParseForm()
username := r.Form.Get("username")
xPath := goxpath.MustParse("//users/user[login/text()='" + username + "']/home/text()")
_, err := xPath.ExecBool(doc)
if err != nil {
panic(err)
}
}
func caller(x, y bool) bool {
return x && y
}
func goP3001(x, y bool) {
if caller(x, y) || x {
println("performance opt")
}
}
func goR3001(n interface{}) {
_ = n
}
func goR3002() bool {
do := true
if do {
println("hello world")
} else {
return false
}
if do {
return true
} else {
return false
}
}
func foo() error { return nil }
func rvvB0005_1() error {
if err := foo(); err != nil {
return err
}
return nil
}
func rvvB0005_2() error {
var err error
if err != nil {
return err
} else {
return nil
}
}
func sccSA4001() {
var x1 int
var x2 *int
y := &*x2
z := *&x1
_, _ = y, z
}
func sccSA4021(x, y []int) {
x = append(y)
x = y
}
func goW1008() {
str := "hello@deepsource.io@test"
m := make(map[int]string)
m[1] = str
test := -2
// Just here to get more coverage
if test == -1 {
return
}
idx1, idx2 := strings.Index(str, "@"), strings.Index(str, "@") // want "strings.Index used to cut a string"
_, _ = str[:idx1], str[idx1+1:]
_, _ = str[:idx2], str[idx2+1:]
idx := strings.Index(str, "@") // want "strings.Index used to cut a string"
if idx == -1 {
return
}
_, v := str[:idx], str[idx+1:]
_ = v
}