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

[SHIRO-742]fix throw exception when request uri is / #201

Merged
merged 1 commit into from Feb 13, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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