|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package tests_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/foxcpp/maddy/tests" |
| 11 | + "github.com/jimlambrt/gldap" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +type searchEntry struct { |
| 16 | + dn string |
| 17 | + options []gldap.Option |
| 18 | +} |
| 19 | + |
| 20 | +type MockLDAP struct { |
| 21 | + T *testing.T |
| 22 | + SearchEntries map[string][]searchEntry |
| 23 | + AllowedBinds map[string]string |
| 24 | +} |
| 25 | + |
| 26 | +func (ml *MockLDAP) HandleBind(w *gldap.ResponseWriter, r *gldap.Request) { |
| 27 | + resp := r.NewBindResponse( |
| 28 | + gldap.WithResponseCode(gldap.ResultInvalidCredentials), |
| 29 | + ) |
| 30 | + |
| 31 | + m, err := r.GetSimpleBindMessage() |
| 32 | + if err != nil { |
| 33 | + require.NoError(ml.T, w.Write(resp)) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + pass, ok := ml.AllowedBinds[m.UserName] |
| 38 | + if ok && pass == string(m.Password) { |
| 39 | + resp.SetResultCode(gldap.ResultSuccess) |
| 40 | + require.NoError(ml.T, w.Write(resp)) |
| 41 | + } |
| 42 | + |
| 43 | + require.NoError(ml.T, w.Write(resp)) |
| 44 | +} |
| 45 | + |
| 46 | +func (ml *MockLDAP) HandleSearch(w *gldap.ResponseWriter, r *gldap.Request) { |
| 47 | + resp := r.NewSearchDoneResponse() |
| 48 | + m, err := r.GetSearchMessage() |
| 49 | + if err != nil { |
| 50 | + ml.T.Logf("not a search message: %s", err) |
| 51 | + require.NoError(ml.T, w.Write(resp)) |
| 52 | + return |
| 53 | + } |
| 54 | + ml.T.Logf("search base dn: %s", m.BaseDN) |
| 55 | + ml.T.Logf("search scope: %d", m.Scope) |
| 56 | + ml.T.Logf("search filter: %s", m.Filter) |
| 57 | + |
| 58 | + entries := ml.SearchEntries[m.Filter] |
| 59 | + for _, entry := range entries { |
| 60 | + ldapEntry := r.NewSearchResponseEntry(entry.dn, entry.options...) |
| 61 | + require.NoError(ml.T, w.Write(ldapEntry)) |
| 62 | + } |
| 63 | + |
| 64 | + resp.SetResultCode(gldap.ResultSuccess) |
| 65 | + require.NoError(ml.T, w.Write(resp)) |
| 66 | +} |
| 67 | + |
| 68 | +func (ml *MockLDAP) Run(address string) { |
| 69 | + s, err := gldap.NewServer() |
| 70 | + if err != nil { |
| 71 | + ml.T.Fatalf("unable to create server: %s", err.Error()) |
| 72 | + } |
| 73 | + |
| 74 | + // create a router and add a bind handler |
| 75 | + r, err := gldap.NewMux() |
| 76 | + if err != nil { |
| 77 | + ml.T.Fatalf("unable to create router: %s", err.Error()) |
| 78 | + } |
| 79 | + require.NoError(ml.T, r.Bind(ml.HandleBind)) |
| 80 | + require.NoError(ml.T, r.Search(ml.HandleSearch)) |
| 81 | + require.NoError(ml.T, s.Router(r)) |
| 82 | + go func() { |
| 83 | + require.NoError(ml.T, s.Run(address)) |
| 84 | + }() |
| 85 | + ml.T.Cleanup(func() { |
| 86 | + require.NoError(ml.T, s.Stop()) |
| 87 | + }) |
| 88 | + |
| 89 | + for !s.Ready() { |
| 90 | + ml.T.Log("Waiting for server to start") |
| 91 | + time.Sleep(100 * time.Millisecond) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestLDAPInjectionFilter(tt *testing.T) { |
| 96 | + tt.Parallel() |
| 97 | + t := tests.NewT(tt) |
| 98 | + |
| 99 | + ldapPort := t.Port("ldap") |
| 100 | + |
| 101 | + ldapSrv := &MockLDAP{ |
| 102 | + T: tt, |
| 103 | + AllowedBinds: map[string]string{ |
| 104 | + "DC=com,CN=bob": "bob_pass", |
| 105 | + "DC=com,CN=alice": "alice_pass", |
| 106 | + }, |
| 107 | + SearchEntries: map[string][]searchEntry{ |
| 108 | + "(&(objectClass=inetOrgPerson)(uid=alice))": { |
| 109 | + { |
| 110 | + dn: "DC=com,CN=alice", |
| 111 | + options: []gldap.Option{ |
| 112 | + gldap.WithAttributes(map[string][]string{ |
| 113 | + "objectClass": {"inetOrgPerson"}, |
| 114 | + "uid": {"alice"}, |
| 115 | + "description": {"prefix_test"}, |
| 116 | + }), |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + "(&(objectClass=inetOrgPerson)(uid=bob))": { |
| 121 | + { |
| 122 | + dn: "DC=com,CN=bob", |
| 123 | + options: []gldap.Option{ |
| 124 | + gldap.WithAttributes(map[string][]string{ |
| 125 | + "objectClass": {"inetOrgPerson"}, |
| 126 | + "uid": {"bob"}, |
| 127 | + "description": {"prefix_test"}, |
| 128 | + }), |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + "(&(objectClass=inetOrgPerson)(uid=bob)(description=prefix*))": { |
| 133 | + { |
| 134 | + dn: "DC=com,CN=bob", |
| 135 | + options: []gldap.Option{ |
| 136 | + gldap.WithAttributes(map[string][]string{ |
| 137 | + "objectClass": {"inetOrgPerson"}, |
| 138 | + "uid": {"bob"}, |
| 139 | + "description": {"prefix_test"}, |
| 140 | + }), |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + } |
| 146 | + ldapSrv.Run(":" + strconv.Itoa(int(ldapPort))) |
| 147 | + |
| 148 | + t.Port("smtp") |
| 149 | + t.DNS(nil) |
| 150 | + t.Config(` |
| 151 | + hostname mx.maddy.test |
| 152 | + tls off |
| 153 | +
|
| 154 | + auth.ldap ldap_auth { |
| 155 | + urls ldap://127.0.0.1:{env:TEST_PORT_ldap} |
| 156 | + bind plain "DC=com,CN=bob" "bob_pass" |
| 157 | + base_dn "DC=com" |
| 158 | + filter "(&(objectClass=inetOrgPerson)(uid={username}))" |
| 159 | + } |
| 160 | +
|
| 161 | + submission tcp://0.0.0.0:{env:TEST_PORT_smtp} { |
| 162 | + auth &ldap_auth |
| 163 | + deliver_to dummy |
| 164 | + } |
| 165 | + `) |
| 166 | + t.Run(1) |
| 167 | + defer t.Close() |
| 168 | + |
| 169 | + smtpConn := t.Conn("smtp") |
| 170 | + defer smtpConn.MustClose() |
| 171 | + smtpConn.SMTPNegotation("clieht.maddy.test", nil, nil) |
| 172 | + smtpConn.SMTPPlainAuth("alice", "alice_pass", true) |
| 173 | + |
| 174 | + smtpConn2 := t.Conn("smtp") |
| 175 | + defer smtpConn2.MustClose() |
| 176 | + smtpConn2.SMTPNegotation("clieht.maddy.test", nil, nil) |
| 177 | + smtpConn2.SMTPPlainAuth("bob)(description=prefix*", "bob_pass", false) |
| 178 | +} |
0 commit comments