Threat: Secret Key Compromise
- Description: An attacker gains access to the Flask application's
SECRET_KEY
. This could be through hardcoding in a public repository, exposed environment variables, insecure configuration files, or server compromise. The attacker can forge valid session cookies (impersonating any user) and decrypt/tamper with data signed using theSECRET_KEY
. - Impact: Complete application compromise. Attacker can impersonate users, access/modify sensitive data, and potentially gain full control.
- Flask Component Affected:
Flask.secret_key
, session management (default client-side signed cookies), any functionality usingitsdangerous
(which Flask uses internally). - Risk Severity: Critical
- Mitigation Strategies:
- Never hardcode the
SECRET_KEY
. - Use environment variables securely (avoid exposure).
- Use a dedicated secrets management system (e.g., HashiCorp Vault, AWS Secrets Manager).
- Regularly rotate the
SECRET_KEY
. - Implement strict access controls to the server and configuration.
- Use
.gitignore
to prevent accidental commits of sensitive files.
- Never hardcode the
- Description: An attacker intercepts a user's session cookie. If the application uses default Flask sessions (client-side) without HTTPS, interception is easy (e.g., public Wi-Fi). Even with HTTPS, missing
SESSION_COOKIE_HTTPONLY
orSESSION_COOKIE_SECURE
allows theft via XSS or other client-side attacks. The attacker then impersonates the user. - Impact: User impersonation, unauthorized access to user data and functionality.
- Flask Component Affected: Session management (
flask.session
), specifically the default client-side cookie implementation. - Risk Severity: High
- Mitigation Strategies:
SESSION_COOKIE_SECURE = True
(HTTPS only).SESSION_COOKIE_HTTPONLY = True
(prevent JavaScript access).SESSION_COOKIE_SAMESITE
to 'Strict' or 'Lax' (mitigate CSRF).- Implement session expiration and refresh.
- Consider server-side sessions (e.g., Flask-Session) for sensitive applications.
Threat: Template Injection (Jinja2)
- Description: An attacker injects malicious code into a Jinja2 template. This happens when user input directly constructs template strings or the
safe
filter is misused. The injected code is executed by Jinja2, leading to XSS, data leakage, or potentially server-side code execution. - Impact: XSS, data leakage, potential server-side code execution.
- Flask Component Affected: Jinja2 templating engine (
flask.render_template
, direct Jinja2 use). - Risk Severity: High
- Mitigation Strategies:
- Avoid constructing templates directly from user input.
- Use
safe
only on trusted data. Never on unsanitized user input. - Prefer template inheritance and blocks.
- Ensure Jinja2's auto-escaping is enabled (default).
- If disabling auto-escaping, use
{% autoescape false %}
and manually escape untrusted data.
Threat: Debug Mode Enabled in Production
- Description:
app.debug = True
in production. Exposes detailed errors (source code, environment variables, stack traces) to anyone encountering an error, aiding attackers. - Impact: Information disclosure, greatly assisting exploitation of other vulnerabilities.
- Flask Component Affected:
Flask.debug
property, overall application configuration. - Risk Severity: Critical
- Mitigation Strategies:
- Never enable debug mode (
app.debug = True
) in production. - Use environment variables to control debug setting (False in production).
- Use a proper WSGI server (Gunicorn, uWSGI), which usually disables debug by default.
- Never enable debug mode (
- Description: An attacker overwhelms the server with requests. Critically vulnerable if the built-in development server (
app.run()
) is used in production (single-threaded, not for high traffic). - Impact: Denial of service, application unavailability.
- Flask Component Affected: Flask's built-in development server (
Flask.run
). - Risk Severity: High (if using the development server in production)
- Mitigation Strategies:
- Never use the built-in development server in production.
- Use a production-ready WSGI server (Gunicorn, uWSGI, Waitress) configured for concurrency and resource limits.
Threat: Unvalidated Route Parameters
- Description: Attackers manipulate dynamic route parameters (e.g.,
/user/<int:user_id>
) to access unauthorized resources. Occurs when the app doesn't validate the parameter and check if the current user is authorized for the resource identified by that parameter. - Impact: Unauthorized access to data/functionality, potential privilege escalation.
- Flask Component Affected: Route definitions (e.g.,
@app.route('/user/<int:user_id>')
), request context (flask.request
). - Risk Severity: High
- Mitigation Strategies:
- Validate route parameters: use Flask's converters (
int
,float
,path
) or custom converters. - Implement authorization checks within the route handler. Ensure the current user has permission, even if the parameter is valid. Don't rely solely on the route definition.
- Validate route parameters: use Flask's converters (