mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-19 12:06:42 +08:00
Fix:async issue and sensitive logging (#11895)
### What problem does this PR solve? change: async issue and sensitive logging ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
@ -18,6 +18,7 @@ import time
|
||||
from typing import Any, Dict, Optional
|
||||
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
|
||||
|
||||
from common import settings
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -73,6 +74,34 @@ def _redact_sensitive_url_params(url: str) -> str:
|
||||
except Exception:
|
||||
return url
|
||||
|
||||
def _is_sensitive_url(url: str) -> bool:
|
||||
"""Return True if URL is one of the configured OAuth endpoints."""
|
||||
# Collect known sensitive endpoint URLs from settings
|
||||
oauth_urls = set()
|
||||
# GitHub OAuth endpoints
|
||||
try:
|
||||
if settings.GITHUB_OAUTH is not None:
|
||||
url_val = settings.GITHUB_OAUTH.get("url")
|
||||
if url_val:
|
||||
oauth_urls.add(url_val)
|
||||
except Exception:
|
||||
pass
|
||||
# Feishu OAuth endpoints
|
||||
try:
|
||||
if settings.FEISHU_OAUTH is not None:
|
||||
for k in ("app_access_token_url", "user_access_token_url"):
|
||||
url_val = settings.FEISHU_OAUTH.get(k)
|
||||
if url_val:
|
||||
oauth_urls.add(url_val)
|
||||
except Exception:
|
||||
pass
|
||||
# Defensive normalization: compare only scheme+netloc+path
|
||||
url_obj = urlparse(url)
|
||||
for sensitive_url in oauth_urls:
|
||||
sensitive_obj = urlparse(sensitive_url)
|
||||
if (url_obj.scheme, url_obj.netloc, url_obj.path) == (sensitive_obj.scheme, sensitive_obj.netloc, sensitive_obj.path):
|
||||
return True
|
||||
return False
|
||||
|
||||
async def async_request(
|
||||
method: str,
|
||||
@ -115,20 +144,23 @@ async def async_request(
|
||||
method=method, url=url, headers=headers, **kwargs
|
||||
)
|
||||
duration = time.monotonic() - start
|
||||
log_url = "<SENSITIVE ENDPOINT>" if _is_sensitive_url else _redact_sensitive_url_params(url)
|
||||
logger.debug(
|
||||
f"async_request {method} {_redact_sensitive_url_params(url)} -> {response.status_code} in {duration:.3f}s"
|
||||
f"async_request {method} {log_url} -> {response.status_code} in {duration:.3f}s"
|
||||
)
|
||||
return response
|
||||
except httpx.RequestError as exc:
|
||||
last_exc = exc
|
||||
if attempt >= retries:
|
||||
log_url = "<SENSITIVE ENDPOINT>" if _is_sensitive_url else _redact_sensitive_url_params(url)
|
||||
logger.warning(
|
||||
f"async_request exhausted retries for {method} {_redact_sensitive_url_params(url)}: {exc}"
|
||||
f"async_request exhausted retries for {method} {log_url}"
|
||||
)
|
||||
raise
|
||||
delay = _get_delay(backoff_factor, attempt)
|
||||
log_url = "<SENSITIVE ENDPOINT>" if _is_sensitive_url else _redact_sensitive_url_params(url)
|
||||
logger.warning(
|
||||
f"async_request attempt {attempt + 1}/{retries + 1} failed for {method} {_redact_sensitive_url_params(url)}: {exc}; retrying in {delay:.2f}s"
|
||||
f"async_request attempt {attempt + 1}/{retries + 1} failed for {method} {log_url}; retrying in {delay:.2f}s"
|
||||
)
|
||||
await asyncio.sleep(delay)
|
||||
raise last_exc # pragma: no cover
|
||||
|
||||
Reference in New Issue
Block a user