Sanitize server file paths from validation error responses (#46415)
Signed-off-by: muhammadfawaz1 <135441198+muhammadfawaz1@users.noreply.github.com> Co-authored-by: Mahad Durrani <114791389+mahadrehmann@users.noreply.github.com>
This commit is contained in:
co-authored by
Mahad Durrani
parent
1cd75b3dd4
commit
e87521626f
@@ -310,8 +310,14 @@ def process_lora_modules(
|
||||
|
||||
|
||||
def sanitize_message(message: str) -> str:
|
||||
# Avoid leaking memory address from object reprs
|
||||
return re.sub(r" at 0x[0-9a-f]+>", ">", message)
|
||||
"""Strip memory addresses, tracebacks, and file paths from error messages."""
|
||||
message = re.sub(r" at 0x[0-9a-f]+>", ">", message)
|
||||
message = re.sub(r'\n?\s*File "[^"]+", line \d+, in \S+(\n\s+.*)?', "", message)
|
||||
message = re.sub(
|
||||
r"/(?:home|usr|opt|var|tmp|root|lib|mnt|srv)(?:/[\w.\-]+)+", "<path>", message
|
||||
)
|
||||
message = re.sub(r"(?:/[\w\-]+)+/[\w\-]+\.\w+", "<path>", message)
|
||||
return message.strip()
|
||||
|
||||
|
||||
def log_version_and_model(lgr: Logger, version: str, model_name: str) -> None:
|
||||
|
||||
@@ -509,13 +509,16 @@ async def validation_exception_handler(req: Request, exc: RequestValidationError
|
||||
if loc:
|
||||
param = clean_loc_for_param(loc)
|
||||
|
||||
exc_str = str(exc)
|
||||
errors_str = str(errors)
|
||||
|
||||
if errors and errors_str and errors_str != exc_str:
|
||||
message = f"{exc_str} {errors_str}"
|
||||
# Build the message from exc.errors() instead of str(exc) - str(exc)
|
||||
# leaks the server's file path via FastAPI's endpoint context.
|
||||
if errors:
|
||||
count = len(errors)
|
||||
label = "error" if count == 1 else "errors"
|
||||
message = f"{count} validation {label}:\n"
|
||||
message += "".join(f" {err}\n" for err in errors)
|
||||
message = message.rstrip()
|
||||
else:
|
||||
message = exc_str
|
||||
message = "Validation error"
|
||||
|
||||
err = ErrorResponse(
|
||||
error=ErrorInfo(
|
||||
|
||||
Reference in New Issue
Block a user