Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend-api/src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

Expand Down Expand Up @@ -140,6 +141,7 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:

def create_app() -> FastAPI:
middleware = [
Middleware(SlowAPIMiddleware),
Middleware(
CORSMiddleware, # ty:ignore[invalid-argument-type]
allow_origins=["*"] if settings.debug else [],
Expand Down
9 changes: 7 additions & 2 deletions backend-api/src/api/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ def _get_connecting_ip(request: Request) -> str:
return request.client.host if request.client else "unknown"


limiter = Limiter(key_func=_get_connecting_ip, default_limits=[], storage_uri="memory://")

SEARCH_LIMIT = "15/minute;100/hour"
ADMIN_LIMIT = "30/minute"
# limit across all clients, prevent dos
GLOBAL_LIMIT = "200/minute"

limiter = Limiter(
key_func=_get_connecting_ip,
default_limits=[],
application_limits=[GLOBAL_LIMIT],
Comment on lines +28 to +30
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a shared key for the global application limit

application_limits=[GLOBAL_LIMIT] is still keyed by the limiter key_func, and _get_connecting_ip returns a per-client IP, so this change enforces 200/minute per IP instead of a single shared cap across all clients. In practice, aggregate traffic can exceed the intended global ceiling whenever requests come from multiple client IPs (or a distributed attacker), so the DOS guard described by GLOBAL_LIMIT is not actually enforced.

Useful? React with 👍 / 👎.

storage_uri="memory://",
)


def rate_limit_exceeded_handler(_request: Request, exc: RateLimitExceeded) -> JSONResponse:
return JSONResponse(
Expand Down
Loading