Skip to content

Commit

Permalink
Improve session management in CsrfPreventionFilter
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1393071 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Konstantin Kolinko committed Oct 2, 2012
1 parent 48fc731 commit d426972
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions java/org/apache/catalina/filters/CsrfPreventionFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
Expand Down Expand Up @@ -153,23 +154,30 @@ public void doFilter(ServletRequest request, ServletResponse response,
}
}

LruCache<String> nonceCache =
(LruCache<String>) req.getSession(true).getAttribute(
Constants.CSRF_NONCE_SESSION_ATTR_NAME);
HttpSession session = req.getSession(false);

@SuppressWarnings("unchecked")
LruCache<String> nonceCache = (session == null) ? null
: (LruCache<String>) session.getAttribute(
Constants.CSRF_NONCE_SESSION_ATTR_NAME);

if (!skipNonceCheck) {
String previousNonce =
req.getParameter(Constants.CSRF_NONCE_REQUEST_PARAM);

if (nonceCache != null && !nonceCache.contains(previousNonce)) {
if (nonceCache == null || previousNonce == null ||
!nonceCache.contains(previousNonce)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
}

if (nonceCache == null) {
nonceCache = new LruCache<>(nonceCacheSize);
req.getSession().setAttribute(
if (session == null) {
session = req.getSession(true);
}
session.setAttribute(
Constants.CSRF_NONCE_SESSION_ATTR_NAME, nonceCache);
}

Expand Down

0 comments on commit d426972

Please sign in to comment.