Unsanitized user input in raw HTML strings (XSS)
- Rule ID: python_lang_raw_html_using_user_input
- Languages: python
- Source: raw_html_using_user_input.yml
Description
Including unsanitized user input in HTML exposes your application to cross-site scripting (XSS) attacks. This vulnerability allows attackers to inject malicious scripts into web pages viewed by other users.
Remediations
- Do not include user input directly in HTML strings. This practice can lead to XSS vulnerabilities.
html = f"<h1>{user_input}</h1>" # unsafe
- Do use a framework or templating language that automatically handles the encoding and sanitization of user input when constructing HTML. This approach minimizes the risk of XSS attacks.
- Do sanitize user input if you must use HTML strings directly. Utilize libraries designed for input sanitization to ensure that user input does not contain malicious content.
from html_sanitizer import Sanitizer
sanitizer = Sanitizer()
sanitized_value = sanitizer.sanitize(user_input)
html = f"<h1>{sanitized_value}</h1>"
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=python_lang_raw_html_using_user_input
To run only this rule during a scan, use the following flag
bearer scan /path/to/your-project/ --only-rule=python_lang_raw_html_using_user_input