Make usernames one case-insensitive namespace (B-57)
The login throttle keyed on body.username.lower() while the lookup matched User.username exactly, so "Bob" and "bob" were two accounts sharing one rate-limit bucket — each able to lock the other out — and registration happily accepted near-duplicate names, which on a custodial system is an impersonation vector. Uniqueness is now the database's job: a unique index on lower(username), with register and login both matching through func.lower(). The name is still stored exactly as typed, since that's what /admin and the audit log display, and the username pattern is ASCII-only so lower() is the whole of the normalization. The migration refuses to run if two existing accounts differ only by case. It can't merge or rename one automatically: both are custodial accounts that may hold funds, so that would be the migration silently deciding who owns what. It names the collisions and leaves them to the operator — the container runs `alembic upgrade head` at startup, so it surfaces as a refusal to start rather than a half-applied schema. Verified both directions against a scratch DB, plus `alembic check` (clean) and the collision guard actually firing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ab65728bdc
commit
57721355f0
6 files changed
+109
-16
No files matched your search
@@ -13,6 +13,16 @@ def utcnow() -> datetime:
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
# B-57: usernames are compared case-insensitively, and that has to be the
|
||||
# database's job, not a convention the query layer remembers. "Bob" and "bob"
|
||||
# used to be two accounts sharing one rate-limit bucket (each locking the other
|
||||
# out) and, worse on a custodial system, a ready-made impersonation vector.
|
||||
# A functional unique index rather than a normalized column: the name stays
|
||||
# stored exactly as the user typed it, which is what /admin and the audit log
|
||||
# display. The username pattern (auth/routes.py) is ASCII-only, so lower() is
|
||||
# the whole of the normalization — no Unicode casefolding subtleties apply.
|
||||
__table_args__ = (Index("ix_users_username_lower", text("lower(username)"), unique=True),)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
username: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
||||
password_hash: Mapped[str] = mapped_column(String(256))
|
||||
|
||||
Reference in new issue
Block a user