Skip to content

[Backend/Security] Add Rate Limiting to FastAPI Scan Endpoint #176

Description

@jpdevhub

Problem

The /api/v1/scan endpoint is publicly accessible and currently has no rate limiting. A malicious user could spam thousands of scan requests, incurring heavy compute costs and degrading service for real users.

Goal

Add per-user rate limiting to the scan endpoint using slowapi (a FastAPI-compatible rate limiter based on limits).

Acceptance Criteria

  • slowapi added to backend/requirements.txt
  • Rate limit applied to POST /api/v1/scan: 10 requests per minute per authenticated user (keyed by user_id from JWT)
  • Rate limit applied to unauthenticated demo scans: 3 requests per minute per IP
  • On limit exceeded, API returns a clean JSON 429 Too Many Requests response:
    { "error": "error.rateLimitExceeded", "retry_after": 60 }
  • Frontend api.ts handles 429 gracefully — shows a toast: 'Scan limit reached. Please wait a moment.'
  • Unit test: backend/tests/test_rate_limit.py

Implementation Reference

from slowapi import Limiter
from slowapi.util import get_remote_address

limiter = Limiter(key_func=get_user_id_or_ip)

@router.post('/scan')
@limiter.limit('10/minute')
async def scan(...):
    ...

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions