Skip to content

Commit

Permalink
Merge pull request #201 from tomsun28/path-match-bug-fix
Browse files Browse the repository at this point in the history
[SHIRO-742]fix throw exception when request uri is /
  • Loading branch information
fpapon committed Feb 13, 2020
2 parents 46583f2 + f902982 commit 9762f97
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
Expand Up @@ -123,10 +123,12 @@ protected String getPathWithinApplication(ServletRequest request) {
*/
protected boolean pathsMatch(String path, ServletRequest request) {
String requestURI = getPathWithinApplication(request);
if (requestURI != null && requestURI.endsWith(DEFAULT_PATH_SEPARATOR)) {
if (requestURI != null && !DEFAULT_PATH_SEPARATOR.equals(requestURI)
&& requestURI.endsWith(DEFAULT_PATH_SEPARATOR)) {
requestURI = requestURI.substring(0, requestURI.length() - 1);
}
if (path != null && path.endsWith(DEFAULT_PATH_SEPARATOR)) {
if (path != null && !DEFAULT_PATH_SEPARATOR.equals(path)
&& path.endsWith(DEFAULT_PATH_SEPARATOR)) {
path = path.substring(0, path.length() - 1);
}
log.trace("Attempting to match pattern '{}' with current requestURI '{}'...", path, Encode.forHtml(requestURI));
Expand Down
Expand Up @@ -105,15 +105,17 @@ public FilterChain getChain(ServletRequest request, ServletResponse response, Fi
// in spring web, the requestURI "/resource/menus" ---- "resource/menus/" bose can access the resource
// but the pathPattern match "/resource/menus" can not match "resource/menus/"
// user can use requestURI + "/" to simply bypassed chain filter, to bypassed shiro protect
if(requestURI != null && requestURI.endsWith(DEFAULT_PATH_SEPARATOR)) {
if(requestURI != null && !DEFAULT_PATH_SEPARATOR.equals(requestURI)
&& requestURI.endsWith(DEFAULT_PATH_SEPARATOR)) {
requestURI = requestURI.substring(0, requestURI.length() - 1);
}


//the 'chain names' in this implementation are actually path patterns defined by the user. We just use them
//as the chain name for the FilterChainManager's requirements
for (String pathPattern : filterChainManager.getChainNames()) {
if (pathPattern != null && pathPattern.endsWith(DEFAULT_PATH_SEPARATOR)) {
if (pathPattern != null && !DEFAULT_PATH_SEPARATOR.equals(pathPattern)
&& pathPattern.endsWith(DEFAULT_PATH_SEPARATOR)) {
pathPattern = pathPattern.substring(0, pathPattern.length() - 1);
}

Expand Down
Expand Up @@ -121,6 +121,20 @@ public void testEnabled() throws Exception {
verify(request);
}

/**
* Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-742">SHIRO-742<a/>.
*/
@Test
public void testPathMatchEqualUrlSeparatorEnabled() {
expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
expect(request.getRequestURI()).andReturn("/").anyTimes();
replay(request);

boolean matchEnabled = filter.pathsMatch("/", request);
assertTrue("PathMatch can match URL end with Separator", matchEnabled);
verify(request);
}

/**
* Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-682">SHIRO-682<a/>.
*/
Expand Down
Expand Up @@ -186,6 +186,28 @@ public void testGetChain() {
verify(request);
}

/**
* Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-742">SHIRO-742<a/>.
*/
@Test
public void testGetChainEqualUrlSeparator() {
HttpServletRequest request = createNiceMock(HttpServletRequest.class);
HttpServletResponse response = createNiceMock(HttpServletResponse.class);
FilterChain chain = createNiceMock(FilterChain.class);

//ensure at least one chain is defined:
resolver.getFilterChainManager().addToChain("/", "authcBasic");

expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
expect(request.getContextPath()).andReturn("");
expect(request.getRequestURI()).andReturn("/");
replay(request);

FilterChain resolved = resolver.getChain(request, response, chain);
assertNotNull(resolved);
verify(request);
}

/**
* Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-682">SHIRO-682<a/>.
*/
Expand Down

0 comments on commit 9762f97

Please sign in to comment.