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.

Randomize auth_version left at the '0' backfill

Juan Denis991c8eae1a5d
diff --git a/backend/migrations/versions/109_randomize_legacy_auth_version.py b/backend/migrations/versions/109_randomize_legacy_auth_version.py
new file mode 100644
index 00000000..0a4c73f4
--- /dev/null
+++ b/backend/migrations/versions/109_randomize_legacy_auth_version.py
@@ -0,0 +1,33 @@
+"""Give users still on the backfilled auth_version '0' a random one.
+
+097 backfilled every existing user with the literal '0', so on an upgraded
+install anyone holding the JWT signing key could mint a valid session
+without also guessing the per-user epoch that fresh installs get. Each such
+user gets a random epoch; their current sessions end and they log in again.
+"""
+import secrets
+
+import sqlalchemy as sa
+from alembic import op
+
+revision = '109_randomize_legacy_auth_version'
+down_revision = '108_drop_webhook_zero_downtime'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ bind = op.get_bind()
+ users = sa.table('users', sa.column('id', sa.Integer),
+ sa.column('auth_version', sa.String(32)))
+ legacy = bind.execute(
+ sa.select(users.c.id).where(sa.or_(users.c.auth_version.is_(None),
+ users.c.auth_version.in_(['0', ''])))
+ ).scalars().all()
+ for user_id in legacy:
+ bind.execute(users.update().where(users.c.id == user_id)
+ .values(auth_version=secrets.token_hex(16)))
+
+
+def downgrade():
+ pass # a random epoch is valid under the old code too
diff --git a/backend/tests/test_migration_109_auth_version.py b/backend/tests/test_migration_109_auth_version.py
new file mode 100644
index 00000000..cb3c7fa8
--- /dev/null
+++ b/backend/tests/test_migration_109_auth_version.py
@@ -0,0 +1,34 @@
+"""109 replaces the guessable backfilled auth_version '0' with a random epoch
+per user, and leaves already-random epochs alone."""
+import importlib.util
+from pathlib import Path
+
+import sqlalchemy as sa
+from alembic.migration import MigrationContext
+from alembic.operations import Operations
+
+
+def _load():
+ path = Path(__file__).parents[1] / 'migrations/versions/109_randomize_legacy_auth_version.py'
+ spec = importlib.util.spec_from_file_location('m109', path)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+def test_legacy_epochs_become_random_and_distinct(tmp_path, monkeypatch):
+ migration = _load()
+ engine = sa.create_engine('sqlite:///' + (tmp_path / 'upgraded.db').as_posix())
+ with engine.begin() as conn:
+ conn.exec_driver_sql('CREATE TABLE users (id INTEGER PRIMARY KEY, auth_version TEXT)')
+ conn.exec_driver_sql("INSERT INTO users VALUES (1, '0'), (2, '0'), (3, ''), "
+ "(4, NULL), (5, 'a1b2c3d4e5f60718293a4b5c6d7e8f90')")
+ monkeypatch.setattr(migration, 'op', Operations(MigrationContext.configure(conn)))
+ migration.upgrade()
+ rows = dict(conn.exec_driver_sql('SELECT id, auth_version FROM users').all())
+ engine.dispose()
+
+ rotated = [rows[i] for i in (1, 2, 3, 4)]
+ assert all(v and len(v) == 32 and v != '0' for v in rotated)
+ assert len(set(rotated)) == 4
+ assert rows[5] == 'a1b2c3d4e5f60718293a4b5c6d7e8f90'