# Unrestricted Upload of File with Dangerous Type (CWE-434) The product allows the upload of files without properly validating the file type, which can lead to execution of malicious code. **Stack:** Python - Prevalence: Wysoka Często wykorzystywana - Impact: Wysoki 3 reguł o wysokim poziomie - Prevention: Udokumentowane 3 przykładów poprawek **OWASP:** Broken Access Control (A01:2021-Broken Access Control) - #1 ## Description When users can upload files without restriction, attackers may upload executable files, scripts, or other dangerous content that can be executed by the server or other users. ## Prevention Strategie zapobiegania dla Unrestricted File Upload oparte na 1 regułach detekcji Shoulder. ### Python Validate file extension, MIME type, and size; use secure_filename() for paths ## Warning Signs - [HIGH] file uploads without proper validation of file type, size, or content ## Consequences - Wykonanie nieautoryzowanego kodu - Odczyt danych aplikacji - Modyfikacja danych aplikacji ## Mitigations - Waliduj typy plików po stronie serwera, nie tylko po rozszerzeniu - Pliki przesłane przez użytkownika przechowuj poza katalogiem głównym serwera WWW - Stosuj listę dozwolonych typów plików - Zmieniaj nazwy przesyłanych plików, aby zapobiec ich wykonaniu ## Detection - Total rules: 3 - Languages: go, javascript, typescript, python ## Rules by Language ### Python (1 rules) - **Insecure File Upload** [HIGH]: Detects file uploads without proper validation of file type, size, or content. Malicious uploads can lead to code execution, path traversal, or denial of service. Always validate file extensions, MIME types, content, and size. - Remediation: Validate file extension, MIME type, and size; use secure_filename() for the filename. ```python from flask import request, jsonify from werkzeug.utils import secure_filename import magic ALLOWED = {'png', 'jpg', 'pdf'} @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] ext = file.filename.rsplit('.', 1)[-1].lower() if ext not in ALLOWED: return jsonify({'error': 'Invalid type'}), 400 filename = secure_filename(file.filename) file.save(f'uploads/{filename}') return jsonify({'filename': filename}) ``` Learn more: https://shoulder.dev/learn/python/cwe-434/insecure-file-upload