📖 Scenario: You are building a simple Flask web app that accepts a username from a user through a form. To keep the app safe and clean, you need to make sure the username input is sanitized before using it.
🎯 Goal: Create a Flask app that receives a username from a form, sanitizes the input by removing unwanted characters, and then displays the cleaned username on a new page.
📋 What You'll Learn
Create a Flask app with a route to show a form
Add a route to handle form submission
Sanitize the username input by removing all characters except letters, numbers, and underscores
Display the sanitized username on a result page
💡 Why This Matters
🌍 Real World
Input sanitization is essential in web apps to prevent security issues like injection attacks and to keep data clean and consistent.
💼 Career
Understanding how to sanitize user input is a key skill for web developers to build secure and reliable applications.
Progress0 / 4 steps
1
Set up Flask app and form route
Import Flask and render_template from flask. Create a Flask app called app. Add a route for /' that returns a template called form.html.
Flask
Hint
Start by importing Flask and render_template, then create the app and a route for the form page.
2
Add form submission route and get username
Import request from flask. Add a route /submit with methods=['POST']. Inside the function submit(), get the username from the form using request.form['username'] and store it in a variable called username.
Flask
Hint
Use request.form['username'] to get the input from the form.
3
Sanitize the username input
Import re module. Use re.sub to remove all characters from username except letters, numbers, and underscores. Store the cleaned result back in username. Use the pattern r'[^\w]' and replace matches with an empty string.
Flask
Hint
Use re.sub with the pattern r'[^\w]' to keep only letters, numbers, and underscores.
4
Render the sanitized username on a result page
Return render_template with 'result.html' and pass the sanitized username as a keyword argument. The key should be username.
Flask
Hint
Use render_template to show the result page with the cleaned username.
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
Step 1: Understand input sanitization
Input sanitization means cleaning user data to avoid harmful code or attacks.
Step 2: Identify the main goal in Flask
In Flask, sanitizing input helps prevent security problems like Cross-Site Scripting (XSS).
Final Answer:
To clean user input and prevent security issues like XSS -> Option A
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
Step 1: Recall Flask's sanitization tools
Flask provides the escape() function to convert special characters to safe HTML entities.
Step 2: Match function to purpose
Only escape() safely converts input to prevent HTML injection.
Final Answer:
escape() -> Option A
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?