From b5c3764681f3b4ce83d0e78f6a9327925640d57e Mon Sep 17 00:00:00 2001 From: Kevin-CB Date: Tue, 21 Dec 2021 13:22:35 +0000 Subject: [PATCH] [SECURITY-2558] --- .../hudson/model/BuildAuthorizationToken.java | 4 -- .../hudson/model/AbstractProjectTest.java | 12 +++- .../BuildAuthorizationTokenSEC2558Test.java | 70 +++++++++++++++++++ .../model/ParameterizedJobMixInTest.java | 19 ++++- 4 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 test/src/test/java/hudson/model/BuildAuthorizationTokenSEC2558Test.java diff --git a/core/src/main/java/hudson/model/BuildAuthorizationToken.java b/core/src/main/java/hudson/model/BuildAuthorizationToken.java index 43563b6f46..5ab6283cfb 100644 --- a/core/src/main/java/hudson/model/BuildAuthorizationToken.java +++ b/core/src/main/java/hudson/model/BuildAuthorizationToken.java @@ -28,7 +28,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; @@ -67,9 +66,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 5457cdfc13..5d5858cf1a 100644 --- a/test/src/test/java/hudson/model/AbstractProjectTest.java +++ b/test/src/test/java/hudson/model/AbstractProjectTest.java @@ -355,17 +355,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..6b4e2fe66d --- /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 a5dccb9c8b..4f94a0b31a 100644 --- a/test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java +++ b/test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java @@ -23,10 +23,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; @@ -58,9 +66,14 @@ public class ParameterizedJobMixInTest { final FreeStyleProject project = j.createFreeStyleProject(); project.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("FOO", "BAR"))); 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 @@ -72,7 +85,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);