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
@@ -124,3 +124,47 @@ async def test_registration_is_rate_limited_per_ip(client):
|
||||
)
|
||||
assert resp.status_code == 429
|
||||
assert resp.json()["detail"]["code"] == "rate_limited"
|
||||
|
||||
|
||||
# --- B-57: usernames are one namespace, case included ---------------------------
|
||||
|
||||
|
||||
async def test_registration_refuses_a_username_differing_only_by_case(client):
|
||||
""""Bob" and "bob" used to be two accounts. On a custodial system that's an
|
||||
impersonation vector — and the two also shared a single rate-limit bucket, since
|
||||
the throttle key has always been lowercased, so each could lock the other out."""
|
||||
await _register(client, username="Bob")
|
||||
|
||||
resp = await client.post("/auth/register", json={"username": "bob", "password": "another-password"})
|
||||
|
||||
assert resp.status_code == 409
|
||||
assert resp.json()["detail"]["code"] == "username_taken"
|
||||
|
||||
|
||||
async def test_login_accepts_the_username_in_any_case(client):
|
||||
"""The flip side of the same rule: one account, reachable however it's typed."""
|
||||
await _register(client, username="Alice", password="original-password")
|
||||
|
||||
resp = await client.post("/auth/login", json={"username": "ALICE", "password": "original-password"})
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["access_token"]
|
||||
|
||||
|
||||
async def test_the_database_itself_rejects_a_case_variant(client):
|
||||
"""Not just the pre-check in the handler: two requests racing between the SELECT
|
||||
and the INSERT must still leave only one account, which is what the unique index
|
||||
on lower(username) guarantees."""
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.db.base import AsyncSessionLocal
|
||||
from app.db.models import User
|
||||
|
||||
await _register(client, username="Carol")
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
session.add(
|
||||
User(username="CAROL", password_hash="x", derivation_index=999, address="plm1-unused")
|
||||
)
|
||||
with pytest.raises(IntegrityError):
|
||||
await session.commit()
|
||||
Reference in new issue
Block a user