XSS prevention stops bad code from running in your app. It keeps users safe from attacks that steal data or break your site.
XSS prevention in templates in Vue
<template>
<div>{{ userInput }}</div>
</template>Vue automatically escapes content inside double curly braces {{ }}.
Use v-html only if you trust the content, because it inserts raw HTML.
<template>
<p>{{ message }}</p>
</template>
<script setup>
const message = '<script>alert("XSS")</script>'
</script>v-html inserts HTML directly. Only do this with safe content.<template> <p v-html="trustedHtml"></p> </template> <script setup> const trustedHtml = '<strong>Safe bold text</strong>' </script>
This example shows how Vue escapes the comment inside {{ }} so the script does not run. But using v-html inserts the raw HTML, which can run harmful code.
<template>
<div>
<h2>User Comment:</h2>
<p>{{ userComment }}</p>
<h2>Unsafe HTML with v-html (dangerous):</h2>
<p v-html="userComment"></p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const userComment = ref('<img src=x onerror=alert("XSS Attack")>')
</script>Always prefer using {{ }} for user content to keep it safe.
Only use v-html with content you fully trust or have sanitized.
Consider using libraries to clean HTML if you must display user HTML safely.
Vue escapes content inside {{ }} to prevent XSS automatically.
Using v-html inserts raw HTML and can cause XSS if content is unsafe.
Always validate or sanitize user input before showing it as HTML.
Practice
Solution
Step 1: Understand Vue's default escaping
Vue automatically escapes content inside{{ }}to prevent XSS by converting special characters to safe HTML entities.Step 2: Compare with other methods
Usingv-htmlinserts raw HTML and can cause XSS if content is unsafe. Direct DOM manipulation bypasses Vue's protections.Final Answer:
Using double curly braces like {{ userInput }} -> Option DQuick Check:
Vue escapes {{ }} content = A [OK]
- Thinking v-html escapes content automatically
- Assuming v-text sanitizes HTML
- Believing direct DOM manipulation is safe
Solution
Step 1: Check syntax correctness
<div>{{ userInput }}</div>uses correct Vue syntax with double curly braces and quotes are properly placed.Step 2: Check XSS safety
<div>{{ userInput }}</div>escapes content automatically, making it safe.<div v-html="userInput"></div>uses v-html which inserts raw HTML and can be unsafe.<div v-text=userInput></div>misses quotes around the binding value, causing syntax error.<div>{{ userInput</div> }}has mismatched braces causing syntax error.Final Answer:
<div>{{ userInput }}</div> -> Option BQuick Check:
Correct syntax + auto-escape = D [OK]
- Using v-html without sanitizing input
- Omitting quotes in directives
- Mismatching curly braces
<pre><template> <div>{{ userInput }}</div> <div v-html="userInput"></div> </template> And
userInput is '<img src=x onerror=alert(1)>', what will be rendered in the browser?Solution
Step 1: Analyze the first div with {{ userInput }}
Vue escapes the content, so the HTML tag is shown as plain text, no alert runs.Step 2: Analyze the second div with v-html
v-html inserts raw HTML, so the<img>tag runs and triggers the alert.Final Answer:
The first div shows the raw HTML tag as text; the second div executes the alert popup. -> Option AQuick Check:
v-html inserts raw HTML = A [OK]
- Assuming both divs escape content
- Thinking v-html escapes content
- Confusing which div triggers alert
<div v-html="userContent"></div>
User input is not sanitized. What is the best fix to prevent XSS?
Solution
Step 1: Identify the risk of v-html with unsanitized input
v-html inserts raw HTML, so unsanitized user input can cause XSS attacks.Step 2: Choose a safe alternative
Replacing v-html with {{ userContent }} escapes content automatically, preventing XSS.Final Answer:
Replace v-html with {{ userContent }} to auto-escape -> Option AQuick Check:
Use {{ }} to escape user input safely = C [OK]
- Adding v-text does not sanitize v-html content
- Trusting all user input without sanitization
- Using script tags incorrectly
<b> and <i> but prevent XSS in Vue. Which approach is best?Solution
Step 1: Understand the need to allow some HTML but block unsafe tags
Using v-html allows HTML rendering but can cause XSS if input is unsafe.Step 2: Use a sanitizer to clean user input
Sanitizing removes dangerous tags/scripts but keeps safe tags like<b>and<i>.Step 3: Bind sanitized content with v-html
This approach safely renders allowed HTML while preventing XSS.Final Answer:
Use v-html with a sanitizer function that removes unsafe tags before binding -> Option CQuick Check:
Sanitize input before v-html = B [OK]
- Using v-html without sanitizing
- Escaping all HTML losing formatting
- Replacing safe tags with script tags
Ready to prove it?
Pass the test and this pattern moves unknown
