From dec7f78a6c4f006139c4983ab3ecd0ddc55a6aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ugo=20M=C3=A9da?= Date: Mon, 6 Oct 2014 10:53:32 +0200 Subject: [PATCH] Add a CrumbExclusion for the /project/ path --- .../gitlabjenkins/GitLabWebHook.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java index b3de3f0..5175460 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java @@ -13,6 +13,7 @@ import hudson.plugins.git.GitSCM; import hudson.plugins.git.util.BuildData; import hudson.scm.SCM; import hudson.security.ACL; +import hudson.security.csrf.CrumbExclusion; import hudson.util.HttpResponses; import java.io.IOException; @@ -26,7 +27,10 @@ import java.util.ListIterator; import java.util.logging.Level; import java.util.logging.Logger; +import javax.servlet.FilterChain; import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import jenkins.model.Jenkins; import net.sf.json.JSONObject; @@ -400,7 +404,7 @@ public class GitLabWebHook implements UnprotectedRootAction { * @param rsp The stapler response to write the output to. * @throws IOException */ - private void writeJSON(StaplerResponse rsp, JSONObject jsonObject) throws IOException { + private void writeJSON(StaplerResponse rsp, JSONObject jsonObject) throws IOException { rsp.setContentType("application/json"); PrintWriter w = rsp.getWriter(); @@ -415,4 +419,21 @@ public class GitLabWebHook implements UnprotectedRootAction { } + @Extension + public static class GitlabWebHookCrumbExclusion extends CrumbExclusion { + + @Override + public boolean process(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException { + String pathInfo = req.getPathInfo(); + if (pathInfo != null && pathInfo.startsWith(getExclusionPath())) { + chain.doFilter(req, resp); + return true; + } + return false; + } + + private String getExclusionPath() { + return '/' + WEBHOOK_URL + '/'; + } + } }