|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "regexp" |
| 8 | + |
| 9 | + ldap "github.com/go-ldap/ldap/v3" |
| 10 | +) |
| 11 | + |
| 12 | +func bad(w http.ResponseWriter, req *http.Request) (interface{}, error) { |
| 13 | + ldapServer := "ldap.example.com" |
| 14 | + ldapPort := 389 |
| 15 | + bindDN := "cn=admin,dc=example,dc=com" |
| 16 | + bindPassword := req.URL.Query()["password"][0] |
| 17 | + |
| 18 | + // Connect to the LDAP server |
| 19 | + l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort)) |
| 20 | + if err != nil { |
| 21 | + log.Fatalf("Failed to connect to LDAP server: %v", err) |
| 22 | + } |
| 23 | + defer l.Close() |
| 24 | + |
| 25 | + // BAD: user input is not sanetized |
| 26 | + err = l.Bind(bindDN, bindPassword) |
| 27 | + if err != nil { |
| 28 | + log.Fatalf("LDAP bind failed: %v", err) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func good1(w http.ResponseWriter, req *http.Request) (interface{}, error) { |
| 33 | + ldapServer := "ldap.example.com" |
| 34 | + ldapPort := 389 |
| 35 | + bindDN := "cn=admin,dc=example,dc=com" |
| 36 | + bindPassword := req.URL.Query()["password"][0] |
| 37 | + |
| 38 | + // Connect to the LDAP server |
| 39 | + l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort)) |
| 40 | + if err != nil { |
| 41 | + log.Fatalf("Failed to connect to LDAP server: %v", err) |
| 42 | + } |
| 43 | + defer l.Close() |
| 44 | + |
| 45 | + hasEmptyInput, _ := regexp.MatchString("^\\s*$", bindPassword) |
| 46 | + |
| 47 | + // GOOD : bindPassword is not empty |
| 48 | + if !hasEmptyInput { |
| 49 | + l.Bind(bindDN, bindPassword) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func good2(w http.ResponseWriter, req *http.Request) (interface{}, error) { |
| 54 | + ldapServer := "ldap.example.com" |
| 55 | + ldapPort := 389 |
| 56 | + bindDN := "cn=admin,dc=example,dc=com" |
| 57 | + bindPassword := req.URL.Query()["password"][0] |
| 58 | + |
| 59 | + // Connect to the LDAP server |
| 60 | + l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort)) |
| 61 | + if err != nil { |
| 62 | + log.Fatalf("Failed to connect to LDAP server: %v", err) |
| 63 | + } |
| 64 | + defer l.Close() |
| 65 | + |
| 66 | + // GOOD : bindPassword is not empty |
| 67 | + if bindPassword != "" { |
| 68 | + l.Bind(bindDN, bindPassword) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func main() { |
| 73 | + bad(nil, nil) |
| 74 | + good1(nil, nil) |
| 75 | + good2(nil, nil) |
| 76 | +} |
0 commit comments