Skip to content

Commit bc2c212

Browse files
committed
(docker-based-examples) coder543#9 Updated example to use filter escaping, and
to protect against timing attacks for user discovery
1 parent 00d361d commit bc2c212

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "simple_bind"
3+
version = "0.1.0"
4+
authors = ["Mathias Myrland <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
clap = {git="https://github.com/clap-rs/clap.git"}
11+
openldap = { path = "../../" }

examples/simple_bind_authentication/src/main.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ fn do_simple_bind(
6262
}
6363

6464
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+
6568
// Show all DNs matching the description "Human"
6669
// ldap_search is a powerful query language, look at
6770
// https://confluence.atlassian.com/kb/how-to-write-ldap-search-filters-792496933.html
6871
// for an overview
6972
//
7073
// This particular filter allows the user to sign in with either
7174
// uid or email
72-
let filter = format!("(|(uid={})(mail={}))", who, who);
75+
let filter = format!("(|(uid={})(mail={}))", safe_who, safe_who);
7376

7477
match ldap.ldap_search(
7578
"ou=people,dc=planetexpress,dc=com",
@@ -122,11 +125,15 @@ fn main() {
122125
// In our test scenario, the professor is the manager.
123126
do_simple_bind(&ldap, ldap_manager_dn, ldap_manager_pass).unwrap();
124127

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+
};
129132

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"),
131138
}
132139
}

0 commit comments

Comments
 (0)