Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Normalize emails (trim + lowercase) on write and match them case-insensitively across login, registration, password reset, admin user CRUD, SSO, and admin scripts, with a one-time migration to lowercase existing addresses

Murtaza Nasirca383dd8dc90
diff --git a/scripts/create_admin.py b/scripts/create_admin.py
index 947a7d0b..c0dccbac 100644
--- a/scripts/create_admin.py
+++ b/scripts/create_admin.py
@@ -50,14 +50,14 @@ def create_admin_user():
# Get email
skip_domain_check = os.environ.get('SKIP_EMAIL_DOMAIN_CHECK', 'false').lower() == 'true'
while True:
- email = input("Enter email address: ").strip()
+ email = User.normalize_email(input("Enter email address: "))
try:
# Validate email (skip DNS/MX check if SKIP_EMAIL_DOMAIN_CHECK=true)
validate_email(email, check_deliverability=not skip_domain_check)
- # Check if email already exists
+ # Check if email already exists (case-insensitive)
with app.app_context():
- existing_email = db.session.query(User).filter_by(email=email).first()
+ existing_email = User.find_by_email(email)
if existing_email:
print(f"Email '{email}' already exists. Please use another.")
continue
diff --git a/scripts/docker_create_admin.py b/scripts/docker_create_admin.py
index e7b97f1a..124e0dfb 100644
--- a/scripts/docker_create_admin.py
+++ b/scripts/docker_create_admin.py
@@ -37,7 +37,7 @@ def create_admin_user_from_env():
# Get values from environment variables
username = os.environ.get('ADMIN_USERNAME')
- email = os.environ.get('ADMIN_EMAIL')
+ email = User.normalize_email(os.environ.get('ADMIN_EMAIL'))
password = os.environ.get('ADMIN_PASSWORD')
# Validate required environment variables
@@ -71,8 +71,8 @@ def create_admin_user_from_env():
print(f"User with username '{username}' already exists.")
sys.exit(0)
- # Check if email already exists
- existing_email = db.session.query(User).filter_by(email=email).first()
+ # Check if email already exists (case-insensitive)
+ existing_email = User.find_by_email(email)
if existing_email:
print(f"User with email '{email}' already exists.")
sys.exit(0)
diff --git a/src/api/admin.py b/src/api/admin.py
index 5bca77af..10b2879e 100644
--- a/src/api/admin.py
+++ b/src/api/admin.py
@@ -209,14 +209,15 @@ def admin_add_user():
if User.query.filter_by(username=data['username']).first():
return jsonify({'error': 'Username already exists'}), 400
- if User.query.filter_by(email=data['email']).first():
+ email = User.normalize_email(data['email'])
+ if User.find_by_email(email):
return jsonify({'error': 'Email already exists'}), 400
-
+
# Create new user
hashed_password = bcrypt.generate_password_hash(data['password']).decode('utf-8')
new_user = User(
username=data['username'],
- email=data['email'],
+ email=email,
password=hashed_password,
is_admin=data.get('is_admin', False),
monthly_token_budget=data.get('monthly_token_budget'),
@@ -267,11 +268,14 @@ def admin_update_user(user_id):
return jsonify({'error': 'Username already exists'}), 400
user.username = data['username']
- if 'email' in data and data['email'] != user.email:
- # Check if email already exists
- if User.query.filter_by(email=data['email']).first():
- return jsonify({'error': 'Email already exists'}), 400
- user.email = data['email']
+ if 'email' in data:
+ new_email = User.normalize_email(data['email'])
+ if new_email != user.email:
+ # Case-insensitive existence check against other accounts.
+ existing = User.find_by_email(new_email)
+ if existing and existing.id != user.id:
+ return jsonify({'error': 'Email already exists'}), 400
+ user.email = new_email
if 'password' in data and data['password']:
user.password = bcrypt.generate_password_hash(data['password']).decode('utf-8')
diff --git a/src/api/auth.py b/src/api/auth.py
index 11491f0e..84d5b11e 100644
--- a/src/api/auth.py
+++ b/src/api/auth.py
@@ -87,9 +87,15 @@ def rate_limit(limit_string):
# --- Forms ---
+# Trim + lowercase an email field before validators run. Fixes the mobile
+# autofill case where a trailing space makes Email() reject an otherwise-valid
+# address, and normalizes what gets stored so accounts stay case-consistent.
+_normalize_email_filter = lambda x: x.strip().lower() if x else x
+
+
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)])
- email = StringField('Email', validators=[DataRequired(), Email()])
+ email = StringField('Email', filters=[_normalize_email_filter], validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired(), password_check])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Sign Up')
@@ -100,13 +106,14 @@ class RegistrationForm(FlaskForm):
raise ValidationError('That username is already taken. Please choose a different one.')
def validate_email(self, email):
- user = User.query.filter_by(email=email.data).first()
- if user:
+ # Case-insensitive so a case-variant of an existing address can't be
+ # registered as a separate account.
+ if User.find_by_email(email.data):
raise ValidationError('That email is already registered. Please use a different one.')
class LoginForm(FlaskForm):
- email = StringField('Email', validators=[DataRequired(), Email()])
+ email = StringField('Email', filters=[_normalize_email_filter], validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
remember = BooleanField('Remember Me')
submit = SubmitField('Login')
@@ -210,7 +217,7 @@ def login():
form = LoginForm()
if form.validate_on_submit():
- user = User.query.filter_by(email=form.email.data).first()
+ user = User.find_by_email(form.email.data)
if user and user.password:
# Check if password login is disabled for non-admins
if password_login_disabled and not user.is_admin:
@@ -422,13 +429,13 @@ def resend_verification():
return redirect(url_for('auth.login'))
# Get email from session (set during failed login) or form
- email = session.get('unverified_email') or request.form.get('email')
+ email = User.normalize_email(session.get('unverified_email') or request.form.get('email'))
if not email:
flash('Email address is required.', 'danger')
return redirect(url_for('auth.login'))
- user = User.query.filter_by(email=email).first()
+ user = User.find_by_email(email)
if not user:
# Don't reveal if user exists
@@ -475,13 +482,13 @@ def forgot_password():
return redirect(url_for('auth.login'))
if request.method == 'POST':
- email = request.form.get('email')
+ email = User.normalize_email(request.form.get('email'))
if not email:
flash('Email address is required.', 'danger')
return render_template('auth/forgot_password.html', title='Forgot Password')
- user = User.query.filter_by(email=email).first()
+ user = User.find_by_email(email)
# Always show the same message to prevent email enumeration
if user:
diff --git a/src/auth/sso.py b/src/auth/sso.py
index 7cac5eb2..e6e3eacd 100644
--- a/src/auth/sso.py
+++ b/src/auth/sso.py
@@ -133,7 +133,7 @@ def create_or_update_sso_user(userinfo: Dict[str, str]) -> User:
"""Create or update a user from SSO (OIDC) claims."""
cfg = get_sso_config()
subject = userinfo.get("sub")
- email = userinfo.get("email")
+ email = User.normalize_email(userinfo.get("email"))
username_claim = cfg["username_claim"]
name_claim = cfg["name_claim"]
@@ -164,7 +164,7 @@ def create_or_update_sso_user(userinfo: Dict[str, str]) -> User:
# Existing by email: attach SSO
if email:
- existing_email_user = User.query.filter_by(email=email).first()
+ existing_email_user = User.find_by_email(email)
if existing_email_user:
existing_email_user.sso_provider = cfg["provider_name"]
existing_email_user.sso_subject = subject
@@ -193,7 +193,7 @@ def create_or_update_sso_user(userinfo: Dict[str, str]) -> User:
def _update_profile_fields(user: User, userinfo: Dict[str, str], name_claim: Optional[str]) -> None:
"""Update optional profile fields from SSO claims."""
if not user.email and userinfo.get("email"):
- user.email = userinfo["email"]
+ user.email = User.normalize_email(userinfo["email"])
if name_claim and userinfo.get(name_claim):
user.name = userinfo[name_claim]
diff --git a/src/init_db.py b/src/init_db.py
index 0333d4ff..f89d076b 100644
--- a/src/init_db.py
+++ b/src/init_db.py
@@ -178,7 +178,40 @@ def initialize_database(app):
app.logger.info("Password column is already nullable, skipping migration")
except Exception as e:
app.logger.warning(f"Could not migrate password column to nullable (may cause issues with SSO): {e}")
-
+
+ # One-time email normalization: lowercase (and trim) existing stored
+ # emails so they match the normalize-on-write behavior. Login and
+ # uniqueness are case-insensitive regardless, so this is cleanup, not a
+ # correctness requirement. Collision-aware: if two accounts differ only
+ # by case/whitespace, both are left untouched and logged, since merging
+ # them is a manual decision. DB-agnostic (operates through the ORM).
+ try:
+ from collections import defaultdict
+ from src.models import User
+ users = User.query.all()
+ by_norm = defaultdict(list)
+ for u in users:
+ by_norm[User.normalize_email(u.email)].append(u)
+ changed = 0
+ for u in users:
+ norm = User.normalize_email(u.email)
+ if norm == u.email:
+ continue # already normalized
+ if len(by_norm[norm]) > 1:
+ app.logger.warning(
+ f"Skipped email normalization for user {u.id}: multiple "
+ f"accounts map to '{norm}' case-insensitively; resolve manually."
+ )
+ continue
+ u.email = norm
+ changed += 1
+ if changed:
+ db.session.commit()
+ app.logger.info(f"Normalized {changed} stored email address(es) to lowercase")
+ except Exception as e:
+ db.session.rollback()
+ app.logger.warning(f"Email normalization migration skipped: {e}")
+
if add_column_if_not_exists(engine, 'recording', 'mime_type', 'VARCHAR(100)'):
app.logger.info("Added mime_type column to recording table")
if add_column_if_not_exists(engine, 'recording', 'audio_duration_seconds', 'FLOAT'):
diff --git a/src/models/user.py b/src/models/user.py
index 24950de3..364fef75 100644
--- a/src/models/user.py
+++ b/src/models/user.py
@@ -7,6 +7,7 @@ and the Speaker model for tracking speaker profiles used in diarization.
from datetime import datetime
from flask_login import UserMixin
+from sqlalchemy import func
from src.database import db
@@ -82,6 +83,32 @@ class User(db.Model, UserMixin):
def __repr__(self):
return f"User('{self.username}', '{self.email}')"
+ @staticmethod
+ def normalize_email(value):
+ """Canonical email form for storage and lookup: trimmed + lowercased.
+
+ Email addresses are case-insensitive in practice (the domain always,
+ the local part at every real provider), so normalizing on write keeps
+ stored data clean and normalizing input before a lookup keeps matching
+ consistent. Also strips the stray whitespace mobile autofill appends.
+ """
+ return value.strip().lower() if value else value
+
+ @classmethod
+ def find_by_email(cls, email):
+ """Case-insensitive lookup by email.
+
+ Uses ``func.lower`` on the column so it matches regardless of the
+ stored case — a user whose address was stored mixed-case (before
+ normalization existed, or via an SSO provider) still resolves, with
+ no migration required.
+ """
+ if not email:
+ return None
+ return cls.query.filter(
+ func.lower(cls.email) == cls.normalize_email(email)
+ ).first()
+
class Speaker(db.Model):
"""Speaker model for tracking voice profiles used in diarization."""
diff --git a/tests/test_email_normalization.py b/tests/test_email_normalization.py
new file mode 100644
index 00000000..f5874f9f
--- /dev/null
+++ b/tests/test_email_normalization.py
@@ -0,0 +1,132 @@
+"""Email normalization and case-insensitive matching.
+
+Fixes the mobile-autofill "Invalid email address" failure (trailing space) and
+makes account matching case-insensitive, without locking out any user whose
+email was stored mixed-case. Motivated by PR #334 (Wladefant); reimplemented as
+a case-insensitive lookup plus normalize-on-write so pre-existing mixed-case
+accounts keep working.
+"""
+
+import os
+import sys
+import uuid
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from src.app import app, db, bcrypt
+from src.models import User
+
+app.config['WTF_CSRF_ENABLED'] = False
+
+
+def _mk_user(stored_email, password='Passw0rd!'):
+ u = User(username=f'u_{uuid.uuid4().hex[:8]}', email=stored_email,
+ password=bcrypt.generate_password_hash(password).decode(),
+ email_verified=True)
+ db.session.add(u)
+ db.session.commit()
+ return u
+
+
+# --- helper unit behavior ---
+
+def test_normalize_email_trims_and_lowercases():
+ assert User.normalize_email(' [email protected] ') == '[email protected]'
+ assert User.normalize_email(None) is None
+ assert User.normalize_email('') == ''
+
+
+def test_find_by_email_is_case_insensitive():
+ with app.app_context():
+ u = _mk_user('[email protected]')
+ try:
+ assert User.find_by_email('[email protected]').id == u.id
+ assert User.find_by_email(' [email protected] ').id == u.id
+ assert User.find_by_email('[email protected]') is None
+ finally:
+ db.session.delete(u); db.session.commit()
+
+
+# --- login route ---
+
+def _post_login(client, email, password='Passw0rd!'):
+ return client.post('/login', data={'email': email, 'password': password},
+ follow_redirects=False)
+
+
+def test_login_succeeds_with_trailing_space_from_autofill():
+ with app.app_context():
+ u = _mk_user('[email protected]')
+ client = app.test_client()
+ try:
+ r = _post_login(client, '[email protected] ') # trailing space
+ assert r.status_code == 302 # redirected into the app, not re-rendered
+ with client.session_transaction() as s:
+ assert s.get('_user_id') == str(u.id)
+ finally:
+ db.session.delete(u); db.session.commit()
+
+
+def test_login_succeeds_with_mixed_case_input():
+ with app.app_context():
+ u = _mk_user('[email protected]')
+ client = app.test_client()
+ try:
+ r = _post_login(client, '[email protected]')
+ assert r.status_code == 302
+ with client.session_transaction() as s:
+ assert s.get('_user_id') == str(u.id)
+ finally:
+ db.session.delete(u); db.session.commit()
+
+
+def test_login_succeeds_for_preexisting_mixed_case_stored_user():
+ """The regression guard: a user whose email row is stored mixed-case (from
+ before normalization existed, or via SSO) must still be able to log in.
+ Lowercasing the input alone would have locked this user out."""
+ with app.app_context():
+ u = _mk_user('[email protected]') # stored with capitals
+ client = app.test_client()
+ try:
+ r = _post_login(client, '[email protected]') # typed lowercase
+ assert r.status_code == 302
+ with client.session_transaction() as s:
+ assert s.get('_user_id') == str(u.id)
+ finally:
+ db.session.delete(u); db.session.commit()
+
+
+def test_login_still_rejects_syntactically_invalid_email():
+ with app.app_context():
+ client = app.test_client()
+ r = _post_login(client, 'not-an-email')
+ assert r.status_code == 200 # form re-rendered, no session
+ with client.session_transaction() as s:
+ assert '_user_id' not in s
+
+
+# --- registration form: normalize-on-write + case-insensitive uniqueness ---
+
+def test_registration_form_normalizes_email_before_storage():
+ from src.api.auth import RegistrationForm
+ with app.test_request_context('/register', method='POST', data={
+ 'username': 'newbie', 'email': ' [email protected] ',
+ 'password': 'Passw0rd!', 'confirm_password': 'Passw0rd!'}):
+ form = RegistrationForm()
+ # The field filter runs on construction, so what gets stored is normalized.
+ assert form.email.data == '[email protected]'
+
+
+def test_registration_rejects_case_variant_of_existing_email():
+ from src.api.auth import RegistrationForm
+ with app.app_context():
+ u = _mk_user('[email protected]')
+ try:
+ with app.test_request_context('/register', method='POST', data={
+ 'username': 'someone', 'email': '[email protected]',
+ 'password': 'Passw0rd!', 'confirm_password': 'Passw0rd!'}):
+ form = RegistrationForm()
+ assert not form.validate()
+ assert any('already registered' in e for e in form.email.errors)
+ finally:
+ db.session.delete(u); db.session.commit()