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

Crumb exclusion should be more forgiving if the user leaves off the trailing slash #152

Merged
merged 1 commit into from
Nov 16, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.apache.commons.lang3.StringUtils.isEmpty;

@Extension
public class GitHubWebHookCrumbExclusion 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;
if (isEmpty(pathInfo)) {
return false;
}
// Github will not follow redirects https://github.com/isaacs/github/issues/574
pathInfo = pathInfo.endsWith("/") ? pathInfo : pathInfo + '/';
if (!pathInfo.equals(getExclusionPath())) {
return false;
}
return false;
chain.doFilter(req, resp);
return true;
}

public String getExclusionPath() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.cloudbees.jenkins;

import org.junit.Before;
import org.junit.Test;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static junit.framework.Assert.assertFalse;
import static junit.framework.TestCase.assertTrue;
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;

public class GitHubWebHookCrumbExclusionTest {

private GitHubWebHookCrumbExclusion exclusion;
private HttpServletRequest req;
private HttpServletResponse resp;
private FilterChain chain;

@Before
public void before() {
exclusion = new GitHubWebHookCrumbExclusion();
req = mock(HttpServletRequest.class);
resp = mock(HttpServletResponse.class);
chain = mock(FilterChain.class);
}

@Test
public void testFullPath() throws Exception {
when(req.getPathInfo()).thenReturn("/github-webhook/");
assertTrue(exclusion.process(req, resp, chain));
verify(chain, times(1)).doFilter(req, resp);
}

@Test
public void testFullPathWithoutSlash() throws Exception {
when(req.getPathInfo()).thenReturn("/github-webhook");
assertTrue(exclusion.process(req, resp, chain));
verify(chain, times(1)).doFilter(req, resp);
}

@Test
public void testInvalidPath() throws Exception {
when(req.getPathInfo()).thenReturn("/some-other-url/");
assertFalse(exclusion.process(req, resp, chain));
verify(chain, never()).doFilter(req, resp);
}

@Test
public void testNullPath() throws Exception {
when(req.getPathInfo()).thenReturn(null);
assertFalse(exclusion.process(req, resp, chain));
verify(chain, never()).doFilter(req, resp);
}

@Test
public void testEmptyPath() throws Exception {
when(req.getPathInfo()).thenReturn("");
assertFalse(exclusion.process(req, resp, chain));
verify(chain, never()).doFilter(req, resp);
}
}