Unsanitized user input in output stream (XSS)

Description

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

Remediations

  • Do use an encoder to handle user input before incorporating it into the output stream. This step helps minimize 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);
  • Do sanitize user input to remove or neutralize unwanted scripts. Sanitization goes beyond encoding by actively removing 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

Associated CWE

OWASP Top 10

Configuration

To skip this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --skip-rule=java_lang_xss_response_writer

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=java_lang_xss_response_writer