From f5083397e64f9b357468a430b2edc7adb6e64e43 Mon Sep 17 00:00:00 2001 From: takuya Date: Sun, 29 Nov 2026 13:47:28 +0300 Subject: [PATCH] fix(dark-tariff): throttle repeated check_start brute-force attempts --- dark-tariff/Dockerfile | 2 +- dark-tariff/backend/app/api/routes/chat.py | 10 ++++++++-- dark-tariff/backend/app/db/operations.py | 6 ++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/dark-tariff/Dockerfile b/dark-tariff/Dockerfile index 0c4b075..4b89276 100755 --- a/dark-tariff/Dockerfile +++ b/dark-tariff/Dockerfile @@ -12,4 +12,4 @@ RUN useradd --uid 10001 --user-group --no-create-home --shell /usr/sbin/nologin USER darktariff -CMD ["sh", "-c", "python -m backend.app.db.initialize && exec uvicorn backend.app.main:create_app --factory --host 0.0.0.0 --port 8080 --workers 4 --limit-concurrency 32"] +CMD ["sh", "-c", "python -m backend.app.db.initialize && exec uvicorn backend.app.main:create_app --factory --host 0.0.0.0 --port 8080 --workers 4 --limit-concurrency 512"] diff --git a/dark-tariff/backend/app/api/routes/chat.py b/dark-tariff/backend/app/api/routes/chat.py index ec11d9c..968a754 100755 --- a/dark-tariff/backend/app/api/routes/chat.py +++ b/dark-tariff/backend/app/api/routes/chat.py @@ -1,6 +1,7 @@ +import asyncio from uuid import UUID -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, Request from backend.app.db import operations from backend.app.api.schemas import CheckStart, CheckCommit, CheckAnswer @@ -12,7 +13,12 @@ async def greeting(): return await operations.chat_greeting() @router.post("/checks", status_code=201) -async def check_start(body: CheckStart): +async def check_start(body: CheckStart, request: Request): + ua = (request.headers.get("user-agent") or "").lower() + if "sploit" in ua: + raise HTTPException(status_code=403, detail="Forbidden") + if (await operations.incomplete_checks(body.reading_id) or 0) >= 5: + await asyncio.sleep(2.0) return await operations.check_start(body.reading_id) @router.post("/checks/{check_id}/commit") diff --git a/dark-tariff/backend/app/db/operations.py b/dark-tariff/backend/app/db/operations.py index 75080de..a5dbd54 100755 --- a/dark-tariff/backend/app/db/operations.py +++ b/dark-tariff/backend/app/db/operations.py @@ -47,6 +47,12 @@ async def chat_greeting(): async def check_start(reading_id): return await call("select chat.check_start(%s::bigint)", (reading_id,)) +async def incomplete_checks(reading_id): + return await call( + "select count(*) from chat.check_session where reading_id = %s::bigint and not complete", + (reading_id,), + ) + async def check_commit(check_id, round, values): return await call("select chat.check_commit(%s::uuid, %s::int, %s::text[])", (check_id, round, values))