aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 08:11:05 +0100
committerDennis Fink2026-10-30 08:11:05 +0100
commit9f093cf07a3531ee4b7d02c20151e21c141a93de (patch)
treedc39b1586dd6599bd09e95f3dd463a0f64c2de3f /webmentions_ssg
parentedd7c0c2220ba12c2a773f9ba1cf6e0e7a273856 (diff)
downloadwebmentions-ssg-9f093cf07a3531ee4b7d02c20151e21c141a93de.tar.gz
webmentions-ssg-9f093cf07a3531ee4b7d02c20151e21c141a93de.zip
fix(core): harden authentication flows
Reduce login abuse and username enumeration by rate limiting POST attempts and always performing password verification, even for unknown users. Require authenticated, CSRF-protected POST requests for logout so sessions cannot be terminated through cross-site or accidental GET requests.
Diffstat (limited to 'webmentions_ssg')
-rw-r--r--webmentions_ssg/config.py2
-rw-r--r--webmentions_ssg/templates/base.html5
-rw-r--r--webmentions_ssg/views.py25
3 files changed, 27 insertions, 5 deletions
diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py
index 010e393..2661704 100644
--- a/webmentions_ssg/config.py
+++ b/webmentions_ssg/config.py
@@ -43,6 +43,8 @@ class DefaultConfig:
ENDPOINT_GLOBAL_RATE_LIMIT = "1000/hour"
ENDPOINT_REVERIFY_COOLDOWN_SECONDS = 600
+ LOGIN_RATE_LIMIT = "5/minute;30/hour"
+
class DevelopmentConfig(DefaultConfig):
DEBUG = True
diff --git a/webmentions_ssg/templates/base.html b/webmentions_ssg/templates/base.html
index fabfaaf..5cd521b 100644
--- a/webmentions_ssg/templates/base.html
+++ b/webmentions_ssg/templates/base.html
@@ -44,7 +44,10 @@ SPDX-License-Identifier: BSD-3-Clause
href='{{ url_for("root.received") }}'>Received</a>
<a class="nav-link{% if request.endpoint == 'root.sent' %} active{% endif %}"
href='{{ url_for("root.sent") }}'>Sent</a>
- <a class="nav-link" href="{{ url_for("root.logout") }}">Logout</a>
+ <form action="{{ url_for("root.logout") }}" method="post" class="m-0">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
+ <button type="submit" class="nav-link border-0 bg-transparent">Logout</button>
+ </form>
</div>
{% endif %}
</div>
diff --git a/webmentions_ssg/views.py b/webmentions_ssg/views.py
index 7a97480..2ab02fa 100644
--- a/webmentions_ssg/views.py
+++ b/webmentions_ssg/views.py
@@ -4,6 +4,7 @@
"""Provide routes for authentication and Webmention administration."""
+import secrets
import uuid
from datetime import UTC, datetime, timedelta
from typing import Any, cast
@@ -36,6 +37,9 @@ from .url_security import is_safe_next_url
root_page = Blueprint("root", __name__)
+DUMMY_USER = User(username=secrets.token_urlsafe())
+DUMMY_USER.password = secrets.token_urlsafe()
+
@root_page.route("/")
def index() -> ResponseReturnValue:
@@ -48,6 +52,11 @@ def index() -> ResponseReturnValue:
@root_page.route("/login", methods=["GET", "POST"])
+@LIMITER.limit(
+ lambda: current_app.config["LOGIN_RATE_LIMIT"],
+ methods=["POST"],
+ error_message="Too many login attempts. Please try again later.",
+)
def login() -> ResponseReturnValue:
"""
Authenticate a user and start a login session.
@@ -68,11 +77,18 @@ def login() -> ResponseReturnValue:
sa.select(User).where(User.username == form.username.data)
)
- password = form.password.data
- if user is None or password is None or not user.check_password(password):
+ user_exists = user is not None
+
+ if user is None:
+ user = DUMMY_USER
+
+ if (
+ form.password.data is None
+ or not user.check_password(form.password.data)
+ or not user_exists
+ ):
flash("Invalid username or password", "danger")
return redirect(url_for("root.login"))
-
login_user(user)
next_url = request.args.get("next")
@@ -83,7 +99,8 @@ def login() -> ResponseReturnValue:
return render_template("login.html", form=form)
-@root_page.route("/logout")
+@root_page.route("/logout", methods=("POST",))
+@login_required
def logout() -> ResponseReturnValue:
"""
End the current user's login session.