Introduction
Uploading files to websites or apps can open doors for harmful attacks if not handled carefully. Protecting file uploads is essential to keep systems safe from viruses, unauthorized access, or data loss.
Imagine a mailroom where packages arrive daily. The staff checks each package's size, contents, and label before allowing it inside the building. Suspicious or oversized packages are held back or rejected to keep everyone safe.
┌─────────────────────────────┐ │ File Upload Process │ ├─────────────┬───────────────┤ │ Validate │ Scan for │ │ File Type │ Malware │ ├─────────────┴───────────────┤ │ Check File Size │ ├─────────────┬───────────────┤ │ Sanitize │ Store in │ │ File Name │ Secure Area │ └─────────────┴───────────────┘
if uploaded_file.content_type == 'image/png' and uploaded_file.size <= 1048576:
save_file(uploaded_file)
else:
reject_upload()
What will happen if a user uploads a 2MB PNG file?if uploaded_file.extension == '.jpg' or '.png':
process_file(uploaded_file)
else:
reject_file()
What is the main problem with this code?Pass the test and this pattern moves unknown