Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Usernames set by an administrator limited to 2-20 characters (the column length)
Murtaza Nasiref5483e8df91
diff --git a/scripts/docker_create_admin.py b/scripts/docker_create_admin.pyindex de7ef3f0..0eb85207 100644--- a/scripts/docker_create_admin.py+++ b/scripts/docker_create_admin.py@@ -60,6 +60,11 @@ def create_admin_user_from_env():print(f"Error: Invalid email: {str(e)}")sys.exit(1)+ # The username column holds 20 characters+ if not 2 <= len(username) <= 20:+ print("Error: ADMIN_USERNAME must be 2 to 20 characters long.")+ sys.exit(1)+# Validate passwordif len(password) < 8:print("Error: Password must be at least 8 characters long.")diff --git a/src/api/admin.py b/src/api/admin.pyindex 440843cd..e865dd8d 100644--- a/src/api/admin.py+++ b/src/api/admin.py@@ -207,6 +207,11 @@ def admin_add_user():if field not in data:return jsonify({'error': f'Missing required field: {field}'}), 400+ # The username column holds 20 characters; PostgreSQL refuses a longer+ # value, which gave a 500 instead of this message+ if not 2 <= len(str(data['username']).strip()) <= 20:+ return jsonify({'error': 'The username must be 2 to 20 characters.'}), 400+# Check if username or email already existsif User.query.filter_by(username=data['username']).first():return jsonify({'error': 'Username already exists'}), 400@@ -272,6 +277,8 @@ def admin_update_user(user_id):# Update user fieldsif 'username' in data and data['username'] != user.username:+ if not 2 <= len(str(data['username']).strip()) <= 20:+ return jsonify({'error': 'The username must be 2 to 20 characters.'}), 400# Check if username already existsif User.query.filter_by(username=data['username']).first():return jsonify({'error': 'Username already exists'}), 400diff --git a/templates/admin.html b/templates/admin.htmlindex 48840c94..f9063d8f 100644--- a/templates/admin.html+++ b/templates/admin.html@@ -1343,7 +1343,7 @@ Summarization Instructions:<div class="modal-body"><div class="mb-4"><label class="block text-sm font-medium text-[var(--text-secondary)] mb-1">${t('adminDashboard.usernameLabel')}</label>- <input v-model="newUser.username" type="text" required class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]">+ <input v-model="newUser.username" type="text" required minlength="2" maxlength="20" class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]"></div><div class="mb-4"><label class="block text-sm font-medium text-[var(--text-secondary)] mb-1">${t('adminDashboard.emailLabel')}</label>@@ -1400,7 +1400,7 @@ Summarization Instructions:<div class="modal-body"><div class="mb-4"><label class="block text-sm font-medium text-[var(--text-secondary)] mb-1">${t('adminDashboard.usernameLabel')}</label>- <input v-model="editingUser.username" type="text" required class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]">+ <input v-model="editingUser.username" type="text" required minlength="2" maxlength="20" class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]"></div><div class="mb-4"><label class="block text-sm font-medium text-[var(--text-secondary)] mb-1">${t('adminDashboard.emailLabel')}</label>diff --git a/tests/test_admin_username_length.py b/tests/test_admin_username_length.pynew file mode 100644index 00000000..152a0b93--- /dev/null+++ b/tests/test_admin_username_length.py@@ -0,0 +1,63 @@+"""Usernames set by an administrator are 2 to 20 characters.++The username column holds 20 characters. Sign-up and SSO already keep to+that; the admin endpoints did not, and on PostgreSQL a longer name gave a+500 error.+"""++import uuid++from src.app import app, db+from src.models import User++app.config["WTF_CSRF_ENABLED"] = False+++def _admin_client():+ with app.app_context():+ s = uuid.uuid4().hex[:8]+ admin = User(username=f"ulen_{s}", email=f"ulen_{s}@local.test", password="x", is_admin=True)+ db.session.add(admin)+ db.session.commit()+ admin_id = admin.id+ client = app.test_client()+ with client.session_transaction() as sess:+ sess["_user_id"] = str(admin_id)+ sess["_fresh"] = True+ return client, admin_id+++def _cleanup(*ids):+ with app.app_context():+ for uid in ids:+ u = db.session.get(User, uid)+ if u:+ db.session.delete(u)+ db.session.commit()+++def test_create_refuses_a_long_username():+ client, admin_id = _admin_client()+ resp = client.post("/admin/users", json={"username": "u" * 21, "email": f"{uuid.uuid4().hex[:8]}@local.test",+ "password": "Passw0rd!long"})+ assert resp.status_code == 400+ assert "20 characters" in resp.get_json()["error"]+ _cleanup(admin_id)+++def test_update_refuses_a_long_username():+ client, admin_id = _admin_client()+ resp = client.put(f"/admin/users/{admin_id}", json={"username": "u" * 21})+ assert resp.status_code == 400+ _cleanup(admin_id)+++def test_twenty_characters_are_accepted():+ client, admin_id = _admin_client()+ name = uuid.uuid4().hex[:20]+ resp = client.post("/admin/users", json={"username": name, "email": f"{name[:8]}@local.test",+ "password": "Passw0rd!long"})+ assert resp.status_code in (200, 201), resp.data+ with app.app_context():+ created = User.query.filter_by(username=name).first().id+ _cleanup(created, admin_id)