🌊 Волна 14: backend #6 — security + БД (8 багов + бонус)

Security:
- cf_connecting_ip_spoof: whitelist CF_IPS, доверие только от Cloudflare (config.py + main.py)

БД:
- get_pool_race: asyncio.Lock вокруг create_pool (database.py)
- init_db_no_transaction: обёрнут в conn.transaction() (database.py)
- drop_constraint_cascade: убран CASCADE (database.py)
- add_pk_on_duplicates: DELETE дублей перед ADD PK (database.py)
- redundant_index_users_telegram: удалены дубли индексов (database.py)
- fstring_sql_migration: whitelist regex (database.py)

Качество:
- cleanup_task_not_cancelled: cancel в shutdown (main.py)

Бонус:
- removed_images: обработка удалённых картинок в feed.py (TODO из v1)

Пропущено осознанно:
- matview_if_not_exists_stale (Alembic, отдельно)
- initdata_in_query_param (fallback для deleteFeedPost)

Аудит v3: Issue #6, backend 8/10 задач
This commit is contained in:
2026-09-24 15:43:13 +00:00
parent 742337fb14
commit 4152b4b2c4
4 changed files with 1024 additions and 871 deletions
+15
View File
@@ -35,6 +35,21 @@ MAX_INITDATA_AGE = 4 * 60 * 60 # 4 часа в секундах
SUPPORTED_LANGUAGES = {"pl", "ru", "uk", "en"} SUPPORTED_LANGUAGES = {"pl", "ru", "uk", "en"}
# 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof —
# доверяем заголовку cf-connecting-ip ТОЛЬКО если запрос пришёл
# с IP Cloudflare. Иначе — берём request.client.host.
# Список: https://www.cloudflare.com/ips-v4
CF_IPS = {
# IPv4
"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22",
"141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20",
"197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13",
"104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22",
# IPv6
"2400:cb00::/32", "2606:4700::/32", "2803:f800::/32",
"2405:b500::/32", "2405:8100::/32", "2a06:98c0::/29", "2c0f:f248::/32",
}
# ============ 🔧 НОВОЕ (18.09.2026): настройки для FeedModal ============ # ============ 🔧 НОВОЕ (18.09.2026): настройки для FeedModal ============
# Папка для хранения картинок feed-постов. # Папка для хранения картинок feed-постов.
# Путь совпадает с backend/, файлы доступны как static mounts (см. main.py при необходимости). # Путь совпадает с backend/, файлы доступны как static mounts (см. main.py при необходимости).
+901 -857
View File
File diff suppressed because it is too large Load Diff
+48 -12
View File
@@ -1,4 +1,5 @@
import asyncio import asyncio
import ipaddress
import logging import logging
import os import os
import re import re
@@ -10,7 +11,7 @@ from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from config import FRONTEND_ORIGIN, UPLOADS_DIR from config import FRONTEND_ORIGIN, UPLOADS_DIR, CF_IPS
from database import init_db from database import init_db
from routers import users, game, hints, leaderboard, sentences, achievements, wordquiz, bubblewords, pronunciation, assistant, learning, tts, typing, teacher, support, admin, feed, admin_content from routers import users, game, hints, leaderboard, sentences, achievements, wordquiz, bubblewords, pronunciation, assistant, learning, tts, typing, teacher, support, admin, feed, admin_content
from routers.sentences import init_sentences from routers.sentences import init_sentences
@@ -125,6 +126,25 @@ class RateLimiter:
rate_limiter = RateLimiter() rate_limiter = RateLimiter()
# 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof —
# проверяем, что IP клиента входит в список Cloudflare.
def _is_cloudflare_ip(ip_str: str) -> bool:
"""Проверяет, что IP входит в CF_IPS (Cloudflare)."""
if not ip_str:
return False
try:
ip = ipaddress.ip_address(ip_str)
except ValueError:
return False
for cidr in CF_IPS:
try:
if ip in ipaddress.ip_network(cidr, strict=False):
return True
except ValueError:
continue
return False
# ============ Фоновое обновление кэша рейтинга ============ # ============ Фоновое обновление кэша рейтинга ============
async def refresh_leaderboard_cache_loop(): async def refresh_leaderboard_cache_loop():
await asyncio.sleep(10) await asyncio.sleep(10)
@@ -151,7 +171,9 @@ async def lifespan(app: FastAPI):
await init_achievements() await init_achievements()
await init_learning_data() await init_learning_data()
asyncio.create_task(rate_limiter.start_cleanup()) # 🔧 ФИКС (24.09.2026): cleanup_task_not_cancelled —
# сохраняем ссылку и отменяем в shutdown.
cleanup_task = asyncio.create_task(rate_limiter.start_cleanup())
from database import get_pool from database import get_pool
pool = await get_pool() pool = await get_pool()
@@ -218,6 +240,14 @@ async def lifespan(app: FastAPI):
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info("Leaderboard refresh task cancelled") logger.info("Leaderboard refresh task cancelled")
# 🔧 ФИКС (24.09.2026): cleanup_task_not_cancelled —
# отменяем cleanup-задачу rate limiter.
cleanup_task.cancel()
try:
await cleanup_task
except asyncio.CancelledError:
logger.info("Rate limiter cleanup task cancelled")
logger.info(f"Filwords API v{APP_VERSION} stopped") logger.info(f"Filwords API v{APP_VERSION} stopped")
@@ -232,16 +262,22 @@ async def rate_limit_middleware(request: Request, call_next):
if path in ["/", "/health"]: if path in ["/", "/health"]:
return await call_next(request) return await call_next(request)
# 🔧 ФИКС (23.09.2026): xforwarded_for_spoof — раньше любой клиент мог # 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof —
# подставить X-Forwarded-For и обойти rate limit. # доверяем cf-connecting-ip ТОЛЬКО если запрос пришёл с IP Cloudflare.
# Теперь порядок: cf-connecting-ip (Cloudflare) → x-forwarded-for (только если # Иначе — берём request.client.host (реальный IP клиента).
# запрос пришёл с доверенного прокси) → request.client.host. real_client_ip = request.client.host if request.client else "unknown"
# TODO: вынести CF_IPS в конфиг и проверять источник по списку.
client_ip = ( if _is_cloudflare_ip(real_client_ip):
request.headers.get("cf-connecting-ip") # Запрос от Cloudflare — доверяем cf-connecting-ip
or request.headers.get("x-real-ip") client_ip = (
or (request.client.host if request.client else "unknown") request.headers.get("cf-connecting-ip")
) or request.headers.get("x-real-ip")
or real_client_ip
)
else:
# Запрос не от Cloudflare — игнорируем заголовки, берём реальный IP
client_ip = real_client_ip
rate_key = f"{client_ip}:{path}" rate_key = f"{client_ip}:{path}"
if not rate_limiter.is_allowed(rate_key, path): if not rate_limiter.is_allowed(rate_key, path):
+60 -2
View File
@@ -424,7 +424,8 @@ async def admin_update_feed_post(request: Request, post_id: int):
"is_pinned": true/false, "is_pinned": true/false,
"is_published": true/false, "is_published": true/false,
"expires_at": "..." | null, "expires_at": "..." | null,
"translate_again": true — перевести заново через AI "translate_again": true, — перевести заново через AI
"removed_images": ["..."] — 🔧 ФИКС (24.09.2026): URL'ы удалённых картинок
} }
""" """
telegram_id, _ = await require_auth(request) telegram_id, _ = await require_auth(request)
@@ -443,6 +444,38 @@ async def admin_update_feed_post(request: Request, post_id: int):
source_language = existing["source_language"] source_language = existing["source_language"]
# 🔧 ФИКС (24.09.2026): existing_image_removal_no_delete —
# обрабатываем удалённые картинки: удаляем из БД и с диска.
removed_images = body.get("removed_images", [])
if removed_images and isinstance(removed_images, list):
for img_url in removed_images:
# img_url приходит как полный URL: https://api.filwordspl.com/uploads/feed/...
# или относительный: /uploads/feed/...
# Приводим к относительному виду для БД
rel_path = img_url
if img_url.startswith("http"):
# Вырезаем /uploads/...
idx = img_url.find("/uploads/")
if idx >= 0:
rel_path = img_url[idx:]
else:
logger.warning(f"[removed_images] cannot parse URL: {img_url}")
continue
# Удаляем из БД
deleted = await conn.execute("""
DELETE FROM feed_attachments
WHERE post_id = $1 AND file_path = $2
""", post_id, rel_path)
deleted_count = int(deleted.split()[-1]) if deleted else 0
if deleted_count > 0:
logger.info(f"[removed_images] deleted from DB: {rel_path}")
else:
logger.warning(f"[removed_images] not found in DB: {rel_path}")
logger.info(f"[removed_images] post #{post_id}: processed {len(removed_images)} items")
# Мерджим поля: если не передано — оставляем старое # Мерджим поля: если не передано — оставляем старое
title = body.get("title", existing.get(f"title_{source_language}") or "") title = body.get("title", existing.get(f"title_{source_language}") or "")
post_body = body.get("body", existing.get(f"body_{source_language}") or "") post_body = body.get("body", existing.get(f"body_{source_language}") or "")
@@ -516,7 +549,32 @@ async def admin_update_feed_post(request: Request, post_id: int):
post_id, post_id,
) )
return {"status": "ok", "post_id": post_id} # 🔧 ФИКС (24.09.2026): existing_image_removal_no_delete —
# удаляем файлы с диска ВНЕ pool.acquire.
if removed_images and isinstance(removed_images, list):
for img_url in removed_images:
rel_path = img_url
if img_url.startswith("http"):
idx = img_url.find("/uploads/")
if idx >= 0:
rel_path = img_url[idx:]
else:
continue
# "/uploads/feed/16_0_abc.jpg" → "feed/16_0_abc.jpg"
rel_disk = rel_path.replace("/uploads/", "", 1).lstrip("/")
abs_path = Path(UPLOADS_DIR) / rel_disk
try:
if abs_path.exists():
abs_path.unlink()
logger.info(f"[removed_images] removed file: {abs_path}")
else:
logger.warning(f"[removed_images] file not found: {abs_path}")
except Exception as e:
logger.error(f"[removed_images] failed to remove {abs_path}: {e}")
return {"status": "ok", "post_id": post_id}
# 🔧 ФИКС (18.09.2026): POST вместо DELETE. # 🔧 ФИКС (18.09.2026): POST вместо DELETE.