Files
LangBot/plugins/Runner/dify-agent/pkg/request_log.py
d3435a6a54 fix(dify-agent): keep form tokens and signed URLs out of httpx request logs (LANG-413) (#2988)
httpx logs every request as "HTTP Request: <METHOD> <full URL>" at INFO, and the
shared plugin runtime logs at INFO. Two Dify requests carry a credential in the URL:
the human-input form token (a path segment that submits the form on its own via
Dify's web API) and input file URLs that may be signed.

A filter on the httpx logger now rewrites the target of exactly those requests to a
redacted form while they are being opened; it is inert for everything else, so
co-located plugins' logs and the logger level are unchanged.

Also correct SHARED_RUNTIME.md/README: human-input continuations live only in the
active run and are not stored. Bump to 0.2.10.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-11-11 18:52:34 +08:00

61 lines
2.2 KiB
Python

"""Keep credential-bearing request targets out of httpx's request log.
httpx logs every response as ``HTTP Request: <METHOD> <full URL> ...`` at INFO,
and the plugin runtime logs at INFO. Some Dify targets carry a credential that
cannot move into a header: the human-input form token is a path segment (and
submits the form on its own through Dify's web API), and input file URLs may be
signed in their query or path.
Changing the ``httpx`` logger level would alter every co-located plugin's logs,
so this module instead installs one filter on that logger that is inert unless
the current task is inside :func:`redacted_target`. Only the log record of the
request opened within that block is rewritten; nothing is suppressed.
"""
from __future__ import annotations
import contextlib
import contextvars
import logging
from urllib.parse import urlsplit
_target: contextvars.ContextVar[str | None] = contextvars.ContextVar("dify_agent_redacted_target", default=None)
class _RedactRequestTarget(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
target = _target.get()
if target is None:
return True
args = record.args
if isinstance(args, tuple) and len(args) >= 2 and str(record.msg).startswith("HTTP Request:"):
record.args = (args[0], target, *args[2:])
else:
record.msg, record.args = "HTTP request to %s (details redacted)", (target,)
return True
_FILTER = _RedactRequestTarget()
logging.getLogger("httpx").addFilter(_FILTER)
def redact(url: str, keep_path: str | None = None) -> str:
"""Return ``scheme://host[:port]`` plus ``keep_path`` (or ``/[redacted]``)."""
parts = urlsplit(str(url))
origin = f"{parts.scheme}://{parts.netloc.rsplit('@', 1)[-1]}"
return origin + (keep_path if keep_path is not None else "/[redacted]")
@contextlib.contextmanager
def redacted_target(target: str):
"""Log ``target`` instead of the real URL for requests sent inside this block.
Keep the block around opening the request only; never yield to a consumer
from inside it.
"""
token = _target.set(target)
try:
yield
finally:
_target.reset(token)