|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +"""Tests that error messages in Anthropic and speech-to-text entrypoints |
| 4 | +are sanitized to prevent memory address leakage. |
| 5 | +
|
| 6 | +Verifies the fix for the incomplete CVE-2026-22778 remediation where |
| 7 | +PIL repr addresses leaked via the Anthropic API router and the |
| 8 | +speech-to-text WebSocket paths. |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from vllm.entrypoints.serve.utils.api_utils import sanitize_message |
| 14 | + |
| 15 | + |
| 16 | +class TestSanitizeMessageCoversLeakPatterns: |
| 17 | + """Ensure sanitize_message strips addresses from realistic exceptions.""" |
| 18 | + |
| 19 | + @pytest.mark.parametrize( |
| 20 | + ("raw", "expected"), |
| 21 | + [ |
| 22 | + ( |
| 23 | + "cannot identify image file <_io.BytesIO object at 0x7a95e299e750>", |
| 24 | + "cannot identify image file <_io.BytesIO object>", |
| 25 | + ), |
| 26 | + ( |
| 27 | + "cannot identify image file <_io.BytesIO object at 0x7f3c1a2b4d90>", |
| 28 | + "cannot identify image file <_io.BytesIO object>", |
| 29 | + ), |
| 30 | + ( |
| 31 | + "<PIL.PngImagePlugin.PngImageFile image mode=RGB " |
| 32 | + "size=8x8 at 0x7f3c1a2b4d90>", |
| 33 | + "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=8x8>", |
| 34 | + ), |
| 35 | + ( |
| 36 | + "Error processing <_io.BytesIO object at 0xdeadbeef>: invalid header", |
| 37 | + "Error processing <_io.BytesIO object>: invalid header", |
| 38 | + ), |
| 39 | + ], |
| 40 | + ids=[ |
| 41 | + "bytesio-standard", |
| 42 | + "bytesio-different-addr", |
| 43 | + "pil-image-repr", |
| 44 | + "mid-string-repr", |
| 45 | + ], |
| 46 | + ) |
| 47 | + def test_address_stripped(self, raw: str, expected: str): |
| 48 | + assert sanitize_message(raw) == expected |
| 49 | + |
| 50 | + def test_safe_message_unchanged(self): |
| 51 | + msg = "Invalid request: missing 'messages' field" |
| 52 | + assert sanitize_message(msg) == msg |
| 53 | + |
| 54 | + def test_multiple_addresses_stripped(self): |
| 55 | + raw = "<obj at 0xaaa> and <obj at 0xbbb>" |
| 56 | + result = sanitize_message(raw) |
| 57 | + assert "0x" not in result |
| 58 | + |
| 59 | + |
| 60 | +class TestAffectedModulesUseSanitize: |
| 61 | + """Verify that affected modules call sanitize_message (source-level).""" |
| 62 | + |
| 63 | + @pytest.mark.parametrize( |
| 64 | + "module", |
| 65 | + [ |
| 66 | + "vllm.entrypoints.anthropic.api_router", |
| 67 | + "vllm.entrypoints.anthropic.serving", |
| 68 | + "vllm.entrypoints.speech_to_text.realtime.connection", |
| 69 | + ], |
| 70 | + ) |
| 71 | + def test_module_calls_sanitize_message(self, module: str): |
| 72 | + import importlib.util |
| 73 | + from pathlib import Path |
| 74 | + |
| 75 | + spec = importlib.util.find_spec(module) |
| 76 | + assert spec is not None and spec.origin is not None, ( |
| 77 | + f"Cannot locate module {module}" |
| 78 | + ) |
| 79 | + source = Path(spec.origin).read_text() |
| 80 | + assert "sanitize_message" in source, f"{module} does not call sanitize_message" |
| 81 | + assert "import" in source and "sanitize_message" in source |
0 commit comments