Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
XSS Prevention in Django Templates
📖 Scenario: You are building a simple Django web page that displays user comments. To keep your site safe, you need to prevent Cross-Site Scripting (XSS) attacks by properly handling user input in your templates.
🎯 Goal: Create a Django template that safely displays user comments by preventing XSS attacks using Django's built-in template escaping features.
📋 What You'll Learn
Create a Python dictionary called comments with exact keys and values.
Add a configuration variable safe_comment to mark a comment as safe.
Use Django template syntax to loop over comments and display each comment safely.
Use the safe filter only on the safe_comment variable to allow HTML rendering.
💡 Why This Matters
🌍 Real World
Web developers must prevent XSS attacks to protect users from malicious scripts embedded in user-generated content.
💼 Career
Understanding template escaping and safe rendering is essential for secure web application development in Django.
Progress0 / 4 steps
1
Create the comments dictionary
Create a Python dictionary called comments with these exact entries: "user1": "Hello, world!", "user2": "", and "user3": "Welcome to Django!".
Django
Hint
Use curly braces to create a dictionary and include the exact keys and values as strings.
2
Add a safe comment variable
Add a variable called safe_comment and set it to the value of comments["user3"].
Django
Hint
Assign the value of the key "user3" from the comments dictionary to safe_comment.
3
Write the Django template loop
Write a Django template loop using {% for user, comment in comments.items %} to iterate over comments and display each comment inside a <p> tag. Use the default escaping to prevent XSS.
Django
Hint
Use Django template tags {% for %} and {% endfor %} with {{ comment }} inside a paragraph tag.
4
Render the safe comment with the safe filter
Add a line in the Django template to display the safe_comment variable inside a <p> tag using the safe filter: {{ safe_comment|safe }}.
Django
Hint
Use the safe filter to allow HTML in safe_comment but keep escaping for others.
Practice
(1/5)
1. What does Django do by default to protect against XSS attacks when rendering variables in templates?
easy
A. It disables rendering of any user input.
B. It automatically escapes variables to prevent malicious code execution.
C. It requires manual escaping of variables in every template.
D. It converts all variables to uppercase before rendering.
Django templates automatically escape variables to prevent malicious scripts from running in the browser.
Step 2: Compare options with this behavior
Only It automatically escapes variables to prevent malicious code execution. correctly states this automatic escaping feature, while others describe incorrect or unrelated behaviors.
Final Answer:
It automatically escapes variables to prevent malicious code execution. -> Option B
Quick Check:
Default escaping = It automatically escapes variables to prevent malicious code execution. [OK]
5. You want to display user comments that may contain safe HTML tags like <b> and <i>, but prevent scripts. Which approach best prevents XSS while allowing these tags?
hard
A. Sanitize the comment in the backend to allow only safe tags, then use {{ comment|safe }}.
B. Use {{ comment|safe }} directly in the template.
C. Escape the comment with {{ comment|escape }} and then use |safe.
D. Store comments as plain text and never allow any HTML tags.
Solution
Step 1: Understand the need to allow some HTML safely
Allowing safe tags requires cleaning input to remove dangerous scripts but keep allowed tags.
Step 2: Choose the correct method
Sanitizing backend input to whitelist safe tags then marking safe in template is the secure way.
Step 3: Evaluate other options
Using {{ comment|safe }} directly risks XSS by trusting raw input; combining |escape and |safe misuses filters; disallowing all HTML prevents desired formatting.
Final Answer:
Sanitize the comment in the backend to allow only safe tags, then use {{ comment|safe }}. -> Option A
Quick Check:
Backend sanitize + safe filter = Sanitize the comment in the backend to allow only safe tags, then use {{ comment|safe }}. [OK]
Hint: Clean input backend, then mark safe in template [OK]