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
@@ -118,3 +118,49 @@ class TestGetMaxTokens:
input_length=150,
default_sampling_params={"max_tokens": 2048},
)
class TestSanitizeMessageFilePaths:
"""sanitize_message should also strip file paths and traceback
frames, not just memory addresses - see #31683."""
def test_strips_traceback_style_frame(self):
msg = (
"1 validation error:\n"
" {'type': 'list_type', 'loc': ('body', 'messages')}\n"
'\n File "/usr/local/lib/python3.12/dist-packages/vllm/'
'entrypoints/serve/utils/api_utils.py", line 40, '
"in create_chat_completion\n"
" POST /v1/chat/completions"
)
result = sanitize_message(msg)
assert "/usr/local/" not in result
assert "api_utils.py" not in result
assert "list_type" in result
def test_strips_arbitrary_absolute_path(self):
result = sanitize_message("Error in /home/user/project/vllm/server.py")
assert "/home/user" not in result
def test_strips_single_parent_container_path(self):
"""Regression: /app/server.py and /workspace/server.py (common in
container deployments) were missed by the original {2,} quantifier."""
assert "/app/" not in sanitize_message("Error in /app/server.py")
assert "/workspace/" not in sanitize_message("Error in /workspace/server.py")
def test_preserves_api_endpoint_paths(self):
msg = "POST /v1/chat/completions failed"
assert "/v1/chat/completions" in sanitize_message(msg)
def test_preserves_short_field_references(self):
msg = "Invalid value for field 'body.messages'"
assert sanitize_message(msg) == msg
def test_strips_both_address_and_path(self):
msg = (
"<Request at 0x7f123> failed at "
"/usr/local/lib/python3.12/dist-packages/vllm/server.py"
)
result = sanitize_message(msg)
assert "0x" not in result
assert "/usr/local/" not in result
@@ -100,3 +100,54 @@ class TestCleanLocForParam:
dot-join rather than returning an empty string."""
loc = ("function-wrap[__log_extra_fields__()]",)
assert clean_loc_for_param(loc) == "function-wrap[__log_extra_fields__()]"
class TestValidationErrorDoesNotLeakServerPaths:
"""FastAPI sets endpoint_file/line/function/path on the exception
during routing, not in the constructor - so we set them manually
here to reproduce the real leak. See #31683."""
@pytest.mark.asyncio
async def test_handler_strips_endpoint_file_context(self):
errors = [
{
"type": "list_type",
"loc": ("body", "messages"),
"msg": "Input should be a valid list",
"input": "not-a-list",
}
]
exc = RequestValidationError(errors)
exc.endpoint_file = (
"/usr/local/lib/python3.12/dist-packages/vllm/"
"entrypoints/serve/utils/api_utils.py"
)
exc.endpoint_line = 40
exc.endpoint_function = "create_chat_completion"
exc.endpoint_path = "POST /v1/chat/completions"
response = await validation_exception_handler(_fake_request(), exc)
body = json.loads(response.body)
message = body["error"]["message"]
assert "/usr/local/" not in message
assert "api_utils.py" not in message
assert "create_chat_completion" not in message
assert "list_type" in message
assert "Input should be a valid list" in message
@pytest.mark.asyncio
async def test_handler_strips_endpoint_path_only_variant(self):
"""Covers the branch where only endpoint_path is set."""
errors = [
{"type": "missing", "loc": ("body", "messages"), "msg": "Field required"}
]
exc = RequestValidationError(errors)
exc.endpoint_path = "POST /v1/chat/completions"
response = await validation_exception_handler(_fake_request(), exc)
body = json.loads(response.body)
message = body["error"]["message"]
assert "missing" in message
assert "Field required" in message