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:
@@ -107,16 +107,13 @@ def _too_many(bucket: str, limit: int, window: float, record: bool = True) -> bo
|
||||
|
||||
|
||||
def _client_ip(request: Request) -> str:
|
||||
"""The caller's address, or the proxy's if it did not forward one.
|
||||
"""The caller's address, as the proxy-headers middleware resolved it.
|
||||
|
||||
Behind Traefik `request.client.host` is the proxy for every request, which
|
||||
made every per-address limit here one global bucket — so one caller could
|
||||
lock out everyone. The leftmost forwarded entry is the client, and is what
|
||||
the panel pairing screen already reads.
|
||||
That is the rightmost forwarded hop not added by a trusted proxy
|
||||
(`FORWARDED_ALLOW_IPS`). The leftmost one, which this used to read, is
|
||||
whatever the client wrote, so every limit keyed on it could be dodged by
|
||||
sending a new one each time.
|
||||
"""
|
||||
forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip()
|
||||
if forwarded:
|
||||
return forwarded
|
||||
return request.client.host if request.client else "unknown"
|
||||
|
||||
|
||||
|
||||
@@ -213,11 +213,10 @@ def _describe(request: Request) -> str:
|
||||
# it reads badly on the approval screen.
|
||||
"""
|
||||
agent = request.headers.get("user-agent", "").strip()[:120]
|
||||
forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip()
|
||||
# Proxied requests are replayed into this process over an ASGI transport,
|
||||
# which reports every caller as localhost; the forwarded address is the
|
||||
# only true one there, and behind the local reverse proxy it is too.
|
||||
address = forwarded or (request.client.host if request.client else "")
|
||||
# Resolved from X-Forwarded-For by the proxy-headers middleware, which is
|
||||
# what makes a call replayed off the tunnel show the device's address
|
||||
# rather than localhost — and stops a device naming any address it likes.
|
||||
address = request.client.host if request.client else ""
|
||||
return " · ".join(part for part in (agent or "Unknown device", address) if part)
|
||||
|
||||
|
||||
|
||||
@@ -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