Summary
Flask-Reuploaded versions prior to 1.5.0 contain a critical path traversal and extension bypass flaw in the file upload handling, allowing user-controlled filenames to influence where files are written on disk.
Combined with Server-Side Template Injection (SSTI), a remote attacker can craft uploads that result in arbitrary file writes and ultimately remote code execution when the application processes or renders those files.
Affected context
This vulnerability affects Flask applications that integrate Flask-Reuploaded for handling file uploads and pass user-controlled values into the name parameter or similar filename fields.
It is most relevant where:
- Uploaded files are stored on the server's filesystem and later accessed or rendered by the application
- Path normalization and extension checks are incomplete or bypassable
- Templating or dynamic rendering logic may be applied to uploaded content or file paths
The issue manifests when the upload endpoint trusts user input for file paths or names without strong canonicalization, validation, and isolation.
Exploit preconditions
- Attack surface: HTTP(S) endpoints that accept file uploads via Flask-Reuploaded.
- Required access: Remote network access to the upload endpoint; no local system access is required.
- Trigger point: Submission of a crafted filename (e.g., via
nameparameter) and file payload that is subsequently saved and used by the application. - Why exploitation is possible:
- The upload mechanism allows user-controllable path segments, enabling path traversal or placement of files in attacker-chosen locations.
- Extension or type checks can be bypassed, allowing unexpected file formats (e.g., template-like content) to be stored where the application will interpret or render them.
- In environments where uploaded files are processed by templating engines, this creates a Server-Side Template Injection channel that can lead to execution of attacker-controlled expressions.
Why this happens
The core issue is insufficient validation and canonicalization of user-supplied filenames, mapping to CWE-22: Path Traversal, combined with unsafe handling of template-like input as executable expressions, mapping to CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine.
Flask-Reuploaded's upload handling permitted user input (e.g., the name parameter) to influence filesystem paths and file extensions with inadequate constraints.
This design lets attackers craft paths that escape intended directories or bypass extension filters, and in applications where those files or their contents are later treated as templates, SSTI becomes possible.
Abstractly, the pattern is: trusted file storage + attacker-controlled path + downstream interpretation of file content → arbitrary file write and potential code execution.
Potential impact
- Arbitrary file creation or overwrite in directories reachable by the upload mechanism
- Placement of executable or interpretable files (e.g., templates, scripts) that can be triggered by application logic
- Remote code execution via Server-Side Template Injection when uploaded files are rendered by a template engine
- Defacement or manipulation of application content served from uploaded files
- Escalation to full application compromise, including access to sensitive data and credentials
- Potential lateral movement within the hosting environment if service account privileges are high
General mitigation guidance
Effective mitigation focuses on strict separation of user input from filesystem paths and template evaluation:
- Avoid user-controlled filenames: Prefer auto-generated, random, and collision-resistant filenames that do not incorporate user input for the storage path.
- Enforce strict upload directories: Resolve and store all uploads in a dedicated directory; disallow any path segments (e.g.,
../) that could escape the intended root, using canonicalization and explicit path checks. - Restrict allowed extensions and types: Implement positive allowlists for file extensions and MIME types, validating both before write; reject or normalize any filenames that fail checks.
- Never treat uploaded content as templates: Do not pass uploaded files or user input directly into template engines; keep template definitions static and separate from user-controlled data.
- Least privilege for file writes: Run the application with minimal filesystem permissions and restrict the upload directory so that even if traversal is attempted, sensitive system or application files remain unwritable.
- Normalize and sanitize all path components: Explicitly remove or reject control characters, path separators, and template metacharacters from any user-supplied identifiers used in file handling logic.
How teams detect issues like this
Organizations can detect and monitor for this vulnerability class using several approaches:
- Static analysis and linting: Configure rules to flag uses of user-controlled input influencing file paths (e.g.,
open,save,os.path.joinwith request data) and template rendering calls that accept untrusted data. - Code review checklists: During manual review, scrutinize file upload endpoints for direct use of request parameters in filenames, directory paths, or template names, and confirm that auto-generated names and allowlists are used.
- Runtime monitoring and logging: Log upload destinations, normalized paths, and filename rejections; alert on attempts containing directory traversal sequences (
../,%2e%2e/) or suspicious extensions. - Security testing and fuzzing: Use DAST or custom scripts to send crafted filenames and payloads to upload endpoints, checking whether files are written outside the expected directory or later rendered as templates.
- Policy enforcement in CI/CD: Integrate automated security checks in pipelines to block deployments that introduce unvalidated path construction or template evaluation of untrusted input.

