View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.filters;
35  
36  import info.magnolia.cms.beans.config.MIMEMapping;
37  import info.magnolia.cms.beans.config.ServerConfiguration;
38  import info.magnolia.cms.core.AggregationState;
39  import info.magnolia.cms.security.SecurityCallbackFilter;
40  import info.magnolia.cms.util.ServletUtil;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.util.EscapeUtil;
43  
44  import java.io.IOException;
45  import java.io.UnsupportedEncodingException;
46  import java.net.URI;
47  import java.net.URLDecoder;
48  
49  import javax.inject.Inject;
50  import javax.servlet.FilterChain;
51  import javax.servlet.ServletException;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.commons.lang3.StringUtils;
56  
57  
58  /**
59   * Sets content type and encoding for requests based on the uri extension and prepares uri path information in the
60   * aggregation state.
61   *
62   * TODO : rename this filter. What it really does is initialize and setup the basic,
63   * non-content related attributes of the AggregationState. ContentType could become an
64   * attribute of the AggregationState too and could be set later.
65   *
66   * FIXME: the original uri should not be reset, MAGNOLIA-3204
67   *
68   * @see MIMEMapping
69   * @see AggregationState
70   */
71  public class ContentTypeFilter extends AbstractMgnlFilter {
72  
73      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ContentTypeFilter.class);
74  
75      private boolean sanitizeXssUri = false;
76  
77      /**
78       * If set we have to reset the aggregation state before setting the original URI/URL with new values.
79       */
80      private static final String AGGREGATION_STATE_INITIALIZED = ContentTypeFilter.class.getName() + ".aggregationStateInitialized";
81  
82      private boolean registeredExtensionsOnly = false;
83      private boolean validateContentType = false;
84  
85      private ServerConfiguration serverConfiguration;
86  
87      /**
88       * @deprecated since 5.4.2, use {@link #ContentTypeFilter(ServerConfiguration)} instead.
89       */
90      @Deprecated
91      public ContentTypeFilter() {
92      }
93  
94      @Inject
95      public ContentTypeFilter(ServerConfiguration serverConfiguration) {
96          this.serverConfiguration = serverConfiguration;
97      }
98  
99  
100     @Override
101     public void doFilter(HttpServletRequest request, HttpServletResponse originalResponse, FilterChain chain) throws IOException, ServletException {
102         final SecurityCallbackFilter.StatusSniffingResponseWrapper response;
103         if (originalResponse instanceof SecurityCallbackFilter.StatusSniffingResponseWrapper) {
104             response = (SecurityCallbackFilter.StatusSniffingResponseWrapper) originalResponse;
105         } else {
106             response = new SecurityCallbackFilter.StatusSniffingResponseWrapper(originalResponse);
107         }
108 
109         // we will set the original uri, to avoid conflicts we have to reset the aggregation state
110         // this will mainly reset the original uri and keep all other information
111         if (request.getAttribute(AGGREGATION_STATE_INITIALIZED) != null) {
112             MgnlContext.resetAggregationState();
113         } else {
114             request.setAttribute(AGGREGATION_STATE_INITIALIZED, Boolean.TRUE);
115         }
116 
117         final String originalUri = ServletUtil.getOriginalRequestURI(request);
118         final String originalUrl = ServletUtil.getOriginalRequestURLIncludingQueryString(request);
119         final String extension = getUriExtension(originalUri);
120         final String mimeType = getMimeType(extension, response);
121         if (isRegisteredExtensionsOnly() && mimeType == null && !response.isCommitted()) {
122             response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unsupported extension=%1$s.", extension));
123             return;
124         }
125         final String characterEncoding = setupCharacterEncoding(mimeType, request, response);
126         final AggregationState aggregationState = MgnlContext.getAggregationState();
127         aggregationState.setCharacterEncoding(characterEncoding);
128 
129         final String decodedOriginalUri = decodeUri(originalUri, characterEncoding);
130         aggregationState.setOriginalURI(decodedOriginalUri);
131 
132         try {
133             final String decodedOriginalUrl = decodeUri(originalUrl, characterEncoding);
134             aggregationState.setOriginalURL(decodedOriginalUrl);
135         } catch (IllegalArgumentException e) {
136             // URL is malformed and cannot be decoded - send back error 400
137             if (!response.isCommitted()) {
138                 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "URL is malformed (not encoded properly).");
139             }
140             // stop filter chain
141             return;
142         }
143         aggregationState.setOriginalBrowserURI(originalUri);
144         aggregationState.setOriginalBrowserURL(originalUrl);
145         final String requestURI = URI.create(ServletUtil.getRequestUri(request)).normalize().getRawPath();
146         final String currentURI = decodeUri(requestURI, characterEncoding);
147         aggregationState.setCurrentURI(currentURI);
148         aggregationState.setExtension(extension);
149         aggregationState.setQueryString(request.getQueryString());
150 
151         if (isValidateContentType()) {
152             ContentTypeCheckingResponseWrapper resWrapper = new ContentTypeCheckingResponseWrapper(response, extension);
153             chain.doFilter(request, resWrapper);
154         } else {
155             chain.doFilter(request, response);
156         }
157         if (response.getContentType() == null) {
158             if (response.getStatus() == HttpServletResponse.SC_OK) {
159                 log.warn("Content type for {} is not set.", originalUrl);
160                 if (!response.isCommitted()) { //if the content type was not set (e.g. in renderer) and response is not committed, set content type so we don't produce a response without the content type
161                     log.warn("Response is not committed yet. Setting content type: {}.", mimeType);
162                     response.setContentType(mimeType);
163                 }
164             } else {
165                 log.debug("Content type for {} is not set, status code of response is {}.", originalUrl, response.getStatus());
166             }
167         }
168     }
169 
170     protected String getUriExtension(String uri) {
171         final String fileName = StringUtils.substringAfterLast(uri, "/");
172         return StringUtils.substringAfterLast(fileName, ".");
173     }
174 
175     protected String getMimeType(String extension, HttpServletResponse response) {
176         final String mimeType;
177 
178         if (isRegisteredExtensionsOnly()) {
179             if (StringUtils.isBlank(extension)) {
180                 extension = serverConfiguration.getDefaultExtension();
181                 if (StringUtils.isBlank(extension)) {
182                     extension = MIMEMapping.DEFAULT_EXTENSION;
183                 }
184             }
185             mimeType = MIMEMapping.getMIMEType(extension);
186             if (mimeType == null) {
187                 return null;
188             }
189         } else {
190             mimeType = MIMEMapping.getMIMETypeOrDefault(extension);
191         }
192 
193         return mimeType;
194     }
195 
196     protected String setupCharacterEncoding(String mimeType, HttpServletRequest request, HttpServletResponse response) {
197         final String characterEncoding = MIMEMapping.getContentEncodingOrDefault(mimeType);
198 
199         try {
200             // let's not override the request encoding if set by the servlet container or the requesting browser
201             if (request.getCharacterEncoding() == null) {
202                 request.setCharacterEncoding(characterEncoding);
203             }
204         } catch (UnsupportedEncodingException e) {
205             log.error("Can't set character encoding for the request (mimetype={})", mimeType, e);
206         }
207 
208         response.setCharacterEncoding(characterEncoding);
209 
210         return characterEncoding;
211     }
212 
213     /**
214      * XSS escaping of a substring after last dot in uri for preventing XSS attack.
215      * If uri doesn't contain dot, the same uri will be returned.
216      * @param uri the uri whose substring after last dot will be XSS escaped.
217      */
218     private String sanitizeXss(String uri) {
219         final String afterLastDot = StringUtils.substringAfterLast(uri, ".");
220         if (StringUtils.isNotEmpty(afterLastDot)) {
221             final String xssEscapedAfterLastDot = EscapeUtil.escapeXss(afterLastDot);
222             final String sanitizedXssUri = StringUtils.removeEnd(uri, afterLastDot).concat(xssEscapedAfterLastDot);
223             return sanitizedXssUri;
224         }
225 
226         return uri;
227     }
228 
229     /**
230      * Decodes an URI using given character encoding and sanitizes the URI against XSS if configured.
231      */
232     private String decodeUri(String uri, String characterEncoding) throws UnsupportedEncodingException {
233         final String decodedUri = URLDecoder.decode(uri, characterEncoding);
234         return isSanitizeXssUri() ? sanitizeXss(decodedUri) : decodedUri;
235     }
236 
237     /**
238      * @deprecated since 5.6 - use {info.magnolia.rendering.engine.RenderingEngine#getEscapeHtml()} instead.
239      */
240     @Deprecated
241     public boolean isSanitizeXssUri() {
242         return sanitizeXssUri;
243     }
244 
245     /**
246      * @deprecated since 5.6 - use {info.magnolia.rendering.engine.RenderingEngine#getEscapeHtml()} instead.
247      */
248     @Deprecated
249     public void setSanitizeXssUri(boolean sanitizeXssUri) {
250         this.sanitizeXssUri = sanitizeXssUri;
251     }
252 
253     public boolean isRegisteredExtensionsOnly() {
254         return registeredExtensionsOnly;
255     }
256 
257     public void setRegisteredExtensionsOnly(boolean registeredExtensionsOnly) {
258         this.registeredExtensionsOnly = registeredExtensionsOnly;
259     }
260 
261     public boolean isValidateContentType() {
262         return validateContentType;
263     }
264 
265     public void setValidateContentType(boolean validateContentType) {
266         this.validateContentType = validateContentType;
267     }
268 
269     /**
270      * @deprecated since 5.4.2, use {@link #getMimeType(String, HttpServletResponse)} and {@link #setupCharacterEncoding(String, HttpServletRequest, HttpServletResponse)} instead.
271      */
272     @Deprecated
273     protected String setupContentTypeAndCharacterEncoding(String extension, HttpServletRequest request, HttpServletResponse response) {
274         final String mimeType = setupContentType(extension, response);
275         return setupCharacterEncoding(mimeType, request, response);
276     }
277 
278     /**
279      * @deprecated since 5.3.13 & 5.4.2, use {@link #getMimeType(String, HttpServletResponse)} instead.
280      */
281     @Deprecated
282     protected String setupContentType(String extension, HttpServletResponse response) {
283         return getMimeType(extension, response);
284     }
285 }