Why should you never trust user-uploaded filenames directly in Flask?
User-uploaded filenames can contain unsafe characters or paths that may lead to security risks like directory traversal attacks. Always sanitize filenames before saving.
Click to reveal answer
beginner
What Flask utility helps to secure filenames before saving them?
Flask provides the werkzeug.utils.secure_filename() function to clean filenames by removing unsafe characters and ensuring a safe, simple filename.
Click to reveal answer
beginner
What does secure_filename('my file.txt') return?
It returns my_file.txt, replacing spaces with underscores and removing unsafe characters.
Click to reveal answer
intermediate
How does secure_filename() protect against directory traversal?
It strips out path separators like / or \, preventing attackers from navigating outside the intended upload folder.
Click to reveal answer
intermediate
What is a good practice besides using secure_filename() when handling file uploads?
Always store uploaded files in a dedicated folder with restricted permissions and validate file types to avoid malicious content.
Click to reveal answer
Which function in Flask helps sanitize filenames for safe saving?
Asecure_filename()
Bsanitize_path()
Cclean_filename()
Dsafe_path()
✗ Incorrect
The correct function is secure_filename() from werkzeug.utils.
What does secure_filename('..\secret.txt') return to prevent directory traversal?
Asecret.txt
B.._secret.txt
C..\secret.txt
Dsecret
✗ Incorrect
secure_filename removes path parts and returns just 'secret.txt' to avoid directory traversal.
Why is it unsafe to save uploaded files using their original filenames directly?
AThey might overwrite system files
BThey might contain unsafe characters or paths
CThey might be too long
DThey might be in uppercase
✗ Incorrect
Original filenames can contain unsafe characters or paths that cause security issues.
Which of these is NOT a recommended practice for secure file uploads in Flask?
AUsing secure_filename()
BValidating file types
CSaving files in a public folder without restrictions
DRestricting upload folder permissions
✗ Incorrect
Saving files in a public folder without restrictions is unsafe.
What does secure_filename() do with spaces in filenames?
ARemoves them
BReplaces them with dashes
CLeaves them as is
DReplaces them with underscores
✗ Incorrect
secure_filename replaces spaces with underscores for safety.
Explain why and how you should use secure_filename() in Flask when handling file uploads.
Think about what could happen if you save files with user-given names directly.
You got /4 concepts.
Describe additional security steps besides using secure_filename() to safely handle uploaded files in Flask.
Consider how to protect your server and users from malicious files.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using secure_filename in Flask when handling file uploads?
easy
A. To clean the filename and remove unsafe characters
B. To compress the uploaded file
C. To encrypt the file contents
D. To rename the file to a random string
Solution
Step 1: Understand the role of secure_filename
This function cleans the filename by removing or replacing unsafe characters that could cause security issues.
Step 2: Differentiate from other file handling tasks
It does not compress, encrypt, or randomly rename files; it only sanitizes the filename.
Final Answer:
To clean the filename and remove unsafe characters -> Option A
Quick Check:
Purpose of secure_filename = clean filename [OK]
Hint: Remember: secure_filename cleans names, not file content [OK]
Common Mistakes:
Thinking it encrypts or compresses files
Assuming it renames files randomly
Confusing filename cleaning with file content processing
2. Which of the following is the correct way to import secure_filename in a Flask app?
easy
A. from flask import secure_filename
B. from werkzeug.utils import secure_filename
C. import secure_filename from flask
D. import werkzeug.secure_filename
Solution
Step 1: Identify the correct module for secure_filename
The function secure_filename is provided by werkzeug.utils, not directly by Flask.
Step 2: Check import syntax
The correct syntax is from werkzeug.utils import secure_filename. Other options are either incorrect syntax or wrong modules.
Final Answer:
from werkzeug.utils import secure_filename -> Option B
Quick Check:
Import secure_filename = werkzeug.utils [OK]
Hint: Import secure_filename from werkzeug.utils, not flask [OK]
Common Mistakes:
Importing directly from flask
Using incorrect import syntax
Trying to import as a module instead of function
3. What will be the output of the following code snippet?
from werkzeug.utils import secure_filename
filename = secure_filename('../../etc/passwd')
medium
A. 'passwd'
B. '.._.._etc_passwd'
C. 'etc_passwd'
D. '../../etc/passwd'
Solution
Step 1: Understand how secure_filename handles path traversal
The function removes directory traversal parts like '../' and replaces unsafe characters with underscores.
Step 2: Apply to '../../etc/passwd'
The '../' parts are removed and slashes replaced by underscores, resulting in 'etc_passwd'.
C. Saving file without checking if upload folder exists
D. Incorrect HTTP method for file upload
Solution
Step 1: Check imports
The code uses request.files but does not import request from Flask, causing a NameError.
Step 2: Verify other parts
The filename is cleaned with secure_filename, the method POST is correct, and folder existence is not checked but not an error here.
Final Answer:
Missing import of request from flask -> Option A
Quick Check:
request must be imported for request.files [OK]
Hint: Always import request when accessing request.files [OK]
Common Mistakes:
Forgetting to import request
Assuming folder existence is automatically handled
Confusing HTTP methods for uploads
5. You want to save uploaded files securely in Flask, ensuring filenames are safe and files are saved inside a folder named 'uploads' inside your project. Which code snippet correctly handles this, assuming file is from request.files?
from werkzeug.utils import secure_filename
import os
UPLOAD_FOLDER = os.path.join(os.getcwd(), 'uploads')
# Which snippet below is correct?
A) filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename))
B) filename = file.filename
file.save(os.path.join(UPLOAD_FOLDER, filename))
C) filename = secure_filename(file.filename)
file.save('/uploads/' + filename)
D) filename = secure_filename(file.filename)
file.save(os.path.join('/uploads', filename))
hard
A. Use secure_filename but join with absolute string '/uploads' ignoring project path
B. Use raw filename without cleaning and os.path.join with absolute path
C. Use secure_filename but save with hardcoded relative path string
D. Use secure_filename and os.path.join with absolute UPLOAD_FOLDER path
Solution
Step 1: Check filename safety
Snippets using secure_filename correctly clean the filename (eliminate the snippet using raw file.filename).
Step 2: Check file save path correctness
The correct snippet uses os.path.join(UPLOAD_FOLDER, filename) with the defined UPLOAD_FOLDER that points inside the project 'uploads' folder. The remaining snippets use hardcoded paths like '/uploads/' + filename or os.path.join('/uploads', filename), which may not save inside the project folder.
Final Answer:
Use secure_filename and os.path.join with absolute UPLOAD_FOLDER path -> Option D
Quick Check:
Safe filename + correct absolute path = Use secure_filename and os.path.join with absolute UPLOAD_FOLDER path [OK]
Hint: Always combine secure_filename with os.path.join and absolute folder path [OK]
Common Mistakes:
Saving files with unsafe raw filenames
Using hardcoded paths that may not exist
Ignoring project directory structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.