@@ -62,14 +62,17 @@ fn do_simple_bind(
62
62
}
63
63
64
64
fn ldap_dn_lookup ( ldap : & RustLDAP , who : & str ) -> Result < String , LDAPError > {
65
+ // First, escape the who parameter to prevent LDAP injection attacks
66
+ let safe_who = escape_filter_assertion_value ( who) ?;
67
+
65
68
// Show all DNs matching the description "Human"
66
69
// ldap_search is a powerful query language, look at
67
70
// https://confluence.atlassian.com/kb/how-to-write-ldap-search-filters-792496933.html
68
71
// for an overview
69
72
//
70
73
// This particular filter allows the user to sign in with either
71
74
// uid or email
72
- let filter = format ! ( "(|(uid={})(mail={}))" , who , who ) ;
75
+ let filter = format ! ( "(|(uid={})(mail={}))" , safe_who , safe_who ) ;
73
76
74
77
match ldap. ldap_search (
75
78
"ou=people,dc=planetexpress,dc=com" ,
@@ -122,11 +125,15 @@ fn main() {
122
125
// In our test scenario, the professor is the manager.
123
126
do_simple_bind ( & ldap, ldap_manager_dn, ldap_manager_pass) . unwrap ( ) ;
124
127
125
- if let Ok ( fry_dn ) = ldap_dn_lookup ( & ldap, user_to_authenticate. as_str ( ) ) {
126
- // Now, perform a bind with the DN we found matching the user attempting to sign in
127
- // and the password provided in the authentication request
128
- do_simple_bind ( & ldap , fry_dn . as_str ( ) , pwd_to_authenticate . as_str ( ) ) . unwrap ( ) ;
128
+ let ( dn , passwd , valid ) = match ldap_dn_lookup ( & ldap, user_to_authenticate. as_str ( ) ) {
129
+ Ok ( fry_dn ) => ( fry_dn , pwd_to_authenticate , true ) ,
130
+ _ => ( "" . into ( ) , "" . into ( ) , false ) ,
131
+ } ;
129
132
130
- println ! ( "Successfully signed in as fry" ) ;
133
+ // We do the simple bind regardless of the user existence, to protect against timing attacks
134
+ // to probe existing users
135
+ match do_simple_bind ( & ldap, dn. as_str ( ) , passwd. as_str ( ) ) . is_ok ( ) && valid {
136
+ true => println ! ( "Successfully signed in as fry" ) ,
137
+ false => println ! ( "Could not log in" ) ,
131
138
}
132
139
}
0 commit comments