[SECURITY-243] Unit Test.

Unit test to show the issue where some form of user property is used for
obtaining the information needed to loging (api token/ user property)
This commit is contained in:
James Nord committed 2016-04-20 18:53:21 +01:00
1 parent d6bcfdac70
commit f4172ebb56
2 files changed
+167

No files matched your search

@@ -539,6 +539,51 @@ public class UserTest {
} catch (AccessDeniedException expected) { }
}
@Issue("SECURITY-243")
@Test
public void resolveByIdThenName() throws Exception{
j.jenkins.setSecurityRealm(new HudsonPrivateSecurityRealm(true, false, null));
User u1 = User.get("user1");
u1.setFullName("User One");
u1.save();
User u2 = User.get("user2");
u2.setFullName("User Two");
u2.save();
assertNotSame("Users should not have the same id.", u1.getId(), u2.getId());
User u = User.get("User One");
assertEquals("'User One' should resolve to u1", u1.getId(), u.getId());
u = User.get("User Two");
assertEquals("'User Two' should resolve to u2", u2.getId(), u.getId());
u = User.get("user1");
assertEquals("'user1' should resolve to u1", u1.getId(), u.getId());
u = User.get("user2");
assertEquals("'user2' should resolve to u2", u2.getId(), u.getId());
u1.setFullName("user2");
u1.save();
u = User.get("user2");
assertEquals("'user2' should resolve to u2", u2.getId(), u.getId());
u = User.get("user1");
assertEquals("'user1' should resolve to u1", u1.getId(), u.getId());
u1.setFullName("user1");
u1.save();
u2.setFullName("user1");
u2.save();
u = User.get("user1");
assertEquals("'user1' should resolve to u1", u1.getId(), u.getId());
u = User.get("user2");
assertEquals("'user2' should resolve to u2", u2.getId(), u.getId());
}
public static class SomeUserProperty extends UserProperty {
@TestExtension
@@ -5,6 +5,24 @@ import static hudson.security.HudsonPrivateSecurityRealm.PASSWORD_ENCODER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.xml.HasXPath.hasXPath;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.junit.Rule;
import org.junit.Test;
@@ -15,6 +33,11 @@ import org.jvnet.hudson.test.WithoutJenkins;
import org.jvnet.hudson.test.recipes.LocalData;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.xml.XmlPage;
import hudson.model.User;
import hudson.remoting.Base64;
import jenkins.security.ApiTokenProperty;
/**
* @author Kohsuke Kawaguchi
@@ -62,4 +85,103 @@ public class HudsonPrivateSecurityRealmTest {
assertFalse(secure.equals(old));
}
@Issue("SECURITY-243")
@Test
public void fullNameCollisionPassword() throws Exception {
HudsonPrivateSecurityRealm securityRealm = new HudsonPrivateSecurityRealm(false, false, null);
j.jenkins.setSecurityRealm(securityRealm);
User u1 = securityRealm.createAccount("user1", "password1");
u1.setFullName("User One");
u1.save();
User u2 = securityRealm.createAccount("user2", "password2");
u2.setFullName("User Two");
u2.save();
WebClient wc1 = j.createWebClient();
wc1.login("user1", "password1");
WebClient wc2 = j.createWebClient();
wc2.login("user2", "password2");
// Check both users can use their token
XmlPage w1 = (XmlPage) wc1.goTo("whoAmI/api/xml", "application/xml");
assertThat(w1, hasXPath("//name", is("user1")));
XmlPage w2 = (XmlPage) wc2.goTo("whoAmI/api/xml", "application/xml");
assertThat(w2, hasXPath("//name", is("user2")));
u1.setFullName("user2");
u1.save();
// check the tokens still work
wc1 = j.createWebClient();
wc1.login("user1", "password1");
wc2 = j.createWebClient();
// throws FailingHttpStatusCodeException on login failure
wc2.login("user2", "password2");
// belt and braces incase the failed login no longer throws exceptions.
w1 = (XmlPage) wc1.goTo("whoAmI/api/xml", "application/xml");
assertThat(w1, hasXPath("//name", is("user1")));
w2 = (XmlPage) wc2.goTo("whoAmI/api/xml", "application/xml");
assertThat(w2, hasXPath("//name", is("user2")));
}
@Issue("SECURITY-243")
@Test
public void fullNameCollisionToken() throws Exception {
HudsonPrivateSecurityRealm securityRealm = new HudsonPrivateSecurityRealm(false, false, null);
j.jenkins.setSecurityRealm(securityRealm);
User u1 = securityRealm.createAccount("user1", "password1");
u1.setFullName("User One");
u1.save();
String u1Token = u1.getProperty(ApiTokenProperty.class).getApiToken();
User u2 = securityRealm.createAccount("user2", "password2");
u2.setFullName("User Two");
u2.save();
String u2Token = u2.getProperty(ApiTokenProperty.class).getApiToken();
WebClient wc1 = j.createWebClient();
wc1.addRequestHeader("Authorization", basicHeader("user1", u1Token));
//wc1.setCredentialsProvider(new FixedCredentialsProvider("user1", u1Token));
WebClient wc2 = j.createWebClient();
wc2.addRequestHeader("Authorization", basicHeader("user2", u2Token));
//wc2.setCredentialsProvider(new FixedCredentialsProvider("user2", u1Token));
// Check both users can use their token
XmlPage w1 = (XmlPage) wc1.goTo("whoAmI/api/xml", "application/xml");
assertThat(w1, hasXPath("//name", is("user1")));
XmlPage w2 = (XmlPage) wc2.goTo("whoAmI/api/xml", "application/xml");
assertThat(w2, hasXPath("//name", is("user2")));
u1.setFullName("user2");
u1.save();
// check the tokens still work
w1 = (XmlPage) wc1.goTo("whoAmI/api/xml", "application/xml");
assertThat(w1, hasXPath("//name", is("user1")));
w2 = (XmlPage) wc2.goTo("whoAmI/api/xml", "application/xml");
assertThat(w2, hasXPath("//name", is("user2")));
}
private static final String basicHeader(String user, String pass) throws UnsupportedEncodingException {
String str = user +':' + pass;
String auth = Base64.encode(str.getBytes("US-ASCII"));
String authHeader = "Basic " + auth;
return authHeader;
}
}