From 327135c09b770fe68b9da402574d8458db7db491 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 29 Sep 2025 08:15:15 +0300 Subject: [PATCH] cleaner-log4 --- auth/oauth.py | 18 ++++++++++++++++++ utils/logger.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/auth/oauth.py b/auth/oauth.py index 6dc8dae6..055bbb3d 100644 --- a/auth/oauth.py +++ b/auth/oauth.py @@ -596,6 +596,15 @@ async def get_oauth_state(state: str) -> dict | None: async def oauth_login_http(request: Request) -> JSONResponse | RedirectResponse: """HTTP handler для OAuth login""" try: + # 🚫 Блокируем запросы от ботов (GPTBot, crawlers) + user_agent = request.headers.get("user-agent", "").lower() + if ( + any(bot in user_agent for bot in ["gptbot", "crawler", "spider", "bot"]) + or "x-openai-host-hash" in request.headers + ): + logger.warning(f"🤖 Blocked OAuth request from bot: {user_agent}") + return JSONResponse({"error": "OAuth not available for bots"}, status_code=403) + provider = request.path_params.get("provider") logger.info( f"🔍 OAuth login request: provider='{provider}', url='{request.url}', path_params={request.path_params}, query_params={dict(request.query_params)}" @@ -671,6 +680,15 @@ async def oauth_login_http(request: Request) -> JSONResponse | RedirectResponse: async def oauth_callback_http(request: Request) -> JSONResponse | RedirectResponse: """HTTP handler для OAuth callback""" try: + # 🚫 Блокируем запросы от ботов (GPTBot, crawlers) + user_agent = request.headers.get("user-agent", "").lower() + if ( + any(bot in user_agent for bot in ["gptbot", "crawler", "spider", "bot"]) + or "x-openai-host-hash" in request.headers + ): + logger.warning(f"🤖 Blocked OAuth request from bot: {user_agent}") + return JSONResponse({"error": "OAuth not available for bots"}, status_code=403) + # 🔍 Диагностика входящего callback запроса logger.info("🔄 OAuth callback received:") logger.info(f" - URL: {request.url}") diff --git a/utils/logger.py b/utils/logger.py index b17773f7..989bd6f5 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -52,6 +52,24 @@ def console_filter(record: logging.LogRecord) -> bool: ): return False + # 🚫 Блокируем OAuth ошибки от ботов (GPTBot, crawlers) + if ( + record.levelno == logging.ERROR + and "Missing OAuth state parameter" in message + and any( + bot_phrase in message + for bot_phrase in [ + "GPTBot", + "gptbot", + "crawler", + "bot", + "spider", + "x-openai-host-hash", + ] + ) + ): + return False + # Define `package` attribute with the relative path. record.package = record.pathname[_leng_path + 1 :].replace(".py", "") record.emoji = (