DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. The Top 10 Web Application Security Vulnerabilities Starting With XSS

The Top 10 Web Application Security Vulnerabilities Starting With XSS

Carol McDonald user avatar by
Carol McDonald
·
Sep. 30, 09 · Interview
Like (0)
Save
Tweet
Share
15.03K Views

Join the DZone community and get the full member experience.

Join For Free
The Top 10 Web Application security vulnerabilities This and the next series of articles will highlight the Top 10 most critical web application security vulnerabilities identified by the Open Web Application Security Project (OWASP).

You can use OWASP's WebGoat to learn more about the OWASP Top Ten security vulnerabilties. WebGoat is an example web application, which has lessons showing "what not to do code", how to exploit the code, and corrected code for each vulnerability.




You can use the OWASP Enterprise Security API Toolkit to protect against the OWASP Top Ten security vulnerabilties.



The ESAPI Swingset is a web application which demonstrates the many uses of the Enterprise Security API.



OWASP Top 10 number 1: XSS = Cross Site Scripting

Cross Site Scripting (XSS) is one of the most common security problems in today's web applications. According to the SANS Top Cyber Security Risks, 60% of the total attack attempts observed on the Internet are against Web applications and SQL injection and Cross-Site Scripting account for more than 80% of the vulnerabilities being discovered. You are at risk of an XSS attack any time you put content that could contain scripts from someone un-trusted into your web pages.
There are 3 types of cross site scripting:
  • Reflected XSS: is when an html page reflects user input data, e.g. from HTTP query parameters or a HTML form, back to the browser, without properly sanitizing the response. Below is an example of this in a servlet:
    out.writeln(“You searched for: “+request.getParameter(“query”);
  • Stored XSS: is when an Attacker’s input script is stored on the server (e.g. a database) and later displayed in the web server html pages, without proper HTML filtering. Examples of this are in blogs, or forums where users can input data that will be displayed to others. Below is an example, in a servlet data is retrieved from the database and returned in the HTML page without any validation:
    out.writeln("<tr><td>" + guest.name + "<td>" + guest.comment); 

  • DOM XSS: is when JavaScript uses input data or data from the server to write dynamic HTML (DOM) elements, again without HTML sanitizing/escaping/filtering.

XSS can be used to:
  • deface web pages
  • hijack user sessions
  • conduct phishing attacks
  • execute malicious code in the context of the user's session
  • spread malware

Protecting against XSS

To protect against XSS all the parameters in the application should be validated and/or encoded before being output in HTML pages.
  • Always validate on the server side for data integrity and security:
    • Validate all input data to the application for type, format, length, range, and context before storing or displaying.
    • Use white-listing (what is allowed), reject if invalid, instead of filtering out black-list (what is not allowed).
  • Output encoding:
    • Explicitly set character encoding for all web pages (ISO-8859-1 or UTF 8):
      <%@ page contentType="text/html;charset=ISO-8859-1" language="java" %>
    • all user supplied data should be HTML or XML entity encoded before rendering.

Java specific Protecting against XSS

Validating Input with Java

  • You can use Java regular expressions to validate input, this example from WebGoat allows whitespace, a-zA-Z_0-9, and the characters - and ,

    String regex = "[\\s\\w-,]*";
    Pattern pattern = Pattern.compile(regex);
    validate(stringToValidate, pattern);
  • Use Framework (Struts, JSF, Spring...) validators. With Java EE 6 you can use the Bean Validation Framework to centrally define validation constraints on model objects and with JSF 2.0 to extend model validation to the UI. For example here is a JSF 2.0 input field:
    <h:inputText id="creditCard" value="#{booking.creditCardNumber}"/>
    Here is the JSF 2.0 booking Managed Bean using the Bean Validation Framework :

    @ManagedBean
    public class Booking {
    ...
    @NotNull(message = "Credit card number is required")
    @Size(min = 16, max = 16,
    message = "Credit card number must 16 digits long")
    @Pattern(regexp = "^\\d*$",
    message = "Credit card number must be numeric")
    public String getCreditCardNumber() {
    return creditCardNumber;
    }
    }

    In addition there are new JSF 2.0 Validators:
    • <f:validateBean> is a validator that delegates the validation of the local value to the Bean Validation API.
    • <f:validateRequired> provides required field validation.
    • <f:validateRegexp> provides regular expression-based validation
  • Use the OWASP Enterprise Security API Java Toolkit's Validator interface:

    ESAPI.validator().getValidInput(String context,String input,String type,int maxLength,
    boolean allowNull,ValidationErrorList errorList)
    ESAPI.validator().getValidInput() returns canonicalized and validated input as a String. Invalid input will generate a descriptive ValidationErrorList, and input that is clearly an attack will generate a descriptive IntrusionException.

Output Encoding with Java

  • You can use Struts output mechanisms such as <bean:write… >, or use the default JSTL escapeXML="true" attribute in <c:out … > 
  • JSF output components filter output and escape dangerous characters as XHTML entities.

    <h:outputText value="#{param.name}"/>
  • You can use the OWASP Enterprise Security API Java Toolkit's ESAPI Encoder.encodeForHTML() method to encode data for use in HTML content. The encodeForHTML() method uses a "whitelist" HTML entity encoding algorithm to ensure that encoded data can not be interpreted as script. This call should be used to wrap any user input being rendered in HTML element content. For example:

     <p>Hello, <%=ESAPI.encoder().encodeForHTML(name)%></p>   

References and More Information:

  • Bean Validation - The Java EE 6 Tutorial, Volume I
  • JSF 2.0 validation
  • XSSed
  • Top 10 most critical web application security vulnerabilities
  • Open Web Application Security Project (OWASP)
  • OWASP Enterprise Security API
Web application Application security Vulnerability Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Test Design Guidelines for Your CI/CD Pipeline
  • Debezium vs DBConvert Streams: Which Offers Superior Performance in Data Streaming?
  • Tracking Software Architecture Decisions

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: