summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2012-04-25 16:35:40 -0400
committerJames Moger <james.moger@gitblit.com>2012-04-25 16:35:40 -0400
commitd2426e1eb5d07664b5c26c4247fae3325282d60d (patch)
tree65d293881c43353d27156928bbc8e9b784824af9 /tests
parent6e15cb51ddcf24c725633c4ab1ff71959b036eb4 (diff)
parent7e0ce40e79a193426e8f7b6f22407422634dafe3 (diff)
downloadgitblit-d2426e1eb5d07664b5c26c4247fae3325282d60d.tar.gz
gitblit-d2426e1eb5d07664b5c26c4247fae3325282d60d.zip
Merge jcrygier's LDAP injection defense and displayname/email retrieval
Add LDAP logic to retrieve display name & email address Add code / test to defend against LDAP injection attacks.
Diffstat (limited to 'tests')
-rw-r--r--tests/com/gitblit/tests/LdapUserServiceTest.java64
-rw-r--r--tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif20
2 files changed, 77 insertions, 7 deletions
diff --git a/tests/com/gitblit/tests/LdapUserServiceTest.java b/tests/com/gitblit/tests/LdapUserServiceTest.java
index 48c97416..390fa001 100644
--- a/tests/com/gitblit/tests/LdapUserServiceTest.java
+++ b/tests/com/gitblit/tests/LdapUserServiceTest.java
@@ -16,6 +16,7 @@
*/
package com.gitblit.tests;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -24,6 +25,7 @@ import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
import com.gitblit.LdapUserService;
@@ -45,22 +47,27 @@ public class LdapUserServiceTest {
private LdapUserService ldapUserService;
- int ldapPort = 1389;
+ static int ldapPort = 1389;
- @Before
- public void createInMemoryLdapServer() throws Exception {
+ @BeforeClass
+ public static void createInMemoryLdapServer() throws Exception {
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=MyDomain");
config.addAdditionalBindCredentials("cn=Directory Manager", "password");
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("default", ldapPort));
config.setSchema(null);
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
- ds.importFromLDIF(true, new LDIFReader(this.getClass().getResourceAsStream("resources/ldapUserServiceSampleData.ldif")));
+ ds.importFromLDIF(true, new LDIFReader(LdapUserServiceTest.class.getResourceAsStream("resources/ldapUserServiceSampleData.ldif")));
ds.startListening();
}
@Before
public void createLdapUserService() {
+ ldapUserService = new LdapUserService();
+ ldapUserService.setup(getSettings());
+ }
+
+ private MemorySettings getSettings() {
Map<Object, Object> backingMap = new HashMap<Object, Object>();
backingMap.put("realm.ldap.server", "ldap://localhost:" + ldapPort);
backingMap.put("realm.ldap.domain", "");
@@ -73,11 +80,11 @@ public class LdapUserServiceTest {
backingMap.put("realm.ldap.groupBase", "OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain");
backingMap.put("realm.ldap.groupPattern", "(&(objectClass=group)(member=${dn}))");
backingMap.put("realm.ldap.admins", "UserThree @Git_Admins \"@Git Admins\"");
+ backingMap.put("realm.ldap.displayName", "displayName");
+ backingMap.put("realm.ldap.email", "email");
MemorySettings ms = new MemorySettings(backingMap);
-
- ldapUserService = new LdapUserService();
- ldapUserService.setup(ms);
+ return ms;
}
@Test
@@ -104,5 +111,48 @@ public class LdapUserServiceTest {
assertNull(userThreeModel.getTeam("git_admins"));
assertTrue(userThreeModel.canAdmin);
}
+
+ @Test
+ public void testDisplayName() {
+ UserModel userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
+ assertNotNull(userOneModel);
+ assertEquals("User One", userOneModel.displayName);
+
+ // Test more complicated scenarios - concat
+ MemorySettings ms = getSettings();
+ ms.put("realm.ldap.displayName", "${personalTitle}. ${givenName} ${surname}");
+ ldapUserService = new LdapUserService();
+ ldapUserService.setup(ms);
+
+ userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
+ assertNotNull(userOneModel);
+ assertEquals("Mr. User One", userOneModel.displayName);
+ }
+
+ @Test
+ public void testEmail() {
+ UserModel userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
+ assertNotNull(userOneModel);
+ assertEquals("userone@gitblit.com", userOneModel.emailAddress);
+
+ // Test more complicated scenarios - concat
+ MemorySettings ms = getSettings();
+ ms.put("realm.ldap.email", "${givenName}.${surname}@gitblit.com");
+ ldapUserService = new LdapUserService();
+ ldapUserService.setup(ms);
+
+ userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
+ assertNotNull(userOneModel);
+ assertEquals("User.One@gitblit.com", userOneModel.emailAddress);
+ }
+
+ @Test
+ public void testLdapInjection() {
+ // Inject so "(&(objectClass=person)(sAMAccountName=${username}))" becomes "(&(objectClass=person)(sAMAccountName=*)(userPassword=userOnePassword))"
+ // Thus searching by password
+
+ UserModel userOneModel = ldapUserService.authenticate("*)(userPassword=userOnePassword", "userOnePassword".toCharArray());
+ assertNull(userOneModel);
+ }
}
diff --git a/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif b/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif
index 84ee243e..df79333e 100644
--- a/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif
+++ b/tests/com/gitblit/tests/resources/ldapUserServiceSampleData.ldif
@@ -62,6 +62,11 @@ objectClass: user
objectClass: person
sAMAccountName: UserOne
userPassword: userOnePassword
+displayName: User One
+givenName: User
+surname: One
+personalTitle: Mr
+email: userone@gitblit.com
memberOf: CN=Git_Admins,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain
memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain
@@ -70,6 +75,11 @@ objectClass: user
objectClass: person
sAMAccountName: UserTwo
userPassword: userTwoPassword
+displayName: User Two
+givenName: User
+surname: Two
+personalTitle: Mr
+email: usertwo@gitblit.com
memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain
memberOf: CN=Git Admins,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain
@@ -78,6 +88,11 @@ objectClass: user
objectClass: person
sAMAccountName: UserThree
userPassword: userThreePassword
+displayName: User Three
+givenName: User
+surname: Three
+personalTitle: Mrs
+email: userthree@gitblit.com
memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain
dn: CN=UserFour,OU=Canada,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain
@@ -85,4 +100,9 @@ objectClass: user
objectClass: person
sAMAccountName: UserFour
userPassword: userFourPassword
+displayName: User Four
+givenName: User
+surname: Four
+personalTitle: Miss
+email: userfour@gitblit.com
memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain \ No newline at end of file