From ecdcc044b7c44d6dc7e3d180a22d35da79c15604 Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Sat, 28 Mar 2026 23:17:26 -0600 Subject: [PATCH] feat: add CSRF protection to all POST forms Installs Flask-WTF and enables CSRFProtect globally. Adds csrf_token hidden fields to all four POST forms (login, delete entry, add user, delete user, and the public guestbook form). Exempts the API endpoint which uses header-based key auth instead. --- app.py | 3 +++ requirements.txt | 1 + templates/admin.html | 1 + templates/admin_login.html | 1 + templates/admin_users.html | 2 ++ templates/index.html.template | 1 + 6 files changed, 9 insertions(+) diff --git a/app.py b/app.py index c868f02..f141f94 100644 --- a/app.py +++ b/app.py @@ -13,6 +13,7 @@ from flask_limiter.util import get_remote_address from flask_login import ( LoginManager, UserMixin, login_user, logout_user, login_required, current_user ) +from flask_wtf.csrf import CSRFProtect from werkzeug.security import generate_password_hash, check_password_hash # Set up logging @@ -28,6 +29,7 @@ if not _secret_key: app.secret_key = _secret_key limiter = Limiter(get_remote_address, app=app, default_limits=[]) +csrf = CSRFProtect(app) app.config.update( SESSION_COOKIE_HTTPONLY=True, @@ -467,6 +469,7 @@ def admin_users_delete(user_id): @app.route('/api/guests', methods=['GET']) @limiter.limit("100 per hour") +@csrf.exempt def api_guests(): api_key = request.headers.get('X-API-Key') if api_key != os.environ.get("API_KEY"): diff --git a/requirements.txt b/requirements.txt index 079758a..203127a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask>=3.1.3 +Flask-WTF>=1.2 Werkzeug>=3.0.6 Flask-Limiter>=3.0 Flask-Login>=0.6 diff --git a/templates/admin.html b/templates/admin.html index a6976ab..ea00317 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -47,6 +47,7 @@ {% if current_user.role != 'viewer' %}
+
{% endif %} diff --git a/templates/admin_login.html b/templates/admin_login.html index 76bc560..2651d0a 100644 --- a/templates/admin_login.html +++ b/templates/admin_login.html @@ -15,6 +15,7 @@
{{ error }}
{% endif %}
+
Add User
+
@@ -57,6 +58,7 @@ + diff --git a/templates/index.html.template b/templates/index.html.template index d342d8e..04c4e39 100644 --- a/templates/index.html.template +++ b/templates/index.html.template @@ -70,6 +70,7 @@ {% endif %}
+