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
MM-893: Solved Authentication Bypass to System Administration #426
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.coreapps.filter; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.openmrs.User; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.module.coreapps.CoreAppsConstants; | ||
import org.openmrs.web.WebConstants; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This filter checks if an authenticated user trying to access administrative functions has the | ||
* <STRONG>System Administration<STRONG> privelege. It will intercept any requests with *admin/* in | ||
* its url. Unauthorised user will be redirected to the home page. | ||
*/ | ||
public class AdminAuthorisationFilter implements Filter { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AdminAuthorisationFilter.class); | ||
|
||
/** | ||
* @see Filter#init(FilterConfig) | ||
*/ | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
|
||
} | ||
|
||
/** | ||
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, | ||
* javax.servlet.ServletResponse, javax.servlet.FilterChain) | ||
*/ | ||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | ||
HttpServletRequest httpReq = (HttpServletRequest) req; | ||
User user = Context.getAuthenticatedUser(); | ||
if (user != null && !user.hasPrivilege(CoreAppsConstants.PRIVILEGE_SYSTEM_ADMINISTRATOR)) { | ||
httpReq.getSession().setAttribute(WebConstants.DENIED_PAGE, httpReq.getRequestURI()); | ||
HttpServletResponse httpRes = (HttpServletResponse) res; | ||
log.info("User {} lacks the privilege {}", user, CoreAppsConstants.PRIVILEGE_SYSTEM_ADMINISTRATOR); | ||
httpRes.sendRedirect(httpReq.getContextPath() + "/login.htm"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ibacher I really don't know how much to thank you. I think I have added the request correctly. |
||
return; | ||
} | ||
chain.doFilter(req, res); | ||
} | ||
|
||
/** | ||
* @see Filter#destroy() | ||
*/ | ||
public void destroy() { | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when the user is null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dkayiwa I think this happens when the user has not logged in. He/she goes to the login page