> For the complete documentation index, see [llms.txt](https://docs.sec1.io/user-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sec1.io/user-docs/4-sast/2-java/unsanitized-user-input-in-output-stream.md).

# 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.

  ```java
  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.

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

## References

* [**OWASP XSS Prevention Cheatsheet**](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html)
* [**OWASP Java Encoder**](https://owasp.org/www-project-java-encoder/)
* [**CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')**](https://cwe.mitre.org/data/definitions/79.html)
* [**OWASP Top 10: A03:2021 - Injection**](https://owasp.org/Top10/A03_2021-Injection/)

## 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**](https://scopy.sec1.io/login)
