Skip to content

feat(rate-limit): SlowAPI rate limiting for critical routes (#62) - #84

Open
SarthakB11 wants to merge 1 commit into
avantifellows:mainfrom
SarthakB11:feat/62-slowapi-rate-limit
Open

SarthakB11 wants to merge 1 commit into
avantifellows:mainfrom
SarthakB11:feat/62-slowapi-rate-limit

Conversation

@SarthakB11

Copy link
Copy Markdown

Implements #62.

What

Wires SlowAPI into the FastAPI app and applies per-route limits on the three endpoints flagged in the issue:

Route Limit
`POST /auth/create-access-token` 2/second
`POST /auth/refresh-token` 2/second
`POST /user/` 10/second

The limiter uses `slowapi.util.get_remote_address` as the rate-limit key and returns HTTP 429 via SlowAPI's default `_rate_limit_exceeded_handler` on violation.

How

  • New module `app/limiter.py` holding the shared `Limiter` instance. Putting it in its own module avoids circular imports between `main.py` and the routers.
  • `app/main.py`: sets `app.state.limiter`, registers the `RateLimitExceeded` exception handler, adds `SlowAPIMiddleware`.
  • `app/router/auth.py`: imports the shared limiter, decorates `create_access_token` and `refresh_token`, and adds the required `request: Request` parameter that SlowAPI needs for key extraction.
  • `app/router/user.py`: imports the limiter and decorates the `POST /` create endpoint. The handler already had `request: Request`.
  • `pyproject.toml` + `uv.lock` regenerated via `uv lock`: adds `slowapi>=0.1.9` (pulls in `limits`, `deprecated`, `wrapt`).

Scope notes

  • The issue mentions `/user/create` — the actual route is `POST /user/` (router prefix `/user` + `@router.post("/")`). Applied the 10/s limit there. Happy to rename the path if that's desired separately.
  • No new limits applied to other routers (`session`, `enrollment_record`, etc.) — the issue asks to "review other routes and determine if they need rate limiting" but leaving that as a follow-up to keep this PR scoped.
  • No tests added — repo has no `tests/` directory yet and `continue-on-error: true` is set on the test CI step.

Verification

Smoke-tested locally with a minimal FastAPI + TestClient harness replicating the wiring: first two requests return 200, third returns 429 as expected. Pre-commit checks (black, flake8) pass on all touched files.

Adds a global SlowAPI limiter wired in main.py (state.limiter,
RateLimitExceeded handler, SlowAPIMiddleware) and a new
app/limiter.py module so routers can import the shared instance
without circular imports.

Rate limits applied per issue avantifellows#62:
  - POST /auth/create-access-token -> 2/second
  - POST /auth/refresh-token       -> 2/second
  - POST /user/                    -> 10/second

The two /auth/ handlers gain a 'request: Request' parameter, which
SlowAPI requires for key extraction via get_remote_address.

Fixes avantifellows#62
@SarthakB11

Copy link
Copy Markdown
Author

@suryabulusu bumping this, still mergeable and rebased on main whenever you have a chance to look.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant