VOOZH about

URL: https://www.javacodegeeks.com/2021/03/handling-cross-site-scripting-xss-in-java.html

⇱ Handling Cross-Site Scripting (XSS) in Java - Java Code Geeks


Cross-site scripting (XSS) is a type of security vulnerability in web applications where an attacker injects malicious scripts through some kind of user input (like input boxes, URL parameters, HTML headers, etc)
It is important to prevent XSS attacks to safeguard the confidentiality, integrity, and availability of the information of the web application. The two main cross-site scripting flaws are reflected and stored:

Reflected XSS

Malicious content from a user request is displayed to the user or it is written into the page after from server response. For instance, in the next screenshot, the credit card number field is vulnerable. After the number, there is a script to be injected:

  
👁 Image

When the purchase button is clicked, the alert windows is displayed:

👁 Image

A patch to the flaw in Java

When you have a String RequestParam, avoid handling it without sanitization:

👁 Image

The OWASP Java encoder has a method called forHtml for that purpose:

👁 Image

Stored XSS

The payload is persisted. For example, in the next screenshot, you can see that a script is added as a comment. When the page is loaded the script is executed and printed as part of the code.

👁 Image

A patch to the flaw in Java

The solution is to sanitize the RequestBody before handling it:

👁 Image

Now, the comment is printed as text, but it is not executed:

👁 Image
Published on Java Code Geeks with permission by Eidher Julian, partner at our JCG program. See the original article here: Handling Cross-Site Scripting (XSS) in Java


Opinions expressed by Java Code Geeks contributors are their own.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Eidher Julian
Eidher Julian
March 3rd, 2021Last Updated: March 1st, 2021
0 2,627 1 minute read

Eidher Julian

Eidher Julian is a Systems Engineer and Software Engineering Specialist with 13+ years of experience as a Java developer. He is an Oracle Certified Associate and SOA Certified Architect.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz