feat: add per-IP rate limiting to WS LLM triggers

Per-account limits alone don't stop one account script-hitting
Ollama from many source IPs. Adds matching per-IP limiters
alongside the existing per-account ones for fragment/question/
summon triggers, per spec's dual per-account-and-per-IP requirement.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PphXq1s43DNRj1uWKGXof
This commit is contained in:
Indiana
2026-07-21 03:45:58 +00:00
parent 0756e677b9
commit 83575f9bb3
2 changed files with 91 additions and 4 deletions

View File

@@ -53,6 +53,16 @@ fragment_limiter = RateLimiter(max_requests=30, window_seconds=60)
question_limiter = RateLimiter(max_requests=6, window_seconds=60)
summon_limiter = RateLimiter(max_requests=4, window_seconds=60)
# Per-IP limiters for the same trigger points (spec §5). Ollama is a shared,
# single-instance, CPU-only resource — per-account limits alone don't stop
# one compromised/scripted account from hammering it across many source IPs,
# nor do they protect the resource from many accounts sharing one IP. IP
# thresholds are looser than the per-account ones since a single IP can
# legitimately host multiple accounts on a shared network.
fragment_ip_limiter = RateLimiter(max_requests=60, window_seconds=60)
question_ip_limiter = RateLimiter(max_requests=12, window_seconds=60)
summon_ip_limiter = RateLimiter(max_requests=8, window_seconds=60)
AUDIO_DIR = Path(settings.data_dir) / "audio"
@@ -65,6 +75,7 @@ def _audio_dir() -> Path:
class SeanceState:
user_id: uuid.UUID
session_id: uuid.UUID
client_ip: str
send_queue: asyncio.Queue = field(default_factory=asyncio.Queue)
mode: str = "unknown"
language: str = "en"
@@ -91,6 +102,10 @@ def serialize_entity(entity: Entity) -> dict:
}
def _client_ip(websocket: WebSocket) -> str:
return websocket.client.host if websocket.client else "unknown"
async def _authenticate(websocket: WebSocket) -> uuid.UUID | None:
raw_token = websocket.cookies.get(SESSION_COOKIE_NAME)
if raw_token is None:
@@ -227,7 +242,12 @@ async def _summon(state: SeanceState, channel: str) -> tuple[Entity, bool]:
async def _handle_summon(state: SeanceState) -> None:
if not summon_limiter.allow(str(state.user_id)):
# Short-circuits: an account already over its own cap never gets far
# enough to spend from the IP budget too.
if not (
summon_limiter.allow(str(state.user_id))
and summon_ip_limiter.allow(state.client_ip)
):
await state.send_queue.put(
{
"type": "error",
@@ -266,7 +286,10 @@ async def _handle_anomaly(state: SeanceState, message: dict) -> None:
await state.send_queue.put({"type": "status", "state": "attuning"})
return
if not fragment_limiter.allow(str(state.user_id)):
if not (
fragment_limiter.allow(str(state.user_id))
and fragment_ip_limiter.allow(state.client_ip)
):
return # anomalies during a crowded veil just pass unheard
try:
@@ -279,7 +302,10 @@ async def _handle_anomaly(state: SeanceState, message: dict) -> None:
async def _handle_question(state: SeanceState, text: str) -> None:
if not question_limiter.allow(str(state.user_id)):
if not (
question_limiter.allow(str(state.user_id))
and question_ip_limiter.allow(state.client_ip)
):
await state.send_queue.put(
{
"type": "error",
@@ -390,7 +416,11 @@ async def session_socket(websocket: WebSocket) -> None:
await db.commit()
await db.refresh(contact_session)
state = SeanceState(user_id=user_id, session_id=contact_session.id)
state = SeanceState(
user_id=user_id,
session_id=contact_session.id,
client_ip=_client_ip(websocket),
)
sender = asyncio.create_task(_sender(state, websocket))
await state.send_queue.put({"type": "session", "id": str(contact_session.id)})