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
| // webapps/<web-app-name>/WEB-INF/classes/CSPPoliciesApplier.java
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
/* for Tomcat 9 and below
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
*/
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Hex;
/**
* Sample filter implementation to define a set of Content Security Policies.<br/>
* This implementation has a dependency on Commons Codec API.<br/>
* This filter set CSP policies using all HTTP headers defined into W3C specification.<br/>
* <br/>
* This implementation is oriented to be easily understandable and easily adapted.<br/>
*/
@WebFilter("/*")
public class CSPPoliciesApplier implements Filter {
/** Configuration member to specify if web app use web fonts */
public static final boolean APP_USE_WEBFONTS = false;
/** Configuration member to specify if web app use videos or audios */
public static final boolean APP_USE_AUDIOS_OR_VIDEOS = false;
/**
* Configuration member to specify if filter must add CSP directive used by Mozilla (Firefox)
*/
public static final boolean INCLUDE_MOZILLA_CSP_DIRECTIVES = false;
/** Filter configuration */
@SuppressWarnings("unused")
private FilterConfig filterConfig = null;
/** List CSP HTTP Headers */
private List<String> cspHeaders = new ArrayList<String>();
/** Collection of CSP polcies that will be applied */
private String policies = null;
/** Used for Script Nonce */
private SecureRandom prng = null;
/**
* Used to prepare (one time for all) set of CSP policies that will be applied on each HTTP
* response.
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig fConfig) throws ServletException {
// Get filter configuration
this.filterConfig = fConfig;
// Init secure random
try {
this.prng = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new ServletException(e);
}
// Define list of CSP HTTP Headers
this.cspHeaders.add("Content-Security-Policy");
// this.cspHeaders.add("X-Content-Security-Policy");
// this.cspHeaders.add("X-WebKit-CSP");
// Define CSP policies
// Loading policies for Frame and Sandboxing will be dynamically defined : We need to know if context use Frame
List<String> cspPolicies = new ArrayList<String>();
String originLocationRef = "'self'";
// --Disable default source in order to avoid browser fallback loading using 'default-src'
// locations
cspPolicies.add("default-src 'none'");
// --Define loading policies for Scripts
// cspPolicies.add("script-src " + originLocationRef + " 'unsafe-inline' 'unsafe-eval'"); <- bad policy!
cspPolicies.add("script-src " + originLocationRef); // not the best but will do
if (INCLUDE_MOZILLA_CSP_DIRECTIVES) {
cspPolicies.add("options inline-script eval-script");
cspPolicies.add("xhr-src 'self'");
}
// --Define loading policies for Plugins
cspPolicies.add("object-src " + originLocationRef);
// --Define loading policies for Styles (CSS)
cspPolicies.add("style-src " + "'unsafe-inline'");
// --Define loading policies for Images
cspPolicies.add("img-src " + originLocationRef);
// --Define loading policies for Form
cspPolicies.add("form-action " + originLocationRef);
// --Define loading policies for Audios/Videos
if (APP_USE_AUDIOS_OR_VIDEOS) {
cspPolicies.add("media-src " + originLocationRef);
}
// --Define loading policies for Fonts
if (APP_USE_WEBFONTS) {
cspPolicies.add("font-src " + originLocationRef);
}
// --Define loading policies for Connection
cspPolicies.add("connect-src " + originLocationRef);
// --Define loading policies for Plugins Types
// cspPolicies.add("plugin-types application/pdf application/x-shockwave-flash");
// --Define browser XSS filtering feature running mode
// cspPolicies.add("reflected-xss block");
// Target formating
this.policies = cspPolicies.toString().replaceAll("(\\[|\\])", "").replaceAll(",", ";")
.trim();
}
/**
* Add CSP policies on each HTTP response.
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fchain)
throws IOException, ServletException {
HttpServletRequest httpRequest = ((HttpServletRequest) request);
HttpServletResponse httpResponse = ((HttpServletResponse) response);
/* Step 1 : Detect if target resource is a Frame */
// Customize here according to your context...
boolean isFrame = false;
/* Step 2 : Add CSP policies to HTTP response */
StringBuilder policiesBuffer = new StringBuilder(this.policies);
// If resource is a frame add Frame/Sandbox CSP policy
if (isFrame) {
// Frame + Sandbox : Here sandbox allow nothing, customize sandbox options depending on
// your app....
policiesBuffer.append(";").append("frame-src 'self';sandbox");
if (INCLUDE_MOZILLA_CSP_DIRECTIVES) {
policiesBuffer.append(";").append("frame-ancestors 'self'");
}
}
/*
// Add Script Nonce CSP Policy
// --Generate a random number
String randomNum = new Integer(this.prng.nextInt()).toString();
// --Get its digest
MessageDigest sha;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new ServletException(e);
}
byte[] digest = sha.digest(randomNum.getBytes());
// --Encode it into HEXA
String scriptNonce = Hex.encodeHexString(digest);
policiesBuffer.append(";").append("script-nonce ").append(scriptNonce);
// --Made available script nonce in view app layer
httpRequest.setAttribute("CSP_SCRIPT_NONCE", scriptNonce);
*/
// Add policies to all HTTP headers
for (String header : this.cspHeaders) {
httpResponse.setHeader(header, policiesBuffer.toString());
}
/* Step 3 : Let request continue chain filter */
fchain.doFilter(request, response);
}
/**
* {@inheritDoc}
*
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
// Not used
}
}
|