|
23 | 23 | import org.apache.axis2.context.ConfigurationContext; |
24 | 24 | import org.apache.axis2.description.Parameter; |
25 | 25 | import org.apache.axis2.transport.http.AxisServlet; |
| 26 | +import org.apache.axis2.transport.http.ForbidSessionCreationWrapper; |
26 | 27 |
|
27 | 28 | import javax.servlet.ServletConfig; |
28 | 29 | import javax.servlet.ServletContext; |
@@ -69,32 +70,43 @@ protected void service(HttpServletRequest request, HttpServletResponse response) |
69 | 70 | ActionHandler actionHandler = actionHandlers.get(action); |
70 | 71 | if (actionHandler != null) { |
71 | 72 | if (actionHandler.isMethodAllowed(request.getMethod())) { |
72 | | - HttpSession session = request.getSession(); |
73 | | - CSRFTokenCache tokenCache = (CSRFTokenCache)session.getAttribute(CSRFTokenCache.class.getName()); |
74 | | - if (tokenCache == null) { |
75 | | - tokenCache = new CSRFTokenCache(); |
76 | | - session.setAttribute(CSRFTokenCache.class.getName(), tokenCache); |
| 73 | + if (!actionHandler.isSessionCreationAllowed()) { |
| 74 | + request = new ForbidSessionCreationWrapper(request); |
77 | 75 | } |
| 76 | + HttpSession session = request.getSession(false); |
78 | 77 | if (actionHandler.isCSRFTokenRequired()) { |
79 | | - String token = request.getParameter("token"); |
80 | | - if (token == null || !tokenCache.isValid(token)) { |
| 78 | + boolean tokenValid; |
| 79 | + if (session == null) { |
| 80 | + tokenValid = false; |
| 81 | + } else { |
| 82 | + CSRFTokenCache tokenCache = (CSRFTokenCache)session.getAttribute(CSRFTokenCache.class.getName()); |
| 83 | + if (tokenCache == null) { |
| 84 | + tokenValid = false; |
| 85 | + } else { |
| 86 | + String token = request.getParameter("token"); |
| 87 | + tokenValid = token != null && tokenCache.isValid(token); |
| 88 | + } |
| 89 | + } |
| 90 | + if (!tokenValid) { |
81 | 91 | response.sendError(HttpServletResponse.SC_FORBIDDEN, "No valid CSRF token found in request"); |
82 | 92 | return; |
83 | 93 | } |
84 | 94 | } |
85 | | - session.setAttribute(Constants.SERVICE_PATH, configContext.getServicePath()); |
86 | | - String statusKey = request.getParameter("status"); |
87 | | - if (statusKey != null) { |
88 | | - StatusCache statusCache = (StatusCache)session.getAttribute(StatusCache.class.getName()); |
89 | | - if (statusCache != null) { |
90 | | - Status status = statusCache.get(statusKey); |
91 | | - if (status != null) { |
92 | | - request.setAttribute("status", status); |
| 95 | + request.setAttribute(Constants.SERVICE_PATH, configContext.getServicePath()); |
| 96 | + if (session != null) { |
| 97 | + String statusKey = request.getParameter("status"); |
| 98 | + if (statusKey != null) { |
| 99 | + StatusCache statusCache = (StatusCache)session.getAttribute(StatusCache.class.getName()); |
| 100 | + if (statusCache != null) { |
| 101 | + Status status = statusCache.get(statusKey); |
| 102 | + if (status != null) { |
| 103 | + request.setAttribute("status", status); |
| 104 | + } |
93 | 105 | } |
94 | 106 | } |
95 | 107 | } |
96 | 108 | ActionResult result = actionHandler.handle(request, axisSecurityEnabled()); |
97 | | - result.process(request, new CSRFPreventionResponseWrapper(response, actionHandlers, tokenCache, random)); |
| 109 | + result.process(request, new CSRFPreventionResponseWrapper(request, response, actionHandlers, random)); |
98 | 110 | } else { |
99 | 111 | response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); |
100 | 112 | } |
@@ -123,7 +135,7 @@ public void init(ServletConfig config) throws ServletException { |
123 | 135 | actionHandlers.put( |
124 | 136 | actionAnnotation.name(), |
125 | 137 | new ActionHandler(actions, method, actionAnnotation.authorizationRequired(), |
126 | | - actionAnnotation.post())); |
| 138 | + actionAnnotation.post(), actionAnnotation.sessionCreationAllowed())); |
127 | 139 | } |
128 | 140 | } |
129 | 141 | this.servletConfig = config; |
|
0 commit comments