JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
An insecure cookie is created or used.
Cookies are tokens of information exchanged over a network connection. If that connection is encrypted, cookies are expected to be encrypted as well. However, this might not be the case if a cookie's secure flag is not set. This checker finds such insecure situations.
| Class Name | Insecure Cookie (Java) | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | security | ||||||||||||
| Mnemonic | JAVA.LIB.HTTP.COOKIE | ||||||||||||
| Categories |
|
||||||||||||
| Availability | Available for Java and Kotlin. |
||||||||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Insecure Cookie (Java)" |
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Cookies extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
test1(resp);
test2(resp);
test3(resp);
test4(resp);
test5(resp);
test6(resp);
test7(resp);
}
private void test1(HttpServletResponse resp) {
Cookie cookie = new Cookie("cake", "special value here");
cookie.setSecure(true);
resp.addCookie(cookie);
}
private void test2(HttpServletResponse resp) {
Cookie cookie = new Cookie("cake", "special value here");
cookie.setSecure(false);
System.out.println("danger here!");
resp.addCookie(cookie); // "Insecure Cookie (Java)" warning issued here
}
private void test3(HttpServletResponse resp) {
Cookie cookie = getSecureCookie();
resp.addCookie(cookie); // ok: getSecureCookie() returns a cookie whose secure flag is set
}
private void test4(HttpServletResponse resp) {
Cookie cookie = getInsecureCookie();
resp.addCookie(cookie); // "Insecure Cookie (Java)" warning issued here
}
private Cookie getSecureCookie() {
Cookie cookie = new Cookie("cake", "special value here");
cookie.setSecure(true);
return cookie;
}
private Cookie getInsecureCookie() {
Cookie cookie = new Cookie("cake", "special value here");
cookie.setSecure(System.currentTimeMillis() % 2 == 0);
return cookie;
}
private void test5(HttpServletResponse resp) {
Cookie cookie = new Cookie("cake", "special value here");
resp.addCookie(cookie); // "Insecure Cookie (Java)" warning issued here
}
private void test6(HttpServletResponse resp) {
Cookie cookie = getInsecureCookie();
Cookie copy = cookie;
copy.setSecure(true);
resp.addCookie(cookie); // ok: secure flag set (via copy)
}
private void test7(HttpServletResponse resp) {
Cookie cookie = getInsecureCookie();
cookie.setSecure(true);
Cookie copy = cookie;
copy.setSecure(false);
resp.addCookie(cookie); // "Insecure Cookie (Java)" warning issued here
}
}
In this example, the programmer should always call setSecure(true) on all cookies that the code creates creates.
Set the secure flag of the cookie, after its creation.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.