Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SECURITY-1025 #11

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/hudson/plugins/batch_task/BatchRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.framework.io.LargeText;
import org.kohsuke.stapler.verb.POST;

import java.io.File;
import java.io.FileOutputStream;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hudson/plugins/batch_task/BatchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.regex.Pattern;

import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
import org.kohsuke.stapler.verb.POST;

/**
* A batch task.
Expand Down Expand Up @@ -264,6 +265,7 @@ public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp)
/**
* Schedules the execution
*/
@POST

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With addition of these annotations UI buttons seem to be broken.
I've create a dummy freestyle job and added 2 batch tasks to it. Then when I go to JENKINS/job/JOB_NAME/batchTasks/task/TASK_NAME/ and click "Build Now" I get a 404. Same with "Delete Task".

I suspect that culprits are https://github.com/jenkinsci/batch-task-plugin/blob/master/src/main/resources/hudson/plugins/batch_task/BatchTask/sidepanel.jelly#L18 and https://github.com/jenkinsci/batch-task-plugin/blob/master/src/main/resources/hudson/plugins/batch_task/BatchTask/delete.jelly#L9C11-L9C23

For l:task there exists a post attribute, so the fix should be straightforward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect. Note that I removed the delete.jelly as it does no seem to be used.

I am still able to delete tasks without it.

public synchronized void doExecute( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
getACL().checkPermission(AbstractProject.BUILD);

Expand All @@ -278,6 +280,7 @@ public synchronized void doExecute( StaplerRequest req, StaplerResponse rsp ) th
/**
* Deletes this task.
*/
@POST
public synchronized void doDoDelete(StaplerResponse rsp) throws IOException, ServletException {
getACL().checkPermission(AbstractProject.DELETE);

Expand Down Expand Up @@ -319,6 +322,7 @@ private int[] parse(String num) {

private static final Pattern BUILD_NUMBER_PATTERN = Pattern.compile("(\\d+)-(\\d+)");

@POST
public void doCancelQueue(StaplerRequest req, StaplerResponse rsp)
throws IOException, ServletException {
checkAbortPermission();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<l:task icon="icon-previous icon-md" href="../../" title="${%Back to Task List}" />
<j:if test="${it.hasBuildPermission()}">
<j:if test="${!it.disabled}">
<l:task icon="icon-clock icon-md" href="${url}/execute" title="${%Build Now}" />
<l:task icon="icon-clock icon-md" href="${url}/execute" title="${%Build Now}" post="true" />
</j:if>
</j:if>
<j:if test="${it.hasDeletePermission()}">
<l:task icon="icon-edit-delete icon-md" href="${url}/delete" title="${%Delete Task}" />
<l:task icon="icon-edit-delete icon-md" href="${url}/doDelete" post="true" requiresConfirmation="true" title="${%Delete Task}" />
</j:if>
<!-- Configure doesn't seem to lead anywhere -->
<!--j:if test="${it.hasConfigurePermission()}">
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/hudson/plugins/batch_task/BatchTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testNoBuilds() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new BatchTaskProperty(new BatchTask("test", "echo hello")));
JenkinsRule.WebClient wc = r.createWebClient();
HtmlPage page = wc.getPage(p, "batchTasks/task/test/execute");
HtmlPage page = TestHelper.assertPost(wc, p.getUrl() + "batchTasks/task/test/execute", "text/html", 200);
String path = page.getWebResponse().getWebRequest().getUrl().getPath();
assertTrue("should redirect to noBuilds page: " + path, path.endsWith("/noBuild"));
}
Expand All @@ -72,7 +72,6 @@ public void testNoBuilds() throws Exception {
*/
@Test
public void testExecute() throws Exception {

r.jenkins.getGlobalNodeProperties().add(new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry("GLOBAL", "global-property"),
new EnvironmentVariablesNodeProperty.Entry("OVERRIDE_ME", "foo")));
Expand All @@ -93,7 +92,7 @@ public void testExecute() throws Exception {
while (freeStyleBuild.isBuilding()) {
Thread.sleep(100);
}
r.createWebClient().getPage(p, "batchTasks/task/test/execute");
TestHelper.assertPost(r.createWebClient(), p.getUrl() + "batchTasks/task/test/execute", "text/html", 200);
BatchRun run = task.getLastRun();
assertNotNull("task did not run", run);
CauseAction ca = run.getAction(CauseAction.class);
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/hudson/plugins/batch_task/TestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hudson.plugins.batch_task;

import org.htmlunit.FailingHttpStatusCodeException;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebRequest;
import org.htmlunit.html.HtmlPage;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;

public class TestHelper {
/**
* Performs an HTTP POST request to the relative url.
*
* @param webClient the client
* @param relative the url relative to the context path
* @param expectedContentType if expecting specific content type or null if not
* @param expectedStatus if expecting a http status code or null if not
* @throws IOException if so
*/
public static HtmlPage assertPost(JenkinsRule.WebClient webClient, String relative,
String expectedContentType, Integer expectedStatus) throws IOException {
WebRequest request = new WebRequest(webClient.createCrumbedUrl(relative), HttpMethod.POST);
try {
HtmlPage p = webClient.getPage(request);
if (expectedContentType != null) {
assertThat(p.getWebResponse().getContentType(), is(expectedContentType));
}
if (expectedStatus != null) {
assertEquals(expectedStatus.intValue(), p.getWebResponse().getStatusCode());
}
return p;
} catch (FailingHttpStatusCodeException e) {
if (expectedStatus != null) {
assertEquals(expectedStatus.intValue(), e.getStatusCode());
return null;
} else {
throw e;
}
}
}
}