|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/md5" // weak hash |
| 5 | + "crypto/tls" // insecure TLS config |
| 6 | + "database/sql" |
| 7 | + "fmt" |
| 8 | + "math/rand" // insecure randomness for secrets |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | +) |
| 13 | + |
| 14 | +var db *sql.DB // uninitialized; fine for static analysis examples |
| 15 | + |
| 16 | +func handler(w http.ResponseWriter, r *http.Request) { |
| 17 | + // SQL injection: tainted input flows into a query. |
| 18 | + user := r.URL.Query().Get("user") |
| 19 | + _, _ = db.Query("SELECT * FROM users WHERE name = '" + user + "'") |
| 20 | + |
| 21 | + // Command injection: shell execution with user-controlled string. |
| 22 | + cmdParam := r.URL.Query().Get("cmd") |
| 23 | + _, _ = exec.Command("sh", "-c", cmdParam).Output() |
| 24 | + |
| 25 | + // Path traversal: user input concatenated into filesystem path. |
| 26 | + file := r.URL.Query().Get("file") |
| 27 | + if data, err := os.ReadFile("/var/data/" + file); err == nil { |
| 28 | + _, _ = w.Write(data) |
| 29 | + } |
| 30 | + |
| 31 | + // SSRF: making requests to a user-controlled URL. |
| 32 | + target := r.URL.Query().Get("url") |
| 33 | + _, _ = http.Get(target) |
| 34 | + |
| 35 | + // Open redirect: redirecting to a user-controlled URL. |
| 36 | + next := r.URL.Query().Get("next") |
| 37 | + http.Redirect(w, r, next, http.StatusFound) |
| 38 | + |
| 39 | + // Weak crypto: MD5 used for a password-like value. |
| 40 | + pw := r.URL.Query().Get("pw") |
| 41 | + sum := md5.Sum([]byte(pw)) |
| 42 | + fmt.Fprintf(w, "md5=%x\n", sum) |
| 43 | + |
| 44 | + // Insecure randomness for a secret/token. |
| 45 | + secret := fmt.Sprintf("%x", rand.Int63()) |
| 46 | + fmt.Fprintf(w, "secret=%s\n", secret) |
| 47 | + |
| 48 | + // Insecure TLS: skip certificate verification. |
| 49 | + client := &http.Client{ |
| 50 | + Transport: &http.Transport{ |
| 51 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec |
| 52 | + }, |
| 53 | + } |
| 54 | + _, _ = client.Get("https://example.com") |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + http.HandleFunc("/", handler) |
| 59 | + _ = http.ListenAndServe(":8080", nil) |
| 60 | +} |
0 commit comments