fix: закрыть IDOR в бронях, пароль админа и обработку callback_data (PRJ-3)

- IDOR: отмена/редактирование брони только владельцем (проверка user_id)
- пароль админа: убран дефолт admin123, обязателен из env; сравнение через hmac.compare_digest; TTL сессии 1ч
- callback_data: безопасный парсинг int + .get() вместо прямого индексирования (KeyError/ValueError)
This commit is contained in:
2026-08-04 19:33:08 +04:00
parent bf26370da9
commit 896c26bd57
5 changed files with 65 additions and 11 deletions
+18 -5
View File
@@ -1,4 +1,7 @@
"""Admin handler for spa-telegram-bot — secure with password."""
import hmac
import time
from aiogram import Router, F
from aiogram.types import Message, CallbackQuery
from aiogram.filters import Command, CommandObject
@@ -10,17 +13,22 @@ router = Router()
ADMIN_IDS = {991309145}
# Store approved sessions — user_ids that passed password check
_approved = set()
_approved: dict[int, float] = {}
APPROVED_TTL_SECONDS = 60 * 60 # 1 hour
def is_admin(user_id: int, password: str | None = None) -> bool:
"""Check if user is admin: by user_id, by password, or by approved session."""
if user_id in ADMIN_IDS:
return True
if user_id in _approved:
now = time.time()
expires_at = _approved.get(user_id)
if expires_at and expires_at > now:
return True
if password and password == ADMIN_PASSWORD:
_approved.add(user_id)
if expires_at:
_approved.pop(user_id, None) # expired session
if password and hmac.compare_digest(password.encode("utf-8"), ADMIN_PASSWORD.encode("utf-8")):
_approved[user_id] = now + APPROVED_TTL_SECONDS
return True
return False
@@ -69,7 +77,12 @@ async def admin_cancel_booking(callback: CallbackQuery):
await callback.answer("⛔ Нет доступа")
return
booking_id = int(callback.data.split("_")[2])
try:
booking_id = int(callback.data.split("_", 2)[2])
except (ValueError, IndexError):
await callback.answer("Некорректные данные")
return
logger.info("Admin {uid} cancelled booking #{bid}", uid=callback.from_user.id, bid=booking_id)
from app.db.memory import cancel_booking