Count per-address limits on the hop a trusted proxy added; limit signup
The limits read the leftmost X-Forwarded-For entry, which the client writes, and the LAN nginx appended to it. The address now comes from uvicorn's proxy-headers middleware (rightmost hop not in FORWARDED_ALLOW_IPS), applied in the app too so tunnel-replayed calls resolve the same way; nginx sets the header to $remote_addr and the compose backend trusts the Docker networks. /users/signup gets a per-address limit of 10 an hour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GAeg4Jo6xjZzjwy9QDWMQ
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
from typing import Annotated, Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from sqlmodel import col, func, select
|
||||
|
||||
from fluksio import crud
|
||||
@@ -11,6 +11,7 @@ from fluksio.api.deps import (
|
||||
get_current_active_superuser,
|
||||
)
|
||||
from fluksio.api.routes.cloud import forget_remote_user
|
||||
from fluksio.api.routes.oauth import too_many
|
||||
from fluksio.core.config import settings
|
||||
from fluksio.core.security import get_password_hash, verify_password
|
||||
from fluksio.models import (
|
||||
@@ -28,6 +29,11 @@ from fluksio.utils import generate_new_account_email, send_email
|
||||
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
|
||||
#: Sign-ups one address may attempt per window, once `SIGNUP_ENABLED` opens
|
||||
#: the route to anyone who reaches it.
|
||||
SIGNUP_ATTEMPTS = 10
|
||||
SIGNUP_WINDOW_S = 3600.0
|
||||
|
||||
|
||||
@router.get(
|
||||
"/",
|
||||
@@ -150,12 +156,18 @@ def delete_user_me(session: SessionDep, current_user: CurrentUser) -> Any:
|
||||
|
||||
|
||||
@router.post("/signup", response_model=UserPublic)
|
||||
def register_user(session: SessionDep, user_in: UserRegister) -> Any:
|
||||
def register_user(request: Request, session: SessionDep, user_in: UserRegister) -> Any:
|
||||
"""
|
||||
Create new user without the need to be logged in.
|
||||
"""
|
||||
if not settings.SIGNUP_ENABLED:
|
||||
raise HTTPException(status_code=403, detail="Signup is disabled")
|
||||
# Every attempt counts, not only the refused ones: a successful one is
|
||||
# an account, and an Argon2 hash on the shared threadpool.
|
||||
if too_many(request, "signup", SIGNUP_ATTEMPTS, SIGNUP_WINDOW_S):
|
||||
raise HTTPException(
|
||||
status_code=429, detail="Too many sign-ups from here. Try again later."
|
||||
)
|
||||
user = crud.get_user_by_email(session=session, email=user_in.email)
|
||||
if user:
|
||||
raise HTTPException(
|
||||
|
||||
Reference in New Issue
Block a user