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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/2-java/unsanitized-user-input-in-output-stream.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
