Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend): per-user rate limit on the subscription status endpoint (#14231)

Bently4304949ed3e8
diff --git a/autogpt_platform/backend/backend/api/features/credits_rate_limit.py b/autogpt_platform/backend/backend/api/features/credits_rate_limit.py
new file mode 100644
index 0000000000..63e649d72f
--- /dev/null
+++ b/autogpt_platform/backend/backend/api/features/credits_rate_limit.py
@@ -0,0 +1,153 @@
+"""Per-user rate limit for ``GET /api/credits/subscription``.
+
+The subscription-status endpoint fans out to uncached Stripe reads per request
+and is fetched on every load of the billing page, so a scripted client can
+drive a lot of upstream traffic from one account. A per-user cap puts a hard
+ceiling on that without affecting normal use: the frontend shares one React
+Query entry with a 60s ``staleTime``, so a real user stays well under the cap.
+
+Atomic fixed-window counter in Redis (Lua ``INCR`` + first-hit ``EXPIRE``),
+keyed per ``user_id``. A single key per check keeps it correct on the Redis
+cluster.
+
+Availability: this puts Redis in front of a billing read, so the check is
+strictly best-effort. It is bounded by a short deadline and **fails open** on
+any Redis trouble — being unable to prove a user is under their cap must never
+cost every user their billing status.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import logging
+from datetime import UTC, datetime
+from typing import Any, cast
+
+import fastapi
+from autogpt_libs.auth import get_user_id
+from redis.exceptions import RedisClusterException, RedisError
+
+from backend.data.redis_client import get_redis_async
+
+logger = logging.getLogger(__name__)
+
+# 60 requests / minute / user — roughly 4x the heaviest realistic legitimate
+# burst (a multi-tab simultaneous hard-reload is ~15/min); the shared React
+# Query entry plus 60s staleTime keep steady-state use near ~1/min per tab.
+SUBSCRIPTION_STATUS_WINDOW_SECONDS = 60
+SUBSCRIPTION_STATUS_MAX_REQUESTS = 60
+
+# Hard deadline on the whole Redis interaction. Without it "fail open" is not
+# "fail fast": a cold client runs redis-py's own connect retry ladder (and each
+# command its per-command retry), so during an outage a request would park an
+# ASGI worker slot for minutes instead of falling through. The limiter is
+# best-effort, so it gets one short budget and is skipped if Redis can't answer
+# inside it.
+SUBSCRIPTION_STATUS_REDIS_TIMEOUT_SECONDS = 0.25
+
+# Atomic fixed-window counter: INCR the key, and set the TTL only on the INCR
+# that opened the window (count == 1). Doing both in one server-side script
+# means a freshly-created key always gets its expiry — the key can never linger
+# without a TTL if it happened to be recreated by INCR, and the window stays
+# fixed (the TTL is not refreshed on later hits).
+_INCR_OPEN_WINDOW = """
+local count = redis.call('INCR', KEYS[1])
+if count == 1 then
+ redis.call('EXPIRE', KEYS[1], ARGV[1])
+end
+return count
+"""
+
+
+async def _incr_window(key: str) -> int:
+ """Run the atomic counter, returning the request's position in the window.
+
+ Split out so the connect and the command share one deadline in the caller.
+ Lua ARGV values are strings; ``EXPIRE`` coerces ``"60"`` back to an int. The
+ cast mirrors the other ``eval()`` call sites (e.g. ``copilot/dream/locks``):
+ the cluster client types ``eval()``'s return as ``str``.
+ """
+ redis = await get_redis_async()
+ return await cast(
+ Any,
+ redis.eval(
+ _INCR_OPEN_WINDOW,
+ 1,
+ key,
+ str(SUBSCRIPTION_STATUS_WINDOW_SECONDS),
+ ),
+ )
+
+
+def _window_key(user_id: str, *, now: datetime) -> str:
+ """Per-user fixed-window key, bucket-aligned to the window so each user
+ has at most one active counter per window."""
+ bucket = int(now.timestamp()) // SUBSCRIPTION_STATUS_WINDOW_SECONDS
+ return f"credits:subscription:rl:{user_id}:{bucket}"
+
+
+async def enforce_subscription_status_rate_limit(
+ user_id: str = fastapi.Security(get_user_id),
+) -> None:
+ """Raise HTTP 429 when ``user_id`` exceeds the per-window cap.
+
+ Wired as a route dependency on ``GET /credits/subscription`` only, so the
+ internal callers of ``get_subscription_status`` (e.g. the POST update flow
+ returning fresh state) never trip it — FastAPI dependencies run for HTTP
+ requests, not for direct function calls.
+
+ On any Redis trouble this fails *open* (logs and lets the call through): a
+ Redis blip must never block the billing status read for every user, which
+ would be far worse than one client briefly exceeding its cap. The whole
+ interaction is bounded by ``SUBSCRIPTION_STATUS_REDIS_TIMEOUT_SECONDS`` so
+ an outage falls through fast instead of holding a worker slot.
+
+ ``RedisClusterException`` is caught explicitly: it does not inherit from
+ ``RedisError``, and ``SlotNotCoveredError`` (raised during a cluster
+ rolling restart / slot migration) would otherwise surface as a 500 — the
+ exact brown-out this fail-open exists for. Same reasoning as the comment in
+ ``backend/copilot/rate_limit.py``.
+ """
+ now = datetime.now(UTC)
+ key = _window_key(user_id, now=now)
+ try:
+ count = await asyncio.wait_for(
+ _incr_window(key),
+ timeout=SUBSCRIPTION_STATUS_REDIS_TIMEOUT_SECONDS,
+ )
+ except (
+ RedisError,
+ RedisClusterException,
+ ConnectionError,
+ OSError,
+ asyncio.TimeoutError,
+ ValueError,
+ ) as e:
+ logger.warning(
+ "Subscription-status rate-limit check failed open for user %s: %s",
+ user_id,
+ e,
+ )
+ return
+
+ if count > SUBSCRIPTION_STATUS_MAX_REQUESTS:
+ # Seconds left in this fixed window. Without Retry-After the client
+ # retries blind (React Query defaults to 3 retries), turning one block
+ # into several extra requests against the endpoint being protected.
+ retry_after = SUBSCRIPTION_STATUS_WINDOW_SECONDS - (
+ int(now.timestamp()) % SUBSCRIPTION_STATUS_WINDOW_SECONDS
+ )
+ logger.info(
+ "Subscription-status rate limit hit for user %s (count=%s)",
+ user_id,
+ count,
+ )
+ raise fastapi.HTTPException(
+ status_code=429,
+ detail=(
+ f"Subscription status rate limit exceeded "
+ f"({SUBSCRIPTION_STATUS_MAX_REQUESTS} requests per "
+ f"{SUBSCRIPTION_STATUS_WINDOW_SECONDS}s). Try again shortly."
+ ),
+ headers={"Retry-After": str(retry_after)},
+ )
diff --git a/autogpt_platform/backend/backend/api/features/credits_rate_limit_test.py b/autogpt_platform/backend/backend/api/features/credits_rate_limit_test.py
new file mode 100644
index 0000000000..62bd6d8087
--- /dev/null
+++ b/autogpt_platform/backend/backend/api/features/credits_rate_limit_test.py
@@ -0,0 +1,201 @@
+"""Unit tests for the GET /credits/subscription per-user rate limiter."""
+
+from __future__ import annotations
+
+import asyncio
+from datetime import UTC, datetime
+from unittest.mock import AsyncMock, MagicMock
+
+import fastapi
+import pytest
+from redis.exceptions import ConnectionError as RedisConnectionError
+from redis.exceptions import RedisClusterException, ResponseError
+
+from backend.api.features import credits_rate_limit as rate_limit
+
+
+class FakeRedis:
+ """Minimal Redis stand-in that actually interprets the limiter's Lua.
+
+ ``eval`` is mocked in most suites, which means the script — the only
+ non-trivial logic in the module — never runs. This executes the same
+ semantics (INCR, and EXPIRE only on the call that opened the window)
+ against real state, so a regression in the script (e.g. dropping the
+ EXPIRE branch, or refreshing the TTL on every hit) fails a test.
+ """
+
+ def __init__(self) -> None:
+ self.counters: dict[str, int] = {}
+ self.ttls: dict[str, int] = {}
+
+ async def eval(self, script: str, numkeys: int, key: str, ttl: str) -> int:
+ assert script == rate_limit._INCR_OPEN_WINDOW
+ assert numkeys == 1
+ count = self.counters.get(key, 0) + 1
+ self.counters[key] = count
+ if count == 1:
+ self.ttls[key] = int(ttl)
+ return count
+
+
+def fake_redis(mocker):
+ """Patch ``get_redis_async`` to return a stateful fake whose ``eval``
+ executes the limiter's script semantics."""
+ redis = FakeRedis()
+ mocker.patch(
+ "backend.api.features.credits_rate_limit.get_redis_async",
+ new=AsyncMock(return_value=redis),
+ )
+ return redis
+
+
+def mock_redis(mocker):
+ """Patch ``get_redis_async`` with a MagicMock whose ``eval`` return value
+ each test sets directly, for driving specific counts/errors."""
+ redis = MagicMock()
+ redis.eval = AsyncMock()
+ mocker.patch(
+ "backend.api.features.credits_rate_limit.get_redis_async",
+ new=AsyncMock(return_value=redis),
+ )
+ return redis
+
+
+async def test_first_hit_sets_ttl(fake_redis):
+ """The window-opening request must set the TTL, or the key would never
+ expire and the user would stay blocked forever after hitting the cap."""
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+ (key,) = fake_redis.ttls
+ assert "u1" in key
+ assert fake_redis.ttls[key] == rate_limit.SUBSCRIPTION_STATUS_WINDOW_SECONDS
+ assert fake_redis.counters[key] == 1
+
+
+async def test_ttl_not_refreshed_on_later_hits(fake_redis):
+ """The window is fixed: later hits increment but must not extend the TTL,
+ otherwise a steady stream of requests would hold the window open."""
+ for _ in range(5):
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+ (key,) = fake_redis.ttls
+ assert fake_redis.counters[key] == 5
+ # Exactly one key, whose TTL was written once when the window opened.
+ assert list(fake_redis.ttls.values()) == [
+ rate_limit.SUBSCRIPTION_STATUS_WINDOW_SECONDS
+ ]
+
+
+async def test_window_rolls_over(fake_redis, mocker):
+ """A later window uses a different key, so the count starts fresh."""
+ base = datetime(2026, 1, 1, 12, 0, 0, tzinfo=UTC)
+ mocker.patch.object(rate_limit, "datetime", MagicMock(now=lambda tz: base))
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+
+ later = datetime(2026, 1, 1, 12, 1, 30, tzinfo=UTC)
+ mocker.patch.object(rate_limit, "datetime", MagicMock(now=lambda tz: later))
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+
+ assert len(fake_redis.counters) == 2
+ assert set(fake_redis.counters.values()) == {1}
+
+
+async def test_cap_is_enforced_over_a_real_sequence(fake_redis):
+ """Exactly MAX requests pass; the next one is rejected."""
+ for _ in range(rate_limit.SUBSCRIPTION_STATUS_MAX_REQUESTS):
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+ with pytest.raises(fastapi.HTTPException) as exc_info:
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+ assert exc_info.value.status_code == 429
+
+
+async def test_over_limit_sets_retry_after(mock_redis):
+ """429 must carry Retry-After: without it clients retry blind and a single
+ block turns into several more requests against the endpoint."""
+ mock_redis.eval.return_value = rate_limit.SUBSCRIPTION_STATUS_MAX_REQUESTS + 1
+ with pytest.raises(fastapi.HTTPException) as exc_info:
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+ headers = exc_info.value.headers or {}
+ retry_after = int(headers["Retry-After"])
+ assert 0 < retry_after <= rate_limit.SUBSCRIPTION_STATUS_WINDOW_SECONDS
+
+
+async def test_at_limit_passes(mock_redis):
+ """Exactly MAX is still allowed — the cap is exclusive (count > MAX raises)."""
+ mock_redis.eval.return_value = rate_limit.SUBSCRIPTION_STATUS_MAX_REQUESTS
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+
+
+ "error",
+ [
+ RedisConnectionError("connection refused"),
+ # Does not inherit from RedisError; raised during a cluster rolling
+ # restart / slot migration. Must not 500 the billing endpoint.
+ RedisClusterException("SlotNotCoveredError"),
+ ResponseError("MISCONF Redis is configured to save RDB snapshots"),
+ OSError("socket blew up"),
+ ],
+ ids=["connection", "cluster", "response", "os"],
+)
+async def test_fails_open_on_redis_errors_from_the_command(mock_redis, error):
+ """Every flavour of Redis trouble on an established client must fail open.
+
+ The client is cached per event loop, so in production the connect path
+ rarely runs — the real surface is ``eval`` raising mid-command.
+ """
+ mock_redis.eval.side_effect = error
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+
+
+async def test_fails_open_when_connect_fails(mocker):
+ """Trouble reaching Redis at all also fails open."""
+ mocker.patch(
+ "backend.api.features.credits_rate_limit.get_redis_async",
+ new=AsyncMock(side_effect=RedisClusterException("cluster down")),
+ )
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+
+
+async def test_fails_open_and_fast_when_redis_hangs(mocker):
+ """A hung Redis must not hold the request: the limiter has a deadline and
+ falls through instead of parking a worker slot for the retry ladder."""
+
+ async def never_returns(_key: str) -> int:
+ await asyncio.sleep(60)
+ return 1
+
+ # Replace the function outright rather than mocking it: an AsyncMock whose
+ # side_effect returns a coroutine hands that coroutine back as the result
+ # instead of awaiting it, so the deadline would never be exercised.
+ mocker.patch.object(rate_limit, "_incr_window", new=never_returns)
+ mocker.patch.object(rate_limit, "SUBSCRIPTION_STATUS_REDIS_TIMEOUT_SECONDS", 0.05)
+
+ loop = asyncio.get_running_loop()
+ started = loop.time()
+ await rate_limit.enforce_subscription_status_rate_limit("u1")
+ # Tight bound: the patched deadline is 50ms, so anything approaching a
+ # second means the timeout was not honoured. Generous enough to absorb CI
+ # scheduling jitter, strict enough to fail if the deadline is dropped.
+ assert loop.time() - started < 1
+
+
+async def test_per_user_keys_are_distinct(fake_redis):
+ """The window key derives from ``user_id`` so two users' counters
+ never collide."""
+ await rate_limit.enforce_subscription_status_rate_limit("alice")
+ await rate_limit.enforce_subscription_status_rate_limit("bob")
+ keys = sorted(fake_redis.counters)
+ assert len(keys) == 2
+ assert any("alice" in k for k in keys)
+ assert any("bob" in k for k in keys)
diff --git a/autogpt_platform/backend/backend/api/features/subscription_routes_test.py b/autogpt_platform/backend/backend/api/features/subscription_routes_test.py
index 0dc296ec2d..c3a67ba516 100644
--- a/autogpt_platform/backend/backend/api/features/subscription_routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/subscription_routes_test.py
@@ -10,6 +10,7 @@ import stripe
from autogpt_libs.auth.jwt_utils import get_jwt_payload
from prisma.enums import SubscriptionTier
+from .credits_rate_limit import enforce_subscription_status_rate_limit
from .v1 import _validate_checkout_redirect_url, v1_router
TEST_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
@@ -31,6 +32,11 @@ def client() -> fastapi.testclient.TestClient:
return {"sub": TEST_USER_ID, "role": "user", "email": "[email protected]"}
app.dependency_overrides[get_jwt_payload] = override_get_jwt_payload
+ # Neutralise the subscription-status limiter: it counts in Redis against a
+ # shared key derived from the hardcoded TEST_USER_ID, so without this the
+ # GET tests here consume a real, never-reset window and start 429ing once
+ # enough of them run.
+ app.dependency_overrides[enforce_subscription_status_rate_limit] = lambda: None
try:
yield fastapi.testclient.TestClient(app)
finally:
diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.py
index 7d5b29d16f..8c4e93e5c5 100644
--- a/autogpt_platform/backend/backend/api/features/v1.py
+++ b/autogpt_platform/backend/backend/api/features/v1.py
@@ -36,6 +36,9 @@ from starlette.status import (
)
from typing_extensions import Optional, TypedDict
+from backend.api.features.credits_rate_limit import (
+ enforce_subscription_status_rate_limit,
+)
from backend.api.features.executions.activity_gate import (
hide_activity_summaries_if_disabled,
hide_activity_summary_if_disabled,
@@ -1003,7 +1006,11 @@ async def _get_stripe_price_amount(price_id: str) -> int | None:
summary="Get subscription tier, current cost, and all tier costs",
operation_id="getSubscriptionStatus",
tags=["credits"],
- dependencies=[Security(requires_user)],
+ dependencies=[
+ Security(requires_user),
+ Depends(enforce_subscription_status_rate_limit),
+ ],
+ responses={429: {"description": "Rate limit exceeded"}},
)
async def get_subscription_status(
user_id: Annotated[str, Security(get_user_id)],
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index b14ed524fa..6e00a2e477 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -5377,7 +5377,8 @@
},
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
- }
+ },
+ "429": { "description": "Rate limit exceeded" }
},
"security": [{ "HTTPBearerJWT": [] }]
},