From 01ddb4ac5a08003fafcaefb0517353eee9cd7da4 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 19 Nov 2026 22:40:23 +0800 Subject: [PATCH] fix: log sanitized spider request URLs at debug level (#7485) --- app/modules/indexer/spider/__init__.py | 10 +++++--- tests/test_indexer_spider_search_url.py | 34 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/modules/indexer/spider/__init__.py b/app/modules/indexer/spider/__init__.py index 66e265d71..3a8b544b0 100644 --- a/app/modules/indexer/spider/__init__.py +++ b/app/modules/indexer/spider/__init__.py @@ -16,7 +16,7 @@ from app.adapters.system import rust as rust_accel from app.foundation import size as size_tools from app.foundation import temporal as time_tools from app.foundation import url as url_tools -from app.foundation.url import UrlUtils +from app.foundation.url import UrlUtils, mask_url_credentials from app.runtime.execution import run_in_threadpool from app.runtime.log import logger from app.runtime.settings import get_runtime_setting @@ -436,6 +436,8 @@ class SiteSpider: """ 同步请求站点搜索页面并解析种子或字幕结果。 + 完整请求地址只写入 DEBUG 日志,并先遮盖 URL 凭据。 + :return: 解析后的记录列表;不允许搜索或请求解析失败时为空,失败详情记录在实例中 """ if not self.__can_search(): @@ -443,7 +445,7 @@ class SiteSpider: searchurl = self.__get_search_url() - logger.info(f"开始请求:{searchurl}") + logger.debug(f"开始请求:{mask_url_credentials(searchurl)}") ret = RequestUtils( ua=self.ua, @@ -461,6 +463,8 @@ class SiteSpider: """ 异步请求站点搜索页面,再在线程池中解析种子或字幕结果。 + 完整请求地址只写入 DEBUG 日志,并先遮盖 URL 凭据。 + :return: 解析后的记录列表;不允许搜索或请求解析失败时为空,失败详情记录在实例中 """ if not self.__can_search(): @@ -468,7 +472,7 @@ class SiteSpider: searchurl = self.__get_search_url() - logger.info(f"开始异步请求:{searchurl}") + logger.debug(f"开始异步请求:{mask_url_credentials(searchurl)}") ret = await AsyncRequestUtils( ua=self.ua, diff --git a/tests/test_indexer_spider_search_url.py b/tests/test_indexer_spider_search_url.py index 9ed48cf08..4ef5c967f 100644 --- a/tests/test_indexer_spider_search_url.py +++ b/tests/test_indexer_spider_search_url.py @@ -2,9 +2,11 @@ 验证站点爬虫构造搜索地址时的查询参数与路径。 """ -from unittest.mock import patch +import asyncio +from unittest.mock import AsyncMock, Mock, patch from urllib.parse import parse_qs, urlparse +from app.modules.indexer import spider as spider_module from app.modules.indexer.spider import SiteSpider from app.modules.indexer.spider.haidan import HaiDanSpider from app.schemas.types import MediaType @@ -53,6 +55,36 @@ def _get_haidan_params(keyword: str | None, mtype: MediaType = None) -> dict: return {key: values[0] for key, values in params.items()} +def test_spider_request_url_is_logged_at_debug_with_credentials_masked(monkeypatch): + """ + 同步与异步搜索都应在 DEBUG 记录脱敏后的完整请求地址。 + """ + indexer = _build_indexer( + domain="https://example.com/", + search={ + "paths": [{"path": "torrents.php"}], + "params": {"search": "{keyword}", "passkey": "secret-value"}, + }, + ) + debug_messages: list[str] = [] + info_messages: list[str] = [] + monkeypatch.setattr(spider_module.logger, "debug", debug_messages.append) + monkeypatch.setattr(spider_module.logger, "info", info_messages.append) + monkeypatch.setattr(spider_module.RequestUtils, "get_res", Mock(return_value=Mock())) + monkeypatch.setattr(spider_module.AsyncRequestUtils, "get_res", AsyncMock(return_value=Mock())) + monkeypatch.setattr(SiteSpider, "_SiteSpider__decode_response", Mock(return_value="")) + monkeypatch.setattr(SiteSpider, "parse", Mock(return_value=[])) + + SiteSpider(indexer=indexer, keyword="Movie", page=1).get_torrents() + asyncio.run(SiteSpider(indexer=indexer, keyword="Movie", page=1).async_get_torrents()) + + assert debug_messages == [ + "开始请求:https://example.com/torrents.php?search_mode=0&search_area=0&page=1¬newword=1&search=Movie&passkey=", + "开始异步请求:https://example.com/torrents.php?search_mode=0&search_area=0&page=1¬newword=1&search=Movie&passkey=", + ] + assert info_messages == [] + + def test_eastgame_imdb_search_uses_imdb_area(): """ TLF 支持 IMDb ID 搜索时应使用站点配置的 IMDb 搜索区域。