Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Security and Tomcat Last updated: September 6, 2022
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Prevent Apache Tomcat from XSS (Cross-site-scripting) attacks

According to Microsoft Developer Network, HttpOnly & Secure is an additional flag included in the Set-Cookie HTTP response header.

Using HttpOnly in Set-Cookie helps in mitigating the most common risk of an XSS attack.

This can be either done within an application by developers or implementing the following in Tomcat.

As a best practice, take a backup of configuration file before modifying and if the possible test in non-production to ensure it doesn’t break the application.

Let’s see how to achieve this.

Implement HttpOnly & Secure flag in Tomcat 6.x

  • Log in to Tomcat server
  • Go to Tomcat installation path and then conf folder
  • Open context.xml using vi editor and update Context section as below
useHttpOnly="true"

Ex:

tomcat-httponly

Next, adding a secure flag.

  • Open server.xml and add below in Connector port section
secure="true"
  • Restart Tomcat server to test the application

Implementing in  Tomcat 7.x/8.x/9.x

  • Go to Tomcat >> conf folder
  • Open web.xml and add below in session-config section
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>

Ex:

tomcat-httponly

  • Save the file and restart Tomcat to test it.

Verification

There are multiple ways.

If you are testing Intranet applications, then you can use developer tools inbuilt in the browser like Chrome, IE, or Firefox.

However, if Internet-facing or want to test it externally then it can use HTTP Header Checker online tool.

I hope this adds a layer of Tomcat security. Learn more about Tomcat administration here.

  • Chandan Kumar
    Author
    As the founder of Geekflare, I’ve helped millions to excel in the digital realm. Passionate about technology, I’m on a mission to explore the world and amplify growth for professionals and businesses alike.
Thanks to our Sponsors
More great readings on Security
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Monday.com is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder