The HttpOnly
attribute when set to true
protects the cookie value from being accessed by client side JavaScript such as reading the document.cookie
values. By enabling this protection, a website that is vulnerable to Cross-Site Scripting (XSS) will be able to block malicious scripts from accessing the cookie value from JavaScript.
1// Noncompliant: The `httponly` attribute of cookies is set to `false`
2fun noncompliant(value: String, response: HttpServletResponse) {
3 val cookie: Cookie = Cookie("cookie", value)
4 cookie.setHttpOnly(false)
5 response.addCookie(cookie)
6}
1// Compliant: The `httponly` attribute of cookies is set to `true`
2fun compliant(value: String, response: HttpServletResponse) {
3 val cookie: Cookie = Cookie("cookie", value)
4 cookie.setSecure(true)
5 cookie.setHttpOnly(true)
6 response.addCookie(cookie)
7}