Unsanitized user input in React inner HTML method (XSS)

Description

Using React's dangerouslySetInnerHTML with unsanitized data can introduce Cross-Site Scripting (XSS) vulnerabilities. This occurs when external input is embedded directly into the HTML without proper sanitization, allowing attackers to inject malicious scripts.

Remediations

  • Do sanitize data before using it with dangerouslySetInnerHTML. This step is crucial to prevent XSS attacks by ensuring that the input does not contain harmful scripts.
<div dangerouslySetInnerHTML={{__html: sanitize(data)}} />

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_react_dangerously_set_inner_html

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=javascript_react_dangerously_set_inner_html