Chapter 74: Vue v-html Directive

V-html

This directive is not like v-if, v-for, or v-model — it’s in a completely different league because it can either be extremely useful or extremely dangerous (XSS security hole) depending on how you use it.

I’m going to explain it very carefully, step by step, like I’m sitting next to you warning you before you touch a live wire.

1. What does v-html actually do?

v-html tells Vue:

“Take this string value, treat it as raw HTML, and inject it directly into the innerHTML of this element.”

In other words — it bypasses Vue’s normal text escaping and rendering system.

Normal text interpolation {{ }}:

HTML

→ If userBio = “<strong>Bold text</strong>” → user sees literal <strong>Bold text</strong> (escaped)

With v-html:

HTML

→ User sees actual bold text: Bold text

Vue takes the string and does element.innerHTML = userBio under the hood.

2. The Golden Rule (Memorize This Forever)

Never use v-html with content that comes from a user or an untrusted source.

Why?

Because any HTML + JavaScript inside the string will be executed.

JavaScript

→ If you do <div v-html=”userBio”></div> → the script runs → cookies stolen → XSS attack

This is exactly how XSS (Cross-Site Scripting) vulnerabilities happen in Vue apps.

3. Safe & Realistic Use-Cases (When v-html is Actually Okay)

You should only use v-html when:

  1. The HTML comes from your own trusted server (you control it 100%)
  2. The HTML is sanitized on the server (never trust client input)
  3. You’re rendering rich text from a CMS (Markdown → HTML, TinyMCE, CKEditor output)
  4. You’re showing pre-formatted content from admin panel / database

Real example 1 – Blog post content from trusted backend

vue

→ post.body contains safe HTML from your own backend → v-html renders it beautifully

Real example 2 – Rendering sanitized Markdown

vue

→ Markdown from trusted source → converted to HTML → safely rendered with v-html

4. The Danger Zone – Never Do This

Never ever do this:

vue

→ If attacker submits <script>alert(‘XSS’);</script> → it executes

Never trust:

  • URL query params
  • LocalStorage values (user can edit)
  • API responses from untrusted users
  • Any client-controlled string

5. Quick Summary Table – v-html Do’s & Don’ts (2026)

Situation Safe to use v-html? Recommended Approach
Trusted backend / CMS content Yes Sanitize on server + v-html
Markdown / rich text from admin Yes marked / markdown-it + sanitize + v-html
User comments / forum posts No Use textContent or safe renderer (DOMPurify)
Dynamic HTML from API (trusted) Yes Sanitize + v-html
URL params / query string No Never – XSS risk
Any client-editable content No Use {{ text }} or textContent

Pro Tips from Real Projects (Hyderabad 2026)

  • Always sanitize HTML on server-side before sending to client

  • If you must render user-generated HTML client-side → use DOMPurify library

    JavaScript
  • Use v-html only when you really need HTML formatting — prefer {{ text }} or v-text when possible

  • In Vue Devtools → you can see exactly what HTML was injected via v-html

  • For email templates / newsletters → v-html is fine (trusted content)

  • Never use v-html inside user-facing areas without sanitization — one XSS vulnerability = big security incident

Your mini practice task:

  1. Create a component that shows blog post content with v-html (use safe static HTML)
  2. Try injecting <script>alert(‘test’)</script> → see it execute (danger demo)
  3. Add DOMPurify → sanitize before v-html → see script is removed

Any part confusing? Want full examples for:

  • v-html + DOMPurify + user comments (safe way)?
  • v-html vs v-text vs {{ }} security comparison?
  • Real blog post renderer with Markdown → HTML → v-html?
  • When to use v-html in production (trusted CMS cases)?

Just tell me — we’ll build the next safe, rich-text component together 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *