1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.csrf.internal; |
21 |
|
|
22 |
|
import java.io.UnsupportedEncodingException; |
23 |
|
import java.net.URLEncoder; |
24 |
|
import java.security.NoSuchAlgorithmException; |
25 |
|
import java.security.SecureRandom; |
26 |
|
import java.util.concurrent.ConcurrentHashMap; |
27 |
|
import java.util.concurrent.ConcurrentMap; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Singleton; |
31 |
|
import javax.servlet.http.HttpServletRequest; |
32 |
|
|
33 |
|
import org.apache.commons.codec.binary.Base64; |
34 |
|
import org.slf4j.Logger; |
35 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.component.phase.Initializable; |
38 |
|
import org.xwiki.component.phase.InitializationException; |
39 |
|
import org.xwiki.container.Container; |
40 |
|
import org.xwiki.container.Request; |
41 |
|
import org.xwiki.container.servlet.ServletRequest; |
42 |
|
import org.xwiki.container.servlet.filters.SavedRequestManager; |
43 |
|
import org.xwiki.csrf.CSRFToken; |
44 |
|
import org.xwiki.csrf.CSRFTokenConfiguration; |
45 |
|
import org.xwiki.model.reference.DocumentReference; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@version |
60 |
|
@since |
61 |
|
|
62 |
|
@Component |
63 |
|
@Singleton |
|
|
| 89.3% |
Uncovered Elements: 8 (75) |
Complexity: 22 |
Complexity Density: 0.44 |
|
64 |
|
public class DefaultCSRFToken implements CSRFToken, Initializable |
65 |
|
{ |
66 |
|
|
67 |
|
private static final int TOKEN_LENGTH = 16; |
68 |
|
|
69 |
|
|
70 |
|
private static final String RESUBMIT_TEMPLATE = "resubmit"; |
71 |
|
|
72 |
|
|
73 |
|
private final ConcurrentMap<DocumentReference, String> tokens = new ConcurrentHashMap<DocumentReference, String>(); |
74 |
|
|
75 |
|
|
76 |
|
private String guestToken; |
77 |
|
|
78 |
|
|
79 |
|
private SecureRandom random; |
80 |
|
|
81 |
|
|
82 |
|
@Inject |
83 |
|
private DocumentAccessBridge docBridge; |
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
private Container container; |
88 |
|
|
89 |
|
|
90 |
|
@Inject |
91 |
|
private CSRFTokenConfiguration configuration; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
private Logger logger; |
98 |
|
|
99 |
|
|
100 |
|
@inheritDoc |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
105 |
29 |
@Override... |
106 |
|
public void initialize() throws InitializationException |
107 |
|
{ |
108 |
29 |
try { |
109 |
29 |
this.random = SecureRandom.getInstance("SHA1PRNG"); |
110 |
|
} catch (NoSuchAlgorithmException e) { |
111 |
|
|
112 |
0 |
this.random = new SecureRandom(); |
113 |
0 |
this.logger.warn("CSRFToken: Using default implementation of SecureRandom"); |
114 |
|
} |
115 |
29 |
byte[] seed = this.random.generateSeed(TOKEN_LENGTH); |
116 |
29 |
this.random.setSeed(seed); |
117 |
29 |
this.logger.debug("CSRFToken: Anti-CSRF secret token component has been initialized"); |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
@param |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
9 |
protected void setRandom(final SecureRandom random)... |
126 |
|
{ |
127 |
9 |
this.random = random; |
128 |
|
} |
129 |
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
130 |
2488 |
@Override... |
131 |
|
public String getToken() |
132 |
|
{ |
133 |
2488 |
DocumentReference key = getTokenKey(); |
134 |
|
|
135 |
2488 |
if (key == null) { |
136 |
193 |
if (guestToken == null) { |
137 |
30 |
guestToken = newToken(); |
138 |
|
} |
139 |
193 |
return guestToken; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
2295 |
String token = this.tokens.get(key); |
144 |
2295 |
if (token != null) { |
145 |
2212 |
return token; |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
83 |
synchronized (this.tokens) { |
150 |
83 |
if (!this.tokens.containsKey(key)) { |
151 |
83 |
this.tokens.put(key, newToken()); |
152 |
|
} |
153 |
83 |
return this.tokens.get(key); |
154 |
|
} |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
157 |
113 |
private String newToken()... |
158 |
|
{ |
159 |
113 |
byte[] bytes = new byte[TOKEN_LENGTH]; |
160 |
113 |
this.random.nextBytes(bytes); |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
113 |
return Base64.encodeBase64URLSafeString(bytes).replaceAll("[_=+-]", "x"); |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
167 |
30 |
@Override... |
168 |
|
public void clearToken() |
169 |
|
{ |
170 |
30 |
this.logger.debug("Forgetting CSRF token for [{}]", getTokenKey()); |
171 |
30 |
this.tokens.remove(getTokenKey()); |
172 |
|
} |
173 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
174 |
330 |
@Override... |
175 |
|
public boolean isTokenValid(String token) |
176 |
|
{ |
177 |
330 |
if (!this.configuration.isEnabled()) { |
178 |
0 |
return true; |
179 |
|
} |
180 |
330 |
String storedToken = getToken(); |
181 |
330 |
if (token == null || token.equals("") || !storedToken.equals(token)) { |
182 |
7 |
this.logger.warn("CSRFToken: Secret token verification failed, token: \"" + token |
183 |
|
+ "\", stored token: \"" + storedToken + "\""); |
184 |
7 |
return false; |
185 |
|
} |
186 |
322 |
return true; |
187 |
|
} |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
189 |
2 |
@Override... |
190 |
|
public String getResubmissionURL() |
191 |
|
{ |
192 |
|
|
193 |
2 |
String srid = SavedRequestManager.saveRequest(getRequest()); |
194 |
2 |
String resubmitUrl = getRequest().getRequestURI(); |
195 |
2 |
resubmitUrl += '?' + SavedRequestManager.getSavedRequestIdentifier() + "=" + srid; |
196 |
2 |
String query = "resubmit=" + urlEncode(resubmitUrl); |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
2 |
String backUrl = getDocumentURL(this.docBridge.getCurrentDocumentReference(), null); |
201 |
2 |
query += "&xback=" + urlEncode(backUrl); |
202 |
|
|
203 |
|
|
204 |
2 |
query += "&xpage=" + RESUBMIT_TEMPLATE; |
205 |
2 |
return backUrl + "?" + query; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
@param |
212 |
|
@param |
213 |
|
@return |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
2 |
private String getDocumentURL(DocumentReference reference, String query)... |
216 |
|
{ |
217 |
2 |
return this.docBridge.getDocumentURL(reference, "view", query, null); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@param |
224 |
|
@return |
225 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
226 |
4 |
private String urlEncode(String str)... |
227 |
|
{ |
228 |
|
|
229 |
4 |
String encoding = "UTF-8"; |
230 |
4 |
try { |
231 |
4 |
return URLEncoder.encode(str, encoding); |
232 |
|
} catch (UnsupportedEncodingException exception) { |
233 |
|
|
234 |
0 |
return ""; |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@return |
242 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
243 |
4 |
private HttpServletRequest getRequest()... |
244 |
|
{ |
245 |
4 |
Request request = this.container.getRequest(); |
246 |
4 |
if (request instanceof ServletRequest) { |
247 |
4 |
return ((ServletRequest) request).getHttpServletRequest(); |
248 |
|
} |
249 |
0 |
throw new RuntimeException("Not supported request type"); |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
@return |
256 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
257 |
2548 |
private DocumentReference getTokenKey()... |
258 |
|
{ |
259 |
2548 |
return this.docBridge.getCurrentUserReference(); |
260 |
|
} |
261 |
|
} |