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>
100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
import ssl
|
|
from collections.abc import AsyncGenerator, Awaitable, Callable
|
|
from contextlib import asynccontextmanager
|
|
from contextvars import ContextVar, Token
|
|
from functools import partial
|
|
from typing import TYPE_CHECKING, TypeVar
|
|
|
|
import aiohttp
|
|
import httpx2
|
|
|
|
from config import has_proxy_env
|
|
from utils.ssrf import (
|
|
INTERNAL_ORIGIN_ALLOWLIST,
|
|
install_async_ssrf_protection,
|
|
install_sync_ssrf_protection,
|
|
validate_url_for_http_request,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from fastapi import Request, Response
|
|
|
|
_T = TypeVar("_T")
|
|
|
|
ctx_aiohttp_session: ContextVar[aiohttp.ClientSession] = ContextVar("aiohttp_session")
|
|
ctx_httpx_client: ContextVar[httpx2.AsyncClient] = ContextVar("httpx_client")
|
|
|
|
|
|
def _validate_request_url_sync(request: httpx2.Request) -> None:
|
|
validate_url_for_http_request(str(request.url))
|
|
|
|
|
|
async def _validate_request_url_async(
|
|
request: httpx2.Request, allowlist: frozenset[tuple[str, int]]
|
|
) -> None:
|
|
validate_url_for_http_request(str(request.url), allowlist=allowlist)
|
|
|
|
|
|
def create_aiohttp_session() -> aiohttp.ClientSession:
|
|
return aiohttp.ClientSession(trust_env=has_proxy_env())
|
|
|
|
|
|
def create_httpx_async_client(
|
|
allowlist: frozenset[tuple[str, int]] = INTERNAL_ORIGIN_ALLOWLIST,
|
|
verify: ssl.SSLContext | bool = True,
|
|
) -> httpx2.AsyncClient:
|
|
"""An SSRF-guarded client that lets only `allowlist` origins reach private addresses."""
|
|
client = httpx2.AsyncClient(
|
|
trust_env=has_proxy_env(),
|
|
verify=verify,
|
|
event_hooks={
|
|
"request": [partial(_validate_request_url_async, allowlist=allowlist)]
|
|
},
|
|
)
|
|
install_async_ssrf_protection(client, allowlist=allowlist)
|
|
return client
|
|
|
|
|
|
def create_httpx_client() -> httpx2.Client:
|
|
client = httpx2.Client(
|
|
trust_env=has_proxy_env(),
|
|
event_hooks={"request": [_validate_request_url_sync]},
|
|
)
|
|
install_sync_ssrf_protection(client)
|
|
return client
|
|
|
|
|
|
@asynccontextmanager
|
|
async def set_context_var(var: ContextVar[_T], value: _T) -> AsyncGenerator[Token[_T]]:
|
|
"""Temporarily set a context variables."""
|
|
token = var.set(value)
|
|
yield token
|
|
var.reset(token)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def initialize_context() -> AsyncGenerator[None]:
|
|
"""Initialize context variables."""
|
|
async with (
|
|
create_aiohttp_session() as aiohttp_session,
|
|
create_httpx_async_client() as httpx_client,
|
|
set_context_var(ctx_aiohttp_session, aiohttp_session),
|
|
set_context_var(ctx_httpx_client, httpx_client),
|
|
):
|
|
yield
|
|
|
|
|
|
async def set_context_middleware(
|
|
request: Request, call_next: Callable[[Request], Awaitable[Response]]
|
|
) -> Response:
|
|
"""Initialize context variables in FastAPI request-response cycle.
|
|
|
|
This middleware is needed because the context initialized during the lifespan
|
|
process is not available in the request-response cycle.
|
|
"""
|
|
async with (
|
|
set_context_var(ctx_aiohttp_session, request.app.state.aiohttp_session),
|
|
set_context_var(ctx_httpx_client, request.app.state.httpx_client),
|
|
):
|
|
return await call_next(request)
|