This repository has been archived by the owner on Sep 30, 2019. It is now read-only.
/
ResponseHeadersEnforcementFilter.java
378 lines (320 loc) · 18.3 KB
/
ResponseHeadersEnforcementFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Licensed to Apereo under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Apereo licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at the following location:
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apereo.cas.security;
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 java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
/**
* Allows users to easily inject the default security headers to assist in protecting the application.
* The default for is to include the following headers:
* <pre>
* Cache-Control: no-cache, no-store, max-age=0, must-revalidate
* Pragma: no-cache
* Expires: 0
* X-Content-Type-Options: nosniff
* Strict-Transport-Security: max-age=15768000 ; includeSubDomains
* X-Frame-Options: DENY
* X-XSS-Protection: 1; mode=block
* </pre>
*
* @author Misagh Moayyed
* @since 2.0.4
*/
public class ResponseHeadersEnforcementFilter extends AbstractSecurityFilter implements Filter {
private static final Logger LOGGER = Logger.getLogger(ResponseHeadersEnforcementFilter.class.getName());
private static final String INIT_PARAM_ENABLE_CACHE_CONTROL = "enableCacheControl";
private static final String INIT_PARAM_ENABLE_XCONTENT_OPTIONS = "enableXContentTypeOptions";
private static final String INIT_PARAM_ENABLE_STRICT_TRANSPORT_SECURITY = "enableStrictTransportSecurity";
private static final String INIT_PARAM_ENABLE_STRICT_XFRAME_OPTIONS = "enableXFrameOptions";
private static final String INIT_PARAM_STRICT_XFRAME_OPTIONS = "XFrameOptions";
private static final String INIT_PARAM_ENABLE_XSS_PROTECTION = "enableXSSProtection";
private static final String INIT_PARAM_XSS_PROTECTION = "XSSProtection";
private static final String INIT_PARAM_CONTENT_SECURITY_POLICY = "contentSecurityPolicy";
private boolean enableCacheControl;
private String cacheControlHeader = "no-cache, no-store, max-age=0, must-revalidate";
private boolean enableXContentTypeOptions;
private String xContentTypeOptionsHeader = "nosniff";
// allow for 6 months; value is in seconds
private boolean enableStrictTransportSecurity;
private String strictTransportSecurityHeader = "max-age=15768000 ; includeSubDomains";
private boolean enableXFrameOptions;
private String XFrameOptions = "DENY";
private boolean enableXSSProtection;
private String XSSProtection = "1; mode=block";
private String contentSecurityPolicy;
public void setXSSProtection(final String XSSProtection) {
this.XSSProtection = XSSProtection;
}
public void setXFrameOptions(final String XFrameOptions) {
this.XFrameOptions = XFrameOptions;
}
public void setEnableStrictTransportSecurity(final boolean enableStrictTransportSecurity) {
this.enableStrictTransportSecurity = enableStrictTransportSecurity;
}
public void setEnableCacheControl(final boolean enableCacheControl) {
this.enableCacheControl = enableCacheControl;
}
public void setEnableXContentTypeOptions(final boolean enableXContentTypeOptions) {
this.enableXContentTypeOptions = enableXContentTypeOptions;
}
public void setEnableXFrameOptions(final boolean enableXFrameOptions) {
this.enableXFrameOptions = enableXFrameOptions;
}
public void setEnableXSSProtection(final boolean enableXSSProtection) {
this.enableXSSProtection = enableXSSProtection;
}
public void setContentSecurityPolicy(final String contentSecurityPolicy) {
this.contentSecurityPolicy = contentSecurityPolicy;
}
public ResponseHeadersEnforcementFilter() {
FilterUtils.configureLogging(getLoggerHandlerClassName(), LOGGER);
}
@Override
public void setLoggerHandlerClassName(final String loggerHandlerClassName) {
super.setLoggerHandlerClassName(loggerHandlerClassName);
FilterUtils.configureLogging(getLoggerHandlerClassName(), LOGGER);
}
@Override
public void init(final FilterConfig filterConfig) throws ServletException {
FilterUtils.configureLogging(getLoggerHandlerClassName(), LOGGER);
final Enumeration initParamNames = filterConfig.getInitParameterNames();
throwIfUnrecognizedParamName(initParamNames);
final String enableCacheControl = filterConfig.getInitParameter(INIT_PARAM_ENABLE_CACHE_CONTROL);
final String enableXContentTypeOptions = filterConfig.getInitParameter(INIT_PARAM_ENABLE_XCONTENT_OPTIONS);
final String enableStrictTransportSecurity = filterConfig.getInitParameter(INIT_PARAM_ENABLE_STRICT_TRANSPORT_SECURITY);
final String enableXFrameOptions = filterConfig.getInitParameter(INIT_PARAM_ENABLE_STRICT_XFRAME_OPTIONS);
final String enableXSSProtection = filterConfig.getInitParameter(INIT_PARAM_ENABLE_XSS_PROTECTION);
try {
this.enableCacheControl = FilterUtils.parseStringToBooleanDefaultingToFalse(enableCacheControl);
} catch (final Exception e) {
FilterUtils.logException(LOGGER, new ServletException("Error parsing parameter [" + INIT_PARAM_ENABLE_CACHE_CONTROL
+ "] with value [" + enableCacheControl + "]", e));
}
try {
this.enableXContentTypeOptions = FilterUtils.parseStringToBooleanDefaultingToFalse(enableXContentTypeOptions);
} catch (final Exception e) {
FilterUtils.logException(LOGGER, new ServletException("Error parsing parameter [" + INIT_PARAM_ENABLE_XCONTENT_OPTIONS
+ "] with value [" + enableXContentTypeOptions + "]", e));
}
try {
this.enableStrictTransportSecurity = FilterUtils.parseStringToBooleanDefaultingToFalse(enableStrictTransportSecurity);
} catch (final Exception e) {
FilterUtils.logException(LOGGER, new ServletException("Error parsing parameter [" + INIT_PARAM_ENABLE_STRICT_TRANSPORT_SECURITY
+ "] with value [" + enableStrictTransportSecurity + "]", e));
}
try {
this.enableXFrameOptions = FilterUtils.parseStringToBooleanDefaultingToFalse(enableXFrameOptions);
this.XFrameOptions = filterConfig.getInitParameter(INIT_PARAM_STRICT_XFRAME_OPTIONS);
if (this.XFrameOptions == null || this.XFrameOptions.isEmpty()) {
this.XFrameOptions = "DENY";
}
} catch (final Exception e) {
FilterUtils.logException(LOGGER, new ServletException("Error parsing parameter [" + INIT_PARAM_ENABLE_STRICT_XFRAME_OPTIONS
+ "] with value [" + enableXFrameOptions + "]", e));
}
try {
this.enableXSSProtection = FilterUtils.parseStringToBooleanDefaultingToFalse(enableXSSProtection);
this.XSSProtection = filterConfig.getInitParameter(INIT_PARAM_XSS_PROTECTION);
if (this.XSSProtection == null || this.XSSProtection.isEmpty()) {
this.XSSProtection = "1; mode=block";
}
} catch (final Exception e) {
FilterUtils.logException(LOGGER, new ServletException("Error parsing parameter [" + INIT_PARAM_ENABLE_XSS_PROTECTION
+ "] with value [" + enableXSSProtection + "]", e));
}
this.contentSecurityPolicy = filterConfig.getInitParameter(INIT_PARAM_CONTENT_SECURITY_POLICY);
}
/**
* Examines the Filter init parameter names and throws ServletException if they contain an unrecognized
* init parameter name.
* <p>
* This is a stateless static method.
* <p>
* This method is an implementation detail and is not exposed API.
* This method is only non-private to allow JUnit testing.
*
* @param initParamNames init param names, in practice as read from the FilterConfig.
*/
static void throwIfUnrecognizedParamName(final Enumeration initParamNames) {
final Set<String> recognizedParameterNames = new HashSet<String>();
recognizedParameterNames.add(INIT_PARAM_ENABLE_CACHE_CONTROL);
recognizedParameterNames.add(INIT_PARAM_ENABLE_XCONTENT_OPTIONS);
recognizedParameterNames.add(INIT_PARAM_ENABLE_STRICT_TRANSPORT_SECURITY);
recognizedParameterNames.add(INIT_PARAM_ENABLE_STRICT_XFRAME_OPTIONS);
recognizedParameterNames.add(INIT_PARAM_STRICT_XFRAME_OPTIONS);
recognizedParameterNames.add(INIT_PARAM_CONTENT_SECURITY_POLICY);
recognizedParameterNames.add(LOGGER_HANDLER_CLASS_NAME);
recognizedParameterNames.add(INIT_PARAM_ENABLE_XSS_PROTECTION);
recognizedParameterNames.add(INIT_PARAM_XSS_PROTECTION);
while (initParamNames.hasMoreElements()) {
final String initParamName = (String) initParamNames.nextElement();
if (!recognizedParameterNames.contains(initParamName)) {
FilterUtils.logException(LOGGER, new ServletException("Unrecognized init parameter [" + initParamName + "]. Failing safe. Typo" +
" in the web.xml configuration? " +
" Misunderstanding about the configuration "
+ RequestParameterPolicyEnforcementFilter.class.getSimpleName() + " expects?"));
}
}
}
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
try {
if (servletResponse instanceof HttpServletResponse) {
final HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
decideInsertCacheControlHeader(httpServletResponse, httpServletRequest);
decideInsertStrictTransportSecurityHeader(httpServletResponse, httpServletRequest);
decideInsertXContentTypeOptionsHeader(httpServletResponse, httpServletRequest);
decideInsertXFrameOptionsHeader(httpServletResponse, httpServletRequest);
decideInsertXSSProtectionHeader(httpServletResponse, httpServletRequest);
decideInsertContentSecurityPolicyHeader(httpServletResponse, httpServletRequest);
}
} catch (final Exception e) {
FilterUtils.logException(LOGGER, new ServletException(getClass().getSimpleName()
+ " is blocking this request. Examine the cause in this stack trace to understand why.", e));
}
filterChain.doFilter(servletRequest, servletResponse);
}
protected void decideInsertContentSecurityPolicyHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
if (this.contentSecurityPolicy == null) {
return;
}
insertContentSecurityPolicyHeader(httpServletResponse, httpServletRequest);
}
protected void insertContentSecurityPolicyHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest) {
this.insertContentSecurityPolicyHeader(httpServletResponse, httpServletRequest, this.contentSecurityPolicy);
}
protected void insertContentSecurityPolicyHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest,
final String contentSecurityPolicy) {
final String uri = httpServletRequest.getRequestURI();
httpServletResponse.addHeader("Content-Security-Policy", contentSecurityPolicy);
LOGGER.fine("Adding Content-Security-Policy response header " + contentSecurityPolicy + " for " + uri);
}
protected void decideInsertXSSProtectionHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
if (!this.enableXSSProtection) {
return;
}
insertXSSProtectionHeader(httpServletResponse, httpServletRequest);
}
protected void insertXSSProtectionHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
insertXSSProtectionHeader(httpServletResponse, httpServletRequest, this.XSSProtection);
}
protected void insertXSSProtectionHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest,
final String XSSProtection) {
final String uri = httpServletRequest.getRequestURI();
httpServletResponse.addHeader("X-XSS-Protection", XSSProtection);
LOGGER.fine("Adding X-XSS Protection " + XSSProtection + " response headers for " + uri);
}
protected void decideInsertXFrameOptionsHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
if (!this.enableXFrameOptions) {
return;
}
insertXFrameOptionsHeader(httpServletResponse, httpServletRequest);
}
protected void insertXFrameOptionsHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest) {
insertXFrameOptionsHeader(httpServletResponse, httpServletRequest, this.XFrameOptions);
}
protected void insertXFrameOptionsHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest,
final String xFrameOptions) {
final String uri = httpServletRequest.getRequestURI();
httpServletResponse.addHeader("X-Frame-Options", xFrameOptions);
LOGGER.fine("Adding X-Frame Options " + xFrameOptions + " response headers for [{}]" + uri);
}
protected void decideInsertXContentTypeOptionsHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
if (!this.enableXContentTypeOptions) {
return;
}
insertXContentTypeOptionsHeader(httpServletResponse, httpServletRequest);
}
protected void insertXContentTypeOptionsHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest) {
insertXContentTypeOptionsHeader(httpServletResponse, httpServletRequest, this.xContentTypeOptionsHeader);
}
protected void insertXContentTypeOptionsHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest,
final String xContentTypeOptionsHeader) {
final String uri = httpServletRequest.getRequestURI();
httpServletResponse.addHeader("X-Content-Type-Options", xContentTypeOptionsHeader);
LOGGER.fine("Adding X-Content Type response headers " + xContentTypeOptionsHeader + " for " + uri);
}
protected void decideInsertCacheControlHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
if (!this.enableCacheControl) {
return;
}
insertCacheControlHeader(httpServletResponse, httpServletRequest);
}
protected void insertCacheControlHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
insertCacheControlHeader(httpServletResponse, httpServletRequest, this.cacheControlHeader);
}
protected void insertCacheControlHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest,
final String cacheControlHeader) {
final String uri = httpServletRequest.getRequestURI();
if (!uri.endsWith(".css")
&& !uri.endsWith(".js")
&& !uri.endsWith(".png")
&& !uri.endsWith(".txt")
&& !uri.endsWith(".jpg")
&& !uri.endsWith(".ico")
&& !uri.endsWith(".jpeg")
&& !uri.endsWith(".bmp")
&& !uri.endsWith(".gif")) {
httpServletResponse.addHeader("Cache-Control", cacheControlHeader);
httpServletResponse.addHeader("Pragma", "no-cache");
httpServletResponse.addIntHeader("Expires", 0);
LOGGER.fine("Adding Cache Control response headers for " + uri);
}
}
protected void decideInsertStrictTransportSecurityHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest) {
if (!this.enableStrictTransportSecurity) {
return;
}
insertStrictTransportSecurityHeader(httpServletResponse, httpServletRequest);
}
protected void insertStrictTransportSecurityHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest) {
insertStrictTransportSecurityHeader(httpServletResponse, httpServletRequest, this.strictTransportSecurityHeader);
}
protected void insertStrictTransportSecurityHeader(final HttpServletResponse httpServletResponse,
final HttpServletRequest httpServletRequest,
final String strictTransportSecurityHeader) {
if (httpServletRequest.isSecure()) {
final String uri = httpServletRequest.getRequestURI();
httpServletResponse.addHeader("Strict-Transport-Security", strictTransportSecurityHeader);
LOGGER.fine("Adding HSTS response headers for " + uri);
}
}
@Override
public void destroy() {
}
}