|
| 1 | +import java.io.IOException; |
| 2 | + |
| 3 | +import javax.servlet.http.Cookie; |
| 4 | +import javax.servlet.http.HttpServletRequest; |
| 5 | +import javax.servlet.http.HttpServletResponse; |
| 6 | +import javax.servlet.ServletException; |
| 7 | + |
| 8 | +import javax.ws.rs.core.NewCookie; |
| 9 | + |
| 10 | +import org.springframework.security.web.csrf.CsrfToken; |
| 11 | + |
| 12 | +class SensitiveCookieNotHttpOnly { |
| 13 | + // GOOD - Tests adding a sensitive cookie with the `HttpOnly` flag set. |
| 14 | + public void addCookie(String jwt_token, HttpServletRequest request, HttpServletResponse response) { |
| 15 | + Cookie jwtCookie = new Cookie("jwt_token", jwt_token); |
| 16 | + jwtCookie.setPath("/"); |
| 17 | + jwtCookie.setMaxAge(3600*24*7); |
| 18 | + jwtCookie.setHttpOnly(true); |
| 19 | + response.addCookie(jwtCookie); |
| 20 | + } |
| 21 | + |
| 22 | + // BAD - Tests adding a sensitive cookie without the `HttpOnly` flag set. |
| 23 | + public void addCookie2(String jwt_token, String userId, HttpServletRequest request, HttpServletResponse response) { |
| 24 | + String tokenCookieStr = "jwt_token"; // $Source |
| 25 | + Cookie jwtCookie = new Cookie(tokenCookieStr, jwt_token); |
| 26 | + Cookie userIdCookie = new Cookie("user_id", userId); |
| 27 | + jwtCookie.setPath("/"); |
| 28 | + userIdCookie.setPath("/"); |
| 29 | + jwtCookie.setMaxAge(3600*24*7); |
| 30 | + userIdCookie.setMaxAge(3600*24*7); |
| 31 | + response.addCookie(jwtCookie); // $Alert |
| 32 | + response.addCookie(userIdCookie); |
| 33 | + } |
| 34 | + |
| 35 | + // GOOD - Tests set a sensitive cookie header with the `HttpOnly` flag set. |
| 36 | + public void addCookie3(String authId, HttpServletRequest request, HttpServletResponse response) { |
| 37 | + response.addHeader("Set-Cookie", "token=" +authId + ";HttpOnly;Secure"); |
| 38 | + } |
| 39 | + |
| 40 | + // BAD - Tests set a sensitive cookie header without the `HttpOnly` flag set. |
| 41 | + public void addCookie4(String authId, HttpServletRequest request, HttpServletResponse response) { |
| 42 | + response.addHeader("Set-Cookie", "token=" +authId + ";Secure"); // $Alert |
| 43 | + } |
| 44 | + |
| 45 | + // GOOD - Tests set a sensitive cookie header using the class `javax.ws.rs.core.Cookie` with the `HttpOnly` flag set through string concatenation. |
| 46 | + public void addCookie5(String accessKey, HttpServletRequest request, HttpServletResponse response) { |
| 47 | + response.setHeader("Set-Cookie", new NewCookie("session-access-key", accessKey, "/", null, null, 0, true) + ";HttpOnly"); |
| 48 | + } |
| 49 | + |
| 50 | + // BAD - Tests set a sensitive cookie header using the class `javax.ws.rs.core.Cookie` without the `HttpOnly` flag set. |
| 51 | + public void addCookie6(String accessKey, HttpServletRequest request, HttpServletResponse response) { |
| 52 | + response.setHeader("Set-Cookie", new NewCookie("session-access-key", accessKey, "/", null, null, 0, true).toString()); // $Alert |
| 53 | + } |
| 54 | + |
| 55 | + // GOOD - Tests set a sensitive cookie header using the class `javax.ws.rs.core.Cookie` with the `HttpOnly` flag set through the constructor. |
| 56 | + public void addCookie7(String accessKey, HttpServletRequest request, HttpServletResponse response) { |
| 57 | + NewCookie accessKeyCookie = new NewCookie("session-access-key", accessKey, "/", null, null, 0, true, true); |
| 58 | + response.setHeader("Set-Cookie", accessKeyCookie.toString()); |
| 59 | + } |
| 60 | + |
| 61 | + // BAD - Tests set a sensitive cookie header using the class `javax.ws.rs.core.Cookie` without the `HttpOnly` flag set. |
| 62 | + public void addCookie8(String accessKey, HttpServletRequest request, HttpServletResponse response) { |
| 63 | + NewCookie accessKeyCookie = new NewCookie("session-access-key", accessKey, "/", null, 0, null, 86400, true); // $Source |
| 64 | + String keyStr = accessKeyCookie.toString(); |
| 65 | + response.setHeader("Set-Cookie", keyStr); // $Alert |
| 66 | + } |
| 67 | + |
| 68 | + // BAD - Tests set a sensitive cookie header using a variable without the `HttpOnly` flag set. |
| 69 | + public void addCookie9(String authId, HttpServletRequest request, HttpServletResponse response) { |
| 70 | + String secString = "token=" +authId + ";Secure"; // $Source |
| 71 | + response.addHeader("Set-Cookie", secString); // $Alert |
| 72 | + } |
| 73 | + |
| 74 | + // GOOD - Tests set a sensitive cookie header with the `HttpOnly` flag set using `String.format(...)`. |
| 75 | + public void addCookie10(HttpServletRequest request, HttpServletResponse response) { |
| 76 | + response.addHeader("SET-COOKIE", String.format("%s=%s;HttpOnly", "sessionkey", request.getSession().getAttribute("sessionkey"))); |
| 77 | + } |
| 78 | + |
| 79 | + public Cookie createHttpOnlyAuthenticationCookie(HttpServletRequest request, String jwt) { |
| 80 | + String PRESTO_UI_COOKIE = "Presto-UI-Token"; |
| 81 | + Cookie cookie = new Cookie(PRESTO_UI_COOKIE, jwt); |
| 82 | + cookie.setHttpOnly(true); |
| 83 | + cookie.setPath("/ui"); |
| 84 | + return cookie; |
| 85 | + } |
| 86 | + |
| 87 | + public Cookie createAuthenticationCookie(HttpServletRequest request, String jwt) { |
| 88 | + String PRESTO_UI_COOKIE = "Presto-UI-Token"; // $Source |
| 89 | + Cookie cookie = new Cookie(PRESTO_UI_COOKIE, jwt); |
| 90 | + cookie.setPath("/ui"); |
| 91 | + return cookie; |
| 92 | + } |
| 93 | + |
| 94 | + public Cookie removeAuthenticationCookie(HttpServletRequest request, String jwt) { |
| 95 | + String PRESTO_UI_COOKIE = "Presto-UI-Token"; |
| 96 | + Cookie cookie = new Cookie(PRESTO_UI_COOKIE, jwt); |
| 97 | + cookie.setPath("/ui"); |
| 98 | + cookie.setMaxAge(0); |
| 99 | + return cookie; |
| 100 | + } |
| 101 | + |
| 102 | + // GOOD - Tests set a sensitive cookie header with the `HttpOnly` flag set using a wrapper method. |
| 103 | + public void addCookie11(HttpServletRequest request, HttpServletResponse response, String jwt) { |
| 104 | + Cookie cookie = createHttpOnlyAuthenticationCookie(request, jwt); |
| 105 | + response.addCookie(cookie); |
| 106 | + } |
| 107 | + |
| 108 | + // BAD - Tests set a sensitive cookie header without the `HttpOnly` flag set using a wrapper method. |
| 109 | + public void addCookie12(HttpServletRequest request, HttpServletResponse response, String jwt) { |
| 110 | + Cookie cookie = createAuthenticationCookie(request, jwt); |
| 111 | + response.addCookie(cookie); // $Alert |
| 112 | + } |
| 113 | + |
| 114 | + // GOOD - Tests remove a sensitive cookie header without the `HttpOnly` flag set using a wrapper method. |
| 115 | + public void addCookie13(HttpServletRequest request, HttpServletResponse response, String jwt) { |
| 116 | + Cookie cookie = removeAuthenticationCookie(request, jwt); |
| 117 | + response.addCookie(cookie); |
| 118 | + } |
| 119 | + |
| 120 | + private Cookie createCookie(String name, String value, Boolean httpOnly){ |
| 121 | + Cookie cookie = null; |
| 122 | + cookie = new Cookie(name, value); |
| 123 | + cookie.setDomain("/"); |
| 124 | + cookie.setHttpOnly(httpOnly); |
| 125 | + |
| 126 | + //for production https |
| 127 | + cookie.setSecure(true); |
| 128 | + |
| 129 | + cookie.setMaxAge(60*60*24*30); |
| 130 | + cookie.setPath("/"); |
| 131 | + |
| 132 | + return cookie; |
| 133 | + } |
| 134 | + |
| 135 | + // GOOD - Tests set a sensitive cookie header with the `HttpOnly` flag set through a boolean variable using a wrapper method. |
| 136 | + public void addCookie14(HttpServletRequest request, HttpServletResponse response, String refreshToken) { |
| 137 | + response.addCookie(createCookie("refresh_token", refreshToken, true)); |
| 138 | + } |
| 139 | + |
| 140 | + // BAD (but not detected) - Tests set a sensitive cookie header with the `HttpOnly` flag not set through a boolean variable using a wrapper method. |
| 141 | + // This example is missed because the `cookie.setHttpOnly` call in `createCookie` is thought to maybe set the HTTP-only flag, and the `cookie` |
| 142 | + // object flows to this `addCookie` call. |
| 143 | + public void addCookie15(HttpServletRequest request, HttpServletResponse response, String refreshToken) { |
| 144 | + response.addCookie(createCookie("refresh_token", refreshToken, false)); // $MISSING:Alert |
| 145 | + } |
| 146 | + |
| 147 | + // GOOD - CSRF token doesn't need to have the `HttpOnly` flag set. |
| 148 | + public void addCsrfCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 149 | + // Spring put the CSRF token in session attribute "_csrf" |
| 150 | + CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf"); |
| 151 | + |
| 152 | + // Send the cookie only if the token has changed |
| 153 | + String actualToken = request.getHeader("X-CSRF-TOKEN"); |
| 154 | + if (actualToken == null || !actualToken.equals(csrfToken.getToken())) { |
| 155 | + // Session cookie that can be used by AngularJS |
| 156 | + String pCookieName = "CSRF-TOKEN"; |
| 157 | + Cookie cookie = new Cookie(pCookieName, csrfToken.getToken()); |
| 158 | + cookie.setMaxAge(-1); |
| 159 | + cookie.setHttpOnly(false); |
| 160 | + cookie.setPath("/"); |
| 161 | + response.addCookie(cookie); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments