mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-10-16 22:51:12 +00:00
Redact sensitive inputs in validation errors (#1268)
This commit is contained in:
@@ -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"]}')
|
||||
|
||||
Reference in New Issue
Block a user