📖 Scenario: You are building a simple Vue app that shows user comments on a blog. You want to make sure the app is safe from common security problems like showing harmful code.
🎯 Goal: Create a Vue component that safely displays user comments by preventing unsafe HTML from running.
📋 What You'll Learn
Create a Vue 3 component with a list of user comments
Add a configuration variable to control whether to allow HTML in comments
Use Vue's built-in features to safely render comments based on the configuration
Complete the component with proper template and script setup
💡 Why This Matters
🌍 Real World
Web apps often show user-generated content. Unsafe HTML can cause security problems like cross-site scripting (XSS). This project shows how to handle user comments safely in Vue.
💼 Career
Understanding how to prevent security risks in frontend frameworks like Vue is essential for web developers to build safe and trustworthy applications.
Progress0 / 4 steps
1
Set up the comments data
Create a Vue 3 component with a comments array containing these exact strings: 'Hello world!', 'Nice post!', and ''.
Vue
Hint
Use const comments = [ ... ] with the exact strings inside.
2
Add a configuration to allow HTML
Add a allowHtml boolean variable set to false to control if comments should render HTML or plain text.
Vue
Hint
Use const allowHtml = false exactly.
3
Render comments safely based on configuration
In the template, use a v-for loop with comment as the item to display each comment. Use v-html to render HTML only if allowHtml is true; otherwise, display the comment as plain text inside a <div>.
Vue
Hint
Use v-for with comment and index. Use v-html only if allowHtml is true, else show plain text.
4
Complete the component with accessibility and structure
Add a <section> wrapper with an aria-label of "User comments" around the comments list for accessibility.
Vue
Hint
Wrap the comments in a <section> with aria-label="User comments" for screen readers.
Practice
(1/5)
1. Why is security important when building Vue applications?
easy
A. To reduce the size of the Vue library
B. To make the app load faster
C. To protect users from malicious attacks like cross-site scripting (XSS)
D. To add more animations and effects
Solution
Step 1: Understand the purpose of security in web apps
Security protects users and data from harmful actions like hacking or data theft.
Step 2: Identify common security risks in Vue apps
Cross-site scripting (XSS) is a common risk where attackers inject harmful code into the app.
Final Answer:
To protect users from malicious attacks like cross-site scripting (XSS) -> Option C
Quick Check:
Security = Protect users from attacks [OK]
Hint: Security protects users from harmful code injections [OK]
Common Mistakes:
Confusing security with performance
Thinking security only affects backend
Believing Vue automatically prevents all risks
2. Which Vue syntax is safe to display user content without risking XSS attacks?
A. v-html renders raw HTML which can run malicious scripts
B. The img tag is invalid and causes syntax errors
C. Vue automatically sanitizes v-html content, so no problem
D. Using const for rawHtml causes reactivity issues
Solution
Step 1: Understand v-html behavior
v-html inserts raw HTML into the DOM without escaping, so scripts can run.
Step 2: Identify the risk in the example
The img tag has an onerror attribute that triggers alert(1), a common XSS attack vector.
Final Answer:
v-html renders raw HTML which can run malicious scripts -> Option A
Quick Check:
v-html = raw HTML injection risk [OK]
Hint: v-html inserts raw HTML; sanitize before use [OK]
Common Mistakes:
Assuming Vue sanitizes v-html automatically
Confusing syntax errors with security issues
Thinking const affects security
5. You want to safely display user comments that may contain some HTML tags like <b> and <i> but prevent any scripts. Which approach best balances security and formatting in Vue?
hard
A. Escape all HTML tags and show comments as plain text
B. Sanitize user comments to allow only safe tags, then use v-html
C. Use v-html directly on user comments without filtering
D. Use triple braces {{{ comment }}} to render raw HTML
Solution
Step 1: Understand the need for safe HTML rendering
Allowing some HTML tags improves formatting but raw HTML can be dangerous.
Step 2: Choose a method to allow safe tags only
Sanitizing user input removes harmful scripts but keeps allowed tags, then v-html safely renders it.
Final Answer:
Sanitize user comments to allow only safe tags, then use v-html -> Option B
Quick Check:
Sanitize then v-html = safe formatted output [OK]
Hint: Sanitize input before v-html to allow safe HTML [OK]
Common Mistakes:
Using v-html without sanitizing
Escaping all HTML losing formatting
Using triple braces which is unsafe
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.