What is it and why should I care?
The HttpOnly flag is an additional flag that is used to prevent an XSS (Cross-Site Scripting) exploit from gaining access to the session cookie. Because one of the most common results of an XSS attack is access to the session cookie, and to subsequently hijack the victim’s session, the HttpOnly flag is a useful prevention mechanism.
What should I do about it?
The resolution here is quite simple. You must add the HttpOnly flag to your session cookie (and preferably to all cookies).
Here’s an example of how a session cookie might look without the HttpOnly flag:
And now, with the HttpOnly flag:
Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H; HttpOnly;
Finally, here is an example of using both the secure and HttpOnly flags:
Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H; HttpOnly; secure;
Not much to it. You can obviously do this manually, but if you’re working in a Servlet 3.0 or newer environment, a simple configuration setting in the web.xml will take care of this for you. You should add this snippet to your web.xml:
And, if you also use the secure flag, it will look like this:
As you can see, resolving this issue is quite simple. So it should be on everyone’s TODO list!