Skip to content

Commit f784e19

Browse files
James William Dumaylanwen
authored andcommitted
GitHubWebHookCrumbExclusion should be more forgiving if the user leaves off the trailing slash (jenkinsci#152)
1 parent d7e3bee commit f784e19

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

src/main/java/com/cloudbees/jenkins/GitHubWebHookCrumbExclusion.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99
import javax.servlet.http.HttpServletResponse;
1010
import java.io.IOException;
1111

12+
import static org.apache.commons.lang3.StringUtils.isEmpty;
13+
1214
@Extension
1315
public class GitHubWebHookCrumbExclusion extends CrumbExclusion {
1416

1517
@Override
1618
public boolean process(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
1719
throws IOException, ServletException {
1820
String pathInfo = req.getPathInfo();
19-
if (pathInfo != null && pathInfo.equals(getExclusionPath())) {
20-
chain.doFilter(req, resp);
21-
return true;
21+
if (isEmpty(pathInfo)) {
22+
return false;
23+
}
24+
// Github will not follow redirects https://github.com/isaacs/github/issues/574
25+
pathInfo = pathInfo.endsWith("/") ? pathInfo : pathInfo + '/';
26+
if (!pathInfo.equals(getExclusionPath())) {
27+
return false;
2228
}
23-
return false;
29+
chain.doFilter(req, resp);
30+
return true;
2431
}
2532

2633
public String getExclusionPath() {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.cloudbees.jenkins;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import javax.servlet.FilterChain;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import static junit.framework.Assert.assertFalse;
11+
import static junit.framework.TestCase.assertTrue;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.never;
14+
import static org.mockito.Mockito.times;
15+
import static org.mockito.Mockito.verify;
16+
import static org.mockito.Mockito.when;
17+
18+
public class GitHubWebHookCrumbExclusionTest {
19+
20+
private GitHubWebHookCrumbExclusion exclusion;
21+
private HttpServletRequest req;
22+
private HttpServletResponse resp;
23+
private FilterChain chain;
24+
25+
@Before
26+
public void before() {
27+
exclusion = new GitHubWebHookCrumbExclusion();
28+
req = mock(HttpServletRequest.class);
29+
resp = mock(HttpServletResponse.class);
30+
chain = mock(FilterChain.class);
31+
}
32+
33+
@Test
34+
public void testFullPath() throws Exception {
35+
when(req.getPathInfo()).thenReturn("/github-webhook/");
36+
assertTrue(exclusion.process(req, resp, chain));
37+
verify(chain, times(1)).doFilter(req, resp);
38+
}
39+
40+
@Test
41+
public void testFullPathWithoutSlash() throws Exception {
42+
when(req.getPathInfo()).thenReturn("/github-webhook");
43+
assertTrue(exclusion.process(req, resp, chain));
44+
verify(chain, times(1)).doFilter(req, resp);
45+
}
46+
47+
@Test
48+
public void testInvalidPath() throws Exception {
49+
when(req.getPathInfo()).thenReturn("/some-other-url/");
50+
assertFalse(exclusion.process(req, resp, chain));
51+
verify(chain, never()).doFilter(req, resp);
52+
}
53+
54+
@Test
55+
public void testNullPath() throws Exception {
56+
when(req.getPathInfo()).thenReturn(null);
57+
assertFalse(exclusion.process(req, resp, chain));
58+
verify(chain, never()).doFilter(req, resp);
59+
}
60+
61+
@Test
62+
public void testEmptyPath() throws Exception {
63+
when(req.getPathInfo()).thenReturn("");
64+
assertFalse(exclusion.process(req, resp, chain));
65+
verify(chain, never()).doFilter(req, resp);
66+
}
67+
}

0 commit comments

Comments
 (0)