Skip to content

Commit 40e464b

Browse files
committed
TRUNK-6051 Prevent CSRF token caching
1 parent 98bdf0f commit 40e464b

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

web/src/main/java/org/openmrs/web/filter/OpenmrsFilter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ protected void doFilterInternal(HttpServletRequest httpRequest, HttpServletRespo
9494
// set the locale on the session (for the servlet container as well)
9595
httpSession.setAttribute("locale", userContext.getLocale());
9696

97-
//TODO We do not cache pages that have CSRF tokens. There are smarter ways of dealing
98-
//with this, like loading just the CSRF token with an AJAX request and replacing the
99-
//form field value with it, and others. But i did not go into these details. So for now,
100-
//i have just done the simplest which is turning off caching for pages that have CSRF tokens.
101-
if (httpRequest.getParameter("OWASP-CSRFTOKEN") != null) {
97+
//TODO We do not cache the csrfguard javascript file because it contains the
98+
//csrf token that is dynamically embedded in forms.
99+
if (httpRequest.getRequestURI().endsWith("csrfguard")) {
102100
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
103101
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
104102
httpResponse.setHeader("Expires", "0"); // Proxies.

webapp/src/main/webapp/WEB-INF/web.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@
134134
<filter-name>multipartFilter</filter-name>
135135
<url-pattern>/*</url-pattern>
136136
</filter-mapping>
137-
138-
<filter>
139-
<filter-name>CSRFGuard</filter-name>
140-
<filter-class>org.owasp.csrfguard.CsrfGuardFilter</filter-class>
141-
</filter>
142-
<filter-mapping>
143-
<filter-name>CSRFGuard</filter-name>
144-
<!-- Filter any URL using CSRFGuard -->
145-
<url-pattern>/*</url-pattern>
146-
</filter-mapping>
147137

148138
<!-- Should be the second filter so that all requests are first wrapped by a
149139
hibernate filter (to help with lazy loading) -->
@@ -173,6 +163,16 @@
173163
<dispatcher>INCLUDE</dispatcher>
174164
</filter-mapping>
175165

166+
<filter>
167+
<filter-name>CSRFGuard</filter-name>
168+
<filter-class>org.owasp.csrfguard.CsrfGuardFilter</filter-class>
169+
</filter>
170+
<filter-mapping>
171+
<filter-name>CSRFGuard</filter-name>
172+
<!-- Filter any URL using CSRFGuard -->
173+
<url-pattern>/*</url-pattern>
174+
</filter-mapping>
175+
176176
<filter>
177177
<filter-name>ModuleFilter</filter-name>
178178
<filter-class>org.openmrs.module.web.filter.ModuleFilter</filter-class>

0 commit comments

Comments
 (0)