Unsanitized user input in raw HTML strings (XSS)
- Rule ID: javascript_lang_raw_html_using_user_input
- Languages: javascript
- 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.
const html = `<h1>${req.params.title}</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.
import sanitizeHtml from 'sanitize-html'
const sanitizedTitle = sanitizeHtml(req.params.title)
const html = `<h1>${sanitizedTitle}</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=javascript_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=javascript_lang_raw_html_using_user_input