fix: audit log records API keys and tokens in plain text (#33746)

With request auditing turned on, only fields whose name ends in "password" were masked, so API keys and tokens saved in the admin settings (OpenAI and Ollama connections, Jupyter, web search and others) were written to the audit log as they were. A secret cut off by the body size limit also had its beginning logged. Fields whose name ends in password, secret, token, key or authorization are now masked, including lists of keys and values cut off by the size limit.

Co-authored-by: xfscott <229475069+xfscott@users.noreply.github.com>
This commit is contained in:
Classic298andxfscott authored and GitHub committed 2026-11-05 07:19:24 +09:00
1 parent 051c357f98
commit a7e51207c0
1 file changed
+6 -6
+6 -6
View File
@@ -141,11 +141,11 @@ def log_model_usage(request: Request, user: UserModel, model_id: str, usage: Opt
logger.error('Failed to log model usage: {}', e)
def redact_passwords(body: str) -> str:
if 'password' not in body.lower():
return body
def redact_secrets(body: str) -> str:
# Secrets cut off by the size limit have no closing quote.
quoted = r'"(?:[^"\\]|\\.)*\\?(?:"|$)'
return re.sub(
r'"(\w*password)"\s*:\s*"(?:[^"\\]|\\.)*"',
rf'"(\w*(?:password|secret|token|key|authorization)s?)"\s*:\s*(?:{quoted}|\[(?:{quoted}|[^\]"])*(?:\]|$))',
r'"\1": "********"',
body,
flags=re.IGNORECASE,
@@ -330,8 +330,8 @@ class AuditLoggingMiddleware:
response_body = context.response_body.decode('utf-8', errors='replace')
# Redact sensitive information
request_body = redact_passwords(request_body)
response_body = redact_passwords(response_body)
request_body = redact_secrets(request_body)
response_body = redact_secrets(response_body)
entry = AuditLogEntry(
id=request.state.audit_request_id,