function allowed_file_v1
Validates whether a given filename has an allowed file extension by checking if the extension exists in a configured whitelist.
/tf/active/vicechatdev/full_smartstat/app.py
103 - 106
simple
Purpose
This function provides security validation for file uploads by ensuring only files with approved extensions are accepted. It prevents potentially malicious files from being uploaded by checking the file extension against a predefined list of allowed extensions stored in app_config.ALLOWED_EXTENSIONS. This is a common pattern in web applications that handle file uploads.
Source Code
def allowed_file(filename):
"""Check if file extension is allowed"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app_config.ALLOWED_EXTENSIONS
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
filename |
- | - | positional_or_keyword |
Parameter Details
filename: A string representing the name of the file to validate, including its extension (e.g., 'document.pdf', 'image.jpg'). The filename should contain at least one dot (.) separating the name from the extension. Can be a full path or just a filename.
Return Value
Returns a boolean value: True if the filename contains a dot AND the extension (the part after the last dot, converted to lowercase) is present in app_config.ALLOWED_EXTENSIONS; False otherwise. Returns False for filenames without extensions or with disallowed extensions.
Dependencies
config
Required Imports
from config import Config
Usage Example
from config import Config
# Initialize config with allowed extensions
app_config = Config()
app_config.ALLOWED_EXTENSIONS = {'pdf', 'png', 'jpg', 'jpeg', 'txt', 'csv'}
def allowed_file(filename):
"""Check if file extension is allowed"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app_config.ALLOWED_EXTENSIONS
# Example usage
print(allowed_file('document.pdf')) # True
print(allowed_file('image.PNG')) # True (case-insensitive)
print(allowed_file('script.exe')) # False
print(allowed_file('noextension')) # False
print(allowed_file('file.tar.gz')) # Checks only 'gz' extension
Best Practices
- Always use this function before processing uploaded files to prevent security vulnerabilities
- Combine with werkzeug.utils.secure_filename() to sanitize filenames before storage
- Ensure app_config.ALLOWED_EXTENSIONS contains only lowercase extensions for consistent matching
- This function only checks extensions, not file content - consider adding MIME type validation for stronger security
- Be aware that this checks only the last extension (e.g., for 'file.tar.gz', only 'gz' is checked)
- Consider the security implications of each allowed extension - avoid executable file types
- This is a first-line defense; implement additional server-side validation and virus scanning for production systems
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function allowed_file 91.3% similar
-
function is_valid_document_file 59.9% similar
-
function api_upload_document_v1 51.5% similar
-
function test_multiple_file_upload 49.9% similar
-
function api_upload 49.0% similar