--- title: Never Use v-html with User-Provided Content impact: HIGH impactDescription: Using v-html with untrusted content leads to XSS vulnerabilities type: capability tags: [vue3, security, xss, v-html, template] --- # Never Use v-html with User-Provided Content **Impact: HIGH** - Dynamically rendering arbitrary HTML with `v-html` can lead to Cross-Site Scripting (XSS) vulnerabilities. Attackers can inject malicious scripts that execute in users' browsers, stealing credentials or performing actions on their behalf. The `v-html` directive renders raw HTML without sanitization. While useful for trusted content, it bypasses Vue's automatic text escaping and should never be used with user input. ## Task Checklist - [ ] Never use `v-html` with user-provided content - [ ] Prefer text interpolation `{{ }}` which automatically escapes HTML - [ ] Use components for template composition instead of `v-html` - [ ] If raw HTML is absolutely needed, sanitize it with a library like DOMPurify - [ ] Audit existing `v-html` usage for potential XSS vectors **Incorrect:** ```vue const userComment = ref(props.comment) ``` **Correct:** ```vue ``` ## When v-html Is Acceptable ```vue ``` ## XSS Attack Examples Attackers can inject various payloads: ```html
Login required...
``` ## Reference - [Vue.js Template Syntax - Raw HTML](https://vuejs.org/guide/essentials/template-syntax.html#raw-html) - [OWASP XSS Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html)