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
15 changes: 14 additions & 1 deletion apps/api/app/api/routes/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
GitHubRateLimitError,
GitHubRepoNotFoundError,
GitHubServiceError,
RepositoryTooLargeError,
fetch_github_metadata,
parse_github_url,
)
Expand All @@ -67,7 +68,7 @@
TestGeneratorError,
generate_test,
)
from app.services.ingestion import ingest_repository
from app.services.ingestion import ingest_repository, RepositorySourceTooLargeError
from app.services.pr_reviewer import (
PRNotFoundError,
review_pull_request,
Expand Down Expand Up @@ -529,6 +530,18 @@ def cb(processed: int, total: int, msg: str) -> None:
files_total=result.files_fetched,
message=f"Ingested {result.files_stored} files",
)
except (RepositoryTooLargeError, RepositorySourceTooLargeError) as e:
set_scan_progress(
repository_id=repository_id,
status="failed",
stage="failed",
progress=0,
message=str(e),
)
raise HTTPException(
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
detail=str(e),
)
except GitHubRepoNotFoundError as e:
set_scan_progress(
repository_id=repository_id,
Expand Down
227 changes: 227 additions & 0 deletions apps/api/app/core/rate_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
"""Application-level API rate limiting for CodeLens.

Architecture
------------
Implemented as a Starlette middleware using the ``limits`` library
(the same backend used by ``slowapi``).

Middleware-based limiting is preferred here because it requires no changes
to individual route function signatures: many CodeLens routes do not accept
a ``Request`` parameter, so decorator-based approaches (slowapi) would have
required adding ``Request`` to every affected route.

Current storage
---------------
``MemoryStorage`` -- process-local, in-memory.

LIMITATION: counters are not shared across multiple worker processes or
Render replicas. If CodeLens is scaled horizontally, the effective limit
seen by a client is ``configured_limit x num_processes``.

Upgrade path: replace ``MemoryStorage()`` with
``RedisStorage("redis://...")`` and the rest of the code is unchanged.

How limits are applied
----------------------
``ROUTE_LIMITS`` is a list of ``(HTTP_method, path_glob, limit_string)``
tuples evaluated top-to-bottom. The first matching rule wins. If no rule
matches the global ``DEFAULT_LIMIT`` is used.

Path globs use ``fnmatch`` syntax. ``*`` matches any characters within a
single path segment (or across segments -- fnmatch treats the whole string).

Client identification
---------------------
Authenticated requests: the CodeLens JWT session cookie is decoded (without
signature/expiry verification -- the normal auth dependency handles that) to
extract ``sub`` (user ID). This prevents a single user from bypassing limits
by rotating IPs.

Unauthenticated requests: ``request.client.host`` (remote IP) is used.

``X-Forwarded-For`` is intentionally NOT trusted: the deployment proxy
configuration has not been audited for header sanitisation.

How to change limits
--------------------
Edit ``ROUTE_LIMITS`` or ``DEFAULT_LIMIT`` in this module. No route-file
changes are required.

Test isolation
--------------
Call ``reset_limiter()`` between tests to flush all in-memory counters.
This prevents counter leakage between test cases.

Limitations
-----------
* Process-local only (see above).
* Path matching uses fnmatch -- complex regex patterns are not supported.
* ``Retry-After`` value is approximate.
"""

from __future__ import annotations

import fnmatch
import logging
import time
from typing import Callable

import jwt
from limits import parse as parse_limit
from limits.storage import MemoryStorage
from limits.strategies import FixedWindowRateLimiter
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import JSONResponse, Response

from app.core.config import settings

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Storage container -- replaced atomically by reset_limiter() during tests.
# dispatch() accesses via module globals (not captured closures) so that
# reset_limiter() takes effect immediately.
# ---------------------------------------------------------------------------
class _State:
storage: MemoryStorage
limiter: FixedWindowRateLimiter

def __init__(self):
self.storage = MemoryStorage()
self.limiter = FixedWindowRateLimiter(self.storage)


_state = _State()


def reset_limiter() -> None:
"""Flush all rate-limit counters.

Call this in test setUp / tearDown to prevent counter leakage between
test cases. Replaces the shared state object so the middleware picks up
fresh counters on the very next request.
"""
global _state
_state = _State()


# ---------------------------------------------------------------------------
# Global default -- applies when no specific rule matches.
# ---------------------------------------------------------------------------
DEFAULT_LIMIT = "60/minute"

# ---------------------------------------------------------------------------
# Per-route limit table.
# Format: (HTTP_method | "*", path_glob, limit_string)
# Evaluated top-to-bottom; first match wins.
# ---------------------------------------------------------------------------
ROUTE_LIMITS: list[tuple[str, str, str]] = [
# Auth
("POST", "/auth/login", "10/minute"),
("POST", "/auth/signup", "5/minute"),
("GET", "/auth/google/login", "10/minute"),
# Google callback is intentionally NOT rate-limited strictly: the browser
# redirect from Google arrives here and the client cannot control the
# timing. The global default (60/min) still applies.

# Repository metadata / connect (cause GitHub API calls)
("POST", "/repositories/github/metadata", "30/minute"),
("GET", "/repositories/github/metadata", "30/minute"),
("POST", "/repositories/github", "10/minute"),

# Scan (expensive background job)
("POST", "/repositories/*/scan", "5/hour"),

# AI endpoints
("POST", "/repositories/*/ask", "20/hour"),
("POST", "/repositories/*/findings/*/explain", "20/hour"),
("POST", "/repositories/*/findings/*/fix", "10/hour"),
("POST", "/repositories/*/findings/*/test", "10/hour"),
("POST", "/repositories/*/pull-requests/*/ai-review", "5/hour"),

# Health (raised limit so Render probes are never blocked)
("GET", "/health", "120/minute"),
("GET", "/db/health", "30/minute"),
]


def _match_path(method: str, path: str) -> str:
"""Return the limit string for the first matching rule, or DEFAULT_LIMIT."""
for rule_method, rule_pattern, limit_str in ROUTE_LIMITS:
if rule_method not in ("*", method):
continue
if fnmatch.fnmatch(path, rule_pattern):
return limit_str
return DEFAULT_LIMIT


def _get_client_key(request: Request) -> str:
"""Return a stable client identifier for rate-limit namespacing.

Prefers the authenticated user-id (from the JWT cookie) so that a single
user cannot bypass limits by rotating IP addresses.

Falls back to ``request.client.host`` for unauthenticated requests.
"""
cookie = request.cookies.get(settings.auth_cookie_name)
if cookie:
try:
payload = jwt.decode(
cookie,
options={"verify_signature": False},
algorithms=["HS256"],
)
sub = payload.get("sub")
if sub:
return f"user:{sub}"
except Exception:
pass # malformed token -- fall back to IP
host = (request.client.host if request.client else None) or "unknown"
return f"ip:{host}"


class RateLimitMiddleware(BaseHTTPMiddleware):
"""Starlette ASGI middleware that enforces per-client rate limits.

Accesses ``_state`` via the module global on every request so that
``reset_limiter()`` takes effect immediately without restarting the app.
"""

async def dispatch(self, request: Request, call_next: Callable) -> Response:
path = request.url.path
method = request.method

limit_str = _match_path(method, path)
item = parse_limit(limit_str)
key = _get_client_key(request)
# Namespace by method+path so limits are independent per endpoint.
namespace = f"rl:{method}:{path}"

# Access via module global so reset_limiter() is respected.
allowed = _state.limiter.hit(item, namespace, key)

if not allowed:
try:
stats = _state.limiter.get_window_stats(item, namespace, key)
retry_after = max(1, int(stats.reset_time - time.time()))
except Exception:
retry_after = 60

logger.warning(
"Rate limit exceeded: %s %s client=%s limit=%s",
method, path, key, limit_str,
)
return JSONResponse(
status_code=429,
content={
"detail": (
"Too many requests. "
"Please slow down and try again later."
),
"code": "RATE_LIMIT_EXCEEDED",
},
headers={"Retry-After": str(retry_after)},
)

return await call_next(request)
9 changes: 8 additions & 1 deletion apps/api/app/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import text

from app.api.routes import auth, repositories, pr_reviews, dashboard, ask
from app.core.ai_errors import AIQuotaExceededError
from app.core.rate_limit import RateLimitMiddleware
from app.db.database import engine

app = FastAPI(
Expand Down Expand Up @@ -37,20 +38,26 @@ def ai_quota_exception_handler(request: Request, exc: AIQuotaExceededError):
allow_headers=["*"],
)

# Rate limiting — added after CORS so 429 responses still carry CORS headers
# for browser clients.
app.add_middleware(RateLimitMiddleware)


app.include_router(auth.router)
app.include_router(repositories.router)
app.include_router(pr_reviews.router)
app.include_router(dashboard.router)
app.include_router(ask.router)


@app.get("/health")
def health():
return {
"status": "ok",
"service": "codelens",
}


@app.get("/db/health")
def db_health():
try:
Expand Down
Loading
Loading