From b4a24ea1692276feb479e9675757f2150b14f5ba Mon Sep 17 00:00:00 2001 From: hiromi-mi Date: Fri, 19 Jun 2020 21:34:09 +0900 Subject: [PATCH] Implement CSP --- app.py | 15 +++++++++++++++ requirements.txt | 1 + templates/layout.html | 2 +- templates/login.html | 4 ++-- templates/new.html | 2 +- templates/u2f.html | 2 +- 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 23f2864..b851d1f 100644 --- a/app.py +++ b/app.py @@ -29,6 +29,7 @@ from little_boxes.errors import Error from little_boxes.httpsig import verify_request from little_boxes.webfinger import get_remote_follow_template from werkzeug.exceptions import InternalServerError +from flask_talisman import Talisman import blueprints.admin import blueprints.indieauth @@ -93,7 +94,21 @@ app.register_blueprint(blueprints.indieauth.blueprint) app.register_blueprint(blueprints.tasks.blueprint) app.register_blueprint(blueprints.well_known.blueprint) app.config.update(WTF_CSRF_CHECK_DEFAULT=False) +app.config.update(SESSION_COOKIE_SECURE=True if config.SCHEME == "https" else False) + +csp = { + "default-src": "'self'", + "script-src": "'self'", + "style-src-attr": "'unsafe-inline'", +} + csrf.init_app(app) +talisman = Talisman( + app, + content_security_policy=csp, + force_https=False, + content_security_policy_nonce_in=["script-src", "style-src"], +) logger = logging.getLogger(__name__) diff --git a/requirements.txt b/requirements.txt index 5c0a865..3a51ede 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,3 +25,4 @@ pillow emoji-unicode html5lib Pygments +flask-talisman diff --git a/templates/layout.html b/templates/layout.html index 7f386c6..81ceab1 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,7 +12,7 @@ {% if not request.args.get("older_than") and not request.args.get("previous_than") %}{% endif %} {% block links %}{% endblock %} {% if config.THEME_COLOR %}{% endif %} -