0
0
Flaskframework~10 mins

Secure filename handling in Flask - Interactive Code Practice

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

Complete the code to import the function that helps secure filenames in Flask.

Flask
from werkzeug.utils import [1]
Drag options to blanks, or click blank then click option'
Asafe_filename
Bclean_filename
Csanitize_filename
Dsecure_filename
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name like 'safe_filename' or 'sanitize_filename'.
Not importing from 'werkzeug.utils'.
2fill in blank
medium

Complete the code to secure the uploaded file's name before saving.

Flask
filename = [1](uploaded_file.filename)
Drag options to blanks, or click blank then click option'
Asecure_filename
Bclean_filename
Csafe_filename
Dsanitize_filename
Attempts:
3 left
💡 Hint
Common Mistakes
Using the raw filename without securing it.
Using a non-existent function.
3fill in blank
hard

Fix the error in the code to correctly save the uploaded file with a secure filename.

Flask
uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'], [1]))
Drag options to blanks, or click blank then click option'
Afilename
Bsecure_filename(uploaded_file.filename)
Cuploaded_file.name
Duploaded_file.filename
Attempts:
3 left
💡 Hint
Common Mistakes
Saving the file with the raw filename.
Using an undefined variable like 'filename' without securing it.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps original filenames to their secure versions.

Flask
secured_files = {file: [1](file) for file in files if file.endswith([2])}
Drag options to blanks, or click blank then click option'
Asecure_filename
Bsanitize_filename
C'.txt'
D'.exe'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name.
Filtering with the wrong file extension.
5fill in blank
hard

Fill all three blanks to create a Flask route that saves an uploaded file securely.

Flask
from flask import Flask, request
import os
from werkzeug.utils import [1]

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/uploads'

@app.route('/upload', methods=['POST'])
def upload_file():
    uploaded_file = request.files.get('[2]')
    if uploaded_file:
        filename = [3](uploaded_file.filename)
        uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return 'File saved successfully'
    return 'No file uploaded'
Drag options to blanks, or click blank then click option'
Asecure_filename
Bfile
Cuploaded_file
Dfilename
Attempts:
3 left
💡 Hint
Common Mistakes
Not securing the filename before saving.
Using the wrong file input name.
Not importing the correct function.