Unsanitized user input in raw HTML strings (XSS)
- Rule ID: php_lang_raw_html_using_user_input
- Languages: php
- 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 = "<h1>{$_GET["title"]}</h1>"; // unsafe
- Do use a templating language like Twig, and keep the template in a separate file. Templating languages automatically handle input sanitization, reducing the risk of XSS.
- Do sanitize user input when HTML strings must be used, to prevent malicious code injection.
$html = "<h1>${htmlspecialchars($_GET["title"])}</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=php_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=php_lang_raw_html_using_user_input