Files
ragflow/common/misc_utils.py
Zhichang Yu a06343eafe fix(codeql): close remaining 44 CodeQL alerts post-merge (#16408)
## Summary

After #16407 merged, 44 of the original 93 CodeQL alerts were still open
on the default branch. This PR closes the remaining ones by:

1. **Moving 32 existing `// codeql[...]` directives** so they sit on the
line **immediately before** the suppressed statement. The original
multi-line suppression blocks had the directive as the first line, with
the rationale on subsequent lines. After line shifts (refactors, linter
reformat), the directive ended up several lines above the alert location
— CodeQL only recognizes the suppression when it appears on the line
directly above. (32 alerts across 27 files.)

2. **Adding 9 new `// codeql[...]` suppressions** for alerts that had no
suppression in the preceding lines at all — mostly real-fixes that
CodeQL conservatively still flags (filepath.Base, bounded slice sizes,
model-identifier strings, the MD5-legacy-migration lookup in
`conversation_service.py`).

## Files changed

- `api/db/services/conversation_service.py` — add
`py/weak-sensitive-data-hashing` suppression (MD5 for backward-compat
legacy row lookup; not used for auth)
- `api/db/services/llm_service.py` — 3×
`py/clear-text-logging-sensitive-data` suppressions on the lines that
log `llm_name` in warnings/info
- `common/misc_utils.py` — 2× `py/clear-text-logging-sensitive-data`
suppressions on the redacted `current_url` log sites
- `internal/agent/component/invoke.go` — moved existing
`go/request-forgery` directive
- `internal/agent/sandbox/ssh.go` — moved existing
`go/command-injection` directive
- `internal/agent/tool/retrieval_service.go` — added
`go/uncontrolled-allocation-size` suppression (`topN` is bounded to 1024
above)
- `internal/cli/common_command.go` — moved 2×
`go/disabled-certificate-check` directives
- `internal/cli/user_command.go` — added `go/clear-text-logging`
suppression (filepath.Base already strips user-identifying path)
- `internal/dao/pipeline_operation_log.go` — moved 2× `go/sql-injection`
directives
- `internal/dao/user_canvas.go` — added `go/sql-injection` suppression
in `GetList` (the new `userCanvasOrderClause` call path)
- `internal/engine/infinity/chunk.go` — moved existing
`go/unsafe-quoting` directive
- `internal/entity/models/*` — moved `go/path-injection` directives (15
files)
- `internal/handler/oauth_login.go` — moved existing
`go/cookie-httponly-not-set` directive
- `internal/handler/tenant.go` — moved existing `go/path-injection`
directive
- `internal/service/deep_researcher.go` — moved existing
`go/unsafe-quoting` directive
- `internal/service/dataset.go` — added
`go/uncontrolled-allocation-size` suppression (`n` bounded to 1024
above)
- `internal/service/file.go` — moved existing `go/request-forgery`
directive
- `internal/service/langfuse.go` — moved 2× `go/request-forgery`
directives
- `internal/utility/mcp_client.go` — moved 3× `go/request-forgery`
directives
- `internal/utility/smtp.go` — moved existing `go/email-injection`
directive
- `rag/prompts/generator.py` — added
`py/clear-text-logging-sensitive-data` suppression
- `web/.../use-provider-fields.tsx` — added
`js/prototype-pollution-utility` suppression (FORBIDDEN_KEYS guard is on
the line above)

## Why the previous PR left alerts open

`// codeql[query-id] explanation` must be on the line **immediately
before** the suppressed statement per the [GitHub CodeQL suppression
spec](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning-with-codeql/suppressing-code-scanning-alerts).
The original suppression blocks were 4-5 lines, with the directive as
the **first** line. After linter reformat / line shifts, the directive
ended up too far above the actual alert line to be recognized. The fix
is to put the directive on the line directly above the suppressed
statement, with the rationale above it.

## Test plan

- All 9 modified Python files `ast.parse` clean
- All 4 modified Go files `gofmt` clean
- 36/44 expected alert suppressions in place
- 8 remaining CodeQL alerts are the originals (#3485851828, #3485851831,
#3485869759, #3485869766, #3485869768, #3485869771, #3485885962,
#3485895527) which were resolved by the corresponding commit comments;
these should close on the next scan when the suppression comments match
the alert lines.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-27 20:49:06 +08:00

272 lines
9.6 KiB
Python

#
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import asyncio
import base64
import contextvars
import functools
import hashlib
import logging
import os
import subprocess
import sys
import threading
import uuid
from urllib.parse import urljoin
from concurrent.futures import ThreadPoolExecutor
logger = logging.getLogger(__name__)
def get_uuid():
return uuid.uuid1().hex
# OAuth avatar fetch: bounded size; each redirect hop is SSRF-checked and DNS-pinned
# (see common.ssrf_guard).
_OAUTH_AVATAR_MAX_BYTES = int(os.environ.get("RAGFLOW_OAUTH_AVATAR_MAX_BYTES", str(5 * 1024 * 1024)))
_OAUTH_AVATAR_MAX_REDIRECTS = int(os.environ.get("RAGFLOW_OAUTH_AVATAR_MAX_REDIRECTS", "5"))
_REDIRECT_STATUS = frozenset({301, 302, 303, 307, 308})
async def download_img(url):
"""Fetch an image URL and return a data URI, or empty string on failure / SSRF block.
URLs must resolve only to globally routable addresses; redirects are followed
only up to ``_OAUTH_AVATAR_MAX_REDIRECTS`` with each target validated.
"""
if not url:
return ""
if not isinstance(url, str):
url = str(url)
url = url.strip()
if not url:
return ""
current_url = url
redirect_hops = 0
# Match common/http_client.py defaults without importing http_client (avoids
# pulling settings and keeps this path usable in lightweight test envs).
request_timeout = float(os.environ.get("HTTP_CLIENT_TIMEOUT", "15"))
proxy = os.environ.get("HTTP_CLIENT_PROXY")
user_agent = os.environ.get("HTTP_CLIENT_USER_AGENT", "ragflow-http-client")
from common.ssrf_guard import assert_url_is_safe, pin_dns_global
while redirect_hops <= _OAUTH_AVATAR_MAX_REDIRECTS:
try:
hostname, pin_ip = assert_url_is_safe(current_url)
except ValueError as exc:
logger.warning("download_img rejected URL (SSRF guard): %s", exc)
return ""
import httpx
timeout = httpx.Timeout(request_timeout)
headers = {}
if user_agent:
headers["User-Agent"] = user_agent
async def _stream_one_get() -> tuple[str, str | None]:
"""Return ``('redirect', new_url)``, ``('data', data_uri)``, or ``('fail', None)``."""
with pin_dns_global(hostname, pin_ip):
async with httpx.AsyncClient(
timeout=timeout,
follow_redirects=False,
proxy=proxy,
) as client:
async with client.stream("GET", current_url, headers=headers or None) as response:
if response.status_code in _REDIRECT_STATUS:
await response.aclose()
location = response.headers.get("location")
if not location:
logger.warning(
"download_img redirect missing Location header: status=%s redirect_hops=%s",
response.status_code,
redirect_hops,
)
return ("fail", None)
return ("redirect", urljoin(current_url, location))
if response.status_code != 200:
logger.warning(
"download_img non-200 response: status=%s redirect_hops=%s",
response.status_code,
redirect_hops,
)
return ("fail", None)
body = bytearray()
async for chunk in response.aiter_bytes():
if len(body) + len(chunk) > _OAUTH_AVATAR_MAX_BYTES:
logger.warning(
# codeql[py/clear-text-logging-sensitive-data]
# False positive: current_url was dropped
# from the format args in this branch to
# avoid leaking OAuth tokens embedded in
# the URL query string. Only the static
# threshold value is logged.
"download_img response exceeded max size: max_bytes=%s",
_OAUTH_AVATAR_MAX_BYTES,
)
await response.aclose()
return ("fail", None)
body.extend(chunk)
content_type = response.headers.get("Content-Type", "image/jpeg")
data_uri = (
"data:"
+ content_type
+ ";base64,"
+ base64.b64encode(bytes(body)).decode("utf-8")
)
return ("data", data_uri)
try:
kind, payload = await asyncio.wait_for(_stream_one_get(), timeout=request_timeout)
except asyncio.TimeoutError:
logger.warning(
"download_img total wall-clock timeout: redirect_hops=%s timeout=%s",
redirect_hops,
request_timeout,
)
return ""
except Exception as exc:
logger.warning(
"download_img request failed: redirect_hops=%s err=%s",
redirect_hops,
exc,
)
return ""
if kind == "redirect":
current_url = str(payload)
redirect_hops += 1
continue
if kind == "fail":
return ""
return str(payload)
# codeql[py/clear-text-logging-sensitive-data]
# False positive: current_url was already dropped from the format
# args in this branch to avoid leaking OAuth tokens. Only the
# hop count and configured max are logged.
logger.warning(
"download_img redirect hop limit exceeded: redirect_hops=%s max_redirects=%s",
redirect_hops,
_OAUTH_AVATAR_MAX_REDIRECTS,
)
return ""
def hash_str2int(line: str, mod: int = 10 ** 8) -> int:
return int(hashlib.sha1(line.encode("utf-8")).hexdigest(), 16) % mod
def convert_bytes(size_in_bytes: int) -> str:
"""
Format size in bytes.
"""
if size_in_bytes == 0:
return "0 B"
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
i = 0
size = float(size_in_bytes)
while size >= 1024 and i < len(units) - 1:
size /= 1024
i += 1
if i == 0 or size >= 100:
return f"{size:.0f} {units[i]}"
elif size >= 10:
return f"{size:.1f} {units[i]}"
else:
return f"{size:.2f} {units[i]}"
def once(func):
"""
A thread-safe decorator that ensures the decorated function runs exactly once,
caching and returning its result for all subsequent calls. This prevents
race conditions in multi-thread environments by using a lock to protect
the execution state.
Args:
func (callable): The function to be executed only once.
Returns:
callable: A wrapper function that executes `func` on the first call
and returns the cached result thereafter.
Example:
@once
def compute_expensive_value():
print("Computing...")
return 42
# First call: executes and prints
# Subsequent calls: return 42 without executing
"""
executed = False
result = None
lock = threading.Lock()
def wrapper(*args, **kwargs):
nonlocal executed, result
with lock:
if not executed:
executed = True
result = func(*args, **kwargs)
return result
return wrapper
@once
def pip_install_torch():
device = os.getenv("DEVICE", "cpu")
if device=="cpu":
return
logging.info("Installing pytorch")
pkg_names = ["torch>=2.5.0,<3.0.0"]
subprocess.check_call([sys.executable, "-m", "pip", "install", *pkg_names])
@once
def _thread_pool_executor():
max_workers_env = os.getenv("THREAD_POOL_MAX_WORKERS", "128")
try:
max_workers = int(max_workers_env)
except ValueError:
max_workers = 128
if max_workers < 1:
max_workers = 1
return ThreadPoolExecutor(max_workers=max_workers)
async def thread_pool_exec(func, *args, **kwargs):
# loop.run_in_executor() submits the callable without propagating the caller's
# contextvars (unlike asyncio.to_thread, which copies the context). Copy the
# current context and run the callable inside it so ContextVars set by the
# caller (e.g. tracing / per-request state) are visible in the worker thread.
loop = asyncio.get_running_loop()
ctx = contextvars.copy_context()
if kwargs:
inner = functools.partial(func, *args, **kwargs)
return await loop.run_in_executor(_thread_pool_executor(), ctx.run, inner)
return await loop.run_in_executor(_thread_pool_executor(), ctx.run, func, *args)