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 |
30 |
31 |
32 | <%if(ConfigUtil.isSessionMode()){ %>
33 |
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 |
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
--------------------------------------------------------------------------------