|
8 | 8 | import javax.servlet.http.*; |
9 | 9 |
|
10 | 10 | class Test { |
11 | | - public static void test(HttpServletRequest request, HttpServletResponse response) { |
| 11 | + |
| 12 | + public static final boolean constTrue = true; |
| 13 | + |
| 14 | + public static void test(HttpServletRequest request, HttpServletResponse response, boolean alwaysSecure, boolean otherInput) { |
12 | 15 | { |
13 | 16 | Cookie cookie = new Cookie("secret" ,"fakesecret"); |
14 | | - |
| 17 | + |
15 | 18 | // BAD: secure flag not set |
16 | 19 | response.addCookie(cookie); |
17 | | - |
| 20 | + |
18 | 21 | } |
19 | | - |
| 22 | + |
20 | 23 | { |
21 | 24 | Cookie cookie = new Cookie("secret" ,"fakesecret"); |
22 | | - |
| 25 | + |
| 26 | + // BAD: secure flag set to false |
| 27 | + cookie.setSecure(false); |
| 28 | + response.addCookie(cookie); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + { |
| 33 | + Cookie cookie = new Cookie("secret" ,"fakesecret"); |
| 34 | + |
| 35 | + // BAD: secure flag set to something not clearly true or request.isSecure() |
| 36 | + cookie.setSecure(otherInput); |
| 37 | + response.addCookie(cookie); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + { |
| 42 | + Cookie cookie = new Cookie("secret" ,"fakesecret"); |
| 43 | + |
| 44 | + // BAD: secure flag sometimes set to something clearly true or request.isSecure() |
| 45 | + boolean secureVal; |
| 46 | + if(alwaysSecure) |
| 47 | + secureVal = true; |
| 48 | + else |
| 49 | + secureVal = otherInput; |
| 50 | + cookie.setSecure(secureVal); |
| 51 | + response.addCookie(cookie); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + { |
| 56 | + Cookie cookie = new Cookie("secret" ,"fakesecret"); |
| 57 | + |
| 58 | + // GOOD: set secure flag |
| 59 | + cookie.setSecure(true); |
| 60 | + response.addCookie(cookie); |
| 61 | + } |
| 62 | + |
| 63 | + { |
| 64 | + Cookie cookie = new Cookie("secret" ,"fakesecret"); |
| 65 | + |
23 | 66 | // GOOD: set secure flag |
24 | 67 | cookie.setSecure(true); |
25 | 68 | response.addCookie(cookie); |
26 | 69 | } |
| 70 | + |
| 71 | + { |
| 72 | + Cookie cookie = new Cookie("secret" ,"fakesecret"); |
| 73 | + |
| 74 | + // GOOD: set secure flag |
| 75 | + cookie.setSecure(constTrue); |
| 76 | + response.addCookie(cookie); |
| 77 | + } |
| 78 | + |
| 79 | + { |
| 80 | + Cookie cookie = new Cookie("secret" ,"fakesecret"); |
| 81 | + |
| 82 | + // GOOD: set secure flag if contacted over HTTPS |
| 83 | + cookie.setSecure(alwaysSecure ? true : request.isSecure()); |
| 84 | + response.addCookie(cookie); |
| 85 | + } |
| 86 | + |
27 | 87 | } |
28 | 88 | } |
0 commit comments