├── .gitignore ├── AntiCSRFLibrary ├── anticsrf.jar ├── build.xml ├── lib │ ├── encoder-1.2.jar │ ├── gson-2.2.4.jar │ ├── keyczar-0.71g-090613.jar │ └── log4j-1.2.17.jar ├── pmd.xml └── src │ └── com │ └── gdssecurity │ └── anticsrf │ ├── CSRFFilter.java │ ├── exceptions │ ├── CSRFConfigException.java │ ├── CSRFSignerException.java │ ├── CSRFTokenGenerationException.java │ ├── CSRFTokenVerificationException.java │ └── CSRFUserSeedException.java │ ├── j2ee │ ├── J2EECSRFProtection.java │ ├── J2EEHmacCSRFProtection.java │ ├── J2EESession.java │ └── J2EESessionCSRFProtection.java │ ├── protections │ ├── CSRFProtection.java │ ├── CSRFProtectionFactory.java │ ├── HMACCSRFProtection.java │ ├── SessionProtection.java │ └── SesssionInterface.java │ ├── tags │ ├── CSRFToken.java │ ├── CSRFTokenFormTag.java │ ├── CSRFTokenParameterName.java │ ├── CSRFTokenUrlSpecificFormTag.java │ ├── CSRFTokenUrlSpecificUrlTag.java │ └── CSRFTokenUrlTag.java │ └── utils │ ├── Base64.java │ ├── Config.java │ ├── ConfigBuilder.java │ ├── ConfigUtil.java │ ├── Constants.java │ ├── KeyczarWrapper.java │ ├── SecureCompare.java │ └── StringUtil.java ├── AntiCSRFTestHarness ├── WebContent │ ├── Error.jsp │ ├── Home.jsp │ ├── META-INF │ │ └── MANIFEST.MF │ ├── WEB-INF │ │ ├── anticsrf.tld │ │ ├── anticsrf.xml │ │ ├── lib │ │ │ ├── anticsrf.jar │ │ │ ├── encoder-1.2.jar │ │ │ ├── gson-2.2.4.jar │ │ │ ├── keyczar-0.71g-090613.jar │ │ │ └── log4j-1.2.17.jar │ │ └── web.xml │ ├── onetimeuse.jsp │ ├── sitewide.jsp │ └── urlspecific.jsp └── src │ └── com │ └── sendsafely │ └── testapp │ ├── filters │ └── CustomCSRFFilter.java │ └── servlets │ ├── CustomOneTimeUseServlet.java │ ├── CustomSiteWideServlet.java │ ├── CustomURLSpecificServlet.java │ ├── ErrorServlet.java │ ├── HomeServlet.java │ ├── OneTimeUseServlet.java │ ├── SiteWideServlet.java │ └── URLSpecificServlet.java ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md ├── TODO.md └── resources ├── anticsrf.tld ├── anticsrf.xml └── lib ├── anticsrf.jar ├── gson-2.2.4.jar ├── keyczar-0.71g-090613.jar └── log4j-1.2.17.jar /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.class 3 | *.iml 4 | .idea/ 5 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/anticsrf.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFLibrary/anticsrf.jar -------------------------------------------------------------------------------- /AntiCSRFLibrary/build.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/lib/encoder-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFLibrary/lib/encoder-1.2.jar -------------------------------------------------------------------------------- /AntiCSRFLibrary/lib/gson-2.2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFLibrary/lib/gson-2.2.4.jar -------------------------------------------------------------------------------- /AntiCSRFLibrary/lib/keyczar-0.71g-090613.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFLibrary/lib/keyczar-0.71g-090613.jar -------------------------------------------------------------------------------- /AntiCSRFLibrary/lib/log4j-1.2.17.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFLibrary/lib/log4j-1.2.17.jar -------------------------------------------------------------------------------- /AntiCSRFLibrary/pmd.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | PMD Plugin preferences rule set 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 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/CSRFFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.util.logging.Logger; 23 | 24 | import javax.servlet.Filter; 25 | import javax.servlet.FilterChain; 26 | import javax.servlet.FilterConfig; 27 | import javax.servlet.RequestDispatcher; 28 | import javax.servlet.ServletException; 29 | import javax.servlet.ServletRequest; 30 | import javax.servlet.ServletResponse; 31 | import javax.servlet.http.HttpServletRequest; 32 | import javax.servlet.http.HttpServletResponse; 33 | import javax.servlet.http.HttpSession; 34 | 35 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 36 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 37 | import com.gdssecurity.anticsrf.j2ee.J2EEHmacCSRFProtection; 38 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 39 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 40 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 41 | import com.gdssecurity.anticsrf.utils.Constants; 42 | import com.gdssecurity.anticsrf.utils.StringUtil; 43 | 44 | public class CSRFFilter implements Filter 45 | { 46 | private static final Logger LOG = Logger.getLogger(CSRFFilter.class.getName()); 47 | FilterConfig filterConfig; 48 | 49 | /* 50 | * (non-Javadoc) 51 | * @see javax.servlet.Filter#destroy() 52 | */ 53 | public void destroy(){} 54 | 55 | public void init(final FilterConfig filterConfig) throws ServletException 56 | { 57 | LOG.info("The CSRFFilter is inititializing"); 58 | 59 | // Check to see if an init param has been set 60 | String configFile = filterConfig.getInitParameter(Constants.CONF_INITPARAMNAME); 61 | 62 | if(configFile == null) 63 | { 64 | configFile = "/WEB-INF/" + Constants.CONFIGNAME; 65 | LOG.info("No Filter init-param set, defaulting to loading AntiCSRF Configuration from "+configFile); 66 | } 67 | else 68 | { 69 | LOG.info("AntiCSRF Configuration init-param specified. Configuration file set to "+configFile); 70 | } 71 | 72 | InputStream inputStream = filterConfig.getServletContext().getResourceAsStream(configFile); 73 | ConfigUtil.loadConfig(inputStream); 74 | this.filterConfig = filterConfig; 75 | } 76 | 77 | public void doFilter(ServletRequest request, ServletResponse response, 78 | FilterChain chain) throws ServletException, IOException 79 | { 80 | HttpServletRequest req = (HttpServletRequest) request; 81 | HttpServletResponse res = (HttpServletResponse) response; 82 | 83 | LOG.fine("The CSRFFilter is running on URL: " + StringUtil.stripNewlines(req.getRequestURI()) ); 84 | 85 | // If Hmac mode, lets add a new token to the request attribute first 86 | // This will allow for a rolling timestamp on the CSRFToken 87 | 88 | // If Session, the token will be valid across the whole life of the 89 | // session token. Therefore, we will only generate a new one if a Token 90 | // is not currently set within session. 91 | 92 | J2EECSRFProtection csrfProtection = (J2EECSRFProtection)CSRFProtectionFactory.getCSRFProtection(); 93 | csrfProtection.setRequestObject(req); 94 | 95 | if(ConfigUtil.isHmacMode()) 96 | { 97 | csrfProtection.generateCSRFToken(); 98 | } 99 | else 100 | { 101 | HttpSession session = req.getSession(true); 102 | String storedCSRFToken = (String) session.getAttribute( 103 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR)); 104 | 105 | if(storedCSRFToken == null || storedCSRFToken.equals("")) 106 | { 107 | csrfProtection.generateCSRFToken(); 108 | } 109 | else 110 | { 111 | req.setAttribute( 112 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR), storedCSRFToken); 113 | } 114 | } 115 | 116 | try 117 | { 118 | if( !csrfProtection.verifyCSRFToken() ) 119 | { 120 | String err = "User submitted an invalid CSRFToken."; 121 | LOG.warning(err+", submittedToken=" + StringUtil.stripNewlines(req.getParameter( 122 | ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM))) ); 123 | throw new CSRFTokenVerificationException(err); 124 | } 125 | } 126 | catch( CSRFTokenVerificationException ex ) 127 | { 128 | // If MonitorMode is disabled, we handle the invalid CSRF Token validation error 129 | // If not, we continue normal execution. 130 | if(ConfigUtil.getProp(Constants.CONF_MONITORMODE).equals("no")) 131 | { 132 | handleError(req, res); 133 | return; 134 | } 135 | 136 | } 137 | 138 | chain.doFilter(request, response); 139 | } 140 | 141 | private void handleError(HttpServletRequest req, HttpServletResponse res) 142 | throws IOException, ServletException 143 | { 144 | // Check if the request is an XMLHTTPRequest/AJAX request 145 | if(ConfigUtil.getProp(Constants.CONF_ERROR_AJAX) != null) 146 | { 147 | if(req.getHeader("X-Requested-With") != null && 148 | req.getHeader("X-Requested-With").equals("XMLHttpRequest") ) 149 | { 150 | res.setContentType("application/json"); 151 | res.getWriter().write("{\"error\":{\"type\":\"invalid_csrf\"}"); 152 | return; 153 | } 154 | } 155 | 156 | // Token validation failed. Lets handle the error based on the config file 157 | if( ConfigUtil.getProp(Constants.CONF_ERROR).equals("redirect") ) 158 | { 159 | res.sendRedirect(ConfigUtil.getProp(Constants.CONF_ERRORVAL)); 160 | } 161 | else if( ConfigUtil.getProp(Constants.CONF_ERROR).equals("forward") ) 162 | { 163 | String forwardUrl = ConfigUtil.getProp(Constants.CONF_ERRORVAL); 164 | RequestDispatcher dispatcher = req.getRequestDispatcher(forwardUrl); 165 | dispatcher.forward(req, res); 166 | } 167 | else if( ConfigUtil.getProp(Constants.CONF_ERROR).equals("status_code")) 168 | { 169 | int statusCode = Integer.parseInt(ConfigUtil.getProp(Constants.CONF_ERRORVAL)); 170 | res.sendError(statusCode, "CSRF Token validation failed"); 171 | } 172 | else 173 | { 174 | // No configuration.. so lets just send them our own error 175 | res.sendError(403, "CSRF Token validation failed"); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/exceptions/CSRFConfigException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.exceptions; 19 | 20 | import javax.servlet.ServletException; 21 | 22 | public class CSRFConfigException extends ServletException 23 | { 24 | private static final long serialVersionUID = 1L; 25 | 26 | String error; 27 | 28 | public CSRFConfigException() 29 | { 30 | super(); 31 | error = "unknown"; 32 | } 33 | 34 | public CSRFConfigException(String err) 35 | { 36 | super(err); 37 | error = err; 38 | } 39 | 40 | public CSRFConfigException(Exception e) 41 | { 42 | super(e); 43 | error = e.getMessage(); 44 | } 45 | 46 | public String getError() 47 | { 48 | return error; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/exceptions/CSRFSignerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.exceptions; 19 | 20 | import javax.servlet.ServletException; 21 | 22 | public class CSRFSignerException extends ServletException 23 | { 24 | private static final long serialVersionUID = 1L; 25 | 26 | String error; 27 | 28 | public CSRFSignerException() 29 | { 30 | super(); 31 | error = "unknown"; 32 | } 33 | 34 | public CSRFSignerException(String err) 35 | { 36 | super(err); 37 | error = err; 38 | } 39 | 40 | public CSRFSignerException(Exception e) 41 | { 42 | super(e); 43 | error = e.getMessage(); 44 | } 45 | 46 | public String getError() 47 | { 48 | return error; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/exceptions/CSRFTokenGenerationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.exceptions; 19 | 20 | import javax.servlet.ServletException; 21 | 22 | public class CSRFTokenGenerationException extends ServletException 23 | { 24 | private static final long serialVersionUID = 1L; 25 | 26 | String error; 27 | 28 | public CSRFTokenGenerationException() 29 | { 30 | super(); 31 | error = "unknown"; 32 | } 33 | 34 | public CSRFTokenGenerationException(String err) 35 | { 36 | super(err); 37 | error = err; 38 | } 39 | 40 | public CSRFTokenGenerationException(Exception e) 41 | { 42 | super(e); 43 | error = e.getMessage(); 44 | } 45 | 46 | public String getError() 47 | { 48 | return error; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/exceptions/CSRFTokenVerificationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.exceptions; 19 | 20 | import javax.servlet.ServletException; 21 | 22 | public class CSRFTokenVerificationException extends ServletException 23 | { 24 | private static final long serialVersionUID = 1L; 25 | 26 | String error; 27 | 28 | public CSRFTokenVerificationException() 29 | { 30 | super(); 31 | error = "unknown"; 32 | } 33 | 34 | public CSRFTokenVerificationException(String err) 35 | { 36 | super(err); 37 | error = err; 38 | } 39 | 40 | public CSRFTokenVerificationException(Exception e) 41 | { 42 | super(e); 43 | error = e.getMessage(); 44 | } 45 | 46 | public String getError() 47 | { 48 | return error; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/exceptions/CSRFUserSeedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.exceptions; 19 | 20 | import javax.servlet.ServletException; 21 | 22 | public class CSRFUserSeedException extends ServletException 23 | { 24 | private static final long serialVersionUID = 1L; 25 | 26 | String error; 27 | 28 | public CSRFUserSeedException() 29 | { 30 | super(); 31 | error = "unknown"; 32 | } 33 | 34 | public CSRFUserSeedException(String err) 35 | { 36 | super(err); 37 | error = err; 38 | } 39 | 40 | public CSRFUserSeedException(Exception e) 41 | { 42 | super(e); 43 | error = e.getMessage(); 44 | } 45 | 46 | public String getError() 47 | { 48 | return error; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/j2ee/J2EECSRFProtection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.j2ee; 19 | 20 | import javax.servlet.http.HttpServletRequest; 21 | 22 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenGenerationException; 23 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 24 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 25 | 26 | public interface J2EECSRFProtection { 27 | 28 | public void setRequestObject(HttpServletRequest req); 29 | public boolean verifyCSRFToken() throws CSRFTokenVerificationException; 30 | public String generateCSRFToken() throws CSRFTokenGenerationException; 31 | public String getCSRFTokenParameterName(); 32 | public String generateUrlSpecificCSRFToken(String url) throws CSRFTokenGenerationException; 33 | public String getCSRFToken() throws CSRFTokenGenerationException; 34 | public void setUserSeed(String userSeed); 35 | } 36 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/j2ee/J2EEHmacCSRFProtection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.j2ee; 19 | 20 | import java.sql.Timestamp; 21 | import java.util.Date; 22 | import java.util.logging.Logger; 23 | 24 | import javax.servlet.http.HttpServletRequest; 25 | 26 | import org.keyczar.Signer; 27 | import org.keyczar.exceptions.KeyczarException; 28 | 29 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenGenerationException; 30 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 31 | import com.gdssecurity.anticsrf.protections.HMACCSRFProtection; 32 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 33 | import com.gdssecurity.anticsrf.utils.Constants; 34 | import com.gdssecurity.anticsrf.utils.KeyczarWrapper; 35 | 36 | public class J2EEHmacCSRFProtection implements J2EECSRFProtection 37 | { 38 | private static final Logger LOG = Logger.getLogger(J2EEHmacCSRFProtection.class.getName()); 39 | 40 | private HttpServletRequest req; 41 | private HMACCSRFProtection protection; 42 | 43 | @Override 44 | public void setRequestObject(HttpServletRequest req) { 45 | this.req = req; 46 | this.protection = new HMACCSRFProtection(getUserSeed()); 47 | } 48 | 49 | 50 | /* 51 | * Additional verification method for URL-specific tokens for a specified token max-age/timeout. 52 | */ 53 | /* 54 | public boolean verifyUrlSpecificCSRFToken(String token, Integer tokenTimeoutSecs) throws CSRFTokenVerificationException 55 | { 56 | return verifyCSRFToken(token, true, (tokenTimeoutSecs != null ? new Long(tokenTimeoutSecs) : null)); 57 | }*/ 58 | 59 | public boolean verifyCSRFToken() throws CSRFTokenVerificationException 60 | { 61 | // Get CSRF Token from configured request parameter 62 | String submittedCSRFToken = req.getParameter( 63 | ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM) ); 64 | return this.protection.verifyCSRFToken(req.getRequestURI(), submittedCSRFToken); 65 | } 66 | 67 | public String generateCSRFToken() throws CSRFTokenGenerationException 68 | { 69 | String csrfToken = this.protection.generateCSRFToken(); 70 | 71 | req.setAttribute( ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR), csrfToken ); 72 | 73 | return csrfToken; 74 | } 75 | 76 | public String generateUrlSpecificCSRFToken(String url) throws CSRFTokenGenerationException 77 | { 78 | return this.protection.generateUrlSpecificCSRFToken(url); 79 | } 80 | 81 | public String getCSRFToken() throws CSRFTokenGenerationException 82 | { 83 | String csrfToken = ""; 84 | 85 | try 86 | { 87 | csrfToken = req.getAttribute(ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR)).toString(); 88 | if(csrfToken == null) 89 | { 90 | csrfToken = generateCSRFToken(); 91 | } 92 | } 93 | catch(NullPointerException ex) 94 | { 95 | csrfToken = generateCSRFToken(); 96 | } 97 | 98 | return csrfToken; 99 | } 100 | 101 | 102 | 103 | public void setUserSeed(String userSeed) 104 | { 105 | req.setAttribute(ConfigUtil.getProp(Constants.CONF_HMAC_USERSEED_ATTR), userSeed); 106 | } 107 | 108 | @Override 109 | public String getCSRFTokenParameterName() { 110 | return this.protection.getCSRFTokenParameterName(); 111 | } 112 | 113 | protected String getUserSeed() 114 | { 115 | String userSeed = (String) req.getAttribute( 116 | ConfigUtil.getProp(Constants.CONF_HMAC_USERSEED_ATTR) ); 117 | 118 | if( userSeed == null ) 119 | { 120 | String err = "User Seed not found in HttpServletRequest attribute. " 121 | + "Defaulting to an generic user seed since we cannot tie the token to a user identity"; 122 | LOG.warning(err); 123 | return "anonymous"; 124 | } 125 | 126 | return userSeed; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/j2ee/J2EESession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.j2ee; 19 | 20 | import javax.servlet.http.HttpSession; 21 | 22 | import com.gdssecurity.anticsrf.protections.SesssionInterface; 23 | 24 | public class J2EESession implements SesssionInterface { 25 | 26 | private HttpSession session; 27 | 28 | public J2EESession(HttpSession session) 29 | { 30 | this.session = session; 31 | } 32 | 33 | @Override 34 | public Object getAttribute(String key) { 35 | return this.session.getAttribute(key); 36 | } 37 | 38 | @Override 39 | public void setAttribute(String key, Object obj) { 40 | session.setAttribute(key, obj); 41 | } 42 | 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/j2ee/J2EESessionCSRFProtection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.j2ee; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | 24 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenGenerationException; 25 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 26 | import com.gdssecurity.anticsrf.protections.SessionProtection; 27 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 28 | import com.gdssecurity.anticsrf.utils.Constants; 29 | 30 | public class J2EESessionCSRFProtection implements J2EECSRFProtection 31 | { 32 | private static final Logger LOG = Logger.getLogger(J2EESessionCSRFProtection.class.getName()); 33 | 34 | private HttpServletRequest req; 35 | private J2EESession session; 36 | private SessionProtection protection; 37 | 38 | @Override 39 | public void setRequestObject(HttpServletRequest req) { 40 | this.req = req; 41 | this.session = new J2EESession(req.getSession()); 42 | this.protection = new SessionProtection(session); 43 | } 44 | 45 | @Override 46 | public boolean verifyCSRFToken() throws CSRFTokenVerificationException 47 | { 48 | String url = req.getRequestURI(); 49 | String tokenFromUser = req.getParameter( ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM) ); 50 | return this.protection.verifyCSRFToken(url, tokenFromUser); 51 | } 52 | 53 | public String generateCSRFToken() throws CSRFTokenGenerationException 54 | { 55 | String encodedCSRFToken = this.protection.generateCSRFToken(); 56 | req.setAttribute( 57 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR), encodedCSRFToken); 58 | 59 | return encodedCSRFToken; 60 | } 61 | 62 | public String generateUrlSpecificCSRFToken(String url) 63 | throws CSRFTokenGenerationException 64 | { 65 | return this.protection.generateUrlSpecificCSRFToken(url); 66 | } 67 | 68 | public String getCSRFToken() 69 | throws CSRFTokenGenerationException 70 | { 71 | String csrfToken = ""; 72 | 73 | try 74 | { 75 | csrfToken = session.getAttribute( ConfigUtil.getProp((Constants.CONF_TOKEN_REQATTR))).toString(); 76 | if( csrfToken == null) 77 | { 78 | csrfToken = generateCSRFToken(); 79 | } 80 | } 81 | catch(NullPointerException ex) 82 | { 83 | csrfToken = generateCSRFToken(); 84 | } 85 | 86 | return csrfToken; 87 | } 88 | 89 | public void setUserSeed(String userSeed) 90 | { 91 | LOG.warning("AntiCSRF Library is running on Session mode and a call to the unsupported setUsedSeed method was performed"); 92 | return; 93 | } 94 | 95 | @Override 96 | public String getCSRFTokenParameterName() { 97 | return this.protection.getCSRFTokenParameterName(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/protections/CSRFProtection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.protections; 19 | 20 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenGenerationException; 21 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 22 | 23 | public interface CSRFProtection 24 | { 25 | public boolean verifyCSRFToken(String url, String tokenFromUser) throws CSRFTokenVerificationException; 26 | public String generateCSRFToken() throws CSRFTokenGenerationException; 27 | public String getCSRFTokenParameterName(); 28 | public String generateUrlSpecificCSRFToken(String url) throws CSRFTokenGenerationException; 29 | } 30 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/protections/CSRFProtectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.protections; 19 | 20 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 21 | import com.gdssecurity.anticsrf.j2ee.J2EEHmacCSRFProtection; 22 | import com.gdssecurity.anticsrf.j2ee.J2EESessionCSRFProtection; 23 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 24 | 25 | public class CSRFProtectionFactory 26 | { 27 | public static J2EECSRFProtection getCSRFProtection() 28 | { 29 | if(ConfigUtil.isHmacMode()) 30 | { 31 | return new J2EEHmacCSRFProtection(); 32 | } 33 | 34 | // Session based Protection mode is the default 35 | return new J2EESessionCSRFProtection(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/protections/HMACCSRFProtection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.protections; 19 | 20 | import java.sql.Timestamp; 21 | import java.util.Date; 22 | import java.util.logging.Logger; 23 | 24 | import org.keyczar.Signer; 25 | import org.keyczar.exceptions.KeyczarException; 26 | 27 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenGenerationException; 28 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 29 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 30 | import com.gdssecurity.anticsrf.utils.Constants; 31 | import com.gdssecurity.anticsrf.utils.StringUtil; 32 | import com.gdssecurity.anticsrf.utils.KeyczarWrapper; 33 | 34 | public class HMACCSRFProtection implements CSRFProtection { 35 | 36 | private static final Logger LOG = Logger.getLogger(HMACCSRFProtection.class.getName()); 37 | 38 | private String userSeed; 39 | 40 | public HMACCSRFProtection(String userSeed) 41 | { 42 | this.userSeed = userSeed; 43 | } 44 | 45 | @Override 46 | public boolean verifyCSRFToken(String url, String tokenFromUser) throws CSRFTokenVerificationException { 47 | return verifyCSRFToken(url, tokenFromUser, ConfigUtil.hasUrlSpecificConfig(url)); 48 | } 49 | 50 | @Override 51 | public String generateCSRFToken() throws CSRFTokenGenerationException { 52 | String csrfToken = handleCSRFTokenGeneration(this.userSeed); 53 | 54 | LOG.fine("Setting csrfToken: attrname=" + 55 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR) + 56 | ", csrftoken=" + csrfToken); 57 | return csrfToken; 58 | } 59 | 60 | @Override 61 | public String getCSRFTokenParameterName() { 62 | return ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 63 | } 64 | 65 | @Override 66 | public String generateUrlSpecificCSRFToken(String url) 67 | throws CSRFTokenGenerationException { 68 | return this.generateCSRFToken(userSeed+":"+url); 69 | } 70 | 71 | private String generateCSRFToken(String userSeed) throws CSRFTokenGenerationException { 72 | String csrfToken = handleCSRFTokenGeneration(userSeed); 73 | 74 | LOG.info("Setting csrfToken: attrname=" + 75 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR) + 76 | ", csrftoken=" + csrfToken); 77 | return csrfToken; 78 | } 79 | 80 | private boolean verifyCSRFToken(String url, String token, boolean isUrlSpecific, Long timeout) throws CSRFTokenVerificationException 81 | { 82 | // Get CSRF User Seed from request attribute and set the default timeout 83 | StringBuffer userSeed = new StringBuffer(this.userSeed); 84 | 85 | if(isUrlSpecific) 86 | { 87 | // Add the current url to the seed and pass in it's configured timeout 88 | userSeed.append( ":" + url ); 89 | LOG.fine("Using URL Specific timeout values"); 90 | } 91 | 92 | return handleCSRFTokenVerification(url, token, userSeed.toString(), timeout); 93 | } 94 | 95 | private boolean handleCSRFTokenVerification(String url, String submittedCSRFToken, String userSeed, Long configuredTimeout ) throws CSRFTokenVerificationException 96 | { 97 | if( ConfigUtil.isURLExempt( url ) ) 98 | { 99 | return true; 100 | } 101 | 102 | try 103 | { 104 | if( submittedCSRFToken == null) 105 | { 106 | String err = "HttpServletRequest is missing CSRFToken Parameter." + 107 | "userSeed=" + StringUtil.stripNewlines(userSeed); 108 | LOG.warning(err); 109 | return false; 110 | } 111 | 112 | String[] csrfTokenContents = submittedCSRFToken.split(":"); 113 | 114 | if( csrfTokenContents.length != 2 ) 115 | { 116 | LOG.warning("CSRF Token contains invalid amount of delimiters. "+ 117 | "userSeed=" + StringUtil.stripNewlines(userSeed) + 118 | ", submittedToken= " + StringUtil.stripNewlines(submittedCSRFToken)); 119 | return false; 120 | } 121 | 122 | String submittedHmac = csrfTokenContents[0]; 123 | String submittedTimestamp = csrfTokenContents[1]; 124 | 125 | // Get Keyczar Signer object 126 | KeyczarWrapper keyczarWrapper = ConfigUtil.getKeyczarWrapper(); 127 | Signer csrfSigner = keyczarWrapper.getCSRFSigner(); 128 | 129 | if( !csrfSigner.verify(userSeed + ":" + submittedTimestamp, submittedHmac) ) 130 | { 131 | LOG.warning("Submitted CSRF Token did not contain a valid HMAC signature. "+ 132 | "userSeed=" + StringUtil.stripNewlines(userSeed) + 133 | ", submittedToken=" + StringUtil.stripNewlines(submittedCSRFToken)); 134 | return false; 135 | } 136 | 137 | if( timestampIsExpired( Long.valueOf(submittedTimestamp), configuredTimeout) ) 138 | { 139 | LOG.warning("Submitted CSRF Token is expired. "+ 140 | "userSeed=" + StringUtil.stripNewlines(userSeed) + 141 | ", submittedToken= " + StringUtil.stripNewlines(submittedCSRFToken)); 142 | return false; 143 | } 144 | 145 | // We passed all the checks. 146 | return true; 147 | } 148 | catch( KeyczarException ex ) 149 | { 150 | String err = "Encountered error performing HMAC signature validation with the Keyczar library"; 151 | 152 | // Logging a warning here since this exception is caught and handled by the filter. This should 153 | // should be considered a security warning. 154 | LOG.warning(err+ ", userSeed=" + StringUtil.stripNewlines(userSeed) + 155 | ", submittedToken=" + StringUtil.stripNewlines(submittedCSRFToken) + 156 | ", exception=" + ex.getMessage() ); 157 | throw new CSRFTokenVerificationException(err); 158 | } 159 | catch( NumberFormatException ex ) 160 | { 161 | String err = "Timestamp submitted within CSRFToken is not in a valid format"; 162 | 163 | LOG.warning(err + ", Submitted CSRFToken="+ 164 | submittedCSRFToken + ", userSeed=" + userSeed); 165 | throw new CSRFTokenVerificationException(err); 166 | } 167 | } 168 | 169 | private boolean timestampIsExpired( Long submittedTimestamp, Long configuredTimeout ) 170 | { 171 | try 172 | { 173 | Date current = new Date(); 174 | 175 | Timestamp currentTime = new Timestamp( current.getTime() ); 176 | Timestamp timeToCompare = new Timestamp(submittedTimestamp); 177 | 178 | Long diff = currentTime.getTime() - timeToCompare.getTime(); 179 | Long diffInSeconds = diff / 1000; // 1,000 MilliSecs in a second 180 | 181 | if (diffInSeconds > configuredTimeout) 182 | { 183 | return true; 184 | } 185 | } 186 | catch (Exception e) 187 | { 188 | //log severe? 189 | LOG.severe("Unexpected error occurred during CSRFToken timestamp verification, " + 190 | "exceptionmessage=" + e.getMessage()); 191 | return true; 192 | } 193 | 194 | return false; 195 | } 196 | 197 | private boolean verifyCSRFToken(String url, String tokenFromUser, boolean isUrlSpecific) throws CSRFTokenVerificationException 198 | { 199 | Long timeout = (isUrlSpecific 200 | ? ConfigUtil.getUrlSpecificConfig(url) 201 | : Long.valueOf( ConfigUtil.getProp(Constants.CONF_HMAC_SITEWIDE_TIMEOUT) )); 202 | 203 | return verifyCSRFToken(url, tokenFromUser, isUrlSpecific, timeout); 204 | } 205 | 206 | private String handleCSRFTokenGeneration(String unhashedToken) throws CSRFTokenGenerationException 207 | { 208 | try 209 | { 210 | Date currentTime = new Date(); 211 | String currentTimeString = String.valueOf( currentTime.getTime() ); 212 | KeyczarWrapper keyczarWrapper = ConfigUtil.getKeyczarWrapper(); 213 | Signer csrfSigner = keyczarWrapper.getCSRFSigner(); 214 | String csrfHmac = csrfSigner.sign(unhashedToken + ":" + currentTimeString); 215 | 216 | return csrfHmac + ":" + currentTimeString; 217 | } 218 | catch( KeyczarException ex ) 219 | { 220 | String err = "Encountered error creating HMAC signature with the Keyczar library" 221 | + ", exceptionmessage=" + ex.getMessage(); 222 | LOG.info(err); 223 | throw new CSRFTokenGenerationException(err); 224 | } 225 | } 226 | 227 | } 228 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/protections/SessionProtection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.protections; 19 | 20 | import java.security.NoSuchAlgorithmException; 21 | import java.security.NoSuchProviderException; 22 | import java.security.SecureRandom; 23 | import java.util.HashMap; 24 | import java.util.logging.Logger; 25 | 26 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenGenerationException; 27 | import com.gdssecurity.anticsrf.exceptions.CSRFTokenVerificationException; 28 | import com.gdssecurity.anticsrf.utils.Base64; 29 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 30 | import com.gdssecurity.anticsrf.utils.Constants; 31 | import com.gdssecurity.anticsrf.utils.StringUtil; 32 | import com.gdssecurity.anticsrf.utils.SecureCompare; 33 | 34 | public class SessionProtection implements CSRFProtection { 35 | 36 | private static final Logger LOG = Logger.getLogger(SessionProtection.class.getName()); 37 | 38 | SesssionInterface session; 39 | 40 | public SessionProtection(SesssionInterface session) 41 | { 42 | this.session = session; 43 | } 44 | 45 | @Override 46 | public boolean verifyCSRFToken(String url, String tokenFromUser) 47 | throws CSRFTokenVerificationException { 48 | if( ConfigUtil.hasOneTimeUseConfig(url) ) 49 | { 50 | return verifyOneTimeUseCSRFToken(url, tokenFromUser); 51 | } 52 | 53 | return verifyCSRFToken(url, tokenFromUser, ConfigUtil.hasUrlSpecificConfig(url)); 54 | } 55 | 56 | @Override 57 | public String generateCSRFToken() throws CSRFTokenGenerationException { 58 | String encodedCSRFToken = generateRandomToken(); 59 | LOG.fine("Setting CSRFToken into Session: StoredToken="+encodedCSRFToken); 60 | 61 | session.setAttribute( 62 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR), encodedCSRFToken); 63 | 64 | return encodedCSRFToken; 65 | } 66 | 67 | @Override 68 | public String getCSRFTokenParameterName() { 69 | return ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 70 | } 71 | 72 | @Override 73 | public String generateUrlSpecificCSRFToken(String url) throws CSRFTokenGenerationException { 74 | @SuppressWarnings("unchecked") 75 | HashMap urlSpecificTokens = 76 | (HashMap) session.getAttribute( 77 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR)+"urlspecific"); 78 | 79 | // If URLSpecificToken Map is not in session, lets create one and add 80 | if(urlSpecificTokens == null) 81 | { 82 | urlSpecificTokens = new HashMap(); 83 | session.setAttribute( 84 | ConfigUtil.getProp( 85 | Constants.CONF_TOKEN_REQATTR)+"urlspecific", urlSpecificTokens); 86 | } 87 | 88 | // Only set a new token if one does not already exist 89 | if(!urlSpecificTokens.containsKey(url)) 90 | { 91 | String encodedCSRFToken = generateRandomToken(); 92 | LOG.fine("Setting a new url specific token. url=" + StringUtil.stripNewlines(url) 93 | +", newToken=" + encodedCSRFToken); 94 | urlSpecificTokens.put(url, encodedCSRFToken); 95 | } 96 | else 97 | { 98 | LOG.fine("URL Specific Mapping already exists. using existing value"); 99 | } 100 | 101 | return urlSpecificTokens.get(url); 102 | } 103 | 104 | // One-time use tokens will be UrlSpecific Tokens which are removed 105 | // from session upon validation. 106 | public boolean verifyOneTimeUseCSRFToken(String url, String tokenFromUser) throws CSRFTokenVerificationException 107 | { 108 | boolean isValidToken = verifyCSRFToken(url, tokenFromUser, true); 109 | 110 | @SuppressWarnings("unchecked") 111 | HashMap urlSpecificTokens = 112 | (HashMap) session.getAttribute( 113 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR)+"urlspecific"); 114 | 115 | if(urlSpecificTokens != null) 116 | { 117 | urlSpecificTokens.remove(url); 118 | } 119 | 120 | return isValidToken; 121 | } 122 | 123 | private boolean verifyCSRFToken(String url, String tokenFromUser, boolean isUrlSpecific) throws CSRFTokenVerificationException 124 | { 125 | if( ConfigUtil.isURLExempt(url) ) 126 | { 127 | return true; 128 | } 129 | 130 | String storedCSRFToken = (String) session.getAttribute( 131 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR) ); 132 | 133 | if(isUrlSpecific) 134 | { 135 | LOG.fine("About to perform urlspecific CSRF Token verification"); 136 | 137 | @SuppressWarnings("unchecked") 138 | HashMap urlSpecificTokens = 139 | (HashMap) session.getAttribute( 140 | ConfigUtil.getProp(Constants.CONF_TOKEN_REQATTR)+"urlspecific"); 141 | try 142 | { 143 | storedCSRFToken = urlSpecificTokens.get(url); 144 | LOG.fine("Reading URL Specific Token prior to verification: tokenread="+StringUtil.stripNewlines(storedCSRFToken)); 145 | if(storedCSRFToken == null) 146 | { 147 | return false; 148 | } 149 | } 150 | catch(NullPointerException ex) 151 | { 152 | String err = "No URL Specific Token found. URL="+StringUtil.stripNewlines(url); 153 | LOG.warning(err); 154 | return false; 155 | } 156 | 157 | } 158 | 159 | return handleCSRFTokenVerification(tokenFromUser, storedCSRFToken); 160 | } 161 | 162 | private boolean handleCSRFTokenVerification(String tokenFromUser, String storedCSRFToken) throws CSRFTokenVerificationException 163 | { 164 | LOG.fine("About to compare: submittedToken="+StringUtil.stripNewlines(tokenFromUser) + 165 | ", storedToken="+storedCSRFToken); 166 | 167 | if( tokenFromUser != null && storedCSRFToken != null ) 168 | { 169 | // SecureCompare validates the entire string and is therefore not susceptible to timing attacks 170 | if(SecureCompare.isEqual(tokenFromUser.getBytes(), storedCSRFToken.getBytes())) 171 | { 172 | return true; 173 | } 174 | 175 | LOG.warning("Failed to validate user's csrfToken: submittedToken=" + 176 | StringUtil.stripNewlines(tokenFromUser) + ", expectedToken="+storedCSRFToken); 177 | } 178 | 179 | return false; 180 | } 181 | 182 | private String generateRandomToken() throws CSRFTokenGenerationException 183 | { 184 | SecureRandom sr; 185 | 186 | try 187 | { 188 | sr = SecureRandom.getInstance("SHA1PRNG", "SUN"); 189 | } 190 | catch (NoSuchAlgorithmException e) 191 | { 192 | String err = "Failed to generate CSRFToken using SecureRandom" 193 | + ", exceptionMessage=" + e.getMessage(); 194 | LOG.severe(err); 195 | throw new CSRFTokenGenerationException(err + ", exceptionMessage=" 196 | + e.getMessage()); 197 | 198 | } 199 | catch (NoSuchProviderException e) 200 | { 201 | // Let's try and get the preferred one if SUN doesn't exist. 202 | try 203 | { 204 | sr = SecureRandom.getInstance("SHA1PRNG"); 205 | } 206 | catch (NoSuchAlgorithmException e1) { 207 | String err = "Failed to generate CSRFToken using SecureRandom" 208 | + ", exceptionMessage=" + e1.getMessage(); 209 | LOG.severe(err); 210 | throw new CSRFTokenGenerationException(err + ", exceptionMessage=" 211 | + e1.getMessage()); 212 | } 213 | } 214 | 215 | byte[] randomBytes = new byte[32]; 216 | sr.nextBytes(randomBytes); 217 | return Base64.encode(randomBytes); 218 | } 219 | 220 | } 221 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/protections/SesssionInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.protections; 19 | 20 | public interface SesssionInterface { 21 | 22 | public Object getAttribute(String key); 23 | public void setAttribute(String key, Object obj); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/tags/CSRFToken.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.tags; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.jsp.tagext.BodyTagSupport; 24 | 25 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 26 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 27 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 28 | 29 | import org.owasp.encoder.*; 30 | 31 | public class CSRFToken extends BodyTagSupport { 32 | 33 | private static final long serialVersionUID = -8218630240154573675L; 34 | 35 | private static final Logger LOG = Logger.getLogger(CSRFTokenFormTag.class.getName()); 36 | 37 | public int doStartTag() 38 | { 39 | try 40 | { 41 | HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); 42 | J2EECSRFProtection csrfProtection = (J2EECSRFProtection)CSRFProtectionFactory.getCSRFProtection(); 43 | csrfProtection.setRequestObject(req); 44 | String csrfToken = csrfProtection.getCSRFToken(); 45 | 46 | pageContext.getOut().print(Encode.forHtmlAttribute(csrfToken)); 47 | } 48 | catch (Exception e) 49 | { 50 | e.printStackTrace(); 51 | LOG.severe("Failed to write CSRF Token through taglib: exceptionmessage=" + e.getMessage()); 52 | } 53 | return SKIP_BODY; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/tags/CSRFTokenFormTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.tags; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.jsp.tagext.BodyTagSupport; 24 | 25 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 26 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 27 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 28 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 29 | import com.gdssecurity.anticsrf.utils.Constants; 30 | 31 | import org.owasp.encoder.*; 32 | 33 | public class CSRFTokenFormTag extends BodyTagSupport 34 | { 35 | private static final long serialVersionUID = 1L; 36 | private static final Logger LOG = Logger.getLogger(CSRFTokenFormTag.class.getName()); 37 | 38 | public int doStartTag() 39 | { 40 | try 41 | { 42 | HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); 43 | J2EECSRFProtection csrfProtection = (J2EECSRFProtection)CSRFProtectionFactory.getCSRFProtection(); 44 | csrfProtection.setRequestObject(req); 45 | String csrfToken = csrfProtection.getCSRFToken(); 46 | String tokenParamName = ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 47 | 48 | pageContext.getOut().print(""); 51 | } 52 | catch (Exception e) 53 | { 54 | e.printStackTrace(); 55 | LOG.severe("Failed to write CSRF Token through taglib: exceptionmessage=" + e.getMessage()); 56 | } 57 | return SKIP_BODY; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/tags/CSRFTokenParameterName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.tags; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.jsp.tagext.BodyTagSupport; 23 | 24 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 25 | import com.gdssecurity.anticsrf.utils.Constants; 26 | 27 | import org.owasp.encoder.*; 28 | 29 | public class CSRFTokenParameterName extends BodyTagSupport { 30 | 31 | private static final long serialVersionUID = 6452788175106246620L; 32 | private static final Logger LOG = Logger.getLogger(CSRFTokenParameterName.class.getName()); 33 | 34 | public int doStartTag() 35 | { 36 | try 37 | { 38 | String tokenParamName = ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 39 | pageContext.getOut().print(Encode.forHtmlAttribute(tokenParamName)); 40 | } 41 | catch (Exception e) 42 | { 43 | e.printStackTrace(); 44 | LOG.severe("Failed to write CSRF Token Parameter name taglib: exceptionmessage=" + e.getMessage()); 45 | } 46 | return SKIP_BODY; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/tags/CSRFTokenUrlSpecificFormTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.tags; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.jsp.tagext.BodyTagSupport; 24 | 25 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 26 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 27 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 28 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 29 | import com.gdssecurity.anticsrf.utils.Constants; 30 | 31 | import org.owasp.encoder.*; 32 | 33 | public class CSRFTokenUrlSpecificFormTag extends BodyTagSupport 34 | { 35 | private static final long serialVersionUID = 1L; 36 | private static final Logger LOG = Logger.getLogger(CSRFTokenUrlSpecificFormTag.class.getName()); 37 | 38 | protected String url = null; 39 | 40 | public String getUrl() 41 | { 42 | return(this.url); 43 | } 44 | 45 | public void setUrl(String url) 46 | { 47 | this.url = url; 48 | } 49 | 50 | public int doStartTag() 51 | { 52 | try 53 | { 54 | HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); 55 | 56 | J2EECSRFProtection csrfProtection = (J2EECSRFProtection)CSRFProtectionFactory.getCSRFProtection(); 57 | csrfProtection.setRequestObject(req); 58 | String csrfToken = csrfProtection.generateUrlSpecificCSRFToken(this.url); 59 | String tokenParamName = ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 60 | 61 | pageContext.getOut().print(""); 64 | } 65 | catch (Exception e) 66 | { 67 | LOG.severe("Failed to write CSRF Token through taglib: exceptionmessage=" + e.getMessage()); 68 | } 69 | return SKIP_BODY; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/tags/CSRFTokenUrlSpecificUrlTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.tags; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.jsp.tagext.BodyTagSupport; 24 | 25 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 26 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 27 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 28 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 29 | import com.gdssecurity.anticsrf.utils.Constants; 30 | 31 | import org.owasp.encoder.*; 32 | 33 | public class CSRFTokenUrlSpecificUrlTag extends BodyTagSupport 34 | { 35 | private static final long serialVersionUID = 1L; 36 | private static final Logger LOG = Logger.getLogger(CSRFTokenUrlSpecificUrlTag.class.getName()); 37 | 38 | protected String url = null; 39 | 40 | public String getUrl() 41 | { 42 | 43 | return(this.url); 44 | } 45 | 46 | public void setUrl(String url) 47 | { 48 | this.url = url; 49 | } 50 | 51 | public int doStartTag() 52 | { 53 | try 54 | { 55 | HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); 56 | 57 | J2EECSRFProtection csrfProtection = (J2EECSRFProtection)CSRFProtectionFactory.getCSRFProtection(); 58 | csrfProtection.setRequestObject(req); 59 | String csrfToken = csrfProtection.generateUrlSpecificCSRFToken(this.url); 60 | String tokenParamName = ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 61 | 62 | pageContext.getOut().print(Encode.forUriComponent(tokenParamName) + 63 | "=" + Encode.forUriComponent(csrfToken)); 64 | } 65 | catch (Exception e) 66 | { 67 | LOG.severe("Failed to write CSRF Token through taglib: exceptionmessage=" + e.getMessage()); 68 | } 69 | 70 | return SKIP_BODY; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/tags/CSRFTokenUrlTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.tags; 19 | 20 | import java.util.logging.Logger; 21 | 22 | import javax.servlet.http.HttpServletRequest; 23 | import javax.servlet.jsp.tagext.BodyTagSupport; 24 | 25 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 26 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 27 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 28 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 29 | import com.gdssecurity.anticsrf.utils.Constants; 30 | 31 | import org.owasp.encoder.*; 32 | 33 | public class CSRFTokenUrlTag extends BodyTagSupport 34 | { 35 | private static final long serialVersionUID = 1L; 36 | private static final Logger LOG = Logger.getLogger(CSRFTokenUrlTag.class.getName()); 37 | 38 | public int doStartTag() 39 | { 40 | try 41 | { 42 | HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); 43 | 44 | J2EECSRFProtection csrfProtection = (J2EECSRFProtection)CSRFProtectionFactory.getCSRFProtection(); 45 | csrfProtection.setRequestObject(req); 46 | String csrfToken = csrfProtection.getCSRFToken(); 47 | String tokenParamName = ConfigUtil.getProp(Constants.CONF_TOKEN_PARAM); 48 | 49 | pageContext.getOut().print(Encode.forUriComponent(tokenParamName) + 50 | "=" + Encode.forUriComponent(csrfToken)); 51 | } 52 | catch (Exception e) 53 | { 54 | e.printStackTrace(); 55 | LOG.severe("Failed to write CSRF Token through taglib: exceptionmessage=" + e.getMessage()); 56 | } 57 | 58 | return SKIP_BODY; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/Base64.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.utils; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | public class Base64 23 | { 24 | public static String encode(byte[] data) 25 | { 26 | char[] tbl = { 27 | 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 28 | 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 29 | 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 30 | 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' }; 31 | 32 | StringBuilder buffer = new StringBuilder(); 33 | int pad = 0; 34 | for (int i = 0; i < data.length; i += 3) { 35 | 36 | int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF; 37 | if (i + 1 < data.length) { 38 | b |= (data[i+1] & 0xFF) << 8; 39 | } else { 40 | pad++; 41 | } 42 | if (i + 2 < data.length) { 43 | b |= (data[i+2] & 0xFF); 44 | } else { 45 | pad++; 46 | } 47 | 48 | for (int j = 0; j < 4 - pad; j++) { 49 | int c = (b & 0xFC0000) >> 18; 50 | buffer.append(tbl[c]); 51 | b <<= 6; 52 | } 53 | } 54 | for (int j = 0; j < pad; j++) { 55 | buffer.append("="); 56 | } 57 | 58 | return buffer.toString(); 59 | } 60 | 61 | public static byte[] decode(String data) 62 | { 63 | int[] tbl = { 64 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 65 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 66 | -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 67 | 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 68 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 69 | 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 70 | 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 71 | 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 72 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 73 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 74 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 75 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 76 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 77 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 78 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; 79 | byte[] bytes = data.getBytes(); 80 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 81 | for (int i = 0; i < bytes.length; ) { 82 | int b = 0; 83 | if (tbl[bytes[i]] != -1) { 84 | b = (tbl[bytes[i]] & 0xFF) << 18; 85 | } 86 | // skip unknown characters 87 | else { 88 | i++; 89 | continue; 90 | } 91 | if (i + 1 < bytes.length && tbl[bytes[i+1]] != -1) { 92 | b = b | ((tbl[bytes[i+1]] & 0xFF) << 12); 93 | } 94 | if (i + 2 < bytes.length && tbl[bytes[i+2]] != -1) { 95 | b = b | ((tbl[bytes[i+2]] & 0xFF) << 6); 96 | } 97 | if (i + 3 < bytes.length && tbl[bytes[i+3]] != -1) { 98 | b = b | (tbl[bytes[i+3]] & 0xFF); 99 | } 100 | while ((b & 0xFFFFFF) != 0) { 101 | int c = (b & 0xFF0000) >> 16; 102 | buffer.write((char)c); 103 | b <<= 8; 104 | } 105 | i += 4; 106 | } 107 | return buffer.toByteArray(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.utils; 19 | 20 | import java.util.List; 21 | 22 | public class Config 23 | { 24 | 25 | private String protectionMode; 26 | private String hmacKeyFile; 27 | private List exemptURLs; 28 | private String tokenRequestAttribute; 29 | private String tokenParameterName; 30 | private Boolean monitorMode; 31 | private String errorMode; 32 | private String errorValue; 33 | 34 | 35 | Config() { }; 36 | 37 | 38 | Config( 39 | String protectionMode, 40 | String hmacKeyFile, 41 | List exemptURLs, 42 | String tokenRequestAttribute, 43 | String tokenParameterName, 44 | Boolean monitorMode, 45 | String errorMode, 46 | String errorValue) 47 | { 48 | this.protectionMode = protectionMode; 49 | this.hmacKeyFile = hmacKeyFile; 50 | this.exemptURLs = exemptURLs; 51 | this.tokenRequestAttribute = tokenRequestAttribute; 52 | this.tokenParameterName = tokenParameterName; 53 | this.monitorMode = monitorMode; 54 | this.errorMode = errorMode; 55 | this.errorValue = errorValue; 56 | } 57 | 58 | 59 | public String getProtectionMode() 60 | { 61 | return protectionMode; 62 | } 63 | 64 | 65 | public String getHMACKeyFile() 66 | { 67 | return hmacKeyFile; 68 | } 69 | 70 | 71 | public List getExemptURLs() 72 | { 73 | return exemptURLs; 74 | } 75 | 76 | 77 | public String getTokenRequestAttribute() 78 | { 79 | return tokenRequestAttribute; 80 | } 81 | 82 | 83 | public String getTokenParameterName() 84 | { 85 | return tokenParameterName; 86 | } 87 | 88 | 89 | public Boolean getMonitorMode() 90 | { 91 | return monitorMode; 92 | } 93 | 94 | 95 | public String getErrorMode() 96 | { 97 | return errorMode; 98 | } 99 | 100 | 101 | public String getErrorValue() 102 | { 103 | return errorValue; 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/ConfigBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.utils; 19 | 20 | import java.util.List; 21 | 22 | public class ConfigBuilder 23 | { 24 | 25 | private String protectionMode; 26 | private String hmacKeyFile; 27 | private List exemptURLs; 28 | private String tokenRequestAttribute; 29 | private String tokenParameterName; 30 | private Boolean monitorMode; 31 | private String errorMode; 32 | private String errorValue; 33 | 34 | 35 | public ConfigBuilder() { } 36 | 37 | 38 | public String getProtectionMode() 39 | { 40 | return protectionMode; 41 | } 42 | 43 | 44 | public ConfigBuilder setProtectionMode(String protectionMode) 45 | { 46 | this.protectionMode = protectionMode; 47 | return this; 48 | } 49 | 50 | 51 | public String getHMACKeyFile() 52 | { 53 | return hmacKeyFile; 54 | } 55 | 56 | 57 | public ConfigBuilder setHMACKeyFile(String hmacKeyFile) 58 | { 59 | this.hmacKeyFile = hmacKeyFile; 60 | return this; 61 | } 62 | 63 | 64 | public List getExemptURLs() 65 | { 66 | return exemptURLs; 67 | } 68 | 69 | 70 | public ConfigBuilder setExemptURLs(List exemptURLs) 71 | { 72 | this.exemptURLs = exemptURLs; 73 | return this; 74 | } 75 | 76 | 77 | public String getTokenRequestAttribute() 78 | { 79 | return tokenRequestAttribute; 80 | } 81 | 82 | 83 | public ConfigBuilder setTokenRequestAttribute(String tokenRequestAttribute) 84 | { 85 | this.tokenRequestAttribute = tokenRequestAttribute; 86 | return this; 87 | } 88 | 89 | 90 | public String getTokenParameterName() 91 | { 92 | return tokenParameterName; 93 | } 94 | 95 | 96 | public ConfigBuilder setTokenParameterName(String tokenParameterName) 97 | { 98 | this.tokenParameterName = tokenParameterName; 99 | return this; 100 | } 101 | 102 | 103 | public Boolean getMonitorMode() 104 | { 105 | return monitorMode; 106 | } 107 | 108 | 109 | public ConfigBuilder setMonitorMode(Boolean monitorMode) 110 | { 111 | this.monitorMode = monitorMode; 112 | return this; 113 | } 114 | 115 | 116 | public String getErrorMode() 117 | { 118 | return errorMode; 119 | } 120 | 121 | 122 | public ConfigBuilder setErrorMode(String errorMode) 123 | { 124 | this.errorMode = errorMode; 125 | return this; 126 | } 127 | 128 | 129 | public String getErrorValue() 130 | { 131 | return errorValue; 132 | } 133 | 134 | 135 | public ConfigBuilder setErrorValue(String errorValue) 136 | { 137 | this.errorValue = errorValue; 138 | return this; 139 | } 140 | 141 | 142 | public Config getConfig() 143 | { 144 | return new Config( 145 | protectionMode, 146 | hmacKeyFile, 147 | exemptURLs, 148 | tokenRequestAttribute, 149 | tokenParameterName, 150 | monitorMode, 151 | errorMode, 152 | errorValue); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/ConfigUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | 19 | package com.gdssecurity.anticsrf.utils; 20 | 21 | import java.io.ByteArrayInputStream; 22 | import java.io.File; 23 | import java.io.FileInputStream; 24 | import java.io.FileNotFoundException; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.util.HashMap; 28 | import java.util.Properties; 29 | import java.util.logging.LogManager; 30 | import java.util.logging.Logger; 31 | import java.util.regex.Matcher; 32 | import java.util.regex.Pattern; 33 | 34 | import javax.xml.parsers.DocumentBuilder; 35 | import javax.xml.parsers.DocumentBuilderFactory; 36 | import javax.xml.parsers.ParserConfigurationException; 37 | 38 | import org.w3c.dom.Document; 39 | import org.w3c.dom.Element; 40 | import org.w3c.dom.Node; 41 | import org.w3c.dom.NodeList; 42 | import org.xml.sax.SAXException; 43 | 44 | import com.gdssecurity.anticsrf.exceptions.CSRFConfigException; 45 | import com.gdssecurity.anticsrf.exceptions.CSRFSignerException; 46 | import com.gdssecurity.anticsrf.utils.StringUtil; 47 | 48 | public class ConfigUtil 49 | { 50 | private static final Logger LOG = Logger.getLogger(ConfigUtil.class.getName()); 51 | private static Properties csrfConfig = new Properties(); 52 | private static HashMap exemptUrls = new HashMap(); 53 | private static HashMap urlSpecificConfig = new HashMap(); 54 | private static HashMap oneTimeUseConfig = new HashMap(); 55 | private static KeyczarWrapper keyczarWrapper; 56 | 57 | 58 | public static Properties getConfig() 59 | { 60 | return csrfConfig; 61 | } 62 | 63 | public static void loadConfig(String configFilename) throws CSRFConfigException 64 | { 65 | try 66 | { 67 | FileInputStream fis = new FileInputStream(configFilename); 68 | loadConfig(fis); 69 | fis.close(); 70 | } 71 | catch (FileNotFoundException ex) 72 | { 73 | String err = "CSRF Configuration file is not found, exception="+ex.getMessage(); 74 | LOG.severe(err); 75 | throw new CSRFConfigException(err); 76 | } 77 | catch (IOException ex) 78 | { 79 | String err = "Failed to properly read CSRF Configuration file"+ 80 | ", exception="+ex.getMessage(); 81 | LOG.severe(err); 82 | throw new CSRFConfigException(err); 83 | } 84 | } 85 | 86 | /* 87 | * New public loadConfig overload that accepts a configuration object to 88 | * allow programmatic configuration. Currently, only the protection mode 89 | * and the keyfile config values are overridden from the config object. 90 | */ 91 | public static void loadConfig(Config config) throws CSRFConfigException 92 | { 93 | try 94 | { 95 | String emptyConfigString = ""; 96 | loadConfig(new ByteArrayInputStream(emptyConfigString.getBytes("UTF-8")), config); 97 | } 98 | catch (Exception ex) 99 | { 100 | throw new CSRFConfigException(ex); 101 | } 102 | } 103 | 104 | public static void loadConfig(InputStream is) throws CSRFConfigException 105 | { 106 | loadConfig(is, null); 107 | } 108 | 109 | private static void loadConfig(InputStream is, Config overrides) throws CSRFConfigException 110 | { 111 | LOG.info("Loading XML Config File"); 112 | try 113 | { 114 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 115 | dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); 116 | dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); 117 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 118 | Document doc = dBuilder.parse(is); 119 | doc.getDocumentElement().normalize(); 120 | 121 | NodeList nl = doc.getElementsByTagName(Constants.CONF_MODE); 122 | Node node = nl.item(0); 123 | 124 | String mode = Constants.MODES.session.toString(); // session based protection is the default mode 125 | String modeOverride = (overrides != null ? overrides.getProtectionMode() : null); 126 | 127 | if (modeOverride != null) 128 | { 129 | mode = modeOverride; 130 | } 131 | else if (node != null) 132 | { 133 | mode = node.getFirstChild().getNodeValue(); 134 | } 135 | 136 | Constants.MODES.valueOf(mode); // Make sure it is an existent mode 137 | csrfConfig.setProperty(Constants.CONF_MODE, mode); 138 | 139 | Element docElement = (Element)doc.getDocumentElement(); 140 | 141 | loadLoggingConfiguration(docElement); 142 | 143 | if(isHmacMode()) 144 | { 145 | handleHMACConfigLoading(doc, overrides); 146 | } 147 | else if(isSessionMode()) 148 | { 149 | handleSessionConfigLoading(doc); 150 | } 151 | 152 | readXmlUrlListing(docElement, Constants.CONF_EXEMPTURLS); 153 | 154 | String tokenRequestAttribute = readElementTextValue(docElement, Constants.CONF_TOKEN_REQATTR); 155 | if(tokenRequestAttribute.equals("")) 156 | { 157 | tokenRequestAttribute = Constants.CONF_DEFAULT_TOKEN_REQATTR; 158 | } 159 | 160 | String tokenRequestParameter = readElementTextValue(docElement, Constants.CONF_TOKEN_PARAM); 161 | if(tokenRequestParameter.equals("")) 162 | { 163 | tokenRequestParameter = Constants.CONF_DEFAULT_TOKEN_PARAM; 164 | } 165 | 166 | String monitorMode = readElementAttributeTextValue(docElement, Constants.CONF_MONITORMODE, "enabled"); 167 | 168 | if(monitorMode.equals("")) 169 | { 170 | monitorMode = "no"; // Disabled by default 171 | } 172 | 173 | if(!monitorMode.equals("yes") && !monitorMode.equals("no")) 174 | { 175 | throw new CSRFConfigException("Invalid monitormode attribute entered. " + 176 | "We are expecting either 'yes' or 'no'. EnteredValue="+monitorMode); 177 | } 178 | 179 | String errorValue = ""; 180 | String errorMode = readElementAttributeTextValue(docElement, Constants.CONF_ERROR, "mode"); 181 | if(errorMode == null) 182 | { 183 | errorMode = ""; 184 | } 185 | 186 | if(errorMode.equals("redirect") || errorMode.equals("forward")) 187 | { 188 | errorValue = getValidatedUrl(readElementTextValue(docElement, Constants.CONF_ERROR)); 189 | 190 | } 191 | else if(errorMode.equals("status_code")) 192 | { 193 | errorValue = readElementTextValue(docElement, Constants.CONF_ERROR); 194 | if(!validateTimeout(errorValue)) 195 | { 196 | throw new CSRFConfigException("Invalid StatusCode passed within configuration error attribute. Submitted StatusCode="+errorValue); 197 | } 198 | } 199 | 200 | csrfConfig.setProperty(Constants.CONF_ERROR, errorMode); 201 | csrfConfig.setProperty(Constants.CONF_ERRORVAL, errorValue); 202 | csrfConfig.setProperty(Constants.CONF_TOKEN_PARAM, tokenRequestParameter ); 203 | csrfConfig.setProperty(Constants.CONF_TOKEN_REQATTR, tokenRequestAttribute); 204 | csrfConfig.setProperty(Constants.CONF_MONITORMODE, monitorMode); 205 | 206 | printConfiguration(); 207 | 208 | } 209 | catch(CSRFSignerException ex) 210 | { 211 | String err = "Error loading Keyczar Keyfile: exception="+ex.getMessage(); 212 | LOG.severe(err); 213 | throw new CSRFConfigException(err); 214 | } 215 | catch (ParserConfigurationException ex) 216 | { 217 | String err = "Failed to parse CSRF Configuration file, exception="+ex.getMessage(); 218 | LOG.severe(err); 219 | throw new CSRFConfigException(err); 220 | } 221 | catch (IOException ex) 222 | { 223 | String err = "Failed to properly read CSRF Configuration file"+ 224 | ", exception="+ex.getMessage(); 225 | LOG.severe(err); 226 | throw new CSRFConfigException(err); 227 | } 228 | catch (SAXException ex) 229 | { 230 | String err = "Failed to parse CSRF Configuration file, exception="+ex.getMessage(); 231 | LOG.severe(err); 232 | throw new CSRFConfigException(err); 233 | } 234 | } 235 | 236 | private static void loadLoggingConfiguration(Element element) throws SecurityException, IOException 237 | { 238 | String loggingConfigPath = readElementTextValue(element, Constants.JAVA_LOGGING_CONF); 239 | 240 | if(!loggingConfigPath.equals("")) 241 | { 242 | LOG.info("Custom Java logging configuration file specified"); 243 | File loggingConfigFile = new File(loggingConfigPath); 244 | if(loggingConfigFile.exists()) 245 | { 246 | FileInputStream loggingConfigFS = new FileInputStream(loggingConfigFile); 247 | LogManager.getLogManager().readConfiguration(loggingConfigFS); 248 | } 249 | else 250 | { 251 | LOG.info("Error loading Java Logging Configuration file. " + 252 | "Could not find the following the specified filename: "+ loggingConfigFile); 253 | } 254 | 255 | csrfConfig.setProperty(Constants.JAVA_LOGGING_CONF, loggingConfigPath); 256 | } 257 | } 258 | 259 | public static void handleHMACConfigLoading(Document doc, Config overrides) throws CSRFConfigException, CSRFSignerException 260 | { 261 | NodeList nl = doc.getElementsByTagName(Constants.CONF_HMACSETTINGS); 262 | Node node = nl.item(0); 263 | 264 | // Set some default values 265 | csrfConfig.setProperty(Constants.CONF_HMAC_USERSEED_ATTR, Constants.CONF_DEFAULT_USERSEED_ATTR); 266 | csrfConfig.setProperty(Constants.CONF_HMAC_SITEWIDE_TIMEOUT, Constants.CONF_DEFAULT_TOKENTIMEOUT); 267 | 268 | if(node != null && node.getNodeType() == Node.ELEMENT_NODE) 269 | { 270 | String hmacKeyfile = readElementTextValue((Element)node, Constants.CONF_HMAC_KEYFILE); 271 | setHMACKeyFile(hmacKeyfile); 272 | 273 | String seedAttributeName = readElementTextValue((Element)node, Constants.CONF_HMAC_USERSEED_ATTR); 274 | if(!seedAttributeName.equals("")) 275 | { 276 | csrfConfig.setProperty(Constants.CONF_HMAC_USERSEED_ATTR, seedAttributeName); 277 | } 278 | 279 | String sitewideTimeout = readElementTextValue((Element)node, Constants.CONF_HMAC_SITEWIDE_TIMEOUT); 280 | if(!validateTimeout(sitewideTimeout)) 281 | { 282 | throw new CSRFConfigException("Invalid Sitewide timeout value submitted. SubmittedTimeout=" 283 | +sitewideTimeout); 284 | } 285 | 286 | if(!sitewideTimeout.equals("")) 287 | { 288 | csrfConfig.setProperty(Constants.CONF_HMAC_SITEWIDE_TIMEOUT, sitewideTimeout); 289 | } 290 | 291 | readXmlUrlListing((Element) node, Constants.CONF_URLSPECIFIC); 292 | } 293 | 294 | // Apply overrides 295 | if (overrides != null) 296 | { 297 | String hmacKeyFile = overrides.getHMACKeyFile(); 298 | 299 | if (hmacKeyFile != null && hmacKeyFile != "") 300 | { 301 | setHMACKeyFile(hmacKeyFile); 302 | } 303 | } 304 | } 305 | 306 | private static void setHMACKeyFile(String hmacKeyFile) throws CSRFConfigException, CSRFSignerException 307 | { 308 | if(hmacKeyFile == null || hmacKeyFile == "") 309 | { 310 | String err = "HMAC-mode CSRF Protection requires Keyczar HMAC File to " + 311 | "be define within the configuration file"; 312 | LOG.severe(err); 313 | throw new CSRFConfigException(err); 314 | } 315 | 316 | csrfConfig.setProperty(Constants.CONF_HMAC_KEYFILE, hmacKeyFile); 317 | keyczarWrapper = new KeyczarWrapper(hmacKeyFile); 318 | } 319 | 320 | private static void handleSessionConfigLoading(Document doc) throws CSRFConfigException 321 | { 322 | NodeList nl = doc.getElementsByTagName(Constants.CONF_SESSIONSETTINGS); 323 | Node node = nl.item(0); 324 | 325 | if(node != null && node.getNodeType() == Node.ELEMENT_NODE) 326 | { 327 | readXmlUrlListing((Element) node, Constants.CONF_SESSION_ONETIMEUSE); 328 | readXmlUrlListing((Element) node, Constants.CONF_URLSPECIFIC); 329 | } 330 | } 331 | 332 | 333 | private static String readElementTextValue(Element element, String elementName) 334 | { 335 | try 336 | { 337 | if(element != null) 338 | { 339 | return (String)element.getElementsByTagName(elementName).item(0).getFirstChild().getNodeValue(); 340 | } 341 | } 342 | catch(NullPointerException ex){}; 343 | 344 | return ""; 345 | } 346 | 347 | private static String readElementAttributeTextValue(Element element, String elementName, String attributeName) 348 | { 349 | try 350 | { 351 | if(element != null) 352 | { 353 | Node node = element.getElementsByTagName(elementName).item(0); 354 | return (String)node.getAttributes().getNamedItem(attributeName).getFirstChild().getNodeValue(); 355 | } 356 | } 357 | catch(NullPointerException ex){} // Purposely left empty since we just want to return an empty string 358 | 359 | return ""; 360 | } 361 | 362 | private static void readXmlUrlListing(Element element, String listName) 363 | throws CSRFConfigException 364 | { 365 | NodeList nl = element.getElementsByTagName(listName); 366 | if(nl.getLength() > 0) { 367 | Element urlSpecificElement = (Element)nl.item(0); 368 | 369 | NodeList urlNodelist = urlSpecificElement.getElementsByTagName("url"); 370 | 371 | for(int i = 0; i < urlNodelist.getLength(); i++) 372 | { 373 | Node urlNode = urlNodelist.item(i); 374 | String timeout = "0"; 375 | String url = getValidatedUrl(urlNode.getFirstChild().getNodeValue()); 376 | 377 | if(isHmacMode()) 378 | { 379 | try 380 | { 381 | timeout = urlNode.getAttributes().getNamedItem("timeout").getFirstChild().getNodeValue(); 382 | if(!validateTimeout(timeout)) 383 | { 384 | throw new CSRFConfigException("Invalid URL Specific timeout value specified. URL=" 385 | + url + ", EnteredTimeout="+timeout); 386 | } 387 | } 388 | catch(NullPointerException ex) 389 | { 390 | // If no timeout was set, we use the sitewide value 391 | timeout = ConfigUtil.getProp(Constants.CONF_HMAC_SITEWIDE_TIMEOUT); 392 | } 393 | } 394 | 395 | if(listName.equals("urlspecific")) 396 | { 397 | if(isSessionMode() && oneTimeUseConfig.containsKey(url)) 398 | { 399 | LOG.info("Not setting URL as URL Specific because has already been set as a OneTimeUse URL. url="+url); 400 | continue; 401 | } 402 | 403 | urlSpecificConfig.put(url, Long.parseLong(timeout)); 404 | } 405 | else if(listName.equals("onetimeuse")) 406 | { 407 | oneTimeUseConfig.put(url, new Integer(0)); 408 | } 409 | else if(listName.equals("exempt_urls")) 410 | { 411 | exemptUrls.put(url, new Integer(0)); 412 | } 413 | } 414 | } 415 | } 416 | 417 | private static boolean validateTimeout(String timeout) 418 | { 419 | try 420 | { 421 | Long timeoutLong = Long.parseLong(timeout); 422 | if(timeoutLong > 0) 423 | { 424 | return true; 425 | } 426 | } 427 | catch(NumberFormatException ex) 428 | { 429 | LOG.severe("Invalid Timout value submitted. Value should be a positive numeric value. EnteredValue="+timeout.toString()); 430 | } 431 | 432 | return false; 433 | } 434 | 435 | private static String getValidatedUrl(String url) throws CSRFConfigException 436 | { 437 | url.replaceAll("\\s", ""); // Strip out the whitespace 438 | 439 | if( !url.startsWith("/") ) 440 | { 441 | throw new CSRFConfigException("Invalid URL is not in a valid format." 442 | + "We are expecting a relative path and should therefore begin with a '/'. EnteredUrl="+url); 443 | } 444 | 445 | Pattern pattern = Pattern.compile("^[A-Za-z1-9_.~:/#@=;,'\\-\\?\\[\\]\\+\\*\\{\\}\\&\\$\\|]+$"); 446 | Matcher matcher = pattern.matcher(url); 447 | 448 | if(!matcher.matches()) 449 | { 450 | throw new CSRFConfigException("Invalid character passed in the URL. EnteredUrl="+url); 451 | } 452 | 453 | return url; 454 | } 455 | 456 | private static void printConfiguration() 457 | { 458 | StringBuffer str = new StringBuffer("\n============\nAntiCSRF Configuration\n============\n" ); 459 | str.append( Constants.CONF_MODE + ": " + csrfConfig.getProperty(Constants.CONF_MODE) + "\n" ); 460 | str.append( Constants.CONF_TOKEN_REQATTR + ": " + csrfConfig.getProperty(Constants.CONF_TOKEN_REQATTR) + "\n" ); 461 | str.append( Constants.CONF_TOKEN_PARAM + ": " + csrfConfig.getProperty(Constants.CONF_TOKEN_PARAM) + "\n" ); 462 | str.append( Constants.CONF_ERROR + ": " + csrfConfig.getProperty(Constants.CONF_ERROR) + "\n" ); 463 | str.append( Constants.CONF_ERRORVAL + ": " + csrfConfig.getProperty(Constants.CONF_ERRORVAL) + "\n" ); 464 | str.append( Constants.JAVA_LOGGING_CONF + ": " + csrfConfig.getProperty(Constants.JAVA_LOGGING_CONF) + "\n" ); 465 | str.append( Constants.CONF_MONITORMODE + ": " + csrfConfig.getProperty(Constants.CONF_MONITORMODE) + "\n" ); 466 | 467 | str.append( "\n-Exempt URLs-\n" ); 468 | 469 | for(String url : exemptUrls.keySet()) 470 | { 471 | str.append( "url: " + url + "\n" ); 472 | } 473 | 474 | if(isHmacMode()) 475 | { 476 | str.append( "\n++HMAC Protection Mode Settings++\n" ); 477 | str.append( Constants.CONF_HMAC_KEYFILE + ": " + csrfConfig.getProperty(Constants.CONF_HMAC_KEYFILE) + "\n" ); 478 | str.append( Constants.CONF_HMAC_SITEWIDE_TIMEOUT + ": " + csrfConfig.getProperty(Constants.CONF_HMAC_SITEWIDE_TIMEOUT) + "\n" ); 479 | str.append( Constants.CONF_HMAC_USERSEED_ATTR + ": " + csrfConfig.getProperty(Constants.CONF_HMAC_USERSEED_ATTR) + "\n" ); 480 | 481 | str.append( "\n--URL Specific Configuration--\n" ); 482 | for(String url : urlSpecificConfig.keySet()) 483 | { 484 | str.append( "url: " + url + " timeout: "+ urlSpecificConfig.get(url) + "\n" ); 485 | } 486 | } 487 | else if(isSessionMode()) 488 | { 489 | str.append( "\n++Session Protection Mode Settings++\n" ); 490 | 491 | str.append( "\n--URL Specific Configuration--\n" ); 492 | for(String url : urlSpecificConfig.keySet()) 493 | { 494 | str.append( "url: " + url + "\n" ); 495 | } 496 | 497 | str.append( "\n--One Time Use Configuration--\n" ); 498 | for(String url : oneTimeUseConfig.keySet()) 499 | { 500 | str.append( "url: " + url + "\n" ); 501 | } 502 | } 503 | LOG.info(str.toString()); 504 | 505 | } 506 | 507 | public static String getProp(String configProperty) 508 | { 509 | try { 510 | return csrfConfig.getProperty(configProperty); 511 | } catch (Exception e) { 512 | LOG.warning("Failed to find property: " + configProperty + " exmsg= " + e.getMessage()); 513 | return Constants.defaultConfigs.get(configProperty); 514 | } 515 | } 516 | 517 | public static KeyczarWrapper getKeyczarWrapper() 518 | { 519 | return keyczarWrapper; 520 | } 521 | 522 | public static boolean isURLExempt(String url) 523 | { 524 | if( exemptUrls.containsKey(url) ) 525 | { 526 | LOG.fine("Current url is configured to be exempt from CSRF Protection, url="+url); 527 | return true; 528 | } 529 | 530 | return false; 531 | } 532 | 533 | public static boolean hasOneTimeUseConfig(String url) 534 | { 535 | LOG.fine("About to check if token is configured for OneTimeUser: RequestURI="+StringUtil.stripNewlines(url)); 536 | return oneTimeUseConfig.containsKey(url); 537 | } 538 | 539 | public static boolean hasUrlSpecificConfig(String url) 540 | { 541 | return urlSpecificConfig.containsKey(url); 542 | } 543 | 544 | public static Long getUrlSpecificConfig(String url) 545 | { 546 | return urlSpecificConfig.get(url); 547 | } 548 | 549 | public static boolean isHmacMode() 550 | { 551 | return csrfConfig.getProperty(Constants.CONF_MODE).equals(Constants.MODES.hmac.toString()); 552 | } 553 | 554 | public static boolean isSessionMode() 555 | { 556 | return csrfConfig.getProperty(Constants.CONF_MODE).equals(Constants.MODES.session.toString()); 557 | } 558 | } 559 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | 19 | package com.gdssecurity.anticsrf.utils; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | public class Constants 25 | { 26 | public static enum MODES { 27 | session, hmac 28 | } 29 | 30 | public static final String CONF_INITPARAMNAME = "anticsrf_config"; 31 | public static final String CONFIGNAME = "anticsrf.xml"; 32 | public static final String JAVA_LOGGING_CONF = "logging_configfile"; 33 | public static final String WEB_CONTAINER = "WEB-INF"; 34 | public static final String FILE_SEPARATOR = System.getProperty("file.separator"); 35 | 36 | public static final String CONF_HMACSETTINGS = "hmac_settings"; 37 | public static final String CONF_HMAC_USERSEED_ATTR = "seed_attribute_name"; 38 | public static final String CONF_HMAC_SITEWIDE_TIMEOUT = "sitewide_timeout"; 39 | public static final String CONF_HMAC_KEYFILE = "keyfile"; 40 | 41 | public static final String CONF_SESSIONSETTINGS = "session_settings"; 42 | public static final String CONF_SESSION_ONETIMEUSE = "onetimeuse"; 43 | 44 | public static final String CONF_MODE = "mode"; 45 | public static final String CONF_MONITORMODE = "monitormode"; 46 | public static final String CONF_ERROR = "error"; 47 | public static final String CONF_ERRORVAL = "errorval"; 48 | public static final String CONF_EXEMPTURLS= "exempt_urls"; 49 | public static final String CONF_TOKEN_REQATTR = "token_attribute"; 50 | public static final String CONF_TOKEN_PARAM = "token_parametername"; 51 | public static final String CONF_URLSPECIFIC = "urlspecific"; 52 | public static final String CONF_ERROR_AJAX = "ajax"; 53 | 54 | public static final String CONF_DEFAULT_TOKEN_REQATTR = "anticsrftoken"; 55 | public static final String CONF_DEFAULT_USERSEED_ATTR = "userseed"; 56 | public static final String CONF_DEFAULT_TOKEN_PARAM = "tok"; 57 | public static final String CONF_DEFAULT_TOKENTIMEOUT = "30"; 58 | 59 | public static final Map defaultConfigs; 60 | static 61 | { 62 | defaultConfigs = new HashMap(); 63 | defaultConfigs.put("token_attribute", "anticsrftoken"); 64 | defaultConfigs.put("userseed", "userseed"); 65 | defaultConfigs.put("token_parametername", "tok"); 66 | defaultConfigs.put("timeout", "30"); 67 | defaultConfigs.put("mode", "session"); 68 | } 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/KeyczarWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.utils; 19 | 20 | import org.keyczar.Signer; 21 | import org.keyczar.exceptions.KeyczarException; 22 | 23 | import com.gdssecurity.anticsrf.exceptions.CSRFSignerException; 24 | 25 | public class KeyczarWrapper { 26 | 27 | private Signer csrfSigner = null; 28 | 29 | public KeyczarWrapper(String hmacKeyfile) throws CSRFSignerException 30 | { 31 | try { 32 | csrfSigner = new Signer(hmacKeyfile); 33 | } catch (KeyczarException e) { 34 | throw new CSRFSignerException(e); 35 | } 36 | } 37 | 38 | public Signer getCSRFSigner() 39 | { 40 | return this.csrfSigner; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/SecureCompare.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.utils; 19 | 20 | public class SecureCompare 21 | { 22 | public static boolean isEqual(byte[] a, byte[] b) 23 | { 24 | if (a.length != b.length) 25 | { 26 | return false; 27 | } 28 | 29 | int result = 0; 30 | 31 | for (int i = 0; i < a.length; i++) 32 | { 33 | result |= a[i] ^ b[i]; 34 | } 35 | 36 | return result == 0; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /AntiCSRFLibrary/src/com/gdssecurity/anticsrf/utils/StringUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.gdssecurity.anticsrf.utils; 19 | 20 | public class StringUtil { 21 | public static String stripNewlines(String str) 22 | { 23 | if(str == null) 24 | { 25 | return ""; 26 | } 27 | 28 | return str.replaceAll("(\\r|\\n)", ""); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/Error.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=US-ASCII" 2 | pageEncoding="US-ASCII"%> 3 | 4 | 5 | 6 | 7 | Generic Error Page 8 | 9 | 10 |

ERROR - Invalid CSRF Token

11 | 12 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/Home.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 2 | <%@ taglib uri="/WEB-INF/anticsrf.tld" prefix="csrf" %> 3 | <%@ page import="com.gdssecurity.anticsrf.utils.ConfigUtil" %> 4 | 5 | 6 |

CSRF Protection Test Harness 7 | <% 8 | if(ConfigUtil.isHmacMode()){ 9 | out.println("(HMAC Protection Mode)"); 10 | } 11 | else{ 12 | out.println("(SESSION Protection Mode)"); 13 | } 14 | %> 15 |

16 | 17 |

AntiCSRF Filter Test Harness

18 |
19 | 20 | Click me for Site-wide token protection
21 | Click me for URL Specific protection
22 | <%if(ConfigUtil.isSessionMode()){ %> 23 | Click me for One Time Use Token protection
24 | <%}%> 25 |
26 |
27 | 28 | Submit Site-wide token protected Form 29 | 30 | 31 |
32 | <%if(ConfigUtil.isSessionMode()){ %> 33 |
34 | 35 | Submit One-time Use token protected Form 36 | 37 | <%}%> 38 | 39 |

AntiCSRF Custom Implementation Test Harness

40 | 41 |

AntiCSRF Filter Test Harness

42 |
43 | Click me for site-wide token protected page
44 | Click me for URL Specific protection
45 | <%if(ConfigUtil.isSessionMode()){ %> 46 | Click me for One Time Use Token protection
47 | <%}%> 48 |
49 |
50 | 51 | Submit Site-wide token protected Form 52 | 53 | 54 |
55 | <%if(ConfigUtil.isSessionMode()){ %> 56 |
57 | 58 | Submit One-time Use token protected Form 59 | 60 | <%}%> 61 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/anticsrf.tld: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | Taglib for outputting GDS Anti-CSRF tokens to JSPs 12 | 13 | GDS Anti-CSRF Solution 14 | 0.1 15 | anticsrf 16 | 17 | http://www.gdssecurity.com 18 | 19 | 20 | 21 | Adds CSRF Token to POST form as hidden variable 22 | 23 | Add CSRF Token to Form 24 | forForm 25 | com.gdssecurity.anticsrf.tags.CSRFTokenFormTag 26 | JSP 27 | 28 | 29 | 30 | 31 | Adds CSRF Token variable for appending to a Query String 32 | 33 | Add CSRF Token to URL 34 | forRequest 35 | com.gdssecurity.anticsrf.tags.CSRFTokenUrlTag 36 | JSP 37 | 38 | 39 | 40 | 41 | Adds Url Specific CSRF Token to POST form as hidden variable 42 | 43 | Add CSRF Token to Form 44 | forFormUrlSpecific 45 | com.gdssecurity.anticsrf.tags.CSRFTokenUrlSpecificFormTag 46 | JSP 47 | 48 | url 49 | true 50 | true 51 | java.lang.String 52 | 53 | 54 | 55 | 56 | 57 | Adds CSRF Token variable for appending to a Query String 58 | 59 | Add CSRF Token to URL 60 | forRequestUrlSpecific 61 | com.gdssecurity.anticsrf.tags.CSRFTokenUrlSpecificUrlTag 62 | JSP 63 | 64 | url 65 | true 66 | true 67 | java.lang.String 68 | 69 | 70 | 71 | 72 | 73 | Adds CSRF token without the CSRF token parameter name 74 | 75 | Adds CSRF token to the page 76 | csrfToken 77 | com.gdssecurity.anticsrf.tags.CSRFToken 78 | JSP 79 | 80 | 81 | 82 | 83 | Adds CSRF token parameter name without the CSRF token 84 | 85 | Adds CSRF token parameter name to the page 86 | csrfToken 87 | com.gdssecurity.anticsrf.tags.CSRFTokenParameterName 88 | JSP 89 | 90 | 91 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/anticsrf.xml: -------------------------------------------------------------------------------- 1 | 2 | hmac 3 | 4 | /opt/keyczar_signkey 5 | 5 6 | userseed 7 | 8 | 9 | /filter/URLSpecificServlet 10 | /custom/CustomURLSpecificServlet 11 | 12 | 13 | 14 | 15 | 16 | /filter/URLSpecificServlet 17 | /custom/CustomURLSpecificServlet 18 | 19 | 20 | /filter/OneTimeUseServlet 21 | /custom/CustomOneTimeUseServlet 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/lib/anticsrf.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFTestHarness/WebContent/WEB-INF/lib/anticsrf.jar -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/lib/encoder-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFTestHarness/WebContent/WEB-INF/lib/encoder-1.2.jar -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/lib/gson-2.2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFTestHarness/WebContent/WEB-INF/lib/gson-2.2.4.jar -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/lib/keyczar-0.71g-090613.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFTestHarness/WebContent/WEB-INF/lib/keyczar-0.71g-090613.jar -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/lib/log4j-1.2.17.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/AntiCSRFTestHarness/WebContent/WEB-INF/lib/log4j-1.2.17.jar -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | AntiCSRF 4 | 5 | index.html 6 | index.htm 7 | index.jsp 8 | default.html 9 | default.htm 10 | default.jsp 11 | 12 | 13 | 14 | HomeServlet 15 | HomeServlet 16 | com.sendsafely.testapp.servlets.HomeServlet 17 | 0 18 | 19 | 20 | HomeServlet 21 | /HomeServlet 22 | 23 | 24 | 25 | CSRFFilter 26 | CSRFFilter 27 | com.gdssecurity.anticsrf.CSRFFilter 28 | 29 | 30 | CSRFFilter 31 | /filter/* 32 | 33 | 34 | 35 | SiteWideServlet 36 | SiteWideServlet 37 | com.sendsafely.testapp.servlets.SiteWideServlet 38 | 39 | 40 | SiteWideServlet 41 | /filter/SiteWideServlet 42 | 43 | 44 | 45 | URLSpecificServlet 46 | URLSpecificServlet 47 | com.sendsafely.testapp.servlets.URLSpecificServlet 48 | 49 | 50 | URLSpecificServlet 51 | /filter/URLSpecificServlet 52 | 53 | 54 | 55 | OneTimeUseServlet 56 | OneTimeUseServlet 57 | com.sendsafely.testapp.servlets.OneTimeUseServlet 58 | 59 | 60 | OneTimeUseServlet 61 | /filter/OneTimeUseServlet 62 | 63 | 64 | 65 | ErrorServlet 66 | ErrorServlet 67 | com.sendsafely.testapp.servlets.ErrorServlet 68 | 69 | 70 | ErrorServlet 71 | /ErrorServlet 72 | 73 | 74 | 75 | CustomSiteWideServlet 76 | CustomSiteWideServlet 77 | com.sendsafely.testapp.servlets.CustomSiteWideServlet 78 | 79 | 80 | CustomSiteWideServlet 81 | /custom/CustomSiteWideServlet 82 | 83 | 84 | 85 | CustomURLSpecificServlet 86 | CustomURLSpecificServlet 87 | com.sendsafely.testapp.servlets.CustomURLSpecificServlet 88 | 89 | 90 | CustomURLSpecificServlet 91 | /custom/CustomURLSpecificServlet 92 | 93 | 94 | 95 | CustomOneTimeUseServlet 96 | CustomOneTimeUseServlet 97 | com.sendsafely.testapp.servlets.CustomOneTimeUseServlet 98 | 99 | 100 | CustomOneTimeUseServlet 101 | /custom/CustomOneTimeUseServlet 102 | 103 | 104 | 105 | CustomCSRFFilter 106 | CustomCSRFFilter 107 | com.sendsafely.testapp.filters.CustomCSRFFilter 108 | 109 | 110 | CustomCSRFFilter 111 | /custom/* 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/onetimeuse.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=US-ASCII" 2 | pageEncoding="US-ASCII"%> 3 | 4 | 5 | 6 | 7 | One Time Use Page 8 | 9 | 10 |

Horray! we passed one time use token validation

11 | 12 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/sitewide.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=US-ASCII" 2 | pageEncoding="US-ASCII"%> 3 | 4 | 5 | 6 | 7 | Site Wide Protected page 8 | 9 | 10 | 11 |

Hooray! we passed Site Wide CSRF token validation

12 | 13 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/WebContent/urlspecific.jsp: -------------------------------------------------------------------------------- 1 | 2 | URL Specific Protected page 3 | 4 |

Hooray! we passed URL Specific token validation

5 | 6 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/filters/CustomCSRFFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.filters; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.Filter; 24 | import javax.servlet.FilterChain; 25 | import javax.servlet.FilterConfig; 26 | import javax.servlet.ServletException; 27 | import javax.servlet.ServletRequest; 28 | import javax.servlet.ServletResponse; 29 | import javax.servlet.http.HttpServletRequest; 30 | import javax.servlet.http.HttpServletResponse; 31 | 32 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 33 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 34 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 35 | import com.gdssecurity.anticsrf.utils.ConfigUtil; 36 | import com.gdssecurity.anticsrf.utils.Constants; 37 | import com.sendsafely.testapp.servlets.CustomOneTimeUseServlet; 38 | 39 | /** 40 | * Servlet Filter implementation class customcsrffilter 41 | */ 42 | public class CustomCSRFFilter implements Filter 43 | { 44 | private static final Logger LOG = Logger.getLogger(CustomCSRFFilter.class.getName()); 45 | FilterConfig filterConfig = null; 46 | 47 | public CustomCSRFFilter() { 48 | 49 | } 50 | 51 | public void destroy() 52 | { 53 | filterConfig = null; 54 | } 55 | 56 | 57 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 58 | { 59 | J2EECSRFProtection csrfProtection = CSRFProtectionFactory.getCSRFProtection(); 60 | csrfProtection.setRequestObject((HttpServletRequest)request); 61 | if( !csrfProtection.verifyCSRFToken() ) 62 | { 63 | LOG.info("CSRF Verification Failed"); 64 | HttpServletResponse res = (HttpServletResponse)response; 65 | res.sendError(403); 66 | return; 67 | } 68 | 69 | // pass the request along the filter chain 70 | chain.doFilter(request, response); 71 | } 72 | 73 | public void init(FilterConfig fConfig) throws ServletException 74 | { 75 | // Check to see if an init param has been set 76 | String configFile = fConfig.getInitParameter(Constants.CONF_INITPARAMNAME); 77 | 78 | if(configFile == null) 79 | { 80 | configFile = fConfig.getServletContext().getRealPath("") + Constants.FILE_SEPARATOR + 81 | Constants.WEB_CONTAINER + Constants.FILE_SEPARATOR + Constants.CONFIGNAME; 82 | LOG.info("No Filter init-param set, defaulting to loading AntiCSRF Configuration from "+configFile); 83 | } 84 | else 85 | { 86 | LOG.info("AntiCSRF Configuration init-param specified. Configuration file set to "+configFile); 87 | } 88 | 89 | ConfigUtil.loadConfig(configFile); 90 | this.filterConfig = fConfig; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/CustomOneTimeUseServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 29 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 30 | 31 | public class CustomOneTimeUseServlet extends HttpServlet 32 | { 33 | private static final long serialVersionUID = 1L; 34 | private static final Logger LOG = Logger.getLogger(CustomOneTimeUseServlet.class.getName()); 35 | 36 | public CustomOneTimeUseServlet() 37 | { 38 | super(); 39 | } 40 | 41 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 42 | throws ServletException, IOException 43 | { 44 | LOG.info("One Time Use Servlet with Custom Impl Hit"); 45 | request.getRequestDispatcher("/onetimeuse.jsp").forward(request, response); 46 | } 47 | 48 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 49 | throws ServletException, IOException 50 | { 51 | doGet(request, response); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/CustomSiteWideServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | import com.gdssecurity.anticsrf.j2ee.J2EECSRFProtection; 29 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 30 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 31 | 32 | /** 33 | * Servlet implementation class CustomSiteWideServlet 34 | */ 35 | public class CustomSiteWideServlet extends HttpServlet { 36 | private static final long serialVersionUID = 1L; 37 | private static final Logger LOG = Logger.getLogger(CustomSiteWideServlet.class.getName()); 38 | 39 | 40 | public CustomSiteWideServlet() { 41 | super(); 42 | } 43 | 44 | 45 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 46 | LOG.info("Site wide Servlet with Custom Impl Hit"); 47 | 48 | J2EECSRFProtection csrfProtection = CSRFProtectionFactory.getCSRFProtection(); 49 | request.getRequestDispatcher("/sitewide.jsp").forward(request, response); 50 | } 51 | 52 | 53 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 54 | doGet(request, response); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/CustomURLSpecificServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | import com.gdssecurity.anticsrf.protections.CSRFProtection; 29 | import com.gdssecurity.anticsrf.protections.CSRFProtectionFactory; 30 | 31 | /** 32 | * Servlet implementation class CustomURLSpecificServlet 33 | */ 34 | public class CustomURLSpecificServlet extends HttpServlet { 35 | private static final long serialVersionUID = 1L; 36 | private static final Logger LOG = Logger.getLogger(CustomSiteWideServlet.class.getName()); 37 | 38 | public CustomURLSpecificServlet() { 39 | super(); 40 | } 41 | 42 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 43 | throws ServletException, IOException { 44 | LOG.info("URL Specific Servlet with Custom Impl Hit"); 45 | request.getRequestDispatcher("/urlspecific.jsp").forward(request, response); 46 | } 47 | 48 | 49 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 50 | throws ServletException, IOException { 51 | doGet(request, response); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/ErrorServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | public class ErrorServlet extends HttpServlet { 29 | 30 | private static final long serialVersionUID = 1L; 31 | private static final Logger LOG = Logger.getLogger(ErrorServlet.class.getName()); 32 | 33 | /** 34 | * @see HttpServlet#HttpServlet() 35 | */ 36 | public ErrorServlet() 37 | { 38 | super(); 39 | } 40 | 41 | /** 42 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 43 | */ 44 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 | 46 | LOG.info("ErrorServlet is hit"); 47 | request.getRequestDispatcher("/Error.jsp").forward(request, response); 48 | } 49 | 50 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 51 | doGet(request, response); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/HomeServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.io.PrintWriter; 22 | import java.util.logging.Logger; 23 | 24 | 25 | import javax.servlet.ServletException; 26 | import javax.servlet.http.HttpServlet; 27 | import javax.servlet.http.HttpServletRequest; 28 | import javax.servlet.http.HttpServletResponse; 29 | 30 | import org.owasp.encoder.*; 31 | 32 | /** 33 | * Servlet implementation class HomeServlet 34 | */ 35 | public class HomeServlet extends HttpServlet { 36 | private static final long serialVersionUID = 1L; 37 | private static final Logger LOG = Logger.getLogger(HomeServlet.class.getName()); 38 | 39 | /** 40 | * @see HttpServlet#HttpServlet() 41 | */ 42 | public HomeServlet() 43 | { 44 | super(); 45 | LOG.info("Running HomeServlet Constructor"); 46 | 47 | } 48 | 49 | /** 50 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 51 | */ 52 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53 | LOG.info("Running HomeServlet doGet"); 54 | if(request.getParameter("user") != null) 55 | { 56 | request.setAttribute("user", Encode.forHtmlAttribute(Encode.forUriComponent((request.getParameter("user"))))); 57 | } 58 | request.getRequestDispatcher("/Home.jsp").forward(request, response); 59 | } 60 | 61 | 62 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 63 | doGet(request, response); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/OneTimeUseServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | /** 29 | * Servlet implementation class OneTimeUseServlet 30 | */ 31 | public class OneTimeUseServlet extends HttpServlet { 32 | private static final long serialVersionUID = 1L; 33 | private static final Logger LOG = Logger.getLogger(OneTimeUseServlet.class.getName()); 34 | 35 | /** 36 | * @see HttpServlet#HttpServlet() 37 | */ 38 | public OneTimeUseServlet() { 39 | super(); 40 | // TODO Auto-generated constructor stub 41 | } 42 | 43 | /** 44 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 45 | */ 46 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 47 | LOG.info("One Time Use Servlet Hit"); 48 | request.getRequestDispatcher("/onetimeuse.jsp").forward(request, response); 49 | } 50 | 51 | /** 52 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 53 | */ 54 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 55 | doGet(request, response); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/SiteWideServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | /** 29 | * Servlet implementation class ProtectedServlet 30 | */ 31 | public class SiteWideServlet extends HttpServlet { 32 | private static final long serialVersionUID = 1L; 33 | private static final Logger LOG = Logger.getLogger(SiteWideServlet.class.getName()); 34 | 35 | 36 | public SiteWideServlet() { 37 | super(); 38 | // TODO Auto-generated constructor stub 39 | } 40 | 41 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 42 | { 43 | LOG.info("SiteWideServlet is hit"); 44 | request.getRequestDispatcher("/sitewide.jsp").forward(request, response); 45 | } 46 | 47 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 48 | doGet(request, response); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /AntiCSRFTestHarness/src/com/sendsafely/testapp/servlets/URLSpecificServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2016 Gotham Digital Science LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package com.sendsafely.testapp.servlets; 19 | 20 | import java.io.IOException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.servlet.ServletException; 24 | import javax.servlet.http.HttpServlet; 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | 28 | /** 29 | * Servlet implementation class Payment 30 | */ 31 | public class URLSpecificServlet extends HttpServlet { 32 | private static final long serialVersionUID = 1L; 33 | private static final Logger LOG = Logger.getLogger(URLSpecificServlet.class.getName()); 34 | 35 | /** 36 | * @see HttpServlet#HttpServlet() 37 | */ 38 | public URLSpecificServlet() 39 | { 40 | super(); 41 | } 42 | 43 | /** 44 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 45 | */ 46 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 47 | LOG.info("UrlSpecificServlet is hit...!"); 48 | request.getRequestDispatcher("/urlspecific.jsp").forward(request, response); 49 | } 50 | 51 | /** 52 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 53 | */ 54 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 55 | doGet(request,response); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | All contributions require acceptance of the Developer Certificate of Origin (DCO) below. To accept the DCO, add a line similar to the following to the commit message. This can be done automatically by using 'git commit -s'. 2 | 3 | Signed-off-by: John Doe 4 | 5 | The DCO is as follows: 6 | 7 | Developer Certificate of Origin 8 | Version 1.1 9 | 10 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 11 | 1 Letterman Drive 12 | Suite D4700 13 | San Francisco, CA, 94129 14 | 15 | Everyone is permitted to copy and distribute verbatim copies of this 16 | license document, but changing it is not allowed. 17 | 18 | 19 | Developer's Certificate of Origin 1.1 20 | 21 | By making a contribution to this project, I certify that: 22 | 23 | (a) The contribution was created in whole or in part by me and I 24 | have the right to submit it under the open source license 25 | indicated in the file; or 26 | 27 | (b) The contribution is based upon previous work that, to the best 28 | of my knowledge, is covered under an appropriate open source 29 | license and I have the right under that license to submit that 30 | work with modifications, whether created in whole or in part 31 | by me, under the same open source license (unless I am 32 | permitted to submit under a different license), as indicated 33 | in the file; or 34 | 35 | (c) The contribution was provided directly to me by some other 36 | person who certified (a), (b) or (c) and I have not modified 37 | it. 38 | 39 | (d) I understand and agree that this project and the contribution 40 | are public and that a record of the contribution (including all 41 | personal information I submit with it, including my sign-off) is 42 | maintained indefinitely and may be redistributed consistent with 43 | this project or the open source license(s) involved. 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Anti-CSRF-Library 2 | Copyright 2014-2016 Gotham Digital Science LLC 3 | 4 | This product includes software developed at 5 | Gotham Digital Science LLC (https://www.gdssecurity.com/) 6 | 7 | =============================================================== 8 | 9 | The binary distribution of this product bundles binaries of 10 | the OWASP encoder (https://github.com/OWASP/owasp-java-encoder), 11 | which has the following notices: 12 | 13 | Copyright (c) 2015 Jeff Ichnowski 14 | 15 | =============================================================== 16 | 17 | The binary distribution of this product bundles binaries of 18 | the Google GSON encoder (https://github.com/google/gson), which 19 | has the following notices: 20 | 21 | 22 | Copyright 2008 Google Inc. 23 | 24 | Licensed under the Apache License, Version 2.0 (the "License"); 25 | you may not use this file except in compliance with the License. 26 | You may obtain a copy of the License at 27 | 28 | http://www.apache.org/licenses/LICENSE-2.0 29 | 30 | Unless required by applicable law or agreed to in writing, software 31 | distributed under the License is distributed on an "AS IS" BASIS, 32 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 | See the License for the specific language governing permissions and 34 | limitations under the License. 35 | 36 | ================================================================== 37 | 38 | The binary distribution of this product bundles binaries of the Google 39 | Keyzar tool (https://github.com/google/keyczar), which has the following 40 | notices: 41 | 42 | Copyright 2008 Google Inc. 43 | 44 | =================================================================== 45 | 46 | The binary distribution of this product bundles binaries of the Log4J 47 | logging framework, which has the following notices: 48 | 49 | This product includes software developed by 50 | The Apache Software Foundation (http://www.apache.org/). 51 | 52 | This product includes source code based on Sun 53 | Microsystems' book titled "Java Nativer Interface: 54 | Programmer's Guide and Specification" and freely available 55 | to the public at http://java.sun.com/docs/books/jni. 56 | 57 | ===================================================================== 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

GDS Anti-CSRF Library

2 | **Authors:** Erik Larsson (elarsson@gdssecurity.com), Ron Gutierrez (rgutierrez@gdssecurity.com)
3 | **Company:** Gotham Digital Science, LLC
4 | 5 | This library is designed to be a secure and flexible solution for protecting J2EE web applications against Cross-Site Request Forgery (CSRF) exposures. The solution was initially derived from the anti-CSRF protection built into the SendSafely platform and the specific requirements of a leading financial institution looking to deploy a common, firm-wide solution to CSRF prevention. Open source alternatives were investigated, and while each had its pros and cons and could have potentially worked in a specific instance, none of them was flexible enough to adapt to the diverse and evolving web technology stack of our financial services partner. Since the initial design and implementation, development has continued and the library has undergone several enterprise integrations. 6 | 7 | The following is a list of requirements and guidelines that governed the initial design and implementation of the GDS Anti-CSRF library. Although the implementation is specific to Java/J2EE web application architectures, these requirements and guidelines have been generalized to the extent possible. The intention is to provide developers of other common web technologies a foundation for developing an Anti-CSRF solution with equivalent security and integration flexibility.
8 | 9 | **1. CSRF Protection Must Support Stateless and Stateful Modes**
10 | The solution must work with web applications that do and do not utilize session storage. 11 | 12 | **2. CSRF Token Life Must Be Configurable**
13 | Token life options supported by Stateless and Stateful modes must be configurable. 14 | 15 | **3. CSRF Token Protection Scope Must Be Configurable**
16 | It must be possible to configure the following: 17 |
    18 |
  • White list of exempt URLs - By default, a non-expired token will be considered valid for all application requests (i.e. site-wide protection). It must be possible to exempt specific URLs from site-wide CSRF token validation.
  • 19 | 20 |
  • Black list of protected URLs – it must be possible to only protect specific URLs with a CSRF token
  • 21 |
22 | 23 | **4. API and Integration Documentation Must Be Provided**
24 | The solution API and integration steps must be clearly documented. 25 | 26 | **5. CSRF Tokens Should be User Specific**
27 | The CSRF token should be tied to the authenticated user’s identity. 28 | 29 | **6. CSRF Token Protection Scope Should Support Form Specific Protection**
30 | The scope of protection can be configured so that a non-expired token is tied to a specific form. 31 | 32 | Note: Currently, the library supports site wide and URL specific tokens. Form specific tokens is on the roadmap for a future version. 33 | 34 | **7. Simple Integration with Existing Web Application Technology Stack**
35 | The solution should adhere to the following principles to ensure it is as plug-n-play as possible: 36 | 37 |
    38 |
  • Designed as a library that can be referenced by existing applications
  • 39 |
  • Utilize core platform-level APIs and specifications
  • 40 |
  • Minimize use of 3rd party libraries
  • 41 |
  • Aim to limit code modifications to source code of the integrating application
  • 42 |
43 | 44 | Refer to the Wiki for complete documentation and setup instructions. 45 | 46 | Copyright 2014-2016 Gotham Digital Science LLC 47 | 48 | Licensed under the Apache License, Version 2.0 (the "License"); 49 | you may not use this file except in compliance with the License. 50 | You may obtain a copy of the License at 51 | 52 | http://www.apache.org/licenses/LICENSE-2.0 53 | 54 | Unless required by applicable law or agreed to in writing, software 55 | distributed under the License is distributed on an "AS IS" BASIS, 56 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 57 | See the License for the specific language governing permissions and 58 | limitations under the License. 59 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | The following list highlights key areas where the GDS solution could improve through community contributions. A priority and estimated effort to complete has been supplied.
2 | 3 | **Update documentation to include all possible configuration options (High, Medium)**
4 | 5 | **Add more code samples / tutorials to documentation (High, Medium)**
6 | 7 | **Add more test cases for recent code additions, such as removal of J2EE dependency (High, Medium)**
8 | 9 | **Inject tokens automatically at runtime (High, High)**
10 | The library should offer the capability to automatically insert tokens at runtime (similar to OWASP CSRFGuard). Both traditional HTML based requests as well as AJAX Post requests must be included. Implementing an automated solution would simplify the integration significantly. Runtime injection can however be dangerous since it might introduce an unpredictable behavior in the application where incorrect functionality is not detected until a runtime exception is raised. It might also break hyperlinks if GET requests are being protected. 11 | 12 | **Add form specific protection to Token Protection Scope (High, TBD)**
13 | 14 | **Integrate FindBugs and Fortify into build script (Medium, Low)**
15 | 16 | **Add Maven Integration (Medium, Low)**
17 | All other Java implementations offer automated build integration using Maven. Integrating Maven into the GDS library would make it easier to integrate the library and its dependencies. Bigger projects tend to use automated solutions to keep track of dependencies and Maven is the most common solution to accomplish this. Using Maven would simplify the integration for many application owners. 18 | 19 | **Implement Customizable Logging (Medium, Medium)**
20 | In order to better adopt to custom applications the logging can be made more flexible. One identified action is to implement support for SLF4J. 21 | 22 | SLF4J is an abstraction framework supporting various popular logging APIs such as Log4J or java.util.Logging. Implementing SLF4J will allow the implementing application to decide what logging utility the CSRF library should use. The current solution will use java.util.Logging and any application that wants to use the CSRF logs is therefore forced to use that logging utility. SLF4J would remove that dependency. 23 | 24 | **Implement Double Cookie Submit Pattern (Low, Medium)**
25 | 26 | **Build a tool to help developers identify all requests that might need CSRF Protections (TBD)**
27 | A tool to help developers identify potential CSRF vulnerabilities could be built alongside the CSRF library. The tool could point out locations where CSRF protection is missing and thereby simplify the manual integration. A development team would have to run the tool and manually add token protection based on the results. Although it forces some manual integration, pointing out where and what code to protect will make the integration faster. The tool would use a similar approach to the runtime injection and look for requests being sent to the server. The tool could be built using known frameworks such as PMD and be integrated into the build procedure. Development teams would in such a solution get an update of the current state of the CSRF protection regularly. 28 | -------------------------------------------------------------------------------- /resources/anticsrf.tld: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | Taglib for outputting GDS Anti-CSRF tokens to JSPs 12 | 13 | GDS Anti-CSRF Solution 14 | 0.1 15 | anticsrf 16 | 17 | http://www.gdssecurity.com 18 | 19 | 20 | 21 | Adds CSRF Token to POST form as hidden variable 22 | 23 | Add CSRF Token to Form 24 | forForm 25 | com.gdssecurity.anticsrf.tags.CSRFTokenFormTag 26 | JSP 27 | 28 | 29 | 30 | 31 | Adds CSRF Token variable for appending to a Query String 32 | 33 | Add CSRF Token to URL 34 | forRequest 35 | com.gdssecurity.anticsrf.tags.CSRFTokenUrlTag 36 | JSP 37 | 38 | 39 | 40 | 41 | Adds Url Specific CSRF Token to POST form as hidden variable 42 | 43 | Add CSRF Token to Form 44 | forFormUrlSpecific 45 | com.gdssecurity.anticsrf.tags.CSRFTokenUrlSpecificFormTag 46 | JSP 47 | 48 | url 49 | true 50 | true 51 | java.lang.String 52 | 53 | 54 | 55 | 56 | 57 | Adds CSRF Token variable for appending to a Query String 58 | 59 | Add CSRF Token to URL 60 | forRequestUrlSpecific 61 | com.gdssecurity.anticsrf.tags.CSRFTokenUrlSpecificUrlTag 62 | JSP 63 | 64 | url 65 | true 66 | true 67 | java.lang.String 68 | 69 | 70 | 71 | 72 | 73 | Adds CSRF token without the CSRF token parameter name 74 | 75 | Adds CSRF token to the page 76 | csrfToken 77 | com.gdssecurity.anticsrf.tags.CSRFToken 78 | JSP 79 | 80 | 81 | 82 | 83 | Adds CSRF token parameter name without the CSRF token 84 | 85 | Adds CSRF token parameter name to the page 86 | csrfToken 87 | com.gdssecurity.anticsrf.tags.CSRFTokenParameterName 88 | JSP 89 | 90 | 91 | -------------------------------------------------------------------------------- /resources/anticsrf.xml: -------------------------------------------------------------------------------- 1 | 2 | session 3 | anticsrf-tokenattr 4 | tok 5 | 6 | 7 | 8 | 403 9 | 10 | 11 | 12 | 13 | / 14 | /HomeServlet 15 | /ErrorServlet 16 | 17 | 18 | 19 | userseed 20 | /opt/keyczar_anticsrf_signkey 21 | 5 22 | 23 | 24 | /hmac/filter/URLSpecificServlet 25 | /hmac/custom/CustomURLSpecificServlet 26 | 27 | 28 | 29 | 30 | 31 | /hmac/custom/CustomURLSpecificServlet 32 | /hmac/filter/URLSpecificServlet 33 | 34 | 35 | 36 | /hmac/custom/CustomOneTimeUseServlet 37 | /hmac/filter/OneTimeUseServlet 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /resources/lib/anticsrf.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/resources/lib/anticsrf.jar -------------------------------------------------------------------------------- /resources/lib/gson-2.2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/resources/lib/gson-2.2.4.jar -------------------------------------------------------------------------------- /resources/lib/keyczar-0.71g-090613.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/resources/lib/keyczar-0.71g-090613.jar -------------------------------------------------------------------------------- /resources/lib/log4j-1.2.17.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GDSSecurity/Anti-CSRF-Library/2671632455dd21272233ef7dd955a5b15e2a6fb3/resources/lib/log4j-1.2.17.jar --------------------------------------------------------------------------------