Flask-Talisman
Flask-Talisman is a small Flask extension that sets HTTP security headers to help protect against common web application security issues like Cross-Site Scripting (XSS) and clickjacking. It provides a simple way to configure Content Security Policy (CSP), HTTP Strict Transport Security (HSTS), X-Frame-Options, and more. The library is actively maintained, with version 1.1.0 released in August 2023, and releases typically occur as needed for bug fixes or new feature/policy additions. [1, 3, 5]
Warnings
- deprecated Python 2.x support was officially deprecated in v1.0.0. Ensure your application runs on Python 3.x.
- gotcha The default Content Security Policy (CSP) is very strict (e.g., `default-src: 'self', 'object-src': 'none'`) and will block inline scripts/styles and external resources. This commonly breaks frontend frameworks or CDN-hosted assets. [1, 3, 5, 15]
- breaking The `X-XSS-Protection` header is disabled by default starting from v1.1.0, aligning with browser deprecation of this header. This is a change in default behavior. [GitHub Release v1.1.0]
- gotcha Flask-Talisman is a fork of an earlier Google-maintained project that became unmaintained. While the current `wntrblm/flask-talisman` project is active, its history may be a consideration for long-term project stability. [5, 17]
- deprecated Permissions Policy directives have changed: `interest-cohort` was removed in v1.0.0, and `browsing-topics` was added and disabled by default in v1.1.0. [GitHub Release v1.0.0, v1.1.0]
Install
-
pip install flask-talisman
Imports
- Talisman
from flask_talisman import Talisman
Quickstart
from flask import Flask
from flask_talisman import Talisman
app = Flask(__name__)
# Initialize Talisman with default strict security headers
talisman = Talisman(app)
@app.route('/')
def hello():
return 'Hello, Secure World!'
if __name__ == '__main__':
# In production, ensure debug=False and serve over HTTPS
# For local development, you might need to adjust Talisman's force_https or debug settings
app.run(debug=True)