From 66a4c7759361b7c5330a69dd6e57964709a3cfda Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Sat, 10 Oct 2026 16:34:41 +0800 Subject: [PATCH] Redact sensitive inputs in validation errors (#1268) --- backend/common/exception/exception_handler.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/common/exception/exception_handler.py b/backend/common/exception/exception_handler.py index 96a55d8d..a1b6a5fb 100644 --- a/backend/common/exception/exception_handler.py +++ b/backend/common/exception/exception_handler.py @@ -43,6 +43,28 @@ async def _validation_exception_handler(exc: RequestValidationError | Validation """ errors = [] for error in exc.errors(): + # 用于 ai-buddy 插件,脱敏校验错误中的敏感输入,避免异常响应泄露请求内容 + if ( + error.get('type') == 'json_invalid' + or any( + str(part).lower() + in { + *settings.OPERA_LOG_REDACT_KEYS, + 'secret', + 'messages', + 'forwardedprops', + 'forwarded_props', + } + for part in error.get('loc', ()) + ) + or ( + isinstance(error.get('input'), dict) + and any(str(key).lower() in {'messages', 'forwardedprops', 'forwarded_props'} for key in error['input']) + ) + ): + error['input'] = '[REDACTED]' + elif isinstance(error.get('input'), dict) and 'secret' in error['input']: + error['input'] = {**error['input'], 'secret': '[REDACTED]'} # 非 en-US 语言下,使用自定义错误信息 if i18n.current_language != 'en-US': custom_message = t(f'pydantic.{error["type"]}')