Skip to content
Permalink
Browse files

Merge pull request #3187 from daniel-beck/JENKINS-34254-v2

[JENKINS-34254] Fix RequirePOST form
  • Loading branch information...
daniel-beck committed Dec 16, 2017
2 parents 814d202 + f0efdba commit 76c9f8beacc681663571c925b5ee090222407e34
@@ -39,7 +39,7 @@ THE SOFTWARE.

<properties>
<staplerFork>true</staplerFork>
<stapler.version>1.253</stapler.version>
<stapler.version>1.254</stapler.version>
<spring.version>2.5.6.SEC03</spring.version>
<groovy.version>2.4.11</groovy.version>
<!-- TODO: Actually many issues are being filtered by src/findbugs/findbugs-excludes.xml -->
@@ -8,6 +8,11 @@
import hudson.util.MultipartFormDataParser;
import jenkins.model.Jenkins;
import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
import org.kohsuke.MetaInfServices;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.ForwardToView;
import org.kohsuke.stapler.interceptor.RequirePOST;

import java.io.IOException;
import java.util.Enumeration;
@@ -41,6 +46,15 @@ public CrumbIssuer getCrumbIssuer() {
return h.getCrumbIssuer();
}

@Restricted(NoExternalUse.class)
@MetaInfServices
public static class ErrorCustomizer implements RequirePOST.ErrorCustomizer {
@Override
public ForwardToView getForwardView() {
return new ForwardToView(CrumbFilter.class, "retry");
}
}

public void init(FilterConfig filterConfig) throws ServletException {
}

@@ -0,0 +1,42 @@
<!--
The MIT License
Copyright (c) 2017, Daniel Beck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<st:statusCode value="405"/>
<l:layout norefresh="true" title="${%Method Not Allowed}">

<l:main-panel>
<h1>${%This URL requires POST}</h1>
<p>
${%blurb}
</p>
<p><tt>${requestURL}</tt></p>
<p><strong>${%warning}</strong></p>
<f:form method="post" name="retry">
<f:submit value="${%Retry using POST}"/>
</f:form>
</l:main-panel>
</l:layout>
</j:jelly>
@@ -0,0 +1,4 @@
blurb = The URL you're trying to access requires that requests be sent using POST (like a form submission). \
The button below allows you to retry accessing this URL using POST. \
URL being accessed:
warning = If you were sent here from an untrusted source, please proceed with caution.
@@ -8,6 +8,8 @@

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import jenkins.model.Jenkins;
import junit.framework.Assert;
import net.sf.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
@@ -135,4 +137,19 @@
wc.assertFails("crumbIssuer/api/json?jsonp=hack", HttpURLConnection.HTTP_FORBIDDEN);
}

@Issue("JENKINS-34254")
@Test public void testRequirePostErrorPageCrumb() throws Exception {
Jenkins.getInstance().setCrumbIssuer(new DefaultCrumbIssuer(false));
WebClient wc = r.createWebClient();
try {
wc.goTo("quietDown");
fail("expected failure");
} catch (FailingHttpStatusCodeException ex) {
Assert.assertEquals("expect HTTP 405 method not allowed", 405, ex.getStatusCode());
}
HtmlPage retry = (HtmlPage) wc.getCurrentWindow().getEnclosedPage();
HtmlPage success = r.submit(retry.getFormByName("retry"));
Assert.assertTrue("quieting down", r.jenkins.isQuietingDown());
}

}

0 comments on commit 76c9f8b

Please sign in to comment.
You can’t perform that action at this time.