Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #491 from liskin/JENKINS-34350-notifycommit-csrf
[JENKINS-34350] Fix POST to /git/notifyCommit with CSRF protection on
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/hudson/plugins/git/GitStatusCrumbExclusion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package hudson.plugins.git; | ||
|
||
import hudson.Extension; | ||
import hudson.security.csrf.CrumbExclusion; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Make POST to /git/notifyCommit work with CSRF protection on. | ||
*/ | ||
@Extension | ||
public class GitStatusCrumbExclusion extends CrumbExclusion { | ||
|
||
@Override | ||
public boolean process(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) | ||
throws IOException, ServletException { | ||
String pathInfo = req.getPathInfo(); | ||
if (pathInfo != null && pathInfo.equals(getExclusionPath())) { | ||
chain.doFilter(req, resp); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public String getExclusionPath() { | ||
return "/git/notifyCommit"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/hudson/plugins/git/GitStatusCrumbExclusionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package hudson.plugins.git; | ||
|
||
import hudson.security.csrf.CrumbFilter; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.mockito.Matchers.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class GitStatusCrumbExclusionTest { | ||
|
||
@Rule | ||
public JenkinsRule r = new JenkinsRule(); | ||
|
||
private CrumbFilter filter; | ||
private HttpServletRequest req; | ||
private HttpServletResponse resp; | ||
private FilterChain chain; | ||
|
||
@Before | ||
public void before() { | ||
filter = new CrumbFilter(); | ||
req = mock(HttpServletRequest.class); | ||
resp = mock(HttpServletResponse.class); | ||
chain = mock(FilterChain.class); | ||
} | ||
|
||
@Test | ||
public void testNotifyCommit() throws Exception { | ||
when(req.getPathInfo()).thenReturn("/git/notifyCommit"); | ||
when(req.getMethod()).thenReturn("POST"); | ||
when(req.getParameterNames()).thenReturn(Collections.<String>emptyEnumeration()); | ||
filter.doFilter(req, resp, chain); | ||
verify(resp, never()).sendError(anyInt(), anyString()); | ||
} | ||
|
||
@Test | ||
public void testInvalidPath() throws Exception { | ||
when(req.getPathInfo()).thenReturn("/git/somethingElse"); | ||
when(req.getMethod()).thenReturn("POST"); | ||
when(req.getParameterNames()).thenReturn(Collections.<String>emptyEnumeration()); | ||
filter.doFilter(req, resp, chain); | ||
verify(resp, times(1)).sendError(anyInt(), anyString()); | ||
} | ||
} |