diff --git a/core/src/main/java/hudson/model/BuildAuthorizationToken.java b/core/src/main/java/hudson/model/BuildAuthorizationToken.java index 48aea15b08..f101eb3d6e 100644 --- a/core/src/main/java/hudson/model/BuildAuthorizationToken.java +++ b/core/src/main/java/hudson/model/BuildAuthorizationToken.java @@ -29,7 +29,6 @@ import hudson.Util; import hudson.security.ACL; import java.io.IOException; import javax.servlet.http.HttpServletResponse; -import jenkins.model.Jenkins; import jenkins.security.ApiTokenProperty; import org.kohsuke.stapler.HttpResponses; import org.kohsuke.stapler.StaplerRequest; @@ -68,9 +67,6 @@ public final class BuildAuthorizationToken { } public static void checkPermission(Job project, BuildAuthorizationToken token, StaplerRequest req, StaplerResponse rsp) throws IOException { - if (!Jenkins.get().isUseSecurity()) - return; // everyone is authorized - if (token != null && token.token != null) { //check the provided token String providedToken = req.getParameter("token"); diff --git a/test/src/test/java/hudson/model/AbstractProjectTest.java b/test/src/test/java/hudson/model/AbstractProjectTest.java index 0229069c07..d26f4d81f6 100644 --- a/test/src/test/java/hudson/model/AbstractProjectTest.java +++ b/test/src/test/java/hudson/model/AbstractProjectTest.java @@ -357,17 +357,23 @@ public class AbstractProjectTest { JenkinsRule.WebClient wc = j.createWebClient() .withThrowExceptionOnFailingStatusCode(false); - WebResponse rsp = wc.goTo(p.getUrl() + "build", null).getWebResponse(); + WebResponse rsp = wc.getPage(wc.addCrumb(new WebRequest(new URL(j.getURL(), p.getUrl() + + "build?delay=0"), + HttpMethod.POST))).getWebResponse(); assertEquals(HttpURLConnection.HTTP_CREATED, rsp.getStatusCode()); assertNotNull(rsp.getResponseHeaderValue("Location")); - WebResponse rsp2 = wc.goTo(p.getUrl() + "build", null).getWebResponse(); + WebResponse rsp2 = wc.getPage(wc.addCrumb(new WebRequest(new URL(j.getURL(), p.getUrl() + + "build?delay=0"), + HttpMethod.POST))).getWebResponse(); assertEquals(HttpURLConnection.HTTP_CREATED, rsp2.getStatusCode()); assertEquals(rsp.getResponseHeaderValue("Location"), rsp2.getResponseHeaderValue("Location")); p.makeDisabled(true); - WebResponse rsp3 = wc.goTo(p.getUrl() + "build", null).getWebResponse(); + WebResponse rsp3 = wc.getPage(wc.addCrumb(new WebRequest(new URL(j.getURL(), p.getUrl() + + "build?delay=0"), + HttpMethod.POST))).getWebResponse(); assertEquals(HttpURLConnection.HTTP_CONFLICT, rsp3.getStatusCode()); } diff --git a/test/src/test/java/hudson/model/BuildAuthorizationTokenSEC2558Test.java b/test/src/test/java/hudson/model/BuildAuthorizationTokenSEC2558Test.java new file mode 100644 index 0000000000..89a79cc7a9 --- /dev/null +++ b/test/src/test/java/hudson/model/BuildAuthorizationTokenSEC2558Test.java @@ -0,0 +1,70 @@ +package hudson.model; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.Assert.assertThrows; + +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.HttpMethod; +import com.gargoylesoftware.htmlunit.WebRequest; +import java.lang.reflect.Field; +import java.net.URL; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.JenkinsRule; + +// The existing @Before is annoying so this class is independent +// TODO : Using @Nested class could be more clean than using separate class +// TODO : Merge back into BuildAuthorizationTokenTest +public class BuildAuthorizationTokenSEC2558Test { + + @Rule + public JenkinsRule jr = new JenkinsRule(); + + private static final String token = "whatever"; + + @Test + @Issue("SECURITY-2558") + public void triggerJobWithoutSecurityRealm_ShouldSucceed_WithPost() throws Exception { + FreeStyleProject project = createFreestyleProjectWithToken(); + JenkinsRule.WebClient wc = jr.createWebClient(); + wc.getPage(wc.addCrumb(new WebRequest(new URL(jr.getURL(), project.getUrl() + + "build?delay=0"), + HttpMethod.POST))); + jr.waitUntilNoActivity(); + assertThat("the project should have been built", project.getBuilds(), hasSize(1)); + } + + @Test + @Issue("SECURITY-2558") + public void triggerJobWithoutSecurityRealm_ShouldFail_WithGet() throws Exception { + FreeStyleProject project = jr.createFreeStyleProject(); + JenkinsRule.WebClient wc = jr.createWebClient(); + FailingHttpStatusCodeException fex = assertThrows( + "should not reach here since only POST request can", + FailingHttpStatusCodeException.class, + () -> wc.getPage(new WebRequest(new URL(jr.getURL(), project.getUrl() + "build?delay=0"), HttpMethod.GET))); + assertThat("Should fail with method not allowed", fex.getStatusCode(), is(405)); + } + + @Test + @Issue("SECURITY-2558") + public void triggerJobWithoutSecurityRealm_ButWithToken_ShouldSucceed_WithGet() throws Exception { + FreeStyleProject project = createFreestyleProjectWithToken(); + JenkinsRule.WebClient wc = jr.createWebClient(); + wc.getPage(new WebRequest(new URL(jr.getURL(), project.getUrl() + "build?delay=0&token=" + token), + HttpMethod.GET)); + jr.waitUntilNoActivity(); + assertThat("the project should have been built", project.getBuilds(), hasSize(1)); + } + + private FreeStyleProject createFreestyleProjectWithToken() throws Exception { + FreeStyleProject fsp = jr.createFreeStyleProject(); + Field f = AbstractProject.class.getDeclaredField("authToken"); + f.setAccessible(true); + f.set(fsp, new BuildAuthorizationToken(token)); + return fsp; + } +} diff --git a/test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java b/test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java index 5d5db2f0a3..0ef25291bd 100644 --- a/test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java +++ b/test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java @@ -24,10 +24,18 @@ package jenkins.model; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; + +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.HttpMethod; +import com.gargoylesoftware.htmlunit.WebRequest; import hudson.model.FreeStyleProject; import hudson.model.ParametersDefinitionProperty; import hudson.model.Queue; import hudson.model.StringParameterDefinition; +import java.net.URL; import javax.servlet.http.HttpServletResponse; import org.junit.Assert; import org.junit.Rule; @@ -61,7 +69,12 @@ public class ParameterizedJobMixInTest { project.doDisable(); final JenkinsRule.WebClient webClient = j.createWebClient(); - webClient.assertFails(project.getUrl() + "buildWithParameters", HttpServletResponse.SC_CONFLICT); + + FailingHttpStatusCodeException fex = assertThrows( + "should fail when invoking disabled project", + FailingHttpStatusCodeException.class, + () -> webClient.getPage(webClient.addCrumb(new WebRequest(new URL(j.getURL(), project.getUrl() + "build?delay=0"), HttpMethod.POST)))); + assertThat("Should fail with conflict", fex.getStatusCode(), is(409)); } @Test @@ -73,7 +86,7 @@ public class ParameterizedJobMixInTest { project.setQuietPeriod(projectQuietPeriodInSeconds); final JenkinsRule.WebClient webClient = j.createWebClient(); - webClient.goTo(project.getUrl() + "build", ""); + webClient.getPage(webClient.addCrumb(new WebRequest(new URL(j.getURL(), project.getUrl() + "build"), HttpMethod.POST))); long triggerTime = System.currentTimeMillis(); Queue.Item item = Jenkins.get().getQueue().getItem(1);