Unsanitized User Input in Output Stream (XSS)

Overview

  • Rule ID: java_lang_xss_response_writer

  • Applicable Languages: Java

  • Weakness ID: CWE-79

Description

Cross-site scripting (XSS) vulnerabilities arise when unsanitized user input is included in web page content. This flaw can lead to the execution of malicious scripts within the user's browser, compromising the security of user data and interactions with the application.

Remediation Guidelines

  • Use an encoder to handle user input before incorporating it into the output stream. This minimizes the risk of XSS attacks by converting potentially dangerous characters into a safe format.

    String userInput = req.getQueryString("user");
    String encodedUserInput = Encode.forHtml(userInput);
    response.getWriter().write(encodedUserInput);
    
  • Additionally, sanitize user input to remove or neutralize unwanted scripts. Sanitization goes beyond encoding by actively eliminating harmful content from user input before it is used in the output.

    String userInput = req.getQueryString("user");
    String sanitizedUserInput = sanitize(userInput);
    response.getWriter().write(sanitizedUserInput);

References

Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our SAST TOOL

Last updated