Bird
Raised Fist0
Flaskframework~10 mins

Input sanitization in Flask - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get user input safely from a form in Flask.

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    user_input = request.form.get([1])
    return f"You entered: {user_input}"
Drag options to blanks, or click blank then click option'
Ainput
B"input"
C"user_input"
Duser_input
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the field name without quotes causes an error.
Using request.args.get instead of request.form.get for POST data.
2fill in blank
medium

Complete the code to sanitize user input by escaping HTML characters.

Flask
from flask import escape

@app.route('/display')
def display():
    raw_input = request.args.get('text')
    safe_input = [1](raw_input)
    return f"Safe output: {safe_input}"
Drag options to blanks, or click blank then click option'
Aescape
Bclean
Csanitize
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using str() does not sanitize input.
Using undefined functions like sanitize or clean.
3fill in blank
hard

Fix the error in the code to prevent injection by validating input length.

Flask
def validate_input(user_text):
    if len(user_text) > [1]:
        return False
    return True
Drag options to blanks, or click blank then click option'
A-1
B'1000'
CNone
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string '1000' instead of integer 1000.
Using None or negative numbers causing logic errors.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters and sanitizes inputs.

Flask
inputs = {'name': '<b>Alice</b>', 'age': '25', 'comment': '<script>'}
safe_inputs = {k: escape(v) for k, v in inputs.items() if len(v) [1] [2]
Drag options to blanks, or click blank then click option'
A>
B<
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' reverses the filter logic.
Using 5 filters too strictly, missing valid inputs.
5fill in blank
hard

Fill all three blanks to create a safe Flask route that gets, sanitizes, and validates input.

Flask
from flask import Flask, request, escape
app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    raw = request.form.get([1])
    safe = [2](raw)
    if len(safe) > [3]:
        return "Input too long", 400
    return f"Received: {safe}"
Drag options to blanks, or click blank then click option'
A"comment"
Bescape
C100
D"input"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names like 'comment'.
Not escaping input before length check.
Using string instead of integer for length.

Practice

(1/5)
1. What is the main purpose of input sanitization in a Flask application?
easy
A. To clean user input and prevent security issues like XSS
B. To speed up the application response time
C. To format the output for better display
D. To store user data without validation

Solution

  1. Step 1: Understand input sanitization

    Input sanitization means cleaning user data to avoid harmful code or attacks.
  2. Step 2: Identify the main goal in Flask

    In Flask, sanitizing input helps prevent security problems like Cross-Site Scripting (XSS).
  3. Final Answer:

    To clean user input and prevent security issues like XSS -> Option A
  4. Quick Check:

    Input sanitization = prevent XSS [OK]
Hint: Sanitize input to stop harmful code from running [OK]
Common Mistakes:
  • Thinking sanitization speeds up app
  • Confusing sanitization with output formatting
  • Ignoring security risks of unsanitized input
2. Which Flask function is commonly used to sanitize user input before displaying it in HTML?
easy
A. escape()
B. validate()
C. clean()
D. sanitize()

Solution

  1. Step 1: Recall Flask's sanitization tools

    Flask provides the escape() function to convert special characters to safe HTML entities.
  2. Step 2: Match function to purpose

    Only escape() safely converts input to prevent HTML injection.
  3. Final Answer:

    escape() -> Option A
  4. Quick Check:

    escape() = sanitize HTML input [OK]
Hint: Use escape() to convert special HTML characters safely [OK]
Common Mistakes:
  • Using non-existent functions like sanitize()
  • Confusing validation with sanitization
  • Assuming clean() is a Flask function
3. What will be the output of this Flask code snippet?
from flask import escape
user_input = ''
safe_input = escape(user_input)
print(safe_input)
medium
A. SyntaxError
B.
C. alert(1)
D. <script>alert(1)</script>

Solution

  1. Step 1: Understand what escape() does

    The escape() function converts special HTML characters like <, > to safe codes like < and >.
  2. Step 2: Apply escape() to the input string

    The input '' becomes '<script>alert(1)</script>'.
  3. Final Answer:

    <script>alert(1)</script> -> Option D
  4. Quick Check:

    escape() output = <script>... [OK]
Hint: escape() changes < and > to < and > [OK]
Common Mistakes:
  • Expecting raw HTML output
  • Confusing escape() with removing tags
  • Thinking escape() causes syntax errors
4. Identify the error in this Flask code snippet for input sanitization:
from flask import escape
user_input = request.args.get('name')
safe_input = escape(user_input)
return safe_input
medium
A. escape() should not be used on user input
B. request.args.get() returns None if 'name' missing, causing error in escape()
C. Missing import of request from flask
D. safe_input should be converted to int before returning

Solution

  1. Step 1: Check the imports

    The code imports escape from Flask but uses request without importing it.
  2. Step 2: Identify the resulting error

    This causes a NameError because request is undefined.
  3. Final Answer:

    Missing import of request from flask -> Option C
  4. Quick Check:

    Missing from flask import request [OK]
Hint: Import request from flask before using it [OK]
Common Mistakes:
  • Thinking None causes TypeError in escape()
  • Believing escape() should not be used on user input
  • Assuming input needs int conversion
5. You want to safely display user comments on a Flask webpage. Which approach correctly sanitizes and stores the input to prevent XSS attacks?
hard
A. Sanitize input with escape() before storing in database
B. Store raw input in database, sanitize with escape() only when displaying
C. Use escape() on input and output both
D. Do not sanitize input, rely on browser security

Solution

  1. Step 1: Understand best practice for input sanitization

    Sanitize user input when displaying it, not before storing, to keep original data intact.
  2. Step 2: Apply escape() at output time

    Using escape() only when showing data prevents XSS without losing original input.
  3. Final Answer:

    Store raw input in database, sanitize with escape() only when displaying -> Option B
  4. Quick Check:

    Sanitize on output, not input [OK]
Hint: Escape user data only when displaying it [OK]
Common Mistakes:
  • Sanitizing before storing causes data loss
  • Sanitizing twice is unnecessary
  • Ignoring XSS risks by trusting browser
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard