mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 12:52:19 +01:00
fix(security): revoke on both sides of the credential write
Revoking only before the write left a wider hole than the one it closed: the old password stays good until the write commits, so a login starting in that window authenticates and writes a session unconditionally, having no existing id for the `XX` guard to refuse. Revoking only after the write cannot abort a change Redis was unreachable for. Doing both keeps each property. What remains is a login that read the old hash before the first pass and writes after the second, which needs the per-user revocation generation rather than an ordering change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NouZquWDxNBqxYyu2E2GF8
This commit is contained in:
@@ -479,20 +479,23 @@ async def update_user(
|
||||
"hashed_password"
|
||||
)
|
||||
|
||||
# Ahead of the write: an unreachable Redis then aborts the change
|
||||
# rather than committing it with the account's sessions left live.
|
||||
# Once before the write, so an unreachable Redis aborts rather than
|
||||
# commits, and once after, to catch a login the old password was still
|
||||
# good for while the write was in progress.
|
||||
if creds_updated:
|
||||
await RedisSessionMiddleware.clear_user_sessions(previous_username)
|
||||
|
||||
db_user_handler.update_user(id, cleaned_data)
|
||||
|
||||
if creds_updated:
|
||||
await RedisSessionMiddleware.clear_user_sessions(previous_username)
|
||||
if request.user.id == id:
|
||||
request.session.clear()
|
||||
|
||||
# A role change alters the user's effective permissions; tell their UI.
|
||||
if "role" in cleaned_data:
|
||||
await emit_permissions_changed(id)
|
||||
|
||||
if creds_updated and request.user.id == id:
|
||||
request.session.clear()
|
||||
|
||||
db_user = db_user_handler.get_user(id)
|
||||
if not db_user:
|
||||
msg = f"Username with id {id} not found"
|
||||
|
||||
@@ -307,8 +307,7 @@ async def test_admin_password_reset_invalidates_the_target_user_sessions(
|
||||
async def test_a_failed_revocation_leaves_the_password_unchanged(
|
||||
client, access_token: str, editor_user: User
|
||||
):
|
||||
"""Revoking before the write keeps the two from disagreeing: a password
|
||||
that changed while its sessions survived would outlive the reset."""
|
||||
"""An unreachable Redis aborts the change instead of committing it."""
|
||||
original_hash = editor_user.hashed_password
|
||||
|
||||
with mock.patch.object(
|
||||
@@ -328,6 +327,40 @@ async def test_a_failed_revocation_leaves_the_password_unchanged(
|
||||
assert db_user.hashed_password == original_hash
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sessions_are_revoked_on_both_sides_of_the_write(
|
||||
client, access_token: str, editor_user: User
|
||||
):
|
||||
"""The second pass catches a login the old password was still good for."""
|
||||
calls: list[str] = []
|
||||
real_update = DBUsersHandler.update_user
|
||||
|
||||
def record_update(self, id, data, *args, **kwargs):
|
||||
# `set_last_active` writes on every authenticated request; only the
|
||||
# credential write is being ordered here.
|
||||
if "hashed_password" in data:
|
||||
calls.append("write")
|
||||
return real_update(self, id, data, *args, **kwargs)
|
||||
|
||||
async def record_revoke(user_id: str) -> None:
|
||||
calls.append("revoke")
|
||||
|
||||
with (
|
||||
mock.patch.object(DBUsersHandler, "update_user", record_update),
|
||||
mock.patch.object(
|
||||
RedisSessionMiddleware, "clear_user_sessions", side_effect=record_revoke
|
||||
),
|
||||
):
|
||||
response = client.put(
|
||||
f"/api/users/{editor_user.id}",
|
||||
data={"password": "another_reset_password"},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert calls == ["revoke", "write", "revoke"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_password_change_invalidates_sessions(client, admin_user: User):
|
||||
# Get the user's session cookie
|
||||
|
||||
@@ -52,8 +52,7 @@ class TestRedisSessionMiddleware:
|
||||
assert client.get("/whoami").json()["sub"] == USERNAME
|
||||
|
||||
def test_revoked_session_is_not_restored_by_an_in_flight_request(self) -> None:
|
||||
"""A request holding a copy of a session revoked mid-flight must not
|
||||
write it back, or a credential change could never lock anyone out."""
|
||||
"""A session revoked mid-flight must not be written back."""
|
||||
client = TestClient(create_test_app())
|
||||
client.post("/login")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user