0
0
Flaskframework~20 mins

XSS prevention in templates in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XSS Prevention Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when rendering user input in a Flask template?

Consider a Flask template that displays a user-provided string using the standard Jinja2 syntax: {{ user_input }}. The user inputs the string <script>alert('XSS')</script>. What will the rendered HTML output be?

Flask
{{ user_input }}
A<script>alert('XSS')</script>
B&lt;script&gt;alert('XSS')&lt;/script&gt;
C&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;
Dalert('XSS')
Attempts:
2 left
💡 Hint

Think about how Jinja2 handles HTML special characters by default.

📝 Syntax
intermediate
2:00remaining
Which template syntax disables autoescaping in Flask?

In a Flask Jinja2 template, you want to display raw HTML from a variable html_content without escaping. Which syntax correctly disables autoescaping?

A{{ safe(html_content) }}
B{{ html_content|raw }}
C{{ html_content|safe }}
D{% raw html_content %}
Attempts:
2 left
💡 Hint

Look for the Jinja2 filter that marks content as safe.

🔧 Debug
advanced
2:00remaining
Why does this Flask template allow XSS despite using {{ }}?

Given the Flask template snippet:

{{ user_input|safe }}

The user_input contains <script>alert('XSS')</script>. Why does this cause an XSS vulnerability?

AThe safe filter encodes the script tag, preventing execution.
BThe {{ }} syntax always escapes content, so no XSS is possible.
CThe user_input is sanitized automatically by Flask before rendering.
DThe safe filter disables escaping, so the script tag is rendered as HTML and runs.
Attempts:
2 left
💡 Hint

Consider what the safe filter does to the output.

🧠 Conceptual
advanced
2:00remaining
How does Flask's autoescaping protect against XSS?

Which statement best describes how Flask's Jinja2 template autoescaping helps prevent XSS attacks?

AIt converts special HTML characters in variables to safe entities, so scripts are not executed.
BIt removes all HTML tags from user input before rendering.
CIt encrypts user input before displaying it in the browser.
DIt disables JavaScript execution in the browser automatically.
Attempts:
2 left
💡 Hint

Think about what happens to characters like < and > in output.

state_output
expert
2:00remaining
What is the rendered output of this Flask template with mixed safe and unsafe variables?

Given the Flask template:

User comment: {{ comment }}
Admin note: {{ admin_note|safe }}

And the variables:

  • comment = "<b>Hello</b>"
  • admin_note = "<i>Important</i>"

What is the exact rendered HTML output?

AUser comment: &lt;b&gt;Hello&lt;/b&gt;<br>Admin note: <i>Important</i>
BUser comment: <b>Hello</b><br>Admin note: &lt;i&gt;Important&lt;/i&gt;
CUser comment: <b>Hello</b><br>Admin note: <i>Important</i>
DUser comment: &lt;b&gt;Hello&lt;/b&gt;<br>Admin note: &lt;i&gt;Important&lt;/i&gt;
Attempts:
2 left
💡 Hint

Remember which variables are escaped and which are marked safe.