mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* feat(auth): use the OIDC picture claim as the user's avatar On each OIDC sign-in, a user with no avatar, or one synced from the provider earlier, gets the picture claim stored as their avatar. An uploaded avatar is never replaced. The download goes through the SSRF-guarded client (the configured provider's origin may reach a LAN address), is capped at 5 MB, must sniff as a supported image, and any failure is logged without blocking the sign-in. Closes #4297 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(auth): address review on the OIDC avatar sync Addresses Greptile's review on #5730: - ask the UserInfo endpoint when the ID token has no picture claim - keep an avatar uploaded while the picture downloaded - allow only the OIDC provider's origins to reach private addresses - trim docstrings and narrow await_args in the callback test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(auth): type the OIDC avatar test helpers for mypy Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: code-review Bound the OIDC picture download with an overall deadline, trust the OIDC CA bundle, catch hostname encoding errors so sign-in never 500s, and set the synced avatar with a conditional update so a concurrent upload wins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: simplify Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: review-polish Type the pytest fixtures and helpers in the OIDC avatar tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pr-ready: prose Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(auth): remove the synced OIDC avatar when the user uploads one Once avatar_path points at the uploaded avatar.<ext>, the sync never touches the user's avatar again, so the old oidc-avatar.<ext> file would otherwise stay on disk for good. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
165 lines
5.6 KiB
Python
165 lines
5.6 KiB
Python
import threading
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
from exceptions.database_exceptions import LastAdminError
|
|
from handler.database import db_user_handler
|
|
from handler.database.base_handler import sync_session
|
|
from models.user import Role, User
|
|
from utils.datetime import to_utc
|
|
|
|
|
|
@pytest.fixture
|
|
def other_admin() -> User:
|
|
return db_user_handler.add_user(
|
|
User(username="other_admin", hashed_password="x", role=Role.ADMIN)
|
|
)
|
|
|
|
|
|
def _role(user: User) -> Role:
|
|
current = db_user_handler.get_user(user.id)
|
|
assert current
|
|
return current.role
|
|
|
|
|
|
class TestKeepAnAdmin:
|
|
def test_the_last_admin_cannot_be_demoted(self, admin_user: User):
|
|
with pytest.raises(LastAdminError):
|
|
db_user_handler.update_user(
|
|
admin_user.id, {"role": Role.USER}, keep_an_admin=True
|
|
)
|
|
|
|
assert _role(admin_user) == Role.ADMIN
|
|
|
|
def test_the_last_admin_cannot_be_deleted(self, admin_user: User):
|
|
with pytest.raises(LastAdminError):
|
|
db_user_handler.delete_user(admin_user.id, keep_an_admin=True)
|
|
|
|
assert db_user_handler.get_user(admin_user.id) is not None
|
|
|
|
def test_one_of_two_admins_can_be_demoted(
|
|
self, admin_user: User, other_admin: User
|
|
):
|
|
db_user_handler.update_user(
|
|
other_admin.id, {"role": Role.USER}, keep_an_admin=True
|
|
)
|
|
|
|
assert _role(other_admin) == Role.USER
|
|
|
|
def test_one_of_two_admins_can_be_deleted(
|
|
self, admin_user: User, other_admin: User
|
|
):
|
|
db_user_handler.delete_user(other_admin.id, keep_an_admin=True)
|
|
|
|
assert db_user_handler.get_user(other_admin.id) is None
|
|
|
|
def test_other_edits_of_the_last_admin_go_through(self, admin_user: User):
|
|
db_user_handler.update_user(
|
|
admin_user.id, {"ra_username": "player"}, keep_an_admin=True
|
|
)
|
|
|
|
def test_a_user_can_be_deleted_with_one_admin_left(
|
|
self, admin_user: User, viewer_user: User
|
|
):
|
|
db_user_handler.delete_user(viewer_user.id, keep_an_admin=True)
|
|
|
|
assert db_user_handler.get_user(viewer_user.id) is None
|
|
|
|
def test_without_the_flag_the_last_admin_can_be_demoted(self, admin_user: User):
|
|
# The OIDC role sync relies on this: the provider's claims win.
|
|
db_user_handler.update_user(admin_user.id, {"role": Role.USER})
|
|
|
|
assert _role(admin_user) == Role.USER
|
|
|
|
def test_two_admins_demoting_each_other_leave_one(
|
|
self, admin_user: User, other_admin: User
|
|
):
|
|
errors: list[Exception] = []
|
|
|
|
def demote_admin_user() -> None:
|
|
try:
|
|
db_user_handler.update_user(
|
|
admin_user.id, {"role": Role.USER}, keep_an_admin=True
|
|
)
|
|
except Exception as exc:
|
|
errors.append(exc)
|
|
|
|
# The first demotion holds its locks while the second one starts.
|
|
with sync_session.begin() as session:
|
|
db_user_handler.update_user(
|
|
other_admin.id,
|
|
{"role": Role.USER},
|
|
keep_an_admin=True,
|
|
session=session,
|
|
)
|
|
second = threading.Thread(target=demote_admin_user)
|
|
second.start()
|
|
second.join(timeout=1)
|
|
assert second.is_alive(), "the second demotion did not wait for the lock"
|
|
second.join(timeout=30)
|
|
|
|
assert not second.is_alive()
|
|
assert [type(error) for error in errors] == [LastAdminError]
|
|
assert (_role(admin_user), _role(other_admin)) == (Role.ADMIN, Role.USER)
|
|
|
|
|
|
class TestUpdateLastActive:
|
|
def test_bumps_a_user_never_active(self, admin_user: User):
|
|
db_user_handler.update_user(admin_user.id, {"last_active": None})
|
|
|
|
db_user_handler.update_last_active(admin_user.id)
|
|
|
|
current = db_user_handler.get_user(admin_user.id)
|
|
assert current is not None
|
|
assert current.last_active is not None
|
|
|
|
def test_skips_a_user_active_within_the_debounce_window(self, admin_user: User):
|
|
recent = datetime.now(timezone.utc) - timedelta(minutes=1)
|
|
db_user_handler.update_user(admin_user.id, {"last_active": recent})
|
|
|
|
db_user_handler.update_last_active(admin_user.id)
|
|
|
|
current = db_user_handler.get_user(admin_user.id)
|
|
assert current is not None
|
|
assert current.last_active is not None
|
|
assert abs((to_utc(current.last_active) - recent).total_seconds()) < 1
|
|
|
|
def test_set_last_active_writes_nothing_inside_the_window(
|
|
self, admin_user: User, executed_statements: list[str]
|
|
):
|
|
user = db_user_handler.update_user(
|
|
admin_user.id, {"last_active": datetime.now(timezone.utc)}
|
|
)
|
|
executed_statements.clear()
|
|
|
|
user.set_last_active()
|
|
|
|
assert executed_statements == []
|
|
|
|
|
|
class TestReplaceAvatarPath:
|
|
def test_sets_the_path_while_it_is_still_the_expected_one(self, admin_user: User):
|
|
db_user_handler.update_user(admin_user.id, {"avatar_path": "old.png"})
|
|
|
|
replaced = db_user_handler.replace_avatar_path(
|
|
admin_user.id, "old.png", "new.png"
|
|
)
|
|
|
|
current = db_user_handler.get_user(admin_user.id)
|
|
assert replaced
|
|
assert current is not None
|
|
assert current.avatar_path == "new.png"
|
|
|
|
def test_leaves_a_path_that_changed_meanwhile(self, admin_user: User):
|
|
db_user_handler.update_user(admin_user.id, {"avatar_path": "uploaded.png"})
|
|
|
|
replaced = db_user_handler.replace_avatar_path(
|
|
admin_user.id, "old.png", "new.png"
|
|
)
|
|
|
|
current = db_user_handler.get_user(admin_user.id)
|
|
assert not replaced
|
|
assert current is not None
|
|
assert current.avatar_path == "uploaded.png"
|