Skip to content

setHtmlDirectives

Reports using Astro's set:html directive, which injects content without escaping.

✅ This rule is included in the astro preset.

Astro’s set:html directive injects raw HTML into the page without escaping it first. That makes it similar to setting innerHTML in the browser. If the value is not fully trusted or sanitized first, it can introduce cross-site scripting (XSS) vulnerabilities.

This rule reports any use of set:html. Prefer normal Astro expressions when you want text content to be escaped automatically, or set:text when you want to set plain text explicitly.

---
const html = await getPostBody();
---
<article set:html={html} />
<p set:html={`Hello, <strong>world!</strong>`} />

This rule is not configurable.

If your project intentionally renders HTML from a source you fully trust, or from content that has already been sanitized, this rule may be too strict. In those cases, consider disabling the rule for the specific line or file and documenting why the HTML is safe.

Made with ❤️‍🔥 around the world by the Flint team and contributors.