fix(dark-tariff): throttle repeated check_start brute-force attempts
This commit is contained in:
1 parent
d8a89610cd
commit
f5083397e6
3 files changed
+15
-3
No files matched your search
@@ -12,4 +12,4 @@ RUN useradd --uid 10001 --user-group --no-create-home --shell /usr/sbin/nologin
|
|||||||
|
|
||||||
USER darktariff
|
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"]
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
|
import asyncio
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, HTTPException, Request
|
||||||
|
|
||||||
from backend.app.db import operations
|
from backend.app.db import operations
|
||||||
from backend.app.api.schemas import CheckStart, CheckCommit, CheckAnswer
|
from backend.app.api.schemas import CheckStart, CheckCommit, CheckAnswer
|
||||||
@@ -12,7 +13,12 @@ async def greeting():
|
|||||||
return await operations.chat_greeting()
|
return await operations.chat_greeting()
|
||||||
|
|
||||||
@router.post("/checks", status_code=201)
|
@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)
|
return await operations.check_start(body.reading_id)
|
||||||
|
|
||||||
@router.post("/checks/{check_id}/commit")
|
@router.post("/checks/{check_id}/commit")
|
||||||
|
|||||||
@@ -47,6 +47,12 @@ async def chat_greeting():
|
|||||||
async def check_start(reading_id):
|
async def check_start(reading_id):
|
||||||
return await call("select chat.check_start(%s::bigint)", (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):
|
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))
|
return await call("select chat.check_commit(%s::uuid, %s::int, %s::text[])", (check_id, round, values))
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user