From 7633767afdc38eb89391c80bf5dafa298d6ba372 Mon Sep 17 00:00:00 2001 From: Ignace Date: Thu, 15 Oct 2026 21:46:19 +0200 Subject: [PATCH] added escape route to allow ios webapp homepage creation --- README.md | 16 +++++++++++++++- auth.py | 10 ++++++++++ lapp.py | 1 + test_auth.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea2660a..e98c505 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,21 @@ Administrators can create additional registration groups from `/groups`. ### Login by password hash -Opening `/` signs in the user whose stored `password_hash` text +The optional `runtime.blank` setting in `instance/secrets.yaml` defaults to +`false`. Set it to `true` and restart the app to make the hash route display only +`thank you`, without checking the hash, logging in, or redirecting: + +```yaml +runtime: + blank: true + # Keep your other existing runtime settings here. +``` + +The same blank behavior applies daily from 22:00:00 (inclusive) until 22:10:00 +(exclusive), using the server's local timezone, even when `blank` is `false`. +Setting `blank` to `true` keeps this behavior enabled all day. + +Outside blank mode, opening `/` signs in the user whose stored `password_hash` text after the third colon exactly matches that URL segment, then redirects to the lists page. For example, `scrypt:32768:8:1$salt$digest` uses `/1$salt$digest`. URL-encode the suffix when building the link. Full hashes are not accepted. diff --git a/auth.py b/auth.py index 092a345..6651436 100644 --- a/auth.py +++ b/auth.py @@ -1,3 +1,5 @@ +from datetime import datetime, time + from flask import Blueprint, render_template, request, redirect, url_for, flash, make_response, current_app from flask_login import login_user, logout_user, login_required, current_user from models import db @@ -13,6 +15,14 @@ AUTOLOGIN_SALT = "lapp-autologin" @auth_bp.route("/", methods=["GET"]) def login_by_password_hash(hashedpassword): + local_time = datetime.now().time() + if current_app.config.get("blank", False) or time(22, 0) <= local_time < time(22, 10): + response = make_response("thank you") + response.headers["Content-Type"] = "text/plain; charset=utf-8" + response.headers["Cache-Control"] = "no-store" + response.headers["Referrer-Policy"] = "no-referrer" + return response + # A hash in this URL is a bearer credential. Never log it here. candidates = User.query.filter( User.password_hash.endswith(":" + hashedpassword, autoescape=True) diff --git a/lapp.py b/lapp.py index 7f1e84b..2883aa6 100644 --- a/lapp.py +++ b/lapp.py @@ -18,6 +18,7 @@ def create_app(): app.config["SECRET_KEY"] = secret_values["runtime"]["secret_key"] app.config["APPLICATION_KEY"] = secret_values["runtime"]["application_key"] app.config["SQLALCHEMY_DATABASE_URI"] = secret_values["runtime"]["database_uri"] + app.config["blank"] = secret_values["runtime"].get("blank", False) db.init_app(app) login_manager.init_app(app) diff --git a/test_auth.py b/test_auth.py index cf14633..9ccbacc 100644 --- a/test_auth.py +++ b/test_auth.py @@ -1,4 +1,6 @@ import unittest +from datetime import datetime +from unittest.mock import patch from urllib.parse import quote from flask import Blueprint, Flask @@ -10,6 +12,10 @@ from models import User, db class PasswordHashLoginTest(unittest.TestCase): def setUp(self): + clock = patch("auth.datetime") + self.clock = clock.start() + self.addCleanup(clock.stop) + self.clock.now.return_value = datetime(2026, 10, 15, 12) self.app = Flask(__name__) self.app.config.update( TESTING=True, SECRET_KEY="test-only", @@ -57,6 +63,44 @@ class PasswordHashLoginTest(unittest.TestCase): def test_unknown_hash(self): self.assert_rejected(self.login("unknown")) + def test_blank_displays_thank_you_without_login(self): + self.app.config["blank"] = True + for suffix in (self.user.password_hash.split(":", 3)[3], "unknown"): + with self.subTest(suffix=suffix): + response = self.login(suffix) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.get_data(as_text=True), "thank you") + self.assertEqual(response.mimetype, "text/plain") + self.assertNotIn("Location", response.headers) + with self.client.session_transaction() as session: + self.assertNotIn("_user_id", session) + + def test_blank_false_preserves_login(self): + self.app.config["blank"] = False + self.test_valid_hash_logs_in_and_redirects() + + def test_daily_blank_window(self): + self.app.config["blank"] = False + for hour, minute, second, expected_status in ( + (21, 59, 59, 302), + (22, 0, 0, 200), + (22, 5, 0, 200), + (22, 9, 59, 200), + (22, 10, 0, 302), + ): + with self.subTest(time=(hour, minute, second)): + self.client = self.app.test_client() + self.clock.now.return_value = datetime( + 2026, 10, 15, hour, minute, second + ) + response = self.login() + self.assertEqual(response.status_code, expected_status) + if expected_status == 200: + self.assertEqual(response.get_data(as_text=True), "thank you") + self.assertNotIn("Location", response.headers) + with self.client.session_transaction() as session: + self.assertNotIn("_user_id", session) + def test_full_hash_is_rejected(self): self.assert_rejected(self.login(self.user.password_hash))