Implement cookie HTTP header flag with HTTPOnly & Secure to protect a website from XSS attacks
Do you know you can mitigate most common XSS attacks using
Secure flag with your cookie?
Without having HttpOnly and Secure flag in the HTTP response header, it is possible to steal or manipulate web application sessions and cookies.
It’s better to manage this within the application code. However, due to developers’ unawareness, it comes to Web Server administrators.
I will not talk about how to set these at the code level. You can refer here.
Implementation Procedure in Apache
- Ensure you have
mod_headers.soenabled in Apache HTTP server
- Add following entry in httpd.conf
Header always edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
- Restart Apache HTTP server to test
Note: Header edit is not compatible with lower than Apache 2.2.4 version.
You can use the following to set the HttpOnly and Secure flag in lower than the 2.2.4 version. Thanks to Ytse for sharing this information.
Header set Set-Cookie HttpOnly;Secure
You can either leverage the browser's inbuilt developer tools to check the response header or use an online tool.
Did it help?
This is one of the many hardening things to do in Apache.
More great readings on Apache HTTP
How to Redirect AMP Page to Non-AMP in Nginx, Apache, Cloudflare?Abhishek Nair on September 27, 2021
How to Install Apache and Secure with Let’s Encrypt Certtificate?Aghilan Baskar on April 5, 2021
How to Block .git in Apache, Nginx and Cloudflare?Chandan Kumar on October 27, 2020
How to Protect Page with Password in Apache, Nginx, WordPress, Hosting?Chandan Kumar on September 11, 2020
How to Implement ZeroSSL Certificate in Apache and Nginx?Asad Ali on May 24, 2020
How to Enable CORS in Apache and Nginx?Chandan Kumar on October 18, 2019
Join Geekflare Newsletter
Every week we share trending articles and tools in our newsletter. More than 10,000 people enjoy reading, and you will love it too.