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:
Muhammad Fawaz
2026-07-09 17:46:29 +08:00
committed by GitHub
co-authored by Mahad Durrani
parent 1cd75b3dd4
commit e87521626f
4 changed files with 114 additions and 8 deletions
+9 -6
View File
@@ -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(