Mitigation Strategy: Never use the built-in development server in production
- Description:
- Identify any instances where
bottle.run()
is used to serve the application in a production or publicly accessible environment. - Replace
bottle.run()
with a production-ready WSGI server such as Gunicorn, uWSGI, or Waitress. - Configure the chosen WSGI server to bind to the appropriate address and port, and to serve the Bottle application.
- Ensure the WSGI server is properly integrated with a process manager (like systemd or supervisord) for reliability and automatic restarts.
- Test the application thoroughly in a staging environment using the production WSGI server setup before deploying to production.
- Identify any instances where
- List of Threats Mitigated:
- Denial of Service (DoS) - Severity: High
- Information Disclosure - Severity: Medium
- Remote Code Execution (in extreme cases due to vulnerabilities in development server) - Severity: High
- Impact:
- DoS: High reduction - Production WSGI servers are designed for concurrency and stability under load.
- Information Disclosure: Medium reduction - Production servers are generally more hardened and less verbose in error handling compared to development servers.
- Remote Code Execution: Medium reduction - Reduces attack surface by removing a less secure component, although RCE is less directly related to the server itself and more to application vulnerabilities.
- Currently Implemented: Yes, in production and staging environments. Gunicorn is used as the WSGI server, managed by systemd. Configuration is in
deployment/gunicorn.conf
. - Missing Implementation: N/A - Implemented across all deployment environments.
Mitigation Strategy: Enable auto-escaping in SimpleTemplate
- Description:
- In your Bottle application code, configure SimpleTemplate to enable auto-escaping globally. This can be done when creating the
Bottle
application instance or when rendering templates. For example:app = Bottle(autoescape=True)
. - Alternatively, enable auto-escaping on a per-template basis if global auto-escaping is not desired for all templates.
- Review all templates to ensure that auto-escaping is correctly applied and does not interfere with intended HTML rendering.
- Document the auto-escaping configuration for future developers.
- In your Bottle application code, configure SimpleTemplate to enable auto-escaping globally. This can be done when creating the
- List of Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High
- Impact:
- XSS: High reduction - Automatically escapes HTML characters, preventing injection of malicious scripts in most common cases.
- Currently Implemented: Yes, auto-escaping is enabled globally for SimpleTemplate in
app.py
during Bottle application initialization:app = Bottle(autoescape=True)
. - Missing Implementation: N/A - Globally enabled. Consider adding comments in the code to explicitly highlight this security setting.
Mitigation Strategy: Be cautious with {{!variable}}
(raw output)
- Description:
- Audit all templates in the application and identify instances where
{{!variable}}
(raw output) is used. - For each instance, carefully analyze the source of the
variable
data. - If the data originates from user input or any untrusted source, replace
{{!variable}}
with{{variable}}
(auto-escaped output) and ensure proper sanitization is applied before passing the data to the template (as per general sanitization best practices). - If raw output is absolutely necessary for trusted data, document the reason and ensure strict control over the data source and its integrity.
- Minimize the use of raw output as much as possible.
- Audit all templates in the application and identify instances where
- List of Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High
- Impact:
- XSS: High reduction - Eliminates potential XSS vulnerabilities arising from unintentional raw output of untrusted data.
- Currently Implemented: Partially implemented. Templates have been reviewed, and some instances of
{{!variable}}
have been replaced with{{variable}}
. However, a systematic audit and documentation are still needed. - Missing Implementation: Complete audit of all templates for
{{!variable}}
usage, replacement with auto-escaping where appropriate, and documentation of justified raw output cases with data source validation procedures.
Mitigation Strategy: Implement CSRF protection
- Description:
- Choose a CSRF protection method, such as the Synchronizer Token Pattern.
- Generate a unique, unpredictable CSRF token for each user session on the server-side within your Bottle application.
- Store the CSRF token securely in the user's session (e.g., using Bottle's session features or a custom session management).
- Embed the CSRF token in all forms and AJAX requests that perform state-changing operations within your Bottle application (e.g., as a hidden form field or a custom HTTP header).
- On the server-side (in your Bottle application routes), for each state-changing request, retrieve the CSRF token from the request data and compare it to the token stored in the user's session.
- Reject the request if the tokens do not match or if the token is missing or invalid.
- Consider using a library or middleware to simplify CSRF token generation, embedding, and validation within your Bottle application.
- List of Threats Mitigated:
- Cross-Site Request Forgery (CSRF) - Severity: High
- Impact:
- CSRF: High reduction - Prevents attackers from performing unauthorized actions on behalf of authenticated users.
- Currently Implemented: No, CSRF protection is currently not implemented in the application.
- Missing Implementation: Full implementation of CSRF protection using the Synchronizer Token Pattern across all forms and AJAX requests that modify data within the Bottle application. This needs to be implemented in all modules handling form submissions and API endpoints that perform state changes.
Mitigation Strategy: Synchronizer Token Pattern
- Description: (This is a specific implementation of CSRF protection, so the description is largely the same as above, but focusing on the details of the Synchronizer Token Pattern in the context of a Bottle application)
- Upon successful user login or session creation within your Bottle application, generate a cryptographically secure, random token (CSRF token).
- Store this CSRF token server-side, associated with the user's session (using Bottle's session mechanisms).
- Embed this CSRF token into every HTML form rendered by your Bottle application as a hidden input field. For AJAX requests, include it as a custom header (e.g.,
X-CSRF-Token
). - When a state-changing request is received by your Bottle application, extract the CSRF token from the request (form data or header).
- Compare the received CSRF token with the token stored in the user's session on the server.
- If the tokens match, the request is considered legitimate and can be processed. If they don't match, reject the request as a potential CSRF attack.
- Ensure tokens are unique per session and regenerated upon session invalidation or logout within your Bottle application's session management.
- List of Threats Mitigated:
- Cross-Site Request Forgery (CSRF) - Severity: High
- Impact:
- CSRF: High reduction - Effectively prevents CSRF attacks by requiring a valid, session-specific token for state-changing requests.
- Currently Implemented: No, CSRF protection is currently not implemented.
- Missing Implementation: Implementation of the Synchronizer Token Pattern is missing across all forms and AJAX endpoints that modify data. This needs to be integrated into the application's request handling and session management logic within the Bottle application.
Mitigation Strategy: Consider using signed cookies for session integrity
- Description:
- Utilize Bottle's signed cookie functionality for session management within your Bottle application. This involves providing a secret key when creating or configuring the Bottle application.
- When setting session cookies using Bottle's cookie setting methods, ensure you are using the features that support signing.
- When retrieving session data from cookies using Bottle's cookie retrieval methods, Bottle will automatically verify the signature to ensure the cookie has not been tampered with.
- Choose a strong, randomly generated secret key and store it securely, outside of the application code if possible (e.g., environment variable).
- Regularly rotate the secret key as a security best practice.
- List of Threats Mitigated:
- Session tampering/modification by client - Severity: Medium
- Impact:
- Session tampering: Medium reduction - Prevents clients from directly modifying session data stored in cookies, ensuring session integrity.
- Currently Implemented: No, signed cookies are not currently used for session management. Standard cookies are used without signing.
- Missing Implementation: Implementation of signed cookies for session management. This requires generating and securely storing a secret key and modifying the session management logic to use Bottle's signed cookie features.