Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(auth): backfill users.auth_version so upgrades keep logins working

Juan Denis01e0925c2abe
diff --git a/backend/app/services/migration_service.py b/backend/app/services/migration_service.py
index ef086712..15b3ac35 100644
--- a/backend/app/services/migration_service.py
+++ b/backend/app/services/migration_service.py
@@ -52,6 +52,19 @@ class MigrationService:
type_name = type(sa_type).__name__.upper()
return cls._TYPE_MAP.get(type_name, 'TEXT')
+ @classmethod
+ def _sqlite_default(cls, col):
+ """Render a column's literal server_default as a SQLite DEFAULT value."""
+ default = getattr(col, 'server_default', None)
+ arg = getattr(default, 'arg', None)
+ if isinstance(arg, bool):
+ return '1' if arg else '0'
+ if isinstance(arg, (int, float)):
+ return repr(arg)
+ if isinstance(arg, str):
+ return "'" + arg.replace("'", "''") + "'"
+ return None
+
@classmethod
def _fix_missing_columns(cls, db):
"""Sync database schema with ORM models.
@@ -78,6 +91,13 @@ class MigrationService:
sqlite_type = cls._sqlite_type(col.type)
sql = f'ALTER TABLE {table_name} ADD COLUMN {col.name} {sqlite_type}'
+ # Honour a declared server_default so existing rows are
+ # backfilled instead of left NULL — this runs BEFORE alembic,
+ # so a migration that only adds the column when missing would
+ # otherwise skip its own default (users.auth_version → no login).
+ default = cls._sqlite_default(col)
+ if default is not None:
+ sql += f' DEFAULT {default}'
try:
with db.engine.begin() as conn:
diff --git a/backend/migrations/versions/097_user_auth_version.py b/backend/migrations/versions/097_user_auth_version.py
index 8cc5578b..079fcba8 100644
--- a/backend/migrations/versions/097_user_auth_version.py
+++ b/backend/migrations/versions/097_user_auth_version.py
@@ -19,6 +19,11 @@ def upgrade():
if 'auth_version' not in columns:
op.add_column('users', sa.Column('auth_version', sa.String(32),
nullable=False, server_default='0'))
+ # The startup schema sync may have added the column ahead of us as a bare
+ # nullable TEXT (no default). A NULL auth_version can never match a token
+ # claim, which locks every existing user out — always backfill.
+ op.execute("UPDATE users SET auth_version = '0' "
+ "WHERE auth_version IS NULL OR auth_version = ''")
if 'revoked_sessions' not in inspector.get_table_names():
op.create_table('revoked_sessions',
sa.Column('session_id', sa.String(32), primary_key=True),
diff --git a/backend/tests/test_auth_version_backfill.py b/backend/tests/test_auth_version_backfill.py
new file mode 100644
index 00000000..f3f4920e
--- /dev/null
+++ b/backend/tests/test_auth_version_backfill.py
@@ -0,0 +1,82 @@
+"""users.auth_version must never be NULL for an existing row.
+
+Regression for the 1.9.27 dev upgrade on a live box: the startup schema sync
+(`MigrationService._fix_missing_columns`) runs BEFORE alembic and added
+`auth_version` as a bare nullable TEXT, so migration 097 saw the column and
+skipped its `'0'` server_default. Every pre-existing user then carried a NULL
+auth_version, no JWT claim could match it, and nobody could log in.
+"""
+import importlib.util
+import os
+from types import SimpleNamespace
+
+import sqlalchemy as sa
+from alembic.migration import MigrationContext
+from alembic.operations import Operations
+
+from app import db
+from app.services.migration_service import MigrationService
+
+_MIGRATION = os.path.join(os.path.dirname(os.path.dirname(__file__)),
+ 'migrations', 'versions', '097_user_auth_version.py')
+
+
+def _legacy_users_engine(tmp_path, *, with_auth_version):
+ """A pre-097 users table holding one real row (plus alembic_version)."""
+ engine = sa.create_engine(f'sqlite:///{tmp_path / "legacy.db"}')
+ users = db.metadata.tables['users']
+ legacy = sa.MetaData()
+ columns = [c.copy() for c in users.columns
+ if c.name != 'auth_version' or with_auth_version]
+ sa.Table('users', legacy, *columns)
+ legacy.create_all(engine)
+ with engine.begin() as conn:
+ conn.execute(sa.text(
+ "INSERT INTO users (id, username, email, password_hash, role, is_active) "
+ "VALUES (1, 'legacy', '[email protected]', 'x', 'admin', 1)"))
+ return engine
+
+
+def test_startup_schema_sync_backfills_server_default(tmp_path, app):
+ engine = _legacy_users_engine(tmp_path, with_auth_version=False)
+ fake_db = SimpleNamespace(engine=engine, metadata=db.metadata, create_all=lambda: None)
+
+ MigrationService._fix_missing_columns(fake_db)
+
+ with engine.connect() as conn:
+ assert conn.execute(sa.text('SELECT auth_version FROM users')).scalar() == '0'
+ ddl = conn.execute(sa.text(
+ "SELECT sql FROM sqlite_master WHERE name = 'users'")).scalar()
+ assert "auth_version TEXT DEFAULT '0'" in ddl
+
+
+def test_migration_097_backfills_a_pre_added_null_column(tmp_path, app):
+ engine = _legacy_users_engine(tmp_path, with_auth_version=False)
+ with engine.begin() as conn:
+ # Exactly what the pre-fix schema sync produced on the live box.
+ conn.execute(sa.text('ALTER TABLE users ADD COLUMN auth_version TEXT'))
+ assert conn.execute(sa.text('SELECT auth_version FROM users')).scalar() is None
+
+ spec = importlib.util.spec_from_file_location('migration_097', _MIGRATION)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+
+ with engine.begin() as conn:
+ ctx = MigrationContext.configure(conn)
+ with Operations.context(ctx):
+ module.upgrade()
+
+ with engine.connect() as conn:
+ assert conn.execute(sa.text('SELECT auth_version FROM users')).scalar() == '0'
+ assert sa.inspect(conn).has_table('revoked_sessions')
+
+
+def test_sqlite_default_rendering():
+ def col(default):
+ return sa.Column('c', sa.String(), server_default=default)
+
+ assert MigrationService._sqlite_default(col('0')) == "'0'"
+ assert MigrationService._sqlite_default(col("it's")) == "'it''s'"
+ assert MigrationService._sqlite_default(col(sa.true())) is None
+ assert MigrationService._sqlite_default(col(sa.text('now()'))) is None
+ assert MigrationService._sqlite_default(sa.Column('c', sa.String())) is None