|
| 1 | +package jenkins.security; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.hasItem; |
| 5 | +import static org.hamcrest.Matchers.hasProperty; |
| 6 | +import static org.hamcrest.Matchers.is; |
| 7 | +import static org.hamcrest.Matchers.not; |
| 8 | +import static org.hamcrest.Matchers.startsWith; |
| 9 | +import static org.hamcrest.xml.HasXPath.hasXPath; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | + |
| 12 | +import java.net.URL; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import org.htmlunit.HttpMethod; |
| 16 | +import org.htmlunit.WebRequest; |
| 17 | +import org.htmlunit.WebResponse; |
| 18 | +import org.htmlunit.html.HtmlForm; |
| 19 | +import org.htmlunit.html.HtmlFormUtil; |
| 20 | +import org.htmlunit.html.HtmlPage; |
| 21 | +import org.htmlunit.util.NameValuePair; |
| 22 | +import org.htmlunit.xml.XmlPage; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.jvnet.hudson.test.Issue; |
| 26 | +import org.jvnet.hudson.test.JenkinsRule; |
| 27 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 28 | + |
| 29 | +@Issue("SECURITY-1166") |
| 30 | +@WithJenkins |
| 31 | +class Security1166Test { |
| 32 | + |
| 33 | + private JenkinsRule j; |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + void setUp(JenkinsRule rule) { |
| 37 | + j = rule; |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void loginRequestFailWithNoCrumb() throws Exception { |
| 42 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 43 | + |
| 44 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 45 | + wc.goTo(""); // to trigger the creation of a session |
| 46 | + |
| 47 | + WebRequest request = new WebRequest(new URL(j.getURL(), "j_spring_security_check"), HttpMethod.POST); |
| 48 | + List<NameValuePair> params = new ArrayList<>(); |
| 49 | + params.add(new NameValuePair("j_username", "alice")); |
| 50 | + params.add(new NameValuePair("j_password", "alice")); |
| 51 | + |
| 52 | + request.setRequestParameters(params); |
| 53 | + |
| 54 | + WebResponse response = wc.getPage(request).getWebResponse(); |
| 55 | + assertEquals(401, response.getStatusCode()); |
| 56 | + assertUserNotConnected(wc, "alice"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void loginRequestFailWithInvalidCrumb() throws Exception { |
| 62 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 63 | + |
| 64 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 65 | + wc.goTo(""); // to trigger the creation of a session |
| 66 | + |
| 67 | + WebRequest request = new WebRequest(new URL(j.getURL(), "j_spring_security_check"), HttpMethod.POST); |
| 68 | + List<NameValuePair> params = new ArrayList<>(); |
| 69 | + params.add(new NameValuePair("j_username", "alice")); |
| 70 | + params.add(new NameValuePair("j_password", "alice")); |
| 71 | + |
| 72 | + String crumbField = j.jenkins.getCrumbIssuer().getCrumbRequestField(); |
| 73 | + params.add(new NameValuePair(crumbField, "invalid-crumb")); |
| 74 | + |
| 75 | + request.setRequestParameters(params); |
| 76 | + |
| 77 | + WebResponse response = wc.getPage(request).getWebResponse(); |
| 78 | + assertEquals(401, response.getStatusCode()); |
| 79 | + assertUserNotConnected(wc, "alice"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void loginRequestSucceedWithValidCrumb() throws Exception { |
| 85 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 86 | + |
| 87 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 88 | + wc.goTo(""); // to trigger the creation of a session |
| 89 | + |
| 90 | + WebRequest request = new WebRequest(new URL(j.getURL(), "j_spring_security_check"), HttpMethod.POST); |
| 91 | + List<NameValuePair> params = new ArrayList<>(); |
| 92 | + params.add(new NameValuePair("j_username", "alice")); |
| 93 | + params.add(new NameValuePair("j_password", "alice")); |
| 94 | + |
| 95 | + String crumbField = j.jenkins.getCrumbIssuer().getCrumbRequestField(); |
| 96 | + String crumbValue = j.jenkins.getCrumbIssuer().getCrumb(); |
| 97 | + params.add(new NameValuePair(crumbField, crumbValue)); |
| 98 | + |
| 99 | + request.setRequestParameters(params); |
| 100 | + |
| 101 | + WebResponse response = wc.getPage(request).getWebResponse(); |
| 102 | + assertEquals(200, response.getStatusCode()); |
| 103 | + assertUserConnected(wc, "alice"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void loginRequestSucceedWithCrumbInHeader() throws Exception { |
| 109 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 110 | + |
| 111 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 112 | + wc.goTo(""); // to trigger the creation of a session |
| 113 | + |
| 114 | + WebRequest request = new WebRequest(new URL(j.getURL(), "j_spring_security_check"), HttpMethod.POST); |
| 115 | + List<NameValuePair> params = new ArrayList<>(); |
| 116 | + params.add(new NameValuePair("j_username", "alice")); |
| 117 | + params.add(new NameValuePair("j_password", "alice")); |
| 118 | + |
| 119 | + String crumbField = j.jenkins.getCrumbIssuer().getCrumbRequestField(); |
| 120 | + String crumbValue = j.jenkins.getCrumbIssuer().getCrumb(); |
| 121 | + request.setAdditionalHeader(crumbField, crumbValue); |
| 122 | + |
| 123 | + request.setRequestParameters(params); |
| 124 | + |
| 125 | + WebResponse response = wc.getPage(request).getWebResponse(); |
| 126 | + assertEquals(200, response.getStatusCode()); |
| 127 | + assertUserConnected(wc, "alice"); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void loginRequestSucceedWithNoCrumbIssuer() throws Exception { |
| 133 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 134 | + |
| 135 | + j.jenkins.setCrumbIssuer(null); |
| 136 | + |
| 137 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 138 | + wc.goTo(""); // to trigger the creation of a session |
| 139 | + |
| 140 | + WebRequest request = new WebRequest(new URL(j.getURL(), "j_spring_security_check"), HttpMethod.POST); |
| 141 | + List<NameValuePair> params = new ArrayList<>(); |
| 142 | + params.add(new NameValuePair("j_username", "alice")); |
| 143 | + params.add(new NameValuePair("j_password", "alice")); |
| 144 | + |
| 145 | + request.setRequestParameters(params); |
| 146 | + |
| 147 | + WebResponse response = wc.getPage(request).getWebResponse(); |
| 148 | + assertEquals(200, response.getStatusCode()); |
| 149 | + assertUserConnected(wc, "alice"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void loginRequestFailWithGET() throws Exception { |
| 155 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 156 | + |
| 157 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 158 | + wc.goTo(""); // to trigger the creation of a session |
| 159 | + wc.setRedirectEnabled(false); // disabling redirection to demonstrates that Spring did not handle the request |
| 160 | + |
| 161 | + WebRequest request = new WebRequest(new URL(j.getURL(), "j_spring_security_check"), HttpMethod.GET); |
| 162 | + List<NameValuePair> params = new ArrayList<>(); |
| 163 | + params.add(new NameValuePair("j_username", "alice")); |
| 164 | + params.add(new NameValuePair("j_password", "alice")); |
| 165 | + |
| 166 | + String crumbField = j.jenkins.getCrumbIssuer().getCrumbRequestField(); |
| 167 | + String crumbValue = j.jenkins.getCrumbIssuer().getCrumb(); |
| 168 | + params.add(new NameValuePair(crumbField, crumbValue)); |
| 169 | + |
| 170 | + request.setRequestParameters(params); |
| 171 | + |
| 172 | + WebResponse response = wc.getPage(request).getWebResponse(); |
| 173 | + assertEquals(404, response.getStatusCode()); |
| 174 | + assertThat(response.getResponseHeaders(), hasItem(hasProperty("name", startsWith("Stapler-Trace")))); |
| 175 | + assertUserNotConnected(wc, "alice"); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void loginThroughSetupWizard() throws Exception { |
| 181 | + j.jenkins.setInstallState(jenkins.install.InstallState.INITIAL_SECURITY_SETUP); |
| 182 | + |
| 183 | + String initialAdminPassword = j.jenkins.getSetupWizard().getInitialAdminPasswordFile().readToString().trim(); |
| 184 | + |
| 185 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false)) { |
| 186 | + HtmlPage page = wc.goTo("login"); |
| 187 | + List<HtmlForm> forms = page.getForms(); |
| 188 | + HtmlForm form = forms.get(0); |
| 189 | + |
| 190 | + assertEquals(1, forms.size()); // It's the only form, which doesn't have a name or an id. |
| 191 | + |
| 192 | + form.getInputByName("j_password").setValue(initialAdminPassword); |
| 193 | + |
| 194 | + HtmlFormUtil.submit(form, null); |
| 195 | + |
| 196 | + assertUserConnected(wc, "admin"); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private void assertUserConnected(JenkinsRule.WebClient wc, String expectedUsername) throws Exception { |
| 201 | + XmlPage page = (XmlPage) wc.goTo("whoAmI/api/xml", "application/xml"); |
| 202 | + assertThat(page, hasXPath("//name", is(expectedUsername))); |
| 203 | + } |
| 204 | + |
| 205 | + private void assertUserNotConnected(JenkinsRule.WebClient wc, String notExpectedUsername) throws Exception { |
| 206 | + XmlPage page = (XmlPage) wc.goTo("whoAmI/api/xml", "application/xml"); |
| 207 | + assertThat(page, hasXPath("//name", not(is(notExpectedUsername)))); |
| 208 | + } |
| 209 | +} |
0 commit comments