| name | unrestricted-file-upload-anti-pattern |
| description | Security anti-pattern for unrestricted file upload vulnerabilities (CWE-434). Use when generating or reviewing code that handles file uploads, processes user-submitted files, or stores uploaded content. Detects missing extension, MIME type, and size validation. |
Unrestricted File Upload Anti-Pattern
Severity: Critical
Summary
Applications accept user-uploaded files without validating type, content, or size, enabling attackers to upload malicious scripts or executables. Leads to remote code execution (web shells), server compromise, or denial-of-service (disk exhaustion).
The Anti-Pattern
The anti-pattern is accepting uploaded files without validating type, content, and size.
BAD Code Example
from flask import Flask, request
import os
UPLOAD_FOLDER = '/var/www/uploads'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return 'No file part', 400
file = request.files['file']
if file.filename == '':
return 'No selected file', 400
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return f'File {filename} uploaded successfully', 200
GOOD Code Example
from flask import Flask, request, jsonify
import os
import uuid
from magic import from_buffer
UPLOAD_FOLDER = '/var/www/safe_uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'pdf'}
MAX_FILE_SIZE = 5 * 1024 * 1024
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload/secure', methods=['POST'])
def upload_file_secure():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
if not allowed_file(file.filename):
return jsonify({'error': 'File type not allowed'}), 400
file.seek(0, os.SEEK_END)
file_length = file.tell()
file.seek(0)
if file_length > MAX_FILE_SIZE:
return jsonify({'error': 'File too large'}), 400
file_buffer = file.read(1024)
file.seek(0)
actual_mime = from_buffer(file_buffer, mime=True)
if actual_mime not in ['image/png', 'image/jpeg', 'image/gif', 'application/pdf']:
return jsonify({'error': f'Invalid file content type: {actual_mime}'}), 400
original_extension = file.filename.rsplit('.', 1)[1].lower()
safe_filename = str(uuid.uuid4()) + '.' + original_extension
file.save(os.path.join(app.config['UPLOAD_FOLDER'], safe_filename))
return jsonify({'message': f'File {safe_filename} uploaded successfully'}), 200
Detection
- Review file upload handlers: Identify all endpoints that allow users to upload files.
- Check validation logic: Examine how filenames, file types, and file contents are validated. Look for:
- Missing extension checks or using blocklists instead of allowlists.
- Relying solely on the
Content-Type HTTP header, which is easily spoofed.
- Not checking the actual content of the file (magic bytes).
- Missing size limits.
- Inspect storage location: Determine where uploaded files are stored. Are they in a web-accessible directory? Can executables be run from there?
Prevention
Related Security Patterns & Anti-Patterns
References